Last active
March 5, 2020 09:53
-
-
Save geniusnhu/278249bb5548092e6de1aa4c3bff7a8c to your computer and use it in GitHub Desktop.
residual autocorrelation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Compute Residual | |
train_pred = stepwise_model.predict(n_periods=106) | |
r_train = train - train_pred | |
r_test = test - pred | |
residual = pd.DataFrame(np.concatenate((r_train,r_test)), columns={"y"}) | |
# Generate lag of Residuals from 1 step to 52 steps | |
# Adding the lag of the target variable from 1 steps back up to 52 | |
for i in range(1, 53): | |
residual["lag_{}".format(i)] = residual.y.shift(i) | |
# Compute correlation of the Residual series and its lags | |
lag_corr = residual.corr() | |
lag_corr = lag_corr.iloc[1:,0] | |
lag_corr.columns = ["corr"] | |
order = lag_corr.abs().sort_values(ascending = False) | |
lag_corr = lag_corr[order.index] | |
# Plot the Residual Autocorrelation | |
plt.figure(figsize=(12, 6)) | |
lag_corr.plot(kind='bar') | |
plt.grid(True, axis='y') | |
plt.title("Autocorrelation") | |
plt.hlines(y=0, xmin=0, xmax=len(lag_corr), linestyles='dashed') | |
# Plot other Criteria (Distribution, Variance, Residual mean) | |
# Residual mean and Distribution | |
print("Residual mean: ",residual.iloc[:,0].mean()) | |
plt.hist(residual.iloc[:,0], bins=20) | |
plt.title("Residual Distribution") | |
# Residual variance plt.plot(residual.iloc[:,0]) | |
plt.title("Residual") | |
plt.hlines(y=0, xmin=0, xmax=len(residual), linestyles='dashed') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment