def hull_white_caplet(sigma, kappa, discount_factor, discount_factor_prev, d_settlement, strike, d_prev, d_mat, nominal=1): ''' :param sigma: Hull-White sigma parameter :param kappa: Hull-White kappa parameter :param discount_factor: discount factor at the time of the caplet pays :param discount_factor_prev: discount factor at the d_prev time :param d_settlement: :param strike: :param d_prev: This is the previous, actual payment date :param d_mat: This is the actual payment date for the maturity :param nominal: :return: the caplet val as calculate by the Hull-White model with parameters (kappa, sigma) and given market data ''' t_i_prev = count_days(d_prev, d_settlement, 'actual') / 360 t_i = count_days(d_mat, d_settlement, 'actual') / 360 # equilavent to delta in Veronesi (amount of time the caplet is active for). Denoted tau_i in course notes. delta = count_days(d_prev, d_mat, method='actual') / 360 sig_p = sigma * np.sqrt((1 - np.exp(-2 * kappa * t_i_prev)) / (2 * kappa)) * \ np.abs((1 - np.exp(-kappa * t_i)) / kappa) h_i = (1 / sig_p) * (np.log(discount_factor * (1 + strike * delta) / discount_factor_prev)) + sig_p / 2 caplet_val = nominal * ( discount_factor_prev * norm.cdf(-h_i + sig_p) - (1 + strike * delta) * discount_factor * norm.cdf(-h_i)) return caplet_val
def black_caplet(black_vol, discount_factor, strike, forward_libor, d_settlement, d_prev, d_expir, d_mat, nominal=1): ''' :param black_vol: Black quoted (implied) vol for this caplet :param discount_factor: discount factor applicable :param strike: :param forward_libor: :param d_settlement: settlement date of the contract :param d_mat: maturity date of the caplet (denoted T_{i + 1} in Veronesi) :param d_prev: in the context of a cap, this is the previous date of payment. This serves to determine the length of the caplet ("delta" in Veronesi, page 686 - 688) :param d_expir: the expiry date (accrual expiry date) :param nominal: :return: ''' # equivalent to delta in Veronesi (amount of time the caplet is active for). Denoted tau_i in course notes. delta = count_days(d_prev, d_mat, method='actual') / 360 # this helps determine the volatility (where the expiry date used, d_expir, is the accrual date from the contract) t_prev_expiry = count_days(d_settlement, d_expir, method='actual') / 365 vol_t = black_vol * np.sqrt( t_prev_expiry) # total volatility until the rate is fixed d_1 = (1 / vol_t) * np.log(forward_libor / strike) + 0.5 * vol_t d_2 = d_1 - vol_t caplet_price = nominal * ( discount_factor * delta * (forward_libor * norm.cdf(d_1) - strike * norm.cdf(d_2))) # print("Delta: {}. T_Prev: {}. Vol_t: {}".format(delta, t_prev_expiry, vol_t)) return caplet_price #TODO # def forward_libor(d_fix, d_eff, d_mat): # ''' # :param d_fix: fixing date (when the benchmark number is published) # :param d_eff: effective date of the underlying deposit (start date of the rate accrual). Typically some business # days after the fixing date based on the libor currency’s central bank business calendar in addition to the # London (EN) business day calendar # :param d_mat: maturity date of the underlying deposit, ie end date of the rate accrual. This is typically the effe # ctive date plus the libor tenor, or the next good business date. # :return: # '''
MODELS = ['linear-regression', 'gaussian-process', 'mlp', 'svr', 'exponential-holt'] STRATEGIES = [ 'aggregated', 'windowed' ] METRICS = ['MAE', 'RMSE', 'MSLE'] def get_date(state, to_drop=10, strategy='aggregated'): if strategy == 'aggregated': df_state = utils.download_state(state=state) df_state = df_state[df_state['cases'] != 0] df_state = df_state.iloc[:-to_drop] if strategy == 'windowed': df_state = utils.download_state(state=state) df_state = df_state[df_state['cases'] != 0] df_state = df_state.iloc[:-to_drop] return df_state dayone = df_state.index[0] dayout = datetime.strptime(df_state.index[-1], '%d/%m/%Y') days = np.array(utils.count_days(dayone=dayone, dayout=dayout, date_string='%d/%m/%Y')) X = days.reshape(-1,1) y = utils.get_labels(df_state['cases']).reshape(-1,1) X, y = utils.check_inputs(X, y)
for model_name, _ in MODELS_EXPONENTIAL.items(): for strategy in STRATEGIES: print('Training model {} for state: {}'.format( model_name, state['uf'])) df_state = utils.download_state(state=state['uf']) print(df_state.tail()) # Create the output dataframe df_filtered = df_state[df_state['cases'] != 0] # df_state_yhat = pd.DataFrame(index=df_filtered.index, columns=['yhat']) dayone = df_state[df_state['cases'] != 0].index[0] days = np.array( utils.count_days(dayone=dayone, date_string='%d/%m/%Y')) X = days.reshape(-1, 1) y = utils.get_labels(df_state['cases']).reshape(-1, 1) tcvs = utils.TimesSeriesSplit(X, method=strategy) for train_idx, _ in tcvs: X_train, y_train = X[train_idx], y[train_idx] # Split train and test set df_train = df_filtered.iloc[train_idx] if df_train.shape[0] == 1: continue dayout = df_filtered.iloc[train_idx + 1].index[-1] print('{} - dayone: {}'.format(state, dayone))