<jupyter_start><jupyter_text>Exercise 1 P76<jupyter_code># real disposable personal income
dspic = pd.read_csv("./data/DSPIC96.csv", index_col=0, parse_dates=True)
# real personal consumption expenditures
pcec = pd.read_csv("./data/PCEC96.csv", index_col=0, parse_dates=True)
dspic.dropna(inplace=True)
pcec.dropna(inplace=True)
comb = pd.concat([dspic, pcec], axis=1)
comb.dropna(inplace=True)
plt.close()
comb.plot()
plt.show()
print(comb.head())
print(comb.tail())
# difference in log is the growth rate.
gr = 100 * np.log(comb).diff()
gr.dropna(inplace=True)
gr.columns = ["GR_DSPIC", "GR_PCEC"]
print(gr.head())
print(gr.tail())
plt.close()
gr.plot()
plt.show()
y = gr["GR_PCEC"]
X = gr["GR_DSPIC"]
X = sm.add_constant(X)
results = sm.OLS(y, X).fit()
print(results.summary())
gr2 = pd.concat([gr, gr.shift(1)], axis=1)
gr2.columns = ["GR_DSPIC", "GR_PCEC", "GR_DSPIC_L1", "GR_PCEC_L1"]
gr2.dropna(inplace=True)
print(gr2.head())
print(gr2.tail())
y = gr2["GR_PCEC"]
X = gr2[["GR_DSPIC", "GR_DSPIC_L1"]]
X = sm.add_constant(X)
results = sm.OLS(y, X).fit()
print(results.summary())<jupyter_output>OLS Regression Results                            
==============================================================================
Dep. Variable:                GR_PCEC   R-squared:                       0.043
Model:                            OLS   Adj. R-squared:                  0.034
Method:                 Least Squares   F-statistic:                     4.482
Date:                Sun, 13 Jan 2019   Prob (F-statistic):             0.0125
Time:                        02:11:01   Log-Likelihood:                -36.436
No. Observations:                 201   AIC:                             78.87
Df Residuals:                     198   BIC:                             88.78
Df Model:                           2                                         
Covariance Type:            nonrobust                                         
===============================================================================
                  coef    std err          t      P[...]<jupyter_text>Exercise 2 P76<jupyter_code>cpi = pd.read_csv("./data/CPIAUCSL.csv", index_col=0, parse_dates=True)
tb = pd.read_csv("./data/TB3MS.csv", index_col=0, parse_dates=True)
print(cpi.describe())
print(tb.describe())
# Calculate inflation
inf = 100 * np.log(cpi).diff()
inf.dropna(inplace=True)
inf.columns = ["INFLATION"]
print(inf.describe())
# Expost real interst rate
comb = pd.concat([inf, tb], axis=1).dropna()
print(comb.tail())
ex_r = pd.DataFrame(comb["TB3MS"].values - comb["INFLATION"].values, index=comb.index)
ex_r.columns = ["EXPOST_IR"]
print(ex_r.tail())
comb3 = pd.concat([gr2, ex_r], axis=1)
comb3.dropna(inplace=True)
print(comb3.head())
X = comb3[["GR_DSPIC", "EXPOST_IR"]]
X = sm.add_constant(X)
y = comb3[["GR_PCEC"]]
results = sm.OLS(y, X).fit()
print(results.summary())<jupyter_output>OLS Regression Results                            
==============================================================================
Dep. Variable:                GR_PCEC   R-squared:                       0.037
Model:                            OLS   Adj. R-squared:                  0.027
Method:                 Least Squares   F-statistic:                     3.792
Date:                Sun, 13 Jan 2019   Prob (F-statistic):             0.0242
Time:                        02:11:03   Log-Likelihood:                -37.109
No. Observations:                 201   AIC:                             80.22
Df Residuals:                     198   BIC:                             90.13
Df Model:                           2                                         
Covariance Type:            nonrobust                                         
==============================================================================
                 coef    std err          t      P>|[...]<jupyter_text>Exercise 7 P78<jupyter_code>sp500 = pd.read_csv("./data/SP500.csv", index_col=0, parse_dates=True)
# missing data are labelled as '.', omit them.
sp500.replace(".", np.nan, inplace=True)
sp500.dropna(inplace=True)
sp500 = sp500.astype(np.float32)
log_sp500 = np.log(sp500)
print(sp500.describe())
print(log_sp500.describe())
# (a)
rcParams["figure.figsize"] = (18, 9)
daily_return = 100 * log_sp500.diff()
daily_return.dropna(inplace=True)
daily_return.columns = ["R"]
# (b)
daily_return.hist(bins=60)
print(daily_return.describe())
plt.show()
# (c)
lags = [daily_return]
for i in [1, 2, 3, 4]:
    L = daily_return.shift(i)
    L.columns = [f"L{i}"]
    lags.append(L)
comb = pd.concat(lags, axis=1)
comb.dropna(inplace=True)
print(comb.head())
for i in [1, 2, 3, 4]:
    ax = plt.subplot(2, 2, i)
#     plt.scatter(comb[f"L{i}"], comb["SP500"])
    sns.regplot(x=f"L{i}", y="R", data=comb, ci=95)
    
plt.show()<jupyter_output>/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/scipy/stats/stats.py:1713: FutureWarning: Using a non-tuple sequence for multidimensional indexing is deprecated; use `arr[tuple(seq)]` instead of `arr[seq]`. In the future this will be interpreted as an array index, `arr[np.array(seq)]`, which will result either in an error or a different result.
  return np.add.reduce(sorted[indexer] * weights, axis=axis) / sumval<jupyter_text>Exercise 8 P78<jupyter_code># (a)
rcParams["figure.figsize"] = (8, 4)
statsmodels.graphics.tsaplots.plot_acf(daily_return, lags=12, zero=False)
plt.show()
statsmodels.graphics.tsaplots.plot_pacf(daily_return, lags=12, zero=False)
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.19314697926059513, 'fraction_numerical': 0.039855725879170426, 'mean_word_length': 2.519035532994924, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '269666', 'license': 'MIT', 'n_tokens_mistral': 2066, 'n_tokens_neox': 1860, 'n_words': 367, 'path': 'pset2.ipynb', 'repo_name': 'TianyuDu/TimeSeries', 'size': '5545'}