程序代写代做代考 In [27]:

In [27]:
import numpy as np
import pandas as pd
from matplotlib import pyplot as plt
from statsmodels.tsa.stattools import adfuller
from statsmodels.tsa.seasonal import seasonal_decompose
from statsmodels.tsa.arima_model import ARIMA
from pandas.plotting import register_matplotlib_converters
register_matplotlib_converters()

df = pd.read_csv(‘04600150301.csv’, parse_dates = [‘Month’], index_col = [‘Month’])

#perform DF test, when Pvalue is very small, we can say the data is stationary
result = adfuller(df[‘Project Sales (m2)’])

print(‘ADF Statistic: {}’.format(result[0]))
print(‘p-value: {}’.format(result[1]))
print(‘Critical Values:’)
for key, value in result[4].items():
print(‘\t{}: {}’.format(key, value))

#plot ACF
from statsmodels.graphics.tsaplots import plot_acf
plot_acf(df, lags=24)
plt.show()

#plot PACF
from statsmodels.graphics.tsaplots import plot_pacf
plot_pacf(df, lags=24)
plt.show()

from statsmodels.tsa.stattools import acf

# Create Training and Test
train = df[:54]
test = df[54:]

# Build Model
model = ARIMA(train, order=(2,0,3))
model_fit = model.fit(disp=0)
print(model_fit.summary())

residuals = pd.DataFrame(model_fit.resid)
fig, ax = plt.subplots(1,2)
residuals.plot(title=”Residuals”, ax=ax[0])
residuals.plot(kind=’kde’, title=’Density’, ax=ax[1])
plt.show()

model_fit.plot_predict(dynamic=False)
plt.show()

# Forecast
fc, se, conf = fitted.forecast(8, alpha=0.05)

# Make as pandas series
fc_series = pd.Series(fc, index=test.index)
lower_series = pd.Series(conf[:, 0], index=test.index)
upper_series = pd.Series(conf[:, 1], index=test.index)

# Plot
plt.figure(figsize=(12,5), dpi=100)
plt.plot(train, label=’training’)
plt.plot(test, label=’actual’)
plt.plot(fc_series, label=’forecast’)
plt.fill_between(lower_series.index, lower_series, upper_series,
color=’k’, alpha=.15)
plt.title(‘Forecast vs Actuals’)
plt.legend(loc=’upper left’, fontsize=8)
plt.show()

ADF Statistic: -6.436674745590186
p-value: 1.6462925389066372e-08
Critical Values:
1%: -3.548493559596539
5%: -2.912836594776334
10%: -2.594129155766944

C:\Users\86137\anaconda3\lib\site-packages\statsmodels\tsa\base\tsa_model.py:162: ValueWarning: No frequency information was provided, so inferred frequency MS will be used.
% freq, ValueWarning)
C:\Users\86137\anaconda3\lib\site-packages\statsmodels\base\model.py:548: HessianInversionWarning: Inverting hessian failed, no bse or cov_params available
‘available’, HessianInversionWarning)
C:\Users\86137\anaconda3\lib\site-packages\statsmodels\base\model.py:568: ConvergenceWarning: Maximum Likelihood optimization failed to converge. Check mle_retvals
“Check mle_retvals”, ConvergenceWarning)
C:\Users\86137\anaconda3\lib\site-packages\statsmodels\tsa\arima_model.py:1490: RuntimeWarning: invalid value encountered in sqrt
return np.sqrt(np.diag(-inv(hess)))
C:\Users\86137\anaconda3\lib\site-packages\scipy\stats\_distn_infrastructure.py:903: RuntimeWarning: invalid value encountered in greater
return (a < x) & (x < b) C:\Users\86137\anaconda3\lib\site-packages\scipy\stats\_distn_infrastructure.py:903: RuntimeWarning: invalid value encountered in less return (a < x) & (x < b) C:\Users\86137\anaconda3\lib\site-packages\scipy\stats\_distn_infrastructure.py:1912: RuntimeWarning: invalid value encountered in less_equal cond2 = cond0 & (x <= _a) ARMA Model Results ============================================================================== Dep. Variable: Project Sales (m2) No. Observations: 54 Model: ARMA(2, 3) Log Likelihood -415.679 Method: css-mle S.D. of innovations 521.398 Date: Sat, 04 Apr 2020 AIC 845.358 Time: 18:02:32 BIC 859.281 Sample: 01-01-2015 HQIC 850.728 - 06-01-2019 ============================================================================================ coef std err z P>|z| [0.025 0.975]
——————————————————————————————–
const 594.8241 22.139 26.868 0.000 551.433 638.215
ar.L1.Project Sales (m2) -0.3074 0.128 -2.406 0.016 -0.558 -0.057
ar.L2.Project Sales (m2) 0.6926 0.140 4.955 0.000 0.419 0.967
ma.L1.Project Sales (m2) 0.4412 nan nan nan nan nan
ma.L2.Project Sales (m2) -1.0000 1.95e-07 -5.12e+06 0.000 -1.000 -1.000
ma.L3.Project Sales (m2) -0.4412 5.55e-05 -7951.065 0.000 -0.441 -0.441
Roots
=============================================================================
Real Imaginary Modulus Frequency
—————————————————————————–
AR.1 -1.0000 +0.0000j 1.0000 0.5000
AR.2 1.4439 +0.0000j 1.4439 0.0000
MA.1 1.0000 +0.0000j 1.0000 0.0000
MA.2 -1.0000 +0.0000j 1.0000 0.5000
MA.3 -2.2664 +0.0000j 2.2664 0.5000
—————————————————————————–


In [ ]: