recursion

Recursion - Applies recursion, shortcut and actuarial formulas

MIT License. Copyright 2022-2023 Terence Lim

class actuarialmath.recursion.PPrint(label: str, *args, **kwargs)[source]

Bases: _Blog

Helper to display recursion steps as actuarial notation in latex format

static q(x: int, s: int = 0, t: int = 1, u: int = 0) str[source]

Return latex string representation of mortality u|t_q_[x+s] term

static p(x: int, s: int = 0, t: int = 1) str[source]

Return latex string representation of survival t_p_[x+s] term

static e(x: int, s: int = 0, t: int = -999, curtate: bool = False, moment: int = 1) str[source]

Return string representation of expected future lifetime e_[x+s]:t term

static E(x: int, s: int = 0, t: int = 1, endowment: int = 1, moment: int = 1) str[source]

Return latex string representation of pure endowment t_E_[x+s]:t term

static IA(x: int, s: int = 0, t: int = -999, b: int = 1, discrete: bool = True) str[source]

Return latex string representation of increasing insurance IA_[x+s]:t term

static DA(x: int, s: int = 0, t: int = -999, b: int = 1, discrete: bool = True) str[source]

Return latex string representation of decreasing insurance DA_[x+s]:t term

static A(x: int, s: int = 0, t: int = -999, u: int = 0, b: int = 1, moment: int = 1, endowment: int = 0, discrete: bool = True) str[source]

Return latex string representation of insurance u|_A_[x+s]:t term

static a(x: int, s: int = 0, t: int = -999, u: int = 0, b: int = 1, variance: bool = False, discrete: bool = True) str[source]

Return latex string representation of annuity u|_a_[x+s]:t term

static m(moment: int, **kwargs) str[source]

Return latex string representation of moment exponent

class actuarialmath.recursion.Recursion(depth: int = 3, verbose: bool = True, **kwargs)[source]

Bases: Reserves

Solve by appling recursive, shortcut and actuarial formulas repeatedly

Parameters:
  • depth – maximum depth of recursions (default is 3)

  • verbose – whether to echo recursion steps (True, default)

Notes

7 types of function values can be loaded for recursion computations:

  • ‘q’ : (deferred) probability (x) dies in t years

  • ‘p’ : probability (x) survives t years

  • ‘e’ : (temporary) expected future lifetime, or moments

  • ‘A’ : deferred, term, endowment or whole life insurance, or moments

  • ‘IA’ : decreasing life insurance of t years

  • ‘DA’ : increasing life insurance of t years

  • ‘a’ : deferred, temporary or whole life annuity of t years, or moments

Blog(*args, **kwargs)[source]

Returns Blog instance to collect messages and display for this query

static blog_options(latex: bool = False, notebook: bool = False)[source]

Static method to change display options for tracing the recursion steps

Parameters:
  • latex – display actuarial notation in latex (True) or raw text (False)

  • notebook – display to jupyter or colab notebook (True) or terminal (False)

Notes

latex and notebook options are set to True if notebook is auto-detected

Examples:

>>> Recursion.blog_options(latex=False)                # display as raw text
>>> Recursion.blog_options(latex=True, notebook=True)  # display latex format
set_q(val: float, x: int, s: int = 0, t: int = 1, u: int = 0) Recursion[source]

Set mortality rate u|t_q_[x+s] to given value

Parameters:
  • val – value to set

  • x – age of selection

  • s – years after selection

  • u – survive u years, then…

  • t – death within next t years

Examples

>>> Recursion(depth=3).set_q(0.02, x=3)
q_x(x: int, s: int = 0, t: int = 1, u: int = 0) float[source]

Probability that [x]+s lives for u, but not t+u years: u|t_q_[x]+s

Parameters:
  • x – age of selection

  • s – years after selection

  • u – survives u years, then

  • t – dies within next t years

Examples:

>>> def ell(x,s): return (1-((x+s)/250)) if x+s < 40 else (1-((x+s)/100)**2)
>>> q = Survival().set_survival(l=ell).q_x(30, t=20)
set_p(val: float, x: int, s: int = 0, t: int = 1) Recursion[source]

Set survival probability t_p_[x+s] to given value

Parameters:
  • val – value to set

  • x – age of selection

  • s – years after selection

  • t – survives next t years

Examples

>>> Recursion(depth=3).set_p(0.99, x=0)
p_x(x: int, s: int = 0, t: int = 1) float[source]

Compute survival probability by calling recursion helper

Parameters:
  • x – age of selection

  • s – years after selection

  • t – survives at least t years

set_e(val: float, x: int, s: int = 0, t: int = -999, curtate: bool = False, moment: int = 1) Recursion[source]

Set expected future lifetime e_[x+s]:t to given value

Parameters:
  • val – value to set

  • x – age of selection

  • s – years after selection

  • t – limit of expected future lifetime

  • curtate – curtate (True) or complete expectation (False)

  • moment – first or second moment of expected future lifetime

e_x(x: int, s: int = 0, t: int = -999, curtate: bool = False, moment: int = 1) float[source]

Compute expected future lifetime by calling recursion helper

Parameters:
  • x – age of selection

  • s – years after selection

  • t – limited at t years

  • curtate – whether curtate (True) or complete (False) lifetime

  • moment – whether to compute first (1) or second (2) moment

set_E(val: float, x: int, s: int = 0, t: int = 1, endowment: int = 1, moment: int = 1) Recursion[source]

Set pure endowment t_E_[x+s] to given value

