---
title: "HowToDetLifeInsurance"
author: "Joaquin Auza & Maria Sol Alvarez"
date: "07/09/2020"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{HowToDetLifeInsurance}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
  
---


DetLifeInsurance is an R package designed to provide tools for:

* Deterministic valuation of actuarial reserves and life insurance and annuities premiums (both constant and variable amounts supported).
* Deterministic valuation of actuarial life insurance and annuities premiums for a group of insurees (constant amounts).
* Applying fractional age assumptions to life tables, and generating new ones based on mortality laws.
* Calculation of equivalent interest-discount rates as well as future and present value of annuities.
* Calculation of loan amortization schedule.

In addition, 47 commonly used mortality tables are included as data.

```{r include=FALSE}
library(DetLifeInsurance)
```

Be aware that functions use 15 digits when performing calculations. 

## Life insurance and annuities premiums

For calculating the actuarial present value:

* Pure Endowment:
`E(x, n, i = 0.04, data, prop = 1, assumption = "none", cap = 1)` For a insuree

`Em(x, n, i=0.04, data, prop = 1, assimption = "none", type = "joint", quant = 1, cap = 1)` For a group of insurees.

* Life Insurance: 

  * Life insurance: `A.(x, h, n, k = 1, i = 0.04, data, prop = 1, assumption = "none", cap = 1)` For a insuree
  
  `Am.(x, h, n, k = 1, i = 0.04, data, prop = 1, assumption = "none", cap = 1)`
  * Varying life insurance according to a arithmetic progression: `Av.(x, h, n, k = 1, r = 1, i = 0.04, data, prop = 1, assumption = "none", variation = "none", cap = 1)`  
    _For an increasing life insurance coverage, 'r' must be 1._
  *  Varying life insurance according to a geometric progression.: `Avg.(x, h, n, k = 1, r = 1, i = 0.04, data, prop = 1, assumption = "none", variation = "none", cap = 1)` 
  * Decreasing life insurance: `AD.(x, h, n, k = 1, i = 0.04, data, prop = 1, assumption = "none", variation = "none", cap = 1)` 
  * Continuous life insurance: `ACont.(x, h, n, i = 0.04, data, prop = 1, assumption = "UDD", cap = 1)` 
  
  
  
* Life Annuity:

  * Life annuity: `a(x, h, n, k = 1, i = 0.04, data, prop = 1, assumption = "none", cap = 1)`  For a insuree
  
  `am(x, h, n, k = 1, i = 0.04, data, prop = 1, assumption = "none", cap = 1)`  For a group of insurees
  * Varying life annuity according to a arithmetic progression: `av(x,h,n,k = 1,r = 1, i = 0.04, data, prop = 1, assumption = "none", variation = "none", cap = 1)`
  _For an increasing life annuity coverage, 'r' must be 1._
  *  Varying life annuity according to a geometric progression: `avg(x,h,n,k = 1,r = 1, i = 0.04, data, prop = 1, assumption = "none", variation = "none", cap = 1)` 
  *  Decreasing life annuity: `aD(x, h,  n, k = 1, i = 0.04, data, prop = 1, assumption = "none", variation = "none", cap = 1)`
  * Continuous life annuities: `aCont(x, h, n, i = 0.04, data, prop = 1, assumption = "constant", cap = 1)` 

The mortality table in data must be a data frame with the age -an integer- in the first column (from 0 to omega), and the second column, the probability of death. 

For calculating an annualized premium:
 `PremiumFrac(px1, x, m, k, i = 0.04, data, prop = 1, effect = "yes", assumption)`
 
 
 
#### Examples
```{r}
Px1 <- Av.(x=25, h=5, n=10,r= 1,i = 0.035, data=CSO80MANB, cap = 150000) #Actuarial Present Value of the increasing life insurance.
Net_Prem <- PremiumFrac(Px1,x=25,m=5, k=1, i=0.035, data=CSO80MANB) #Net premium to be paid at the begining of each year of the defferal term.
```

```{r}
Prem<- a(x=45,h=0, n=20,k=2,i=0.06, data=CSO80FALB, cap=12000, assumption = "constant") #Actuarial Present Value of the life annuity, paid twice a year.
Annualized_Prem<- PremiumFrac(Prem,x=45,m=10,k=4,data=CSO80FALB, i=0.06, assumption = "constant") #Annualized  value of the premium to be paid four times a year for 10 years.
PremQuart<-Annualized_Prem/4

```



## Actuarial reserves

Calculates the reserve up to the moment 't':

- Pure Endowments: ` V_E(px, x, n, cantprem = 1, premperyear = 1, i = 0.04, data, prop = 1, assumption = "none", cap, t) ` 

