You are on page 1of 4

1.

October 16, 2020

(Oct 02) Consider a portfolio of 1-year term life insurance independent policies from the file LIFEin-
surance.csv that specifies age and insured amount for each policy, with the additional#benefit of
twice the insured amount in case of accidental death, assuming that 1 out of 10 deaths is accidental
(no matter the age). Use the mortality table in the file mortality.csv and do the following: a)
Calculate the theoretical mean and variance for total claims. b) Through 60000 claims simulations
of the portfolio, estimate mean and variance of total claims, and compare to the theoretical values
obtained in the previous item. c) Also from b) estimate Value at Risk (VaR) of level 99.5% for
total claims. d) Using item a) results, calculate normal approximation of VaR of level 99.5% for
total claims, and compare to the VaR obtained in item c). e) Graph a histogram of the simulations
of total claims from item b) and add to the same graph the approximated normal density.

[3]: import pandas as pd


import numpy as np
from timeit import default_timer
from scipy.stats import norm
import random
import statistics
import matplotlib.pyplot as plt

[4]: mortality=pd.read_csv('/home/carlos-vazquez/Descargas/mortality.csv')
mortality=pd.DataFrame(mortality)
#mortality

[5]: life=pd.read_csv('/home/carlos-vazquez/Descargas/LIFEinsurance.csv')
life=pd.DataFrame(life)
#life

1. Calculate the theoretical mean and variance for total claims.


[6]: #Creo un diccionario como Erdely para que cuando ponga q[50] me de probabilidad␣
,→de que muera 50

q={k: float(v) for k,v in mortality.groupby("AGE")["qx"]}


k=1/10
ES,VS=0,0

for j in range(len(life['AGE'])):
qj = q[life.AGE[j]]
cj = life.INSAMOUNT[j]

1
ES += (1+k)*qj*cj
VS += cj**2*(1.3)*qj-(cj*qj*1.1)**2
print("E(S)= %F " % ES)
print("VAR(S)= %F" %VS)
#print("E(S)= %F",%)

E(S)= 345.349800
VAR(S)= 4202.942122
2. Through 60000 claims simulations of the portfolio, estimate mean and variance of total claims,
and compare to the theoretical values obtained in the previous item.
[12]: #Numero de simulaciones, no pongas las 60,000
m= 2000

[13]: #Fijo semilla


np.random.seed(0)
'''
Hago una matriz donde simulo si se muere o no la persona
las filas son las m simulaciones de cada persona de la tabla life,
cada columna es una simulacion de toda la tabla
'''
inicio=default_timer()
cmuera= [list(np.random.binomial(size=m,n=1,p=q[life.AGE[i]])) for i in␣
,→range(len(life.AGE)) ]

fin=default_timer()
print(fin-inicio)

6.350041851001151

[14]: '''
Fijo semilla
'''
np.random.seed(1)
'''
Creo una matriz donde simulo si la persona muere de un accidente o no,
las filas son simulaciones de cada persona,
las colmnas es la simulacion de toda la tabla, hay 10,000 filas y m columnas
'''
inicio=default_timer()
accident=[list(np.random.binomial(size=m,n=1,p=k)) for i in range(len(life.
,→AGE)) ]

fin=default_timer()
print(fin-inicio)

#accident

8.599715409996861

2
[15]: '''
Creo otra matriz donde si la persona murio le multiplico su suma asegurada, si␣
,→murio por accidente le sumo 2 veces

la suma asegurada
'''
inicio=default_timer()
todo =[[life.INSAMOUNT[i]*cmuera[i][j]*(1+accident[i][j]) for j in range(m) ]␣
,→for i in range(len(life.AGE))]

fin=default_timer()
print(fin-inicio)

#todo

426.1776272920033

[16]: '''
Creo una lista donde guardo la suma de cada columna , es decir una simulacion␣
,→de S,

es unna lista con m simulaciones de S


'''
S=[sum([todo[i][j] for i in range(len(life.AGE))]) for j in range(m)]
print("sim E(S)= %f" %statistics.mean(S))
print("sim VAR(S)= %f" %statistics.variance(S))

sim E(S)= 344.704350


sim VAR(S)= 4229.844838
3. Also, from item 2, estimate Value at Risk (VaR) of level 99.5% for total claims.

[17]: simVaR=pd.DataFrame(S).quantile(0.995)
print("VaR(S)_0.995 = %f " %simVaR)

VaR(S)_0.995 = 523.408500
d) Using item a) results, calculate normal approximation of VaR of level 99.5% for total claims,
and compare to the VaR obtained in item c).

[18]: norm.ppf(0.995,ES,(VS)**0.5)

[18]: 512.3410762579526

[ ]:

e) Graph a histogram of the simulations of total claims from item b) and add to the same graph
the approximated normal density.
[19]: plt.title('SImulations of S')
x=range(100,700)

3
plt.hist(S,label='Simulation of S',edgecolor = 'black',bins=50, alpha=1,density␣
,→=True,color='yellow')

plt.plot(x,norm.pdf(x,loc=ES,scale= (VS**0.5)),color='blue',label='Normal␣
,→aprox')

plt.ylabel('density')
plt.xlabel('S')
plt.legend()
plt.show()

[ ]:

[ ]:

[ ]:

You might also like