Parameters:
  • val – value to set

  • x – age of selection

  • s – years after selection

  • t – death within next t years

  • endowment – endowment value

  • moment – first or second moment of pure endowment

E_x(x: int, s: int = 0, t: int = 1, endowment: int = 1, moment: int = 1) float[source]

Compute pure endowment by calling recursion helper

Parameters:
  • x – age of selection

  • s – years after selection

  • t – term of pure endowment

  • endowment – amount of pure endowment

  • moment – compute first or second moment

set_IA(val: float, x: int, s: int = 0, t: int = -999, b: int = 1, discrete: bool = True) Recursion[source]

Set increasing insurance IA_[x+s]:t to given value

Parameters:
  • val – value to set

  • x – age of selection

  • s – years after selection

  • t – term of increasing insurance

  • b – benefit after year 1

  • discrete – discrete or continuous increasing insurance

increasing_insurance(x: int, s: int = 0, t: int = -999, b: int = 1, discrete: bool = True) float[source]

Compute increasing insurance with recursive helper

Parameters:
  • x – age of selection

  • s – years after selection

  • t – term of insurance

  • b – amount of benefit in first year

  • discrete – benefit paid year-end (True) or moment of death (False)

set_DA(val: float, x: int, s: int = 0, t: int = -999, b: int = 1, discrete: bool = True) Recursion[source]

Set decreasing insurance DA_[x+s]:t to given value

Parameters:
  • val – value to set

  • x – age of selection

  • s – years after selection

  • t – term of decreasing insurance

  • b – benefit after year 1

  • discrete – discrete or continuous decreasing insurance

decreasing_insurance(x: int, s: int = 0, t: int = -999, b: int = 1, discrete: bool = True) float[source]

Compute decreasing insurance by attempting recursive helper first

Parameters:
  • x – age of selection

  • s – years after selection

  • t – term of insurance

  • b – amount of benefit in first year

  • discrete – benefit paid year-end (True) or moment of death (False)

set_A(val: float, x: int, s: int = 0, t: int = -999, u: int = 0, b: int = 1, moment: int = 1, endowment: int = 0, discrete: bool = True) Recursion[source]

Set insurance u|_A_[x+s]:t to given value

Parameters:
  • val – value to set

  • x – age of selection

  • s – years after selection

  • u – defer u years

  • t – term of insurance

  • endowment – endowment amount

  • discrete – discrete (True) or continuous (False) insurance

  • moment – first or second moment of insurance

Examples
>>> Recursion().set_interest(i=0.06).set_A(A, x=0)
whole_life_insurance(x: int, s: int = 0, b: int = 1, discrete: bool = True, moment: int = 1) float[source]

Compute whole life insurance A_x by attempting recursion and twin first

Parameters:
  • x – age of selection

  • s – years after selection

  • b – amount of benefit

  • moment – compute first or second moment

  • discrete – benefit paid year-end (True) or moment of death (False)

term_insurance(x: int, s: int = 0, t: int = 1, b: int = 1, moment: int = 1, discrete: bool = True) float[source]

Compute term life insurance A_x:t^1 by attempting recursion first

Parameters:
  • x – age of selection

  • s – years after selection

  • t – term of insurance

  • b – amount of benefit

  • moment – compute first or second moment

  • discrete – benefit paid year-end (True) or moment of death (False)

deferred_insurance(x: int, s: int = 0, b: int = 1, u: int = 0, t: int = -999, moment: int = 1, discrete: bool = True) float[source]

Compute deferred life insurance u|A_x:t^1 by attempting recursion first

endowment_insurance(x: int, s: int = 0, t: int = 1, b: int = 1, endowment: int = -1, moment: int = 1, discrete: bool = True) float[source]

Compute endowment insurance u|A_x:t by attempting recursion first

set_a(val: float, x: int, s: int = 0, t: int = -999, u: int = 0, b: int = 1, variance: bool = False, discrete: bool = True) Recursion[source]

Set annuity u|_a_[x+s]:t to given value

Parameters:
  • val – value to set

  • x – age of selection

  • s – years after selection

  • u – defer u years

  • t – term of annuity

  • b – benefit amount

  • discrete – whether annuity due (True) or continuous (False)

  • variance – whether first moment (False) or variance (True)

Examples

>>> Recursion().set_interest(i=0.06).set_a(7, x=1)
whole_life_annuity(x: int, s: int = 0, b: int = 1, variance: bool = False, discrete: bool = True) float[source]

Compute whole life annuity a_x by attempting recursion then twin first

Parameters:
  • x – age of selection

  • s – years after selection

  • b – annuity benefit amount

  • variance (bool) – return EPV (True) or variance (False)

  • discrete – annuity due (True) or continuous (False)

temporary_annuity(x: int, s: int = 0, t: int = -999, b: int = 1, variance: bool = False, discrete: bool = True) float[source]

Compute temporary annuity a_x:t by attempting recursion then twin first

Parameters:
  • x – age of selection

  • s – years after selection

  • t – term of annuity in years

  • b – annuity benefit amount

  • variance (bool) – return EPV (True) or variance (False)

  • discrete – annuity due (True) or continuous (False)

deferred_annuity(x: int, s: int = 0, t: int = -999, u: int = 0, b: int = 1, discrete: bool = True) float[source]

Compute deferred annuity u|a_x:t by attempting recursion first

Parameters:
  • x – age of selection

  • s – years after selection

  • u – years deferred

  • t – term of annuity in years

  • b – annuity benefit amount

  • discrete – annuity due (True) or continuous (False)