%matplotlib inline

import numpy as np
import matplotlib.pyplot as plt

import statsmodels.api as sm

plt.rc("figure", figsize=(16,8))
plt.rc("font", size=14)

"""
Explanation: Prediction (out of sample)
End of explanation
"""


nsample = 50
sig = 0.25
x1 = np.linspace(0, 20, nsample)
X = np.column_stack((x1, np.sin(x1), (x1-5)**2))
X = sm.add_constant(X)
beta = [5., 0.5, 0.5, -0.02]
y_true = np.dot(X, beta)
y = y_true + sig * np.random.normal(size=nsample)

"""
Explanation: Artificial data
End of explanation
"""


olsmod = sm.OLS(y, X)
olsres = olsmod.fit()
print(olsres.summary())

"""
Explanation: Estimation
End of explanation
"""


ypred = olsres.predict(X)
print(ypred)

"""
Explanation: In-sample prediction
End of explanation
"""


x1n = np.linspace(20.5,25, 10)
Xnew = np.column_stack((x1n, np.sin(x1n), (x1n-5)**2))
Xnew = sm.add_constant(Xnew)
ynewpred =  olsres.predict(Xnew) # predict out of sample
print(ynewpred)

"""
Explanation: Create a new sample of explanatory variables Xnew, predict and plot
End of explanation
"""


import matplotlib.pyplot as plt

fig, ax = plt.subplots()
ax.plot(x1, y, 'o', label="Data")
ax.plot(x1, y_true, 'b-', label="True")
ax.plot(np.hstack((x1, x1n)), np.hstack((ypred, ynewpred)), 'r', label="OLS prediction")
ax.legend(loc="best");

"""
Explanation: Plot comparison
End of explanation
"""


from statsmodels.formula.api import ols

data = {"x1" : x1, "y" : y}

res = ols("y ~ x1 + np.sin(x1) + I((x1-5)**2)", data=data).fit()

"""
Explanation: Predicting with Formulas
Using formulas can make both estimation and prediction a lot easier
End of explanation
"""


res.params

"""
Explanation: We use the I to indicate use of the Identity transform. Ie., we do not want any expansion magic from using **2
End of explanation
"""


res.predict(exog=dict(x1=x1n))

"""
Explanation: Now we only have to pass the single variable and we get the transformed right-hand side variables automatically
End of explanation
"""


