Monday, 21 April 2014

Mortality Projection Functions

Overview

I have now completed the Ribbon and Custom Task Panes relating to Mortality Projections within Mortality Manager. This includes functionality to:
  • Generate Mortality Projection using the CMI 2012 model 
  • Save these Generated projected rates
  • View and Edit Saved Projected Rates (This also allows you to create these by hand or sourced from other data)
  • Compare two sets of Projected Rates
  • Generate Single Life and Joint Life Mortality Functions (qx, dx, lx, ex) from a set of Projected Rates (depending on a specified age and calendar year)
  • Generate Single Life and Joint Life Commutation Functions (Dx, Nx, Sx, Cx, Mx, Rx) from a set of Projected Rates (depending on a specified age and calendar year)
  • Generate Single Life and Joint Life Present Value Functions (ax, Ax, Px) from a set of Projected Rates (depending on a specified age and calendar year)
The updated code is available on bitbucket. Mortality Manager now looks like this:

Persisting using Records and Json

In a very similar way to the Mortality tables, I have used a combination of F# Record Types and Json.NET to support saving these projected rates. The Record used is defined as:
1: 
2: 
3: 
4: 
5: 
6: 
7: 
8: 
9: 
type Qxt = 
    {   BaseName : string
        BaseRates : float []
        BaseDate : System.DateTime
        ProjName : string
        StartYear : int
        EndYear : int
        QxtName : string
        QxtRates : float [,] }

This includes details of the Base Mortality rates used (their name, the rates  and the assumed date these rates apply), then the name of the Projection used and then details of the projected rates (start and end year, the name used to identify these rates and the rates as a 2D array).

These can be simple saved to Json using this code:
1: 
2: 
3: 
4: 
/// save Qxt to json
let save (ptbl : Qxt) jsonfil = 
    let jsontxt = JsonConvert.SerializeObject(ptbl)
    File.WriteAllText(jsonfil, jsontxt)

The saved sets of rates can be viewed using the View/Create option for Projections in Mortality Manager:



Re-using the Mortality Code 

To generate the functionality to calculate Mortality Functions, Commutation Functions and Present
Value functions, the simplest approach was to re-use the code already written for Mortality Tables. To do this, it was simple to generate an intermediate Mortality Table Record Type and then pass this to the existing functions.

To create this intermediate type, this code was used:

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
// get mort table given age and year and proj table
let getmtbl iage iyr qxts = 
    //get qs from this age and year forwards
    let pj_qxs = pj_qx iage iyr qxts
    //zero fill for prior ages
    let rts = [|0..120|]|> Array.map (fun a -> if a <iage then 0.0 else pj_qxs.[a-iage])
    //return mortality table record
    { Name = (qxts.QxtName + " at age " + iage.ToString() + " and year_" + iyr.ToString())
      MortInfo = dummy_mi
      MortRates = Ultimate(rts) }


Please note the need to supply a specific age and year to allow a single array of rates to be extracted from the Projected Rates. Also, note that the rates have been zero filled for ages below the specified age. The function just returns a dummy Mortality Table Record with a dummy name and info, just holding the projected values as a set of Ultimate rates.  The results using this code therefore apply to someone who is the specified age in the specified year and allows for mortality improvements from that date onwards. 

The next blog will complete the functionality for Mortality Projections by adding related User Defined Functions and the existing UDFs will be revisited allowing for the extra options available under FCell 2.1.

No comments:

Post a Comment