- Life annuity: ` V_a(px, x, h, n, k = 1, cantprem = 1, premperyear = 1, i = 0.04, data, prop = 1, assumption = "none", cap, t) `  

` V_A.(px, x, h, n, k = 1, cantprem = 1, premperyear = 1, i = 0.04, data, prop = 1, assumption = "none", cap, t) ` Life insurance

- Varying life annuity according to a arithmetic progression: ` V_av(px, x, h, n, k = 1, r, cantprem = 1, premperyear = 1, i = 0.04, data, prop = 1, assumption = "none", variation = "none", cap, t) ` 

-  Varying life insurance according to a arithmetic progression: ` V_Av.(px, x, h, n, k = 1, r, cantprem = 1, premperyear = 1, i = 0.04, data, prop = 1, assumption = "none", variation = "none", cap, t) `

- Varying life annuity according to a geometric progression: ` V_avg(px, x, h, n, k = 1, r, cantprem = 1, premperyear = 1, i = 0.04, data, prop = 1, assumption = "none", variation = "none", cap, t) ` 

- Varying life insurance according to a geometric progression:` V_Avg.(px, x, h, n, k = 1, r, cantprem = 1, premperyear = 1, i = 0.04, data, prop = 1, assumption = "none", variation = "none", cap, t) ` 

- Decreasing life annuity: ` V_aD(x, h, n, k = 1, i = 0.04, data, prop = 1, assumption = "none", variation = "none", cap = 1) ` 

- Decreasing life insurance: ` V_AD.(px, x, h, n, k = 1, cantprem = 1, premperyear = 1, i = 0.04, data, prop = 1, assumption = "none", variation ="none", cap, t) ` 

```{r}
Prem<-av(x=45,h=7,n=13,r=0.05, i=0.045, data = CSO80FALB, cap=12000) #Actuarial present value of a varying life annuity that follows an arithmetic progression
AnnualPrem<-PremiumFrac(px1 = Prem, x=45, m=7, k=1, i=0.045,data= CSO80FALB) #Annual premium to be paid at the beginning of each year of the deferral term. 
V_av(px= AnnualPrem, x=45, h=7, n=13, r=0.05, cantprem = 7, premperyear = 1, i=0.045, data= CSO80FALB, cap=12000, t=20) #Reserve of the coverage up to the year 20
```

```{r}
Prem<- Avg.(x=24, h=10,n=10, k=4, r=0.06, data= CSO80MALB, i=0.03, cap= 100000, assumption = "UDD", variation = "inter") #Actuarial present value of a varying life insurance according to a geometric progression
AnnualPrem<- PremiumFrac(px1 = Prem, x=24, m=10, k=1, data= CSO80MALB, i=0.03) #Annual premium to be paid at the beginning of each year of the deferral term 
V_Avg.(px =AnnualPrem, x=24, h=10, n=10, k=4, r=0.06, i=0.03, data= CSO80MALB, cantprem = 10, premperyear = 1, assumption = "UDD", variation = "inter", cap=100000, t=240) #Reserve of the coverage up to the month 240

```

```{r}
Prem<- A.(x=40, h=0, n=15, k=1, i=0.06, data= CSO80FANB, cap=105000) #Actuarial present value of the life insurance
AnnualizedPrem<-PremiumFrac(px1 = Prem, x=40, m=15, k=4, data= CSO80FANB, i=0.06, assumption = "UDD") #Annualized value of the premium to be paid four time a year. 
V_A.(px = AnnualizedPrem/4, x=40, h=0, n=15, cantprem = 60, premperyear = 4, data=CSO80FANB, i=0.06, assumption = "UDD", t= 180, cap=105000)  #Reserve of the coverage up to the month 180
```

```{r}
Prem<- aD(x=20, h=5, n=5, k=2, i=0.055, data= CSO80MANB, assumption = "constant", variation = "intra", cap=12000) #Actuarial present value of a decreasing life annuity
Annualized_Prem<- PremiumFrac(px1 = Prem, x=20, m=5, k=12, assumption = "constant", data=CSO80MANB, i=0.055) #Annualized value of the premium to be paid monthly
V_aD(px= Annualized_Prem/12 ,x=20,h=5,n=5, k=2, premperyear = 12, cantprem = 60, i=0.055, data= CSO80MANB, assumption = "constant", variation = "intra", cap=12000, t=120) #Reserve up to the month 120
```

