statsmodels.api (OLS, ARIMA, SARIMAX) に関するメモ

## OLS
```python
import statsmodels.api as sm

n_samples = 20

a_true = 1.0
b_true = 3.0

x = np.array([i / n_samples for i in range(n_samples)])
y = a_true + b_true * x + np.random.normal(loc = 0, scale = 0.2, size = [n_samples])
```

```python
from statsmodels.regression.linear_model import OLS

X = np.column_stack((np.repeat(1, n_samples), x))

model = OLS(y, X)
results = model.fit()

print (results.summary())
```

![image.png](https://qiita-image-store.s3.amazonaws.com/0/254604/dbc22960-536f-2219-9fb1-979e877c59d5.png)


```python
a, b = results.params

plt.plot(x, y, 'o')
plt.plot(x, a + b * x)
#plt.text(0, 0, "a = {:.3f}, b = {:.3f}".format(a,b))
plt.title('fitting')
plt.show()

residuals = y - (a + b * x)
plt.scatter(x, residuals)
plt.title('residuals')
plt.show()
```

![image.png](https://qiita-image-store.s3.amazonaws.com/0/254604/7fb5fdeb-9c9e-f6f5-7385-c1331152c414.png)

##ARIMA
```python
n_samples = 120

a_true = 0.75
b_true = 0.5
x_ = 0.0
u_ = 0.0

x = []
for i in range(n_samples):
  u = np.random.normal(loc = 0.0, scale = 1.0)
  t = a_true * x_ + u + b_true * u_
  
  if i % 12 == 0:
    t += 2
  
  x.append(t)
  x_ = t
  u_ = u

plt.plot(range(n_samples), x)
plt.show()
```

![image.png](https://qiita-image-store.s3.amazonaws.com/0/254604/4f2e5047-c2f0-3249-a75c-3098736cb174.png)

```python
fig = plt.figure(figsize = (12, 8))
ax1 = fig.add_subplot(211)
fig = sm.graphics.tsa.plot_acf(x, lags = 40, ax = ax1)
ax2 = fig.add_subplot(212)
fig = sm.graphics.tsa.plot_pacf(x, lags = 40, ax = ax2)
```

![image.png](https://qiita-image-store.s3.amazonaws.com/0/254604/1b962877-ef68-2db6-f90c-beb12149d4b3.png)


```python
order_select = sm.tsa.arma_order_select_ic(x, ic = 'aic', trend = 'nc')
print (order_select)
```

![image.png](https://qiita-image-store.s3.amazonaws.com/0/254604/6877d729-0bd5-6c31-0430-70df7b6da92a.png)


```python
from statsmodels.tsa.arima_model import ARIMA

model = ARIMA(x, order = (1, 0, 1))
results = model.fit(trend = 'nc')

print (results.summary())

residuals = results.resid

fig = plt.figure(figsize = (12, 8))
ax1 = fig.add_subplot(211)
fig = sm.graphics.tsa.plot_acf(residuals, lags = 40, ax = ax1)
ax2 = fig.add_subplot(212)
fig = sm.graphics.tsa.plot_pacf(residuals, lags = 40, ax = ax2)
```

![image.png](https://qiita-image-store.s3.amazonaws.com/0/254604/d23d6b66-88f5-62e3-78e1-c287c01b64bd.png)

##SARIMAX
```python
from statsmodels.tsa.statespace.sarimax import SARIMAX

model = SARIMAX(x, order = (1, 0, 1), seasonal_order = (1, 1, 1, 12))
results = model.fit()

print (results.summary())

residuals = results.resid

fig = plt.figure(figsize = (12, 8))
ax1 = fig.add_subplot(211)
fig = sm.graphics.tsa.plot_acf(residuals, lags = 40, ax = ax1)
ax2 = fig.add_subplot(212)
fig = sm.graphics.tsa.plot_pacf(residuals, lags = 40, ax = ax2)
```

![image.png](https://qiita-image-store.s3.amazonaws.com/0/254604/f3947287-9f4d-553c-1c4e-2c6133ff85f5.png)

```python
prediction = results.predict(0, 160)

plt.plot(x, label = 'true')
plt.plot(prediction, 'r', label = 'prediction')
plt.legend(loc = 'upper right')
plt.show()
```

![image.png](https://qiita-image-store.s3.amazonaws.com/0/254604/1687e46a-3d91-fb3d-660e-2eb4608c46ff.png)