Improvements on ResidualAnalysis

This commit is contained in:
Petrônio Cândido 2018-05-25 09:40:25 -03:00
parent b058ed5daa
commit a2002c20b1

View File

@ -33,25 +33,21 @@ def compare_residuals(data, models):
Compare residual's statistics of several models Compare residual's statistics of several models
:param data: test data :param data: test data
:param models: :param models:
:return: :return: a Pandas dataframe with the Box-Ljung statistic for each model
""" """
ret = "Model & Order & Mean & STD & Box-Pierce & Box-Ljung & P-value \\\\ \n" from statsmodels.stats.diagnostic import acorr_ljungbox
rows = []
columns = ["Model","Order","AVG","STD","Box-Ljung","p-value"]
for mfts in models: for mfts in models:
forecasts = mfts.forecast(data) forecasts = mfts.forecast(data)
res = residuals(data, forecasts, mfts.order) res = residuals(data, forecasts, mfts.order)
mu = np.mean(res) mu = np.mean(res)
sig = np.std(res) sig = np.std(res)
ret += mfts.shortname + " & " row = [mfts.shortname, mfts.order, mu, sig]
ret += str(mfts.order) + " & " stat, pval = acorr_ljungbox(res)
ret += str(round(mu,2)) + " & " row.extend([stat, pval])
ret += str(round(sig,2)) + " & " rows.append(row)
q1 = Measures.BoxPierceStatistic(res, 10) return pd.DataFrame(rows, columns=columns)
ret += str(round(q1,2)) + " & "
q2 = Measures.BoxLjungStatistic(res, 10)
ret += str(round(q2,2)) + " & "
ret += str(chi_squared(q2, 10))
ret += " \\\\ \n"
return ret
def plotResiduals(targets, models, tam=[8, 8], save=False, file=None): def plotResiduals(targets, models, tam=[8, 8], save=False, file=None):