```{r}
ages<-c(20,34,36,50)
Px<-Am.(x=ages,h=5,n=30,i=0.06,data=CSO80MANB,ndeath=2,cap=200000) #Actuarial present value of a life insurnace for a group to be paid when the second death occur.
Px/am(x=ages,h=0,n=5,i=0.06,data=CSO80MANB,type="atleast",quant=3) #Annual premium to be paid when at least 3 of the group are still alive
```



## Life tables

Creates a mortality table under a certain law of mortality: 

- `Table_Moivre(x0, omega)` de Moivre's law
- `Table_Dormoy(x0, omega, a)` Dormoy's law 
- `Table_Makeham(x0, omega, A, B, C)` Makeham's law
- `Table_Gompertz(x0, omega, B, C)` Gompertz's law

Creates a fractional mortality table for a given mortality table:

`Fractional_table(data, frac, i = 0.04, assumption = "UDD")`

#### Examples
```{r}
Table_Gompertz(x0=0, omega=100, B=0.00008, C= 1.07)

```

```{r}
MortTable<-Table_Moivre(0,100)
Px<- A.(x=35,h=0,n=20,i=0.03,data= MortTable, cap=10000) + E(x=35,n=20, i=0.03, data=MortTable, cap=10000) #A Dotal (endowment) coverage
AnnualPrem<-PremiumFrac(px1 = Px, x=35, m=10, k=6, data= MortTable, effect = "yes", assumption = "UDD") #The annualized value of the premium
AnnualPrem/6 #The premium to be paid every two months
```


```{r}
Fractional_table(CSO58MANB, frac=3, i= 0.047, assumption = "UDD")

```


## Loan Amortization


`Loan_amortization(V0, n, i, i2 = 0, alic = 0, ins = 0, method)` For calculating the loan amortization schedule.


```{r}
Loan_amortization(V0=130000,n=15,i=0.04,method = "interest_only")

```

```{r}
Loan_amortization(V0=123000,n=12,i=0.06,alic=0.21,method="constant_installment")
```
```{r}
Loan_amortization(V0=300000,n=10,i=0.03,ins=0.01,method = "constant_principal")

```



` Payment_Protection(x, n, k = 1, V0, i = 0.04, ip = 0.04, data, prop = 1, type = "outstanding_debt", method = "interest_only")` For calculating the actuarial present value of a loan insurance.



If "outstanding_debt" is chosen, the insurance company pays the outstanding debt at the moment of the next theoretical payment. If "payments" is selected, the company takes responsibility for the payment
of each installment at each accorded moment. 


```{r}
Px1<-Payment_Protection(x=35,n=15,V0=1050000,i=0.05,ip=0.055,data=CSO58MALB,type="outstanding_debt",method="constant_principal") #Actuarial present value of the coverage
Annual_Px1 <- PremiumFrac(px1=Px1,x=35,m=5, k = 1,i=0.05,data=CSO58MALB) #Annual Premium to be paid
V_Payment_Protection(px=Annual_Px1,x=35,n=15,k=1,cantprem=5,premperyear=1,i=0.05,ip=0.055,data =CSO58MALB, type="outstanding_debt",method="constant_principal",V0=1050000,t=15) #Reserve uo to the year 15


```

```{r}
Px1 <- Payment_Protection(x=30,n=10,k=2,V0=1000000,i=0.035,ip=0.06,data=CSO80FANB,type="payments",method="constant_instalment") #Actuarial present value of the coverage
Annual_Px1 <- PremiumFrac(px1=Px1,x=30,m=3, k = 6,i=0.035,data=CSO80FANB, assumption = "UDD") #Annualized value of the premium to be paid every two months
V_Payment_Protection(px=Annual_Px1/6,x=30,n=10,k=2,cantprem=18,premperyear=6,i=0.035,ip=0.06,data =CSO80FANB, type="payments",method="constant_instalment",V0=1000000,t=120) #Reserve up to the month 120

```


## Annuities and Interest Rates

 `sf(l = 0, n, i) ` For calculating the final value of an annuity
 
 `af(l = 0, n, i) ` For calculating the present value of an annuity
 
 `Rate_converter(num, rate1, m, rate2, k, type = "days") `  For converting nominal and effective interest and discount rates.

```{r}
i<-Rate_converter(0.4,"j",60,"i",365, "days") #Convert the annual nominal rate of 2 periods to a effective annual interest rate
af(0,20,i)*1200 # Calculate the present value of an annuity-due of amount $1200 paid annually for 20 years at the rate of interest of i.
```
```{r}
i<-Rate_converter(0.07,"f",4,"i",1, "frac") #Convert the annual nominal discount rate of 4 periods to a effective annual  interest rate
sf(1,10,i)*3000 # Calculate the final value of an annuity-immediate of amount $3000 paid annually for 20 years at the rate of interest of i.
```

