<jupyter_start><jupyter_text><jupyter_code>## Authors: <NAME> & <NAME>
## 31st May 2020
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import pandas_datareader as pdr
import seaborn as sns
#!pip install statsmodels --upgrade
import statsmodels.api as sm
from statsmodels.tsa.ar_model import AutoReg, ar_select_order
from statsmodels.tsa.api import acf, pacf, graphics
from datetime import datetime<jupyter_output><empty_output><jupyter_text>The objective of this exercise is to extract the trend component of a time-series Example: In macroeconomics, the moving natural rate theory suggests that the unemployment rate is a stationary series around its moving target and its steady state- the natural rate of unemployment. Hence, the trend component of the unemployment series is equal to the natural rate. We will estimate the natural rate using data on monthly unemployment rate, using the following methodologies:*   Hodrick-Prescott filter*   Baxter-King filter*   Christiano-Fitzgerald filter*   Beveridge-Nelson decomposition<jupyter_code>unemp_m = pd.read_csv("unemp_monthly.csv", parse_dates=True, index_col = 'DATE')
unemp_m.dropna(inplace = True)
unemp_m.head()<jupyter_output><empty_output><jupyter_text>Hodrick-Prescott (HP) filter<jupyter_code>hp_cycle, hp_trend = sm.tsa.filters.hpfilter(unemp_m['UNRATE'])
#unemp_m['hp_cycle'] = hp_cycle
unemp_m['hp_trend'] = hp_trend

unemp_m.plot(subplots = True, figsize = (10,4))<jupyter_output><empty_output><jupyter_text>Baxter-King approximate band-pass filter<jupyter_code>bk_cycles = sm.tsa.filters.bkfilter(unemp_m['UNRATE'])
unemp_m['bk_trend'] = unemp_m['UNRATE'] - pd.Series(bk_cycles)
unemp_m.plot(subplots = True, figsize = (10,4))<jupyter_output><empty_output><jupyter_text>Christiano-Fitzgerald approximate band-pass filter<jupyter_code>cf_cycles, cf_trend = sm.tsa.filters.cffilter(unemp_m['UNRATE'])
#unemp_m['cf_cycle'] = cf_cycles
unemp_m['cf_trend'] = cf_trend
unemp_m.plot(subplots = True, figsize = (10,6))<jupyter_output><empty_output><jupyter_text>Beveridge-Nelson decomposition<jupyter_code>bn_dec = pd.read_csv("unemp_monthly.csv", parse_dates=True, index_col = 'DATE')
bn_dec.head()
bn_dec["diff"] = bn_dec["UNRATE"] - bn_dec["UNRATE"].shift(1)
bn_dec["diff_mean"] = bn_dec["diff"] - bn_dec['diff'].mean()
bn_dec.dropna(inplace=True)
bn_dec.head()
# Taking 8 lags and auto-selecting the best orders of autoregression on the differenced series of unemployment rate

select = ar_select_order(bn_dec["diff_mean"], 8, trend= 'n', glob=True)
select.ar_lags
res = select.model.fit()
print(res.summary())
'''The formula for BN trend component = y_t + (phi/1-phi) * delta(differenced(1)-mean)_t
From the results above, all our 3 selected AR lags have p-values=0; we choose diff_mean.L4 i.e. the 4th lag = 0.1791 = phi and put in the above formula
RHS gives the BN trend = natural rate estimates'''

bn_dec['natural_rate'] = bn_dec['UNRATE'] + (0.1791/(1-0.1791)) * bn_dec['diff_mean']
bn_dec.head()
# Plotting the natural rate derived from BN decomposition alongside the actual unemployment rate series

plt.figure(figsize=(10, 4))
plt.plot(bn_dec.index, bn_dec['UNRATE'], label="Unemployment rate", c="dodgerblue", linewidth=1.5)
plt.plot(bn_dec.index, bn_dec['natural_rate'], linestyle = '--', c="crimson", label="BN decomposition", linewidth=1.25)
plt.xlabel("Year")
plt.ylabel("Unemployment (%)")
plt.legend()
plt.tight_layout()<jupyter_output><empty_output><jupyter_text>Plotting all versions of the trend component<jupyter_code>unemp_m = pd.concat([unemp_m, bn_dec['natural_rate']], axis=1)
unemp_m.iloc[0,4] = 6.10
unemp_m.head()
unemp_m.dropna(inplace = True)

unemp_m.plot(subplots = True, figsize = (10,5))
lst = [i+"_error" for i in unemp_m.columns[1:]]
lst
unemp_m[lst[0]] = unemp_m['UNRATE'] - unemp_m['hp_trend']
unemp_m[lst[1]] = unemp_m['UNRATE'] - unemp_m['bk_trend']
unemp_m[lst[2]] = unemp_m['UNRATE'] - unemp_m['cf_trend']
unemp_m[lst[3]] = unemp_m['UNRATE'] - unemp_m['natural_rate']
unemp_m.head()
unemp_m[lst] = unemp_m[lst].apply(lambda x: x**2)
unemp_m.head()
for i in lst:
  k = np.sqrt(unemp_m[i].sum())/len(unemp_m)
  print('RMSE for {} is {}'.format(i,k))<jupyter_output>RMSE for hp_trend_error is 0.014041228833606955
RMSE for bk_trend_error is 0.013034547182042819
RMSE for cf_trend_error is 0.030583030254028563
RMSE for natural_rate_error is 0.0019633594614106866
{'fraction_non_alphanumeric': 0.1314650262018683, 'fraction_numerical': 0.03189792663476874, 'mean_word_length': 7.025594149908592, 'pattern_counts': {'":': 0, '<': 30, '<?xml version=': 0, '>': 30, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '534974', 'license': 'Apache-2.0', 'n_tokens_mistral': 1754, 'n_tokens_neox': 1648, 'n_words': 369, 'path': 'Trend_component__time_series.ipynb', 'repo_name': 'abhiruchi97/Econometrics-in-Python', 'size': '4402'}