def calc_OAS(m, theta_df, kappa, sigma, gamma, p, beta, r0, bond_list, Tranche_bal_arr, wac, tenor, antithetic, Pool1_bal, Pool2_bal, Pool1_mwac, Pool1_age, Pool1_term, Pool2_mwac, Pool2_age, Pool2_term, coupon_rate): spot_simulate_df = lib.simulate_rate(m, theta_df, kappa, sigma, r0, antithetic) # Calculate 10 yr rates tenor_rate = calc_tenor_rate(spot_simulate_df, kappa, sigma, theta_df, tenor) v1 = wac - tenor_rate v2 = v1.copy() * 0.0 v2[(v2.index.month >= 5) & (v2.index.month <= 8)] = 1 smm_df = calc_hazard(gamma, p, beta, v1, v2) cash_df = np.vectorize( calc_cashflow_CA, signature='(n),(n),(),(),(),(),(),(),(),(),(),(k)->(m)')( smm_df.T.values.astype(float), spot_simulate_df.T.values.astype(float), Pool1_bal, Pool2_bal, Pool1_mwac, Pool1_age, Pool1_term, Pool2_mwac, Pool2_age, Pool2_term, coupon_rate, Tranche_bal_arr) cash_df = pd.DataFrame(cash_df) foward_rate = calc_tenor_rate(spot_simulate_df, kappa, sigma, theta_df, 1) r0 = 0 oas = scipy.optimize.fsolve(calc_PV_diff, r0, args=(cash_df, foward_rate, Tranche_bal_arr[-2]))[0] return oas
def mc_bond(m, theta_df, kappa, sigma, gamma, p, beta, r0, bond_list, Tranche_bal_arr, wac, tenor, antithetic, Pool1_bal, Pool2_bal, Pool1_mwac, Pool1_age, Pool1_term, Pool2_mwac, Pool2_age, Pool2_term, coupon_rate): spot_simulate_df = lib.simulate_rate(m, theta_df, kappa, sigma, r0, antithetic) # Calculate 10 yr rates tenor_rate = calc_tenor_rate(spot_simulate_df, kappa, sigma, theta_df, tenor) v1 = wac - tenor_rate v2 = v1.copy() * 0.0 v2[(v2.index.month >= 5) & (v2.index.month <= 8)] = 1 smm_df = calc_hazard(gamma, p, beta, v1, v2) price_df = np.vectorize( calc_cashflow, signature='(n),(n),(),(),(),(),(),(),(),(),(),(k)->(m)')( smm_df.T.values.astype(float), spot_simulate_df.T.values.astype(float), Pool1_bal, Pool2_bal, Pool1_mwac, Pool1_age, Pool1_term, Pool2_mwac, Pool2_age, Pool2_term, coupon_rate, Tranche_bal_arr) price_df = pd.DataFrame(price_df, columns=bond_list) return price_df, smm_df
def mc_bond(m, theta_df, kappa, sigma, sol_arm_p, sol_arm_d, sol_frm_p, sol_frm_d, r0, tenor, antithetic, Pool1_bal, Pool2_bal, Pool1_mwac, Pool1_age, Pool1_term, sprd, Pool2_age, Pool2_term, sprd_arr, current_principal, current_ltv, orig_bal, Tranche_bal_arr, cds_mat, m5_fixed_coupon, m2_fixed_coupon): """ current_principal current_ltv array[2] gamma, p: 2D arrays for hazards. (FRM, ARM) x (prepay, default) beta : 2x2x2 array for covariate coefficients for hazards. (FRM, ARM) x (prepay, default) x () [0,:] is for prepay, [1,0] is for default (LTV) """ spot_simulate_df = lib.simulate_rate(m, theta_df, kappa, sigma, r0, antithetic).astype(float) / 12 hp_array1, hp_array2 = simulate_homeprice(m, spot_simulate_df.values, current_principal, current_ltv) # Calculate 10 yr rates tenor_rate = lib_2.calc_tenor_rate(spot_simulate_df, kappa, sigma, theta_df, tenor) v_frm_prepay1 = Pool1_mwac * 12 - tenor_rate v_frm_prepay2 = v_frm_prepay1.copy() * 0.0 v_frm_prepay2[(v_frm_prepay2.index.month >= 5) & (v_frm_prepay2.index.month <= 8)] = 1 v_arm_prepay1 = sprd * 12 - tenor_rate + spot_simulate_df v_arm_prepay2 = v_arm_prepay1.copy() * 0.0 v_arm_prepay2[(v_arm_prepay2.index.month >= 5) & (v_arm_prepay2.index.month <= 8)] = 1 smm_frm_df = lib_2.calc_hazard(sol_frm_p[0], sol_frm_p[1], sol_frm_p[2:], v_frm_prepay1, v_frm_prepay2, age=Pool1_age).astype( float) # prepay hazard smm_arm_df = lib_2.calc_hazard(sol_arm_p[0], sol_arm_p[1], sol_arm_p[2:], v_arm_prepay1, v_arm_prepay2, age=Pool2_age).astype( float) # prepay hazard # function definition #def calc_cashflow(SMM_array_arm, SMM_array_frm, r, hpi_1, hpi_2, Pool1_bal, Pool2_bal, Pool1_mwac, Pool1_age, Pool1_term, # Pool2_sprd, Pool2_age, Pool2_term, sprd_arr, Tranche_bal_arr, orig_bal, def_haz): (price_df, cds_val_df) = np.vectorize( calc_cashflow, signature= '(n),(n),(n),(n),(n),(),(),(),(),(),(),(),(),(k),(k),(),(t),(t),(),(),()->(m),(l)' )(smm_frm_df.T.values, smm_arm_df.T.values, spot_simulate_df.T.values, hp_array1.T, hp_array2.T, Pool1_bal, Pool2_bal, Pool1_mwac, Pool1_age, Pool1_term, sprd, Pool2_age, Pool2_term, sprd_arr, Tranche_bal_arr, orig_bal, sol_frm_d, sol_arm_d, cds_mat, m5_fixed_coupon, m2_fixed_coupon) price_df = pd.DataFrame(price_df, columns=range(Tranche_bal_arr.shape[0])) cds_val_df = pd.DataFrame(cds_val_df, columns=range(Tranche_bal_arr.shape[0])) return price_df, cds_val_df