def test_naax(): # todo check why for some of the large ages the test fails i = 2 g = 0 m = 1 # this works with almost all ages and fails with some other, e.g. fails with 116 x = 121 n = 5 method = 'udd' cf_grf95 = commutation_table.CommutationFunctions(i=i, g=g, mt=soa_GRF95.table_qx) cf_tv7377 = commutation_table.CommutationFunctions(i=i, g=g, mt=soa_TV7377.table_qx) a_grf = annuities.naax(mt=mt_GRF95, x=x, n=n, i=i, g=g, m=m, method=method) a_tv = annuities.naax(mt=mt_TV7377, x=x, n=n, i=i, g=g, m=m, method=method) a_grf_2 = cf_grf95.naax(x=x, m=m, n=n) a_tv_2 = cf_tv7377.naax(x=x, m=m, n=n) assert a_grf == pytest.approx(a_grf_2, rel=1e-16) assert a_tv == pytest.approx(a_tv_2, rel=1e-16)
def test_nAx_(): i = 2 g = 0 n = 5 method = 'udd' cf_grf95 = commutation_table.CommutationFunctions(i=i, g=g, mt=soa_GRF95.table_qx) cf_tv7377 = commutation_table.CommutationFunctions(i=i, g=g, mt=soa_TV7377.table_qx) ages = range(0, max(mt_TV7377.w, mt_GRF95.w) + 10) a_grf = [ mortality_insurance.nAx_(mt=mt_GRF95, x=x, n=n, i=i, g=g, method=method) for x in ages ] a_tv = [ mortality_insurance.nAx_(mt=mt_TV7377, x=x, n=n, i=i, g=g, method=method) for x in ages ] a_grf_2 = [cf_grf95.nAx_(x=x, n=n) for x in ages] a_tv_2 = [cf_tv7377.nAx_(x=x, n=n) for x in ages] for idx_a, a in enumerate(a_grf): assert (idx_a, a_grf[idx_a]) == pytest.approx((idx_a, a_grf_2[idx_a]), rel=1e-16) for idx_a, a in enumerate(a_tv): assert (idx_a, a_tv[idx_a]) == pytest.approx((idx_a, a_tv_2[idx_a]), rel=1e-16)
def test_ax(): i = 2 g = 0 m = 1 x = 200 n = 5 method = 'udd' cf_grf95 = commutation_table.CommutationFunctions(i=i, g=g, mt=soa_GRF95.table_qx) cf_tv7377 = commutation_table.CommutationFunctions(i=i, g=g, mt=soa_TV7377.table_qx) a_grf = annuities.ax(mt=mt_GRF95, x=x, i=i, g=g, m=m, method=method) a_tv = annuities.ax(mt=mt_TV7377, x=x, i=i, g=g, m=m, method=method) a_grf_2 = cf_grf95.ax(x=x, m=m) a_tv_2 = cf_tv7377.ax(x=x, m=m) assert a_grf == pytest.approx(0, rel=1e-16) assert a_tv == pytest.approx(0, rel=1e-16) assert a_grf == pytest.approx(a_grf_2, rel=1e-16) assert a_tv == pytest.approx(a_tv_2, rel=1e-16)
def test_t_ax(): i = 2 g = 1 m = 1 defer = 5 method = 'udd' cf_grf95 = commutation_table.CommutationFunctions(i=i, g=g, mt=soa_GRF95.table_qx) cf_tv7377 = commutation_table.CommutationFunctions(i=i, g=g, mt=soa_TV7377.table_qx) ages = range(0, max(mt_TV7377.w, mt_GRF95.w) + 10) a_grf = [ annuities.t_ax(mt=mt_GRF95, x=x, i=i, g=g, m=m, defer=defer, method=method) for x in ages ] a_tv = [ annuities.t_ax(mt=mt_TV7377, x=x, i=i, g=g, m=m, defer=defer, method=method) for x in ages ] a_grf_2 = [cf_grf95.t_ax(x=x, m=m, defer=defer) for x in ages] a_tv_2 = [cf_tv7377.t_ax(x=x, m=m, defer=defer) for x in ages] assert a_grf == pytest.approx(a_grf_2, rel=1e-16) assert a_tv == pytest.approx(a_tv_2, rel=1e-16)
def test_t_nax(): i = 2 g = 1 m = 1 x = 45 defer = 10 n = 5 method = 'udd' cf_grf95 = commutation_table.CommutationFunctions(i=i, g=g, mt=soa_GRF95.table_qx) cf_tv7377 = commutation_table.CommutationFunctions(i=i, g=g, mt=soa_TV7377.table_qx) a_grf = annuities.t_nax(mt=mt_GRF95, x=x, n=n, i=i, g=g, m=m, defer=defer, method=method) a_tv = annuities.t_nax(mt=mt_TV7377, x=x, n=n, i=i, g=g, m=m, defer=defer, method=method) a_grf_2 = cf_grf95.t_nax(x=x, n=n, m=m, defer=defer) a_tv_2 = cf_tv7377.t_nax(x=x, n=n, m=m, defer=defer) assert a_grf == pytest.approx(a_grf_2, rel=1e-16) assert a_tv == pytest.approx(a_tv_2, rel=1e-16)
def test_t_naax(): # todo check why for some of the large ages the test fails i = 2 g = 0 m = 1 x = 111 defer = 10 n = 5 method = 'udd' cf_grf95 = commutation_table.CommutationFunctions(i=i, g=g, mt=soa_GRF95.table_qx) cf_tv7377 = commutation_table.CommutationFunctions(i=i, g=g, mt=soa_TV7377.table_qx) a_grf = annuities.t_naax(mt=mt_GRF95, x=x, n=n, i=i, g=g, m=m, defer=defer, method=method) a_tv = annuities.t_naax(mt=mt_TV7377, x=x, n=n, i=i, g=g, m=m, defer=defer, method=method) a_grf_2 = cf_grf95.t_naax(x=x, n=n, m=m, defer=defer) a_tv_2 = cf_tv7377.t_naax(x=x, n=n, m=m, defer=defer) assert a_grf == pytest.approx(a_grf_2, rel=1e-16) assert a_tv == pytest.approx(a_tv_2, rel=1e-16)
t = np.linspace(0, 100, 1000 + 1) x_s = [0, 20, 50, 80] ''' Compute Life Table and commutation table ''' interest_rate = 5 px = np.array([mml.S(x, t=1) for x in range(0, 130 + 1)]) qx = 1 - px lt = mortality_table.MortalityTable(mt=list(np.append(0, qx))) lt.df_life_table().to_excel(excel_writer='makeham' + '.xlsx', sheet_name='makeham', index=False, freeze_panes=(1, 1)) ct = commutation_table.CommutationFunctions(i=interest_rate, g=0, mt=list(np.append(0, qx))) ct.df_commutation_table().to_excel(excel_writer='makeham' + '_comm' + '.xlsx', sheet_name='makeham', index=False, freeze_panes=(1, 1)) ''' annuities ''' x_s = range(20, 80 + 20, 20) rendas_dict = {} for x in x_s: for m in [1, 4]: ts = np.arange(0, lt.w - x + 1 / m, 1 / m) v = 1 / (1 + interest_rate / 100)
this_py = os.path.split(sys.argv[0])[-1][:-3] def parse_table_name(name): return name.replace(' ', '').replace('/', '') table_names = ['TV7377', 'GRF95', 'GRM95'] interest_rate = 6 mt_lst = [ rst.SoaTable('../../soa_tables/' + name + '.xml') for name in table_names ] lt_lst = [mortality_table.MortalityTable(mt=mt.table_qx) for mt in mt_lst] ct_lst = [ commutation_table.CommutationFunctions(i=interest_rate, g=0, mt=mt.table_qx) for mt in mt_lst ] ages = np.linspace(start=20, stop=40, num=5, dtype=int) ''' Prepare the solution for Equal Instalments ''' capital = 100000 terms = 25 ac = annuities_certain.Annuities_Certain(interest_rate=interest_rate, frequency=1) ac_certain = ac.annuity_immediate(terms=terms) equal_instalments_dict = { 'table': [], 'x': [], 'annuity_certain': [],
import pandas as pd from soa_tables import read_soa_table_xml as rst from essential_life import mortality_table, commutation_table import matplotlib.pyplot as plt table_names = ['TV7377', 'GRF95', 'GRM95'] interest_rate = 4 name = table_names[0] mt = rst.SoaTable('../../soa_tables/' + name + '.xml') lt = mortality_table.MortalityTable(mt=mt.table_qx) ct = commutation_table.CommutationFunctions(i=interest_rate, g=0, mt=mt.table_qx) lx = 1000 frequency = 1 first_instalment_at = 5 number_of_instalments = 1000 premium_instalments = 5 instalment = 10000 dict_annuity = {'x': 50, 'n': 1000, 'm': 1, 'defer': first_instalment_at} renda_premium = ct.naax(x=dict_annuity['x'], n=premium_instalments) full_liability = ct.t_naax(x=dict_annuity['x'], n=dict_annuity['n'], m=dict_annuity['m'], defer=dict_annuity['defer']) * \ instalment * lx premium1 = full_liability / lx / renda_premium i = interest_rate / 100 dict_liability = { 't': [], 'x': [],
import matplotlib.pyplot as plt this_py = os.path.split(sys.argv[0])[-1][:-3] def parse_table_name(name): return name.replace(' ', '').replace('/', '') table_names = ['TV7377', 'GRF95', 'GRM95'] mt_lst = [ rst.SoaTable('../../soa_tables/' + name + '.xml') for name in table_names ] lt_lst = [mortality_table.MortalityTable(mt=mt.table_qx) for mt in mt_lst] ct_lst = [ commutation_table.CommutationFunctions(i=4, g=0, mt=mt.table_qx) for mt in mt_lst ] for idx, lt in enumerate(lt_lst): name = parse_table_name(mt_lst[idx].name) lt.df_life_table().to_excel(excel_writer=name + '.xlsx', sheet_name=name, index=False, freeze_panes=(1, 1)) ct_lst[idx].df_commutation_table().to_excel(excel_writer=name + '_comm' + '.xlsx', sheet_name=name, index=False, freeze_panes=(1, 1)) '''
__author__ = "PedroCR" from soa_tables import read_soa_table_xml as rst from toDelete.mortality_tables_old import TV7377, GRF95 from essential_life import mortality_insurance, mortality_table as mt, mortality_table, commutation_table lt_tv7377 = mortality_table.MortalityTable(mt=TV7377) lt_grf95 = mortality_table.MortalityTable(mt=GRF95) soa_TV7377 = rst.SoaTable('../soa_tables/TV7377.xml') soa_GRF95 = rst.SoaTable('../soa_tables/GRF95.xml') mt_GRF95 = mt.MortalityTable(mt=soa_GRF95.table_qx) mt_TV7377 = mt.MortalityTable(mt=soa_TV7377.table_qx) i = 2 x = 45 inc = 1 method = 'udd' cf_grf95 = commutation_table.CommutationFunctions(i=i, g=0, mt=soa_GRF95.table_qx) cf_tv7377 = commutation_table.CommutationFunctions(i=i, g=0, mt=soa_TV7377.table_qx) a_grf = mortality_insurance.IA_x(mt=mt_GRF95, x=x, x_first=46, x_last=50, i=i, inc=inc, method=method) a_grf = mortality_insurance.IA_x(mt=mt_TV7377, x=x, x_first=46, x_last=50, i=i, inc=inc, method=method) a_grf_2 = cf_grf95.IAx(x=x) cf_tv_2 = cf_tv7377.IAx(x=x)
# author: PedroCR # from soa_tables import read_soa_table_xml as rst from essential_life import mortality_table, commutation_table mt_TV7377 = rst.SoaTable('../soa_tables/TV7377.xml') mt_GRF95 = rst.SoaTable('../soa_tables/GRF95.xml') mt_GRM95 = rst.SoaTable('../soa_tables/GRM95.xml') lt_tv7377 = mortality_table.MortalityTable(mt=mt_TV7377.table_qx) lt_gRF95 = mortality_table.MortalityTable(mt=mt_GRF95.table_qx) lt_gRM95 = mortality_table.MortalityTable(mt=mt_GRM95.table_qx) cf_tv7377 = commutation_table.CommutationFunctions(i=1.4, g=1, mt=mt_TV7377.table_qx) cf_grf95 = commutation_table.CommutationFunctions(i=1.4, g=1, mt=mt_GRF95.table_qx) cf_grm95 = commutation_table.CommutationFunctions(i=1.4, g=1, mt=mt_GRM95.table_qx) print(cf_tv7377.df_commutation_table()) print(cf_tv7377.t_aax(55, m=1, defer=(67 - 55))) print(cf_grm95.t_aax(55, m=1, defer=(67 - 55))) print(cf_grf95.t_aax(55, m=1, defer=(67 - 55)))
# author: PedroCR # from toDelete.mortality_tables_old import TV7377, GKM95_lx_15 from essential_life import mortality_table, commutation_table lt_tv7377 = mortality_table.MortalityTable(mt=TV7377) lt_gkm95 = mortality_table.MortalityTable(data_type='l', mt=GKM95_lx_15) cf_tv7377 = commutation_table.CommutationFunctions(i=2, g=1.5, mt=TV7377) print(cf_tv7377.df_commutation_table()) # test to probabilities p = lt_gkm95.tpx(x=50, t=5) q = lt_gkm95.tqx(x=50, t=5) print(p) print(q) print(p + q) print(lt_gkm95.msn) print(lt_gkm95.lx_udd(25.5)) print(lt_gkm95.lx_cfm(25.5)) print(lt_gkm95.lx_bal(25.5)) print(lt_gkm95.t_nqx(x=25.5, t=5.2, n=2.3, method='cfm')) print(lt_gkm95.msn) print('Insurances') print(cf_tv7377.nEx(30, 350)) print(cf_tv7377.Ax(cf_tv7377.w), '=', cf_tv7377.msn[-1]) print(cf_tv7377.Ax(150), '=', cf_tv7377.msn[-1]) print(cf_tv7377.Ax_(30), '=', cf_tv7377.msn[-1])
this_py = os.path.split(sys.argv[0])[-1][:-3] def parse_table_name(name): return name.replace(' ', '').replace('/', '') table_names = ['TV7377', 'GRF95', 'GRM95'] mt_lst = [ rst.SoaTable('../../../soa_tables/' + name + '.xml') for name in table_names ] lt_lst = [mortality_table.MortalityTable(mt=mt.table_qx) for mt in mt_lst] ct_lst = [ commutation_table.CommutationFunctions(i=1.4, g=1, mt=mt.table_qx) for mt in mt_lst ] ''' Exercises ''' mt = lt_lst[0] a = mt.tqx(x=52.4, t=0.2, method='udd') print(a) b = mt.tqx(x=52.4, t=0.2, method='cfm') print(b) c = mt.tpx(x=52.4, t=5.7, method='udd') print(c) d = mt.tpx(x=52.4, t=5.7, method='cfm') print(d) e = mt.t_nqx(x=52.4, t=3.2, n=2.5, method='udd')
import pytest from soa_tables import read_soa_table_xml as rst from essential_life import mortality_table, commutation_table table_names = ['TV7377', 'GRF95', 'GRM95'] mt_lst = [rst.SoaTable('../../../soa_tables/' + name + '.xml') for name in table_names] lt_lst = [mortality_table.MortalityTable(mt=mt.table_qx) for mt in mt_lst] ct_lst = [commutation_table.CommutationFunctions(i=4, g=0, mt=mt.table_qx) for mt in mt_lst] def test_wlai_1(): a = ct_lst[0].t_aax(x=55, m=1, defer=10) b = ct_lst[0].t_ax(x=55, m=1, defer=9) assert 7.774902297975 == pytest.approx(a, rel=1e-12) assert 7.774902297975 == pytest.approx(b, rel=1e-12) def test_wlai_2(): a = ct_lst[1].t_aax(x=55, m=1, defer=10) b = ct_lst[1].t_ax(x=55, m=1, defer=9) assert 10.683798089358 == pytest.approx(a, rel=1e-12) assert 10.683798089358 == pytest.approx(b, rel=1e-12) def test_wlai_3(): a = ct_lst[2].t_aax(x=55, m=1, defer=10) b = ct_lst[2].t_ax(x=55, m=1, defer=9) assert 8.365856975602 == pytest.approx(a, rel=1e-12) assert 8.365856975602 == pytest.approx(b, rel=1e-12)