← Back to Cashflow Modelling in Python

Survival rate

Awesome work on the input data preparation! Now, we will move the core part of the model. Let's do this!

1. Importing data

First, import the input data from the file input.py. Open the file model.py.

In the file model.py, you will find an example variable. You may comment it out or remove it to keep the file clean.

Import the following data:

  • policy - policy data,
  • interest_rate - interest rate.

In your script, complete the code below:

# model.py

from input import _____, _____

2. Probability of surviving t months

Create a variable survival_rate(t) that calculates the probability of surviving t months from the start of the contract.

Use the following recursive relationship.

\[ \text{survival_rate(t)} = {}_tp_x = {}_{t-1}p_x \cdot (1-q_{x+t-1}) \]

Assume that:

  • for t = 0, the survival probability equals 1,
  • the monthly probability of death is constant and equals to policy.get("mortality_rate").

Complete the function below:

# model.py

@variable()
def survival_rate(t):
    if t == 0:
        return 1
    else:
        q = policy.get("_____")
        return survival_rate(t - 1) * (_____ )

Task:

  • Fill in the name of the attribute that contains the monthly probability of death.
  • Write the expression for the probability of surviving t months.
  Next