def reserves_predicted_scale_knn(x, n, i, a, m, stress_MT=0, stress_interest_rates=0, adapt=True): if adapt == True: ## First, We compute the best model model = best_model_scale_knn(stress_MT, stress_interest_rates, X=X) stress = stress_MT stress_i = stress_interest_rates newTH = stresstest.StressTest_table(TH, stress)[0] i = i + stress_i listcontract = np.zeros((1, n + 1)) list_annual_premium1 = np.zeros((1, n + 1)) list_natural_premium1 = np.zeros((1, n + 1)) annual_premium = model.predict([[x, m, n, i, a]]) for term in range(1, n + 1): qx = stresstest.Qx(x + term - 1, newTH) down = 1 / (1 + i) * (1 - qx) if (term <= m): left = listcontract[0][term - 1] + annual_premium list_annual_premium1[0][term - 1] = annual_premium else: left = listcontract[0][term - 1] right = a * (1 / (1 + i)) * qx listcontract[0][term] = (left - right) / down list_natural_premium1[0][term - 1] = a * qx recurrence1 = listcontract[0] else: model = best_model_scale_knn(stress_MT=0, stress_interest=0, X=X) list_annual_premium1 = np.zeros((1, n + 1)) list_natural_premium1 = np.zeros((1, n + 1)) stress = stress_MT stress_i = stress_interest_rates newTH = stresstest.StressTest_table(TH, stress)[0] listcontract = np.zeros((1, n + 1)) annual_premium = model.predict([[x, m, n, i, a]]) for term in range(1, n + 1): qx = stresstest.Qx(x + term - 1, newTH) down = 1 if (term <= m): left = listcontract[0][term - 1] + annual_premium list_annual_premium1[0][term - 1] = annual_premium else: left = listcontract[0][term - 1] right = a * qx listcontract[0][term] = (left - right) / down list_natural_premium1[0][term] = a * qx recurrence1 = listcontract[0] return ([i for i in range(1, n + 2)], recurrence1, list_annual_premium1[0], list_natural_premium1[0])
def reserves_true(x, n, i, a, m, stress_MT=0, stress_interest_rates=0, adapt=True): if adapt == True: stress = stress_MT stress_i = stress_interest_rates i = i + stress_i newTH = stresstest.StressTest_table(TH, stress)[0] listcontract = np.zeros((1, n + 1)) list_annual_premium2 = np.zeros((1, n + 1)) list_natural_premium2 = np.zeros((1, n + 1)) annual_premium = stresstest.TermInsuranceAnnual(x, n, i, a, m, newTH) for term in range(1, n + 1): qx = stresstest.Qx(x + term - 1, newTH) down = 1 / (1 + i) * (1 - qx) if (term <= m): left = listcontract[0][term - 1] + annual_premium list_annual_premium2[0][term - 1] = annual_premium else: left = listcontract[0][term - 1] right = a * (1 / (1 + i)) * qx list_natural_premium2[0][term - 1] = a * (1 / (1 + i)) * qx listcontract[0][term] = (left - right) / down recurrence2 = listcontract[0] else: stress = stress_MT stress_i = stress_interest_rates newTH = stresstest.StressTest_table(TH, stress)[0] listcontract = np.zeros((1, n + 1)) annual_premium = stresstest.TermInsuranceAnnual(x, n, i, a, m, TH) list_annual_premium2 = np.zeros((1, n + 1)) list_natural_premium2 = np.zeros((1, n + 1)) for term in range(1, n + 1): qx = stresstest.Qx(x + term - 1, newTH) down = 1 / (1 + i + stress_i) * (1 - qx) if (term <= m): left = listcontract[0][term - 1] + annual_premium list_annual_premium2[0][term - 1] = annual_premium else: left = listcontract[0][term - 1] right = a * (1 / (1 + i + stress_i)) * qx list_natural_premium2[0][term - 1] = a * (1 / (1 + i + stress_i)) * qx listcontract[0][term] = (left - right) / down recurrence2 = listcontract[0] return ([i for i in range(1, n + 2)], recurrence2, list_annual_premium2[0], list_natural_premium2[0])
def reserves_predicted_model(x, n, i, a, m, model, stress_MT=0, stress_interest_rates=0, adapt=True): if adapt == True: stress = stress_MT stress_i = stress_interest_rates newTH = stresstest.StressTest_table(TH, stress)[0] i = i + stress_i listcontract = np.zeros((1, n + 1)) list_annual_premium1 = np.zeros((1, n + 1)) list_natural_premium1 = np.zeros((1, n + 1)) annual_premium = model.predict([[x, m, n, i, a]]) for term in range(1, n + 1): qx = stresstest.Qx(x + term - 1, newTH) down = 1 / (1 + i) * (1 - qx) if (term <= m): left = listcontract[0][term - 1] + annual_premium list_annual_premium1[0][term - 1] = annual_premium else: left = listcontract[0][term - 1] right = a * (1 / (1 + i)) * qx listcontract[0][term] = (left - right) / down list_natural_premium1[0][term - 1] = a * qx * (1 / (1 + i)) recurrence1 = listcontract[0] else: list_annual_premium1 = np.zeros((1, n + 1)) list_natural_premium1 = np.zeros((1, n + 1)) stress = stress_MT stress_i = stress_interest_rates newTH = stresstest.StressTest_table(TH, stress)[0] listcontract = np.zeros((1, n + 1)) annual_premium = model.predict([[x, m, n, i, a]]) for term in range(1, n + 1): qx = stresstest.Qx(x + term - 1, newTH) down = 1 / (1 + i + stress_i) * (1 - qx) if (term <= m): left = listcontract[0][term - 1] + annual_premium list_annual_premium1[0][term - 1] = annual_premium else: left = listcontract[0][term - 1] right = a * qx * (1 / (1 + i + stress_i)) listcontract[0][term] = (left - right) / down list_natural_premium1[0][term] = a * qx * (1 / (1 + i + stress_i)) recurrence1 = listcontract[0] return (recurrence1, list_annual_premium1[0], list_natural_premium1[0])
def balance_sheet_true(x, n, i, a, m, stress_MT=0, stress_interest_rates=0, adapt=True): if adapt == True: bs = reserves.reserves_true(x, n, i, a, m, stress_MT, stress_interest_rates, adapt) Premiums = bs[1 + 1] Financial_income = (bs[0 + 1] + Premiums) * (i + stress_interest_rates) Last_premium_reserves = bs[0 + 1] Claims = bs[2 + 1] * (1 + (i + stress_interest_rates)) Premium_reserves = bs[0 + 1][1:len(bs[0 + 1])] Premium_reserves = Premium_reserves + [0] Total_Asset = list() Total_liability = list() for age in range(0, len(Premium_reserves)): Premium_reserves[age] = Premium_reserves[age] * stresstest.NPX( x + age, 1, stresstest.StressTest_table(TH, stress_MT)[0]) Total_Asset.append(Financial_income[age] + Premiums[age] + Last_premium_reserves[age]) Total_liability.append(Claims[age] + Premium_reserves[age]) else: bs = reserves.reserves_true(x, n, i, a, m, stress_MT, stress_interest_rates, adapt) Premiums = bs[1 + 1] Financial_income = (bs[0 + 1] + Premiums) * (i + stress_interest_rates) Last_premium_reserves = bs[0 + 1] Claims = bs[2 + 1] * (1 + (i + stress_interest_rates)) Premium_reserves = bs[0 + 1][1:len(bs[1])] Premium_reserves = Premium_reserves + [0] Total_Asset = list() Total_liability = list() for age in range(0, len(Premium_reserves)): Premium_reserves[age] = Premium_reserves[age] * stresstest.NPX( x + age, 1, stresstest.StressTest_table(TH, stress_MT)[0]) Total_Asset.append(Financial_income[age] + Premiums[age] + Last_premium_reserves[age]) Total_liability.append(Claims[age] + Premium_reserves[age]) return Premiums[0:-1], Financial_income[0:-1], Last_premium_reserves[ 0:-1], Claims[0:-1], Premium_reserves, Total_Asset, Total_liability
def best_model_scale_knn(stress_MT=0, stress_interest=0, X=X): ##put the stress in % y_stressed = list() stress = stress_MT #/100 stress_i = stress_interest #/100 TH_stressed = stresstest.StressTest_table(TH, stress)[0] X_new = X.copy() X_new.interest_rate = X_new.interest_rate + stress_i for contract in range(0, len(X)): y_stressed.append( stresstest.TermInsuranceAnnual( int(X_new.iloc[contract].age), int(X_new.iloc[contract].maturity), X_new.iloc[contract].interest_rate, X_new.iloc[contract].amount, int(X_new.iloc[contract].nb_payements), TH_stressed)) X_trainval, X_test, y_trainval, y_test = train_test_split(X_new, y_stressed, random_state=1) X_train, X_valid, y_train, y_valid = train_test_split(X_trainval, y_trainval, random_state=1) test_scores_scaled = list() train_scores_scaled = list() nn_list_scaled = list() for nn in range(7, 9): nn_list_scaled.append(nn) scaler = MinMaxScaler() X_train_scaled = scaler.fit_transform(X_train) X_test_scaled = scaler.transform(X_test) poly_scaled = PolynomialFeatures(degree=nn, include_bias=False) poly_scaled.fit(X_train_scaled) X_poly_train_scaled = poly_scaled.transform(X_train_scaled) X_poly_test_scaled = poly_scaled.transform(X_test_scaled) model = LinearRegression().fit(X_poly_train_scaled, y_train) test_scores_scaled.append(model.score(X_poly_test_scaled, y_test)) train_scores_scaled.append(model.score(X_poly_train_scaled, y_train)) m = max(test_scores_scaled) max_nn_scaled = 0 for nn in range(7, 9): if test_scores_scaled[nn - 7] == m: max_nn_scaled = nn scaler = MinMaxScaler() X_train_scaled = scaler.fit_transform(X_train) X_test_scaled = scaler.transform(X_new) poly_scaled = PolynomialFeatures(degree=max_nn_scaled, include_bias=False) poly_scaled.fit(X_train_scaled) X_poly_train_scaled = poly_scaled.transform(X_train_scaled) X_poly_test_scaled = poly_scaled.transform(X_test_scaled) model = LinearRegression().fit(X_poly_train_scaled, y_train) y_poly = model.predict(X_poly_test_scaled) y_poly = np.abs(y_poly) ##now we train knn model = KNeighborsRegressor(n_neighbors=20, weights="distance") model.fit(X_new, y_poly) return model
def reserves_sum_knn(stress_MT=0, stress_interest_rates=0, adapt=True): level_annual_premium = np.zeros((len(X), 41)) natural_premium = np.zeros((len(X), 41)) if adapt == True: ## First, We compute the best model model = best_model_scale_knn(stress_MT, stress_interest_rates, X) # model=stresstest. best_model_scale_knn(stress_MT,stress_interest_rates,X)[0] stress = stress_MT #/100 stress_i = stress_interest_rates #/100 newTH = stresstest.StressTest_table(TH, stress)[0] listcontract = np.zeros((len(X), 41)) for contract in range(0, len(X)): x = int(X.iloc[contract].age) m = int(X.iloc[contract].nb_payements) n = int(X.iloc[contract].maturity) i = X.iloc[contract].interest_rate + stress_i a = X.iloc[contract].amount annual_premium = model.predict([[x, m, n, i, a]]) for term in range(1, n + 1): qx = stresstest.Qx(x + term - 1, newTH) down = 1 / (1 + i) * (1 - qx) if (term <= m): left = listcontract[contract][term - 1] + annual_premium level_annual_premium[contract][term - 1] = annual_premium else: left = listcontract[contract][term - 1] right = a * (1 / (1 + i)) * qx natural_premium[contract][term - 1] = a * qx * (1 / (1 + i)) listcontract[contract][term] = (left - right) / down recurrence2 = list() level_annual_premium_total = list() natural_premium_total = list() for term in range(0, 41): ## print(np.sum(listcontract[:,40])) recurrence2.append(np.sum(listcontract[:, term])) natural_premium_total.append(np.sum(natural_premium[:, term])) level_annual_premium_total.append( np.sum(level_annual_premium[:, term])) else: model = best_model_scale_knn(0, 0, X) # model=stresstest. best_model_stress(0,0,X)[0] stress = stress_MT #/100 stress_i = stress_interest_rates #/100 newTH = stresstest.StressTest_table(TH, stress)[0] listcontract = np.zeros((len(X), 41)) for contract in range(0, len(X)): x = int(X.iloc[contract].age) m = int(X.iloc[contract].nb_payements) n = int(X.iloc[contract].maturity) i = X.iloc[contract].interest_rate a = X.iloc[contract].amount annual_premium = model.predict([[x, m, n, i, a]]) for term in range(1, n + 1): qx = stresstest.Qx(x + term - 1, newTH) down = 1 / (1 + i + stress_i) * (1 - qx) if (term <= m): left = listcontract[contract][term - 1] + annual_premium level_annual_premium[contract][term - 1] = annual_premium else: left = listcontract[contract][term - 1] right = a * qx * (1 / (1 + i + stress_i)) natural_premium[contract][term - 1] = a * qx * (1 / (1 + i + stress_i)) listcontract[contract][term] = (left - right) / down recurrence2 = list() level_annual_premium_total = list() natural_premium_total = list() for term in range(0, 41): recurrence2.append(np.sum(listcontract[:, term])) natural_premium_total.append(np.sum(natural_premium[:, term])) level_annual_premium_total.append( np.sum(level_annual_premium[:, term])) reserve_total = recurrence2 return ([i for i in range(1, 42)], reserve_total, natural_premium_total, level_annual_premium_total)
def reserves_sum(stress_MT=0, stress_interest_rates=0, adapt=False): #put stress in % level_annual_premium = np.zeros((len(X), 41)) natural_premium = np.zeros((len(X), 41)) if adapt == True: stress = stress_MT stress_i = stress_interest_rates newTH = stresstest.StressTest_table(TH, stress)[0] listcontract = np.zeros((len(X), 41)) for contract in range(0, len(X)): x = int(X.iloc[contract].age) m = int(X.iloc[contract].nb_payements) n = int(X.iloc[contract].maturity) i = X.iloc[contract].interest_rate + stress_i a = X.iloc[contract].amount annual_premium = stresstest.TermInsuranceAnnual( x, n, i, a, m, newTH) for term in range(1, n + 1): qx = stresstest.Qx(x + term - 1, newTH) natural_premium[contract][term - 1] = a * (1 / (1 + i)) * qx down = 1 / (1 + i) * (1 - qx) if (term <= m): level_annual_premium[contract][term - 1] = annual_premium left = listcontract[contract][term - 1] + annual_premium else: left = listcontract[contract][term - 1] right = a * (1 / (1 + i)) * qx listcontract[contract][term] = (left - right) / down reserve_total = list() level_annual_premium_total = list() natural_premium_total = list() for term in range(0, 41): ## print(np.sum(listcontract[:,40])) level_annual_premium_total.append( np.sum(level_annual_premium[:, term])) natural_premium_total.append(np.sum(natural_premium[:, term])) reserve_total.append(np.sum(listcontract[:, term])) else: stress = stress_MT stress_i = stress_interest_rates newTH = stresstest.StressTest_table(TH, stress)[0] listcontract = np.zeros((len(X), 41)) for contract in range(0, len(X)): x = int(X.iloc[contract].age) m = int(X.iloc[contract].nb_payements) n = int(X.iloc[contract].maturity) i = X.iloc[contract].interest_rate a = X.iloc[contract].amount annual_premium = stresstest.TermInsuranceAnnual(x, n, i, a, m, TH) for term in range(1, n + 1): qx = stresstest.Qx(x + term - 1, newTH) down = 1 / (1 + i + stress_i) * (1 - qx) natural_premium[contract][term - 1] = a * (1 / (1 + i + stress_i)) * qx if (term <= m): level_annual_premium[contract][term - 1] = annual_premium left = listcontract[contract][term - 1] + annual_premium else: left = listcontract[contract][term - 1] right = a * (1 / (1 + i + stress_i)) * qx listcontract[contract][term] = (left - right) / down reserve_total = list() level_annual_premium_total = list() natural_premium_total = list() for term in range(0, 41): level_annual_premium_total.append( np.sum(level_annual_premium[:, term])) natural_premium_total.append(np.sum(natural_premium[:, term])) reserve_total.append(np.sum(listcontract[:, term])) return ([i for i in range(1, 42)], reserve_total, natural_premium_total, level_annual_premium_total)