Exemple #1
0
# Visualization options
pd.options.display.max_columns = 50
pd.options.display.width = 250

# Grab total return indexes
df = tracker_feeder()
df = df[df.index >= '2010-01-01']

# Grab funding series
sgs = SGS()
df_cdi = sgs.fetch({12: 'CDI'})
df_cdi = df_cdi / 100

# Compute ERIs
df_eri = compute_eri(total_return_index=df, funding_return=df_cdi['CDI'])
df_returns = df_eri.pct_change(1).dropna()

# Correlation
emp_cov = empirical_covariance(df_returns)
emp_corr, _ = cov2corr(emp_cov)
# print(emp_corr, '\n')

# Shirinkage
shrunk_cov = shrink_cov(df_returns, alpha=0.5)
shrunk_corr, _ = cov2corr(shrunk_cov)
# print(shrunk_corr, '\n')

# Marchenko-Pastur
mp_cov, _, _ = marchenko_pastur(df_returns)
mp_corr, _ = cov2corr(mp_cov)
Exemple #2
0
show_charts = False
chosen_assets = ['NTNB Longa', 'NTNF Longa', 'BOVA']

# SGS
sgs = SGS()
df_sgs = sgs.fetch({12: 'CDI', 1: 'BRL'})
df_cdi = df_sgs['CDI'] / 100

# Grab data
df_tri = tracker_feeder()
df_tri = df_tri[chosen_assets]
df_tri = df_tri.dropna(how='all')
df_tri['BRL'] = df_sgs['BRL']

# Compute ERI
df_eri = compute_eri(df_tri, df_cdi)

# Get HMM
hmm = GaussianHMM(returns=df_eri.resample('M').last().pct_change().dropna())
# hmm.select_order(show_chart=True, select_iter=20)
hmm.fit(n_states=2, fit_iter=100)

# attributes
print(hmm.score)
print(hmm.trans_mat.round(3) * 100, '\n')
print(hmm.avg_duration.round(1), '\n')
print(hmm.state_freq.round(3) * 100)
print(hmm.stationary_dist.round(3) * 100, '\n')

# Plots
hmm.predicted_state.plot(title='Predicted State')