y_in = x_in @ c_hat_sel * 10000 # out-of-sample cointegrated series (basis points) y_out = x_out @ c_hat_sel * 10000 # - # ## [Step 4](https://www.arpm.co/lab/redirect.php?permalink=s_cointegration_detection-implementation-step04): AR(1) long term parameters # + exp_infty = np.zeros(3) sd_infty = np.zeros(3) tau_halflife = np.zeros(3) for k in range(3): # AR1 fit b_hat, mu_hat_epsi, sig2_hat_epsi = fit_var1(y_in[:, [k]]) # long-run expectation exp_infty[k] = mu_hat_epsi / (1 - b_hat) # long-run standard deviation sd_infty[k] = np.sqrt(sig2_hat_epsi / (1 - b_hat**2)) # half life tau_halflife[k] = -np.log(2) / np.log(abs(b_hat)) # - # ## Plots # + plt.style.use('arpm')
index_col=0, parse_dates=True) db_riskdrivers = db_riskdrivers.loc[t_start:t_end] dates = pd.to_datetime(np.array(db_riskdrivers.index))[1:] tau_implvol = np.array( [col[col.find(' tau=') + 5:] for col in db_riskdrivers.columns]) ind_select = np.in1d(tau_implvol, tau_select) # - # ## [Step 1](https://www.arpm.co/lab/redirect.php?permalink=s_fit_var1_implvol-implementation-step01): Compute risk drivers x = np.log(db_riskdrivers.iloc[:, ind_select].values) # ## [Step 2](https://www.arpm.co/lab/redirect.php?permalink=s_fit_var1_implvol-implementation-step02): Perform VAR1 fit b_hat, mu_epsi_hat, _ = fit_var1(x) # ## [Step 3](https://www.arpm.co/lab/redirect.php?permalink=s_fit_var1_implvol-implementation-step03): Extract invariants realizations epsi_var1 = x[1:, :] - x[:-1, :] @ b_hat.T # ## [Step 4](https://www.arpm.co/lab/redirect.php?permalink=s_fit_var1_implvol-implementation-step04): Save database # + out = pd.DataFrame( { d: epsi_var1[:, d1] for d1, d in enumerate(db_riskdrivers.columns.values[ind_select]) }, index=dates)
t_ = y.shape[0] # length of the time series of rolling values # ## [Step 1](https://www.arpm.co/lab/redirect.php?permalink=s_fit_yields_var1-implementation-step01): Realized risk drivers (yield or shadow rates) if yields: x = y[:] else: x = ytm_shadowrates(y, eta=0.013) # ## [Step 2](https://www.arpm.co/lab/redirect.php?permalink=s_fit_yields_var1-implementation-step02): Flexible probabilities p = exp_decay_fp(t_, tau_hl) # exponential decay # ## [Step 3](https://www.arpm.co/lab/redirect.php?permalink=s_fit_yields_var1-implementation-step03): Perform VAR(1) fit b_hat, mu_epsi_hat, sig2_epsi_hat = fit_var1(x, p, nu=nu) # ## [Step 4](https://www.arpm.co/lab/redirect.php?permalink=s_fit_yields_var1-implementation-step04): Recovered values of the risk drivers from the fit x_fit = x[-1, :] @ b_hat.T + mu_epsi_hat.reshape((1, -1)) # ## [Step 5](https://www.arpm.co/lab/redirect.php?permalink=s_fit_yields_var1-implementation-step05): Expectation and covariance of the conditional next step prediction ind = (np.array([ np.where(tau == tau_select[i])[0] for i, item in enumerate(tau_select) ]).reshape((-1))) # next-step expectation for all times to maturity mu_ns = x[-1, :] @ b_hat.T + mu_epsi_hat # next-step expectation for selected times to maturity mu_select_ns = mu_ns[ind] # next-step covariance for all times to maturity
# ## [Step 4](https://www.arpm.co/lab/redirect.php?permalink=s_checklist_scenariobased_step02-implementation-step04): AR(1) fit of Nelson-Siegel parameters # + # the fit is performed only on non-nan entries t_bonds = np.sum(np.isfinite(x[:, ind_ns_bonds[0]])) x_obligor = np.zeros((t_bonds, i_bonds)) epsi_obligor = np.zeros((t_bonds - 1, i_bonds)) b_ar_obligor = np.zeros(i_bonds) # initialize AR(1) parameter for i in range(i_bonds): # risk driver (non-nan entries) x_obligor[:, i] = x[t_ - t_bonds + 1:, ind_ns_bonds[i]] # fit parameter b_ar_obligor[i], _, _ = fit_var1(x_obligor[:, i]) # invariants epsi_obligor[:, i] = x_obligor[1:, i] - b_ar_obligor[i] * x_obligor[:-1, i] # store the next-step function and the extracted invariants k = 0 for i in ind_ns_bonds: db_invariants[i] = np.r_[np.full(t_ - t_bonds + 1, np.nan), epsi_obligor[:, k]] db_nextstep[i] = 'AR(1)' tmp = np.full(4, np.nan) tmp[1] = b_ar_obligor[k] db_param[i] = tmp k = k + 1 # -
# + tau = np.array([tau]) path = '../../../databases/global-databases/fixed-income/db_yields' y = pd.read_csv(path + '/data.csv', header=0, index_col=0) y = y[tau.astype(float).astype(str)] x = y.values # yield to maturity x = x[::20, :] x = x[-t_:, :].reshape(-1) # - # ## [Step 1](https://www.arpm.co/lab/redirect.php?permalink=s_elltest_ytm_monthly-implementation-step01): AR(1) fit # + p = np.ones(x.shape) / x.shape b_hat, _, _ = fit_var1(x) # realized invariant epsi = x[1:] - x[:-1] * b_hat # - # ## [Step 2](https://www.arpm.co/lab/redirect.php?permalink=s_elltest_ytm_monthly-implementation-step02): ellipsoid tests # + plt.style.use('arpm') # perform and show ellipsoid test for invariance on monthly yield name1 = 'Invariance test on monthly yield' acf_x, conf_int_x = \ invariance_test_ellipsoid(x, l_, conf_lev=conf_lev, fit=0, r=2, title=name1)