simplelife Space Overview

This notebook tutorial explains the basics of spaces, by taking a closer look at spaces defined in the simplelife model as an example.

If you’re viewing this page as a static HTML page on https://lifelib.io, the same contents are also available here on binder as Jupyter notebook executable online (it may take a while to load). To run this notebook and get all the outputs below, Go to the Cell menu above, and then click Run All.

Building a simplelife model

A new simplelife model is created and returned by build function in lifelib project. Import simplelife in your working folder.

import simplelife
In [2]:
model = simplelife.build()
model
Started loading data from 'input.xlsx'.
Loading PolicyData...Done. (2.74secs)
Loading MortalityTables...Done. (0.30secs)
Loading ProductSpec...Done. (0.30secs)
Loading OtherParam1...Done. (0.26secs)
Loading OtherParams2...Done. (0.23secs)
Loading Assumption...Done. (0.30secs)
Loading AssumptionTables...Done. (0.24secs)
Loading Scenarios...Done. (0.34secs)
Input space and its sub spaces are saved in '[project name].mx'.
You can load input data from the saved file instead of 'input.xlsx'
by passing 'load_saved=True' to simplelife.build function.
Out[2]:
<Model simplelife>

Nextime you create the same model, you can give load_saved parameter True, to save time to load input data.

In [3]:
model = simplelife.build(True)
UserWarning: Existing model 'simplelife' renamed to 'simplelife_BAK1'

The previously created model is renamed automatically to avoid name conflict. To get all existing models, get_models modelx API function can be used. get_models returns a dict of all the existing models associated with their names.

In [4]:
import modelx as mx
mx.get_models()
Out[4]:
{'simplelife_BAK1': <Model simplelife_BAK1>, 'simplelife': <Model simplelife>}

Access spaces in the model

In the model, there are child spaces and other objects referenced in the model. spaces property holds pairs of child space names and objects as a dict-like view.

In [5]:
model.spaces
Out[5]:
{'Input': <StaticSpace Input in simplelife>, 'LifeTable': <StaticSpace LifeTable in simplelife>, 'Policy': <StaticSpace Policy in simplelife>, 'Assumption': <StaticSpace Assumption in simplelife>, 'Economic': <StaticSpace Economic in simplelife>, 'BaseProj': <StaticSpace BaseProj in simplelife>, 'PV': <StaticSpace PV in simplelife>, 'Projection': <StaticSpace Projection in simplelife>}

To just get all the names of the spaces, use the idiomatic expression below. To get all the space objects, values method can be used instead of keys.

In [6]:
list(model.spaces.keys())
Out[6]:
['Input',
 'LifeTable',
 'Policy',
 'Assumption',
 'Economic',
 'BaseProj',
 'PV',
 'Projection']

To get a specific space, simply type its name as model’s attribute, or pass the name as a key to spaces property. For example, to get Input space:

In [7]:
model.Input
Out[7]:
<StaticSpace Input in simplelife>

or

In [8]:
model.spaces['Input']
Out[8]:
<StaticSpace Input in simplelife>

A space can in turn contain other spaces, forming a space tree. To access child spaces of a space, the spaces property and attribute access by name as we used on models can be used.

In [9]:
model.Input.spaces
Out[9]:
{'PolicyData': <StaticSpace PolicyData in simplelife.Input>, 'MortalityTables': <StaticSpace MortalityTables in simplelife.Input>, 'ProductSpec': <StaticSpace ProductSpec in simplelife.Input>, 'Assumption': <StaticSpace Assumption in simplelife.Input>, 'AssumptionTables': <StaticSpace AssumptionTables in simplelife.Input>, 'Scenarios': <StaticSpace Scenarios in simplelife.Input>}
In [10]:
model.Input.PolicyData
Out[10]:
<StaticSpace PolicyData in simplelife.Input>
In [11]:
model.Input.spaces['PolicyData']
Out[11]:
<StaticSpace PolicyData in simplelife.Input>

Space overview

Below is the list of the names of the spaces in the simplelife model.

In [12]:
list(model.spaces.keys())
Out[12]:
['Input',
 'LifeTable',
 'Policy',
 'Assumption',
 'Economic',
 'BaseProj',
 'PV',
 'Projection']

Below is a brief explanation of each space directly under the model.

  • Input: Parent space containing spaces and cells hoding data read from the input file.
  • LifeTable: Space containing cells related to commutation functions and actuarial notations.
  • Policy: Parametric space whose dynamic child spaces hold attribute data of each policy read from the input file.
  • Assumption: Parametric space whose dynamic child spaces hold assumptions for each policy.
  • Economic: Parametric space whose dynamic child spaces hold economic assumptions for each scenario.
  • BaseProj: Base space of Projection space, which contains cells to carry out projections.
  • PV: Mixin space to Projection space, which contains cells to calculate present values of cashflows.
  • Projection: Parametric space, whose dynamic child spaces carry out projection for each policy.

Base spaces

BaseProj and PV are base spaces of Projection, and cells defined in those cells are copied into Projection space.

In [13]:
model.Projection.bases
Out[13]:
[<StaticSpace BaseProj in simplelife>, <StaticSpace PV in simplelife>]

Parametric spaces

Among the spaces listed above, parametric spaces along with their parameters are listed below.

  • LifeTable[Sex, IntRate, TableID]
  • Policy[PolicyID]
  • Assumption[PolicyID]
  • Economic[ScenID]
  • Projection[PolicyID, ScenID=1]

