0

I am trying to compare the results of two time series filters. One is Hodrick-Prescott. I am successful using that one in this piece of code

import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
plt.style.use(['seaborn-paper'])
import quantecon as qe
import statsmodels.api as sm
from statsmodels.tsa.api import VAR
from sklearn.preprocessing import PolynomialFeatures 
from statsmodels.tsa.base.datetools import dates_from_str
import datetime as dt

hp1 = {}
for reg in regions_only:
    for var in variables_some1:
        x = seasonal[f'{var}sa_{reg}'].seasadj
        temp_cycle, temp_trend = sm.tsa.filters.hpfilter(np.log(x).dropna(), lamb = 1600)
        hp1[f'{var}sa_{reg}_c'] = temp_cycle

for var in variables_some1:
    x = seasonal[f'{var}sa_espana'].seasadj
    temp_cycle, temp_trend = sm.tsa.filters.hpfilter(np.log(x).dropna(), lamb = 1600)
    hp1[f'{var}sa_espana_c'] = temp_cycle

# This seasonally adjusts all variables available to some countries       
hp2 = {}
for reg in regions_only_some:
    for var in variables_some2:
        x = seasonal[f'{var}sa_{reg}'].seasadj
        temp_cycle, temp_trend = sm.tsa.filters.hpfilter(np.log(x).dropna(), lamb = 1600)
        hp2[f'{var}sa_{reg}_c'] = temp_cycle

for var in variables_some2:
    x = seasonal[f'{var}sa_espana'].seasadj
    temp_cycle, temp_trend = sm.tsa.filters.hpfilter(np.log(x).dropna(), lamb = 1600)
    hp2[f'{var}sa_espana_c'] = temp_cycle

hp_ir = {}
for reg in regions_only:
    x = seasonal[f'irsa_{reg}']
    temp_cycle, temp_trend = sm.tsa.filters.hpfilter(x.dropna(), lamb = 1600)
    hp_ir[f'irsa_{reg}_c'] = temp_cycle    

hp_filtered = {**hp1, **hp2, **hp_ir}  

# We transform our dictionary onto a dataframe for ease of manipulation
hp_filtered = pd.DataFrame.from_dict(hp_filtered)

resulting a data frame that I can explore graphically later on. I try to do the same with an analogous code

import xlsxwriter
import quantecon as qe
hp1h = {}
for reg in regions_only:
    for var in variables_some1:
        x = seasonal[f'{var}sa_{reg}'].seasadj
        temp_cycle, temp_trend = qe.hamilton_filter(np.log(x), 8,4)
        hp1h[f'{var}sa_{reg}_c'] = temp_cycle

for var in variables_some1:
    x = seasonal[f'{var}sa_espana'].seasadj
    temp_cycle, temp_trend = qe.hamilton_filter(np.log(x), 8,4)
    hp1h[f'{var}sa_espana_c'] = temp_cycle

# This seasonally adjusts all variables available to some countries       
hp2h = {}
for reg in regions_only_some:
    for var in variables_some2:
        x = seasonal[f'{var}sa_{reg}'].seasadj
        temp_cycle, temp_trend = qe.hamilton_filter(np.log(x), 8,4)
        hp2[f'{var}sa_{reg}_c'] = temp_cycle

for var in variables_some2:
    x = seasonal[f'{var}sa_espana'].seasadj
    temp_cycle, temp_trend = qe.hamilton_filter(np.log(x), 8,4)
    hp2h[f'{var}sa_espana_c'] = temp_cycle


hp_irh = {}
for reg in regions_only:
    x = seasonal[f'irsa_{reg}']
    temp_cycle, temp_trend = qe.hamilton_filter(x, 8,4)
    hp_irh[f'irsa_{reg}_c'] = temp_cycle  

hp_datah = {**hp1h, **hp2h, **hp_irh}  

I create successfully a dictionary of numpy arrays rather than a dictionary of data frames and I am really struggling to figure out how to merge these to filter dictionaries. In particular I don't see how to attach the same datetime index I have in the former dictionary.

So far I managed to put the dictionary into an excel file through

workbook = xlsxwriter.Workbook(path_to_data + 'HamiltonFiltered.xlsx')
worksheet = workbook.add_worksheet()

row = 0
col = 0
worksheet.write(0, 0, 'variable')
worksheet.write(0, 1, 'data')
for key in hp_datah.keys():
    row += 1
    worksheet.write(row, col, key)
    for item in hp_datah[key]:
        worksheet.write(row, 0, key)
        worksheet.write(row, col + 1, np.nan_to_num(item))
        row += 1

workbook.close()

but it requires a lot of edditing work later on.

Thanks

Jorge Alonso
  • 103
  • 11
  • This is not a good approach for asking a question. Try to break your question down in small pieces, also with an example dataset so people can quickly see what you want to achieve. You cannot expect people to go through 100 lines of code without any data to solve your problem. [Here's](https://stackoverflow.com/questions/57774352/fill-in-same-amount-of-characters-where-other-column-is-nan) an example of one of my questions. – Erfan Mar 18 '20 at 15:48
  • Thank you. I will try to deconstruc my question in a better way – Jorge Alonso Mar 18 '20 at 15:55

0 Answers0