Monday, 23 June 2014

Stochastic Mortality (Limited)

Introduction

I have now added some stochastic mortality functionality to Mortality Manager. However, I have limited this to just generating projected rates using R and simply adding the ability to use these rates.

This post therefore covers how I created the rates using R (but through F#) and how these rates can be viewed and plotted in Mortality Manager.

Generating the Rates

To generate the rates I used the following:
To create the rates you first need to ensure that the demography package is downloaded to make it available to R. You then need to download the mx and population rates, which are needed as inputs to the Lee-Carter code in the R package.

I chose to use UK rates. These contain rates for the general UK population from 1922 to 2011. I downloaded the two required files - Mx_1x1.txt and Exposures_1x1.txt. Here is a sample from each:

Mx_1x1.txt


United Kingdom, Death rates (period 1x1)     Last modified: 06-May-2013, MPv5 (May07)

   Year      Age       Female       Male         Total
   1922        0     0.070053     0.092574     0.081500
   1922        1     0.024976     0.027255     0.026130
   1922        2     0.012143     0.012979     0.012566
   1922        3     0.006072     0.006324     0.006199
   1922        4     0.004426     0.004305     0.004365
   1922        5     0.003861     0.003781     0.003821
   1922        6     0.002958     0.003157     0.003058

...

Exposures_1x1.txt

United Kingdom, Exposure to risk (period 1x1)     Last modified: 06-May-2013, MPv5 (May07)

   Year      Age       Female          Male         Total
   1922        0       446849.54    461921.95    908771.49
   1922        1       452480.82    463987.76    916468.59
   1922        2       427577.73    439102.38    866680.10
   1922        3       359372.64    367637.22    727009.86
   1922        4       326025.47    331448.37    657473.84
   1922        5       349516.32    355195.04    704711.36
   1922        6       379820.11    385139.04    764959.16
   1922        7       404411.29    407120.72    811532.01
   1922        8       418049.48    419024.29    837073.76
   1922        9       418158.45    420482.13    838640.58 

...

I then created an F# Console Application to generate the projected rates:

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
24: 
25: 
26: 
27: 
28: 
29: 
30: 
31: 
open System
open System.IO
open RProvider
open RDotNet
open RProvider.demography
open MortMgr

[<EntryPoint>]
let main argv = 
    let uk = R.read_demogdata("c:/Mx_1x1.txt","c:/Exposures_1x1.txt", "mortality", label="UK")
    let maxage = 110
    let h,nsim,ser,snm = 100,1000,"Male","UK Males"
    let LCm=R.lca(uk,series=box ser,max_age=box maxage,interpolate=box true)
    let fcastLCm=R.forecast_lca(LCm,h=h)
    let yrs = fcastLCm.AsList().["year"].AsInteger()|>Seq.toArray
    let sim=R.simulate_fmforecast(namedParams["object",box fcastLCm;"nsim",box nsim])
    let simv = sim.AsNumeric()|>Seq.toArray
    let qxts =
        let ans = Array.zeroCreate nsim
        for s = 0 to nsim-1 do
            let qxt = Array2D.create 121 h 1.0
            for a = 0 to 120 do
                for t = 0 to h-1 do
                    let mxt = simv.[s*h*(maxage+1)+t*(maxage+1)+(min a maxage)]
                    qxt.[a,t] <- mxt/(1.0+0.5*mxt)
            ans.[s]<-qxt
        ans
    let stbl = {StochName=snm;StartYear=yrs.[0];EndYear=yrs.[yrs.Length-1];Nosims=qxts.Length}
    IndStoch.addtbl stbl
    qxts|>Array.mapi (fun i q -> IndStoch.addsim stbl (i+1) q)|>ignore
    0 // return an integer exit code

Lines 1 to 6 set up the required references. Line 3 and 4 reference the R Provider. Line 5 references the specific R package. Line 6 references Mortality Manager, which is used to store the generated simulations.

Line 10 loads the files using R. Lines 11 and 12 set up some required parameters. In this case, we are going to generate 1,000 simulations for Males, projecting for 100 years.

Line 13 uses R to fit the input data to the Lee-Carter model. Line 14 then generates the forecast function. Line 15 gets the years to be forecast and converts this to an array. Line 16 then generates 1,000 sample forecast simulations. Line 17 then converts these simulations into an array.

Lines 18 to 27 then converts these simulations of mx to a 2D array of qx values.

Lines 28 to 30 store the results. Lines 28 and 29 just stores the general summary of the data as a Record Type. Line 30 separately stores each simulation. These results are stored as Json using Json.NET.

 Viewing the Simulations

I have just added a single Task Pane to allow you to view these simulations.


This provides the ability to view an individual simulation using the View Simulation button. In this case, simulation 1 is displayed. This shows the projected values of qx for each age and for each projected year. Note that the projection starts at 2012, one year after the last year of the data from the Human Mortality Database.

Simulation from a Specific Age/Year

The other facility provided is to view projected rates for a specified age and year.

This allows you to get simulations for a particular age from a particular year using the View Simulations button. This shows how the model might simulate rates of mortality for a particular individual. In this case, we are simulating from year 2020 for a male then aged 30.

You also get a plot of these simulations:


Mortality Manager v004

I have also packaged up Mortality Manager into a new distribution. These files are stored with the source code on bitbucket. The application is available in macro enabled workbooks for 32 bit and 64 bit versions of Excel:
The application is also available as an XLL, again for 32 bit or 64 bit Excel:
If you use these, you may also want to download an example workbook which uses the application:

2 comments:

  1. Hello Phil.

    I stumbled across your blog here by accident whilst searching for some of my old Project Euler in F# blogs.
    I noticed that you referenced a couple of F# snippets (Single/Joint Life Annuity) that I'd posted some time ago to the F# snippets site. I'm really enjoying your posts here, and I'm going to devote some time to studying them.
    Thanks
    Kevin

    ReplyDelete
    Replies
    1. Kevin,
      Thanks very much for your interest in the blog and your comments and for creating the F# snippets.
      I hope you like the material and if you notice any areas for improvement it would be great to know these.
      Regards
      Phil

      Delete