In [14]:
%matplotlib inline
%config InlineBackend.figure_formats = {'svg', 'retina'}
import statsmodels.api as sm
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
In [23]:
np.random.seed(0)
# make sample
p = sm.tsa.ArmaProcess([1,-0.6], [1, 0.3])
y = p.generate_sample(100)
In [24]:
# arma(1,1) model
m = sm.tsa.ARMA(y, (1, 1))
r = m.fit()
print(r.summary())
In [20]:
np.random.seed(0)
# const값 r.params[0]을 더해줌
# S.D. of innovations 시그마
p = sm.tsa.ArmaProcess(np.r_[1, -r.arparams], [1])
for i in range(5):
y = p.generate_sample(len(y)) * np.sqrt(r.sigma2) + r.params[0] # 결과 값은 시그마 제곱과 const 값을 곱해줘야 한다.
plt.plot(y)
plt.plot(y, lw=5);
In [41]:
df = pd.read_csv("./oil.price.dat")
df.index = pd.date_range("01/1986", "01/2006", freq="MS")
df.plot();
In [42]:
df["y"] = np.log(df["oil.price"]).diff() # log + diff 수익률과 같이 (이전 값과의 차이 값을 구해줌)
df.y.fillna(0, inplace=True) # fill NaN
df.y.plot();
In [43]:
m = sm.tsa.ARMA(df.y.values, (1, 1))
r = m.fit()
print(r.summary())
In [54]:
np.random.seed(0)
p = sm.tsa.ArmaProcess([1], np.r_[1, r.maparams])
for i in range(5):
y = p.generate_sample(len(df)) * np.sqrt(r.sigma2) + r.params[0]
z = np.exp(y.cumsum())*df['oil.price'].values[0]
plt.plot(z)
plt.semilogy(df['oil.price'].values, lw=5);
In [48]:
df["y"] = np.log(df["oil.price"])
df.tail()
Out[48]:
In [49]:
df.y.plot();
In [50]:
m = sm.tsa.ARIMA(df.y, [0,1,1])
r = m.fit()
print(r.summary())
In [52]:
r.plot_predict(end="2010-12", plot_insample=False);
In [ ]: