Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Challenge 1 | Non-Stationary Models
Time Series Analysis

book
Challenge 1

Task

Swipe to start coding

Time for new challenges! Here is the first challenge, the idea of which is to process the pr_HH Spot Price.csv dataset to turn it from non-stationary to stationary:

  1. Read the dataset.
  2. Test for data stationarity (use adfuller) and display results.
  3. Visualize the initial values of the "Price" column.
  4. Transform data (the "Price" column of the df DataFrame) from non-stationary to stationary using the difference method (using the .diff() method with periods = 1 parameter). Drop NA values. Assign the result to the new_diff variable.
  5. Visualize the modified data (new_diff).
  6. Rerun the ADF test for updated data (new_diff).

Solution

# Importing libraries
import pandas as pd
import matplotlib.pyplot as plt
from statsmodels.tsa.stattools import adfuller

# Reading dataset
df = pd.read_csv("https://codefinity-content-media.s3.eu-west-1.amazonaws.com/943e906e-4de6-4694-a1df-313ceed7cfe7/pr_HH+Spot+Price.csv")

# Make ADF Test
result = adfuller(df['Price'].values, autolag="AIC")
print("ADF Statistic: %f" % (result[0]))
print("p-value: %f" % (result[1]))

# Visualizing dataset
plt.plot(df["Price"])
plt.show()

# Use differencing method
new_diff = df["Price"].diff(periods=1).dropna()

# Plot stationary data
plt.plot(new_diff)
plt.show()

# Remake ADF test
new_result = adfuller(new_diff.values, autolag="AIC")
print("ADF Statistic: %f" % (new_result[0]))
print("p-value: %f" % (new_result[1]))

Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 5. Chapter 3
single

single

# Importing libraries
import pandas as pd
import matplotlib.pyplot as plt
from statsmodels.tsa.stattools import adfuller

# Reading dataset
df = pd.___("https://codefinity-content-media.s3.eu-west-1.amazonaws.com/943e906e-4de6-4694-a1df-313ceed7cfe7/pr_HH+Spot+Price.csv")

# Make ADF Test
result = ___(df["___"].values, autolag="AIC")
print("ADF Statistic: %f" % (result[0]))
print("p-value: %f" % (result[1]))

# Visualizing dataset
plt.___(___)
plt.___()

# Use differencing method
new_diff = df["___"].___(periods=___).___()

# Plot stationary data
plt.plot(___)
plt.show()

# Remake ADF test
new_result = adfuller(___.values, autolag="AIC")
print("ADF Statistic: %f" % (new_result[0]))
print("p-value: %f" % (new_result[1]))

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

some-alt