Improvement on Box-Ljung text in Residual Analysis
This commit is contained in:
parent
16edb0deee
commit
461b3bd850
@ -29,7 +29,7 @@ def chi_squared(q, h):
|
|||||||
return p
|
return p
|
||||||
|
|
||||||
|
|
||||||
def compare_residuals(data, models):
|
def compare_residuals(data, models, alpha=.05):
|
||||||
"""
|
"""
|
||||||
Compare residual's statistics of several models
|
Compare residual's statistics of several models
|
||||||
|
|
||||||
@ -39,15 +39,16 @@ def compare_residuals(data, models):
|
|||||||
"""
|
"""
|
||||||
from statsmodels.stats.diagnostic import acorr_ljungbox
|
from statsmodels.stats.diagnostic import acorr_ljungbox
|
||||||
rows = []
|
rows = []
|
||||||
columns = ["Model","Order","AVG","STD","Box-Ljung","p-value"]
|
columns = ["Model","Order","AVG","STD","Box-Ljung","p-value","Result"]
|
||||||
for mfts in models:
|
for mfts in models:
|
||||||
forecasts = mfts.forecast(data)
|
forecasts = mfts.predict(data)
|
||||||
res = residuals(data, forecasts, mfts.order)
|
res = residuals(data, forecasts, mfts.order+1)
|
||||||
mu = np.mean(res)
|
mu = np.mean(res)
|
||||||
sig = np.std(res)
|
sig = np.std(res)
|
||||||
row = [mfts.shortname, mfts.order, mu, sig]
|
row = [mfts.shortname, mfts.order, mu, sig]
|
||||||
stat, pval = acorr_ljungbox(res)
|
stat, pval = acorr_ljungbox(res)
|
||||||
row.extend([stat, pval])
|
test = 'H0 Accepted' if pval > alpha else 'H0 Rejected'
|
||||||
|
row.extend([stat, pval, test])
|
||||||
rows.append(row)
|
rows.append(row)
|
||||||
return pd.DataFrame(rows, columns=columns)
|
return pd.DataFrame(rows, columns=columns)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user