del out # annual credit transition matrix out = pd.DataFrame({'p_credit': pd.Series(p_credit.reshape(-1))}) out.to_csv(path + 'db_invariants_p_credit.csv', index=None) del out # - # ## [Step 7](https://www.arpm.co/lab/redirect.php?permalink=s_checklist_scenariobased_step02-implementation-step07): Perform invariance tests # + plt.style.use('arpm') invar = db_invariants[i_plot - 1][~np.isnan(db_invariants[i_plot - 1])] _ = invariance_test_ellipsoid(invar, lag_) fig_ellipsoid = plt.gcf() fig_ellipsoid.set_dpi(72.0) fig_ellipsoid.set_size_inches(1280.0 / 72.0, 720.0 / 72.0) add_logo(fig_ellipsoid, set_fig_size=False) plt.show() invariance_test_ks(invar) fig_ks = plt.gcf() fig_ks.set_dpi(72.0) fig_ks.set_size_inches(1280.0 / 72.0, 720.0 / 72.0) add_logo(fig_ks, set_fig_size=False) plt.tight_layout() _ = invariance_test_copula(invar, lag_) fig_cop = plt.gcf()
nok_stock_long = df_nokia_stock[(df_nokia_stock.date_tmstmp >= t_first) & (df_nokia_stock.date_tmstmp < t_last)] nok_dividends = df_nok_dividends[(df_nok_dividends.date_tmstmp >= t_first) & (df_nok_dividends.date_tmstmp < t_last)] # extract values dates = nok_stock_long.date_tmstmp.values r = nok_dividends.date_tmstmp.values cf_r = nok_dividends.dividends.values v_stock = nok_stock_long.close.values # - # ## [Step 1](https://www.arpm.co/lab/redirect.php?permalink=s_elltest_compret-implementation-step01): Dividend-adjusted values and invariant series v_adj = adjusted_value(v_stock, dates, cf_r, r, fwd) epsi = np.diff(np.log(v_adj)) # ## [Step 2](https://www.arpm.co/lab/redirect.php?permalink=s_elltest_compret-implementation-step02): Ellipsoid test # + plt.style.use('arpm') # perform and show ellipsoid test for invariance rho, conf_int = \ invariance_test_ellipsoid(epsi, l_, conf_lev=conf_lev, fit=0, r=2, title='Invariance test on daily yield changes') fig = plt.gcf() add_logo(fig, set_fig_size=False, size_frac_x=1/8)
log_underlying = np.log(np.array(db_stocks.loc[dates, 'GE'])) # compounded return and its absolute value comp_return = np.diff(log_underlying) abs_comp_return = np.abs(comp_return) # - # ## Plots # + plt.style.use('arpm') fig = plt.figure(figsize=(15.5, 9)) plt.plot(dates[1:], comp_return.flatten(), '*', markersize=3) plt.title('Compounded return') add_logo(fig, set_fig_size=False) plt.figure() acf_x, conf_int_x = \ invariance_test_ellipsoid(comp_return, l_, conf_lev=conf_lev, title='Compounded return', plot_test=True) fig = plt.gcf() add_logo(fig, set_fig_size=False, size_frac_x=1/8) plt.figure() acf_delta_x, conf_int_detal_x = \ invariance_test_ellipsoid(abs_comp_return, l_, conf_lev=conf_lev, bl=-0.025, title='Absolute compounded return', plot_test=True) fig = plt.gcf() add_logo(fig, set_fig_size=False, size_frac_x=1/8)
# - # ## [Step 1](https://www.arpm.co/lab/redirect.php?permalink=s_elltest_ytm_ns-implementation-step01): Compute increments NS aprameters # + delta_theta = np.diff(theta, axis=0) # - # ## [Step 2](https://www.arpm.co/lab/redirect.php?permalink=s_elltest_ytm_ns-implementation-step02): Ellipsoid tests # + plt.style.use('arpm') # names of figures name = {} name[0] = r'Invariance test(increments of level parameter $\theta_1$)' name[1] = r'Invariance test(increments of slope parameter $\theta_2$)' name[2] = r'Invariance test(increments of curvature parameter $\theta_3$)' name[3] = r'Invariance test(increments of decay parameter $\theta_4^2$)' acf = np.zeros((4, l_)) conf_int_x = np.zeros((4, 2)) # perform and show ellipsoid test for invariance on NS parameters for k in range(4): acf[k, :], conf_int_x[k, :] = invariance_test_ellipsoid(delta_theta[:, k], l_, conf_lev=conf_lev, title=name[k]) fig = plt.gcf() add_logo(fig, set_fig_size=False, size_frac_x=1 / 8)
db_stocks.index = pd.to_datetime(db_stocks.index) dates = pd.to_datetime(db_stocks.index) # ## [Step 1](https://www.arpm.co/lab/redirect.php?permalink=s_elltest_garchres_stock-implementation-step01): Compute risk drivers for GE and compounded return # + # risk drivers log_underlying = np.log(np.array(db_stocks.loc[dates, 'GE'])) # compounded return comp_return = np.diff(log_underlying) # - # ## [Step 2](https://www.arpm.co/lab/redirect.php?permalink=s_elltest_garchres_stock-implementation-step02): Fit GARCH(1,1), compute residuals and their absolute values # + _, _, epsi = fit_garch_fp(comp_return) abs_epsi = np.abs(epsi) # - # ## Plots fig = plt.figure() acf_x, conf_int_x = \ invariance_test_ellipsoid(abs_epsi, l_, conf_lev=conf_lev, bl=-0.75, title='Absolute residuals of a GARCH(1, 1) model fitted on stock compounded return', plot_test=True) fig = plt.gcf() add_logo(fig, set_fig_size=False, size_frac_x=1/8)
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) fig = plt.gcf() add_logo(fig, set_fig_size=False, size_frac_x=1/8) plt.style.use('arpm') # perform and show ellipsoid test for invariance on AR(1) residuals name2 = 'Invariance test on AR(1) residuals' acf_epsi, conf_int_epsi = \ invariance_test_ellipsoid(epsi, l_, conf_lev=conf_lev, fit=0, r=2, title=name2) fig = plt.gcf() add_logo(fig, set_fig_size=False, size_frac_x=1/8) plt.style.use('arpm') # -
epsi_abs = abs(epsi) # ## [Step 2](https://www.arpm.co/lab/redirect.php?permalink=s_elltest_normal-implementation-step02): Compute normalized absolute values # grades of absolute values epsi_abs_grade, *_ = cop_marg_sep(epsi_abs) # normalized absolute values epsiabs_tilde = st.norm.ppf(epsi_abs_grade).squeeze() # ## [Step 3](https://www.arpm.co/lab/redirect.php?permalink=s_elltest_normal-implementation-step03): Ellipsoid test on normal simulations plt.style.use('arpm') name1 = 'Invariance test on normal simulations' acf_epsi, conf_int = \ invariance_test_ellipsoid(epsi, l_, conf_lev=conf_lev, fit=0, r=1.8, title=name1) fig = plt.gcf() add_logo(fig, set_fig_size=False, size_frac_x=1 / 8) # ## [Step 4](https://www.arpm.co/lab/redirect.php?permalink=s_elltest_normal-implementation-step04): Ellipsoid test on absolute value of normal simulations plt.style.use('arpm') name2 = 'Invariance test on absolute values of normal simulations' acf_abs, conf_int = \ invariance_test_ellipsoid(epsi_abs, l_, conf_lev=conf_lev, fit=0, r=1.8, title=name2, bl=[-0.1], bu=[0.7]) fig = plt.gcf() add_logo(fig, set_fig_size=False, size_frac_x=1 / 8)