To check parameters of a parametric space, call its parameters property.

In [14]:
model.LifeTable.parameters
Out[14]:
('Sex', 'IntRate', 'TableID')

A space becomes parametric when it has the formula property. By calling the formula property, the source code of the function the formula is created from is printed.

The paramters of a parametric space, and their default values if any, are taken from the signature of its associated function.

In [15]:
model.LifeTable.formula
Out[15]:
def _formula(Sex, IntRate, TableID):
    refs={'MortalityTable': Input.MortalityTables(TableID).MortalityTable}
    return {'refs': refs}

Parameter spaces serve as factories to create their child spaces dynamically.

When a parametric space is called with specific arguments being passed to the parameters, a dynamic child space associated with the arguments is created if it’s not yet created, and returned. This can be achieved either by the call operation () or by subscription [] on the parametric space. If a parameter has its default value, and the arguments to the parameter is omitted, the default value is passed.

In [16]:
model.Projection[1]
Out[16]:
<DynamicSpace Projection[1, 1] in simplelife>

Space formula

Space formulas have 2 roles relating to dynamic element spaces.

  • To define the space’s parameters and their default values.
  • To specify arguments used for constructing element spaces.

When the user tries to access the element space of a space, such as model.Projection[1], for the first time, the formula of the space is called in order to pass a dictionary of pairs of parameters and arguments used for constructing and initializing the element space.

The parameters include:

  • bases: a list of base spaces of the element space. If not specified, the space itself becomes the direct base space of the element space.
  • refs: a dictionary of references to be defined in the element space.

Both of the parameters are optional. As we have seen in the above, space formulas are created from function definitions.

In [17]:
model.Projection.formula
Out[17]:
def _formula(PolicyID, ScenID=1):
    refs = {'pol': Policy[PolicyID],
            'asmp': Assumption[PolicyID],
            'scen': Economic[ScenID],
            'DiscRate': Economic[ScenID].DiscRate}
    return {'refs': refs}
The formula code is executed in the namespace associated with the space. Note that the global scope of the function underlying the formula has nothing to do with the formula’s scope. So, names such as Policy, Assumption, Economic that appear in the formula above are defined in the namespace of model.Projection. The names defined in the namespace consists of child cells, child spaces and references in the space,
i.e. the namespace is the union of cells, spaces, refs properties.

dir function on a space lists all the names defined in the namespace associated with the space.

In [18]:
dir(model.Projection)
Out[18]:
['AccumCF',
 'Assumption',
 'AttAge',
 'BenefitAccDth',
 'BenefitAccHosp',
 'BenefitAnn',
 'BenefitDeath',
 'BenefitLiving',
 'BenefitMat',
 'BenefitOther',
 'BenefitSickHosp',
 'BenefitSurg',
 'BenefitSurr',
 'BenefitTotal',
 'ChangeRsrv',
 'Economic',
 'ExpsAcq',
 'ExpsAcqTotal',
 'ExpsCommInit',
 'ExpsCommRen',
 'ExpsCommTotal',
 'ExpsMaint',
 'ExpsMaintTotal',
 'ExpsOther',
 'ExpsTotal',
 'IncomeTotal',
 'InsurIF_Beg1',
 'InsurIF_End',
 'IntAccumCF',
 'InterestNetCF',
 'InvstIncome',
 'NetInsurCF',
 'PV_BenefitDeath',
 'PV_BenefitMat',
 'PV_BenefitSurr',
 'PV_BenefitTotal',
 'PV_Check',
 'PV_ExpsAcq',
 'PV_ExpsCommTotal',
 'PV_ExpsMaint',
 'PV_ExpsTotal',
 'PV_NetCashflow',
 'PV_NetCashflowForCheck',
 'PV_PremIncome',
 'PV_SumInsurIF',
 'Policy',
 'PolsAccDeath',
 'PolsAccHosp',
 'PolsAnnuity',
 'PolsDeath',
 'PolsIF_AftMat',
 'PolsIF_Beg',
 'PolsIF_Beg1',
 'PolsIF_End',
 'PolsLiving',
 'PolsMaturity',
 'PolsNewBiz',
 'PolsOther',
 'PolsRenewal',
 'PolsSickHosp',
 'PolsSurg',
 'PolsSurr',
 'PremIncome',
 'ProfitBefTax',
 'ReserveHospRsrvEnd',
 'ReservePremRsrvEnd',
 'ReserveTotal_End',
 'ReserveUernPremEnd',
 'SizeAnnPrem',
 'SizeBenefitAccDth',
 'SizeBenefitAccHosp',
 'SizeBenefitAnn',
 'SizeBenefitDeath',
 'SizeBenefitLiving',
 'SizeBenefitMat',
 'SizeBenefitOther',
 'SizeBenefitSickHosp',
 'SizeBenefitSurg',
 'SizeBenefitSurr',
 'SizeExpsAcq',
 'SizeExpsCommInit',
 'SizeExpsCommRen',
 'SizeExpsMaint',
 'SizeExpsOther',
 'SizeInvstIncome',
 'SizePremium',
 'SizeReservePremRsrvAftMat',
 'SizeReservePremRsrvEnd',
 'SizeReserveTotalAftMat',
 'SizeReserveUernPremAftMat',
 'SizeReserveUernPremEnd',
 'SizeSumAssured',
 'Space1',
 '__builtins__',
 '_self',
 '_space',
 'last_t']