Saturday, 21 December 2013

Mortality Data

Data Sources


There are a number of alternative sources of mortality data found on the web. I give below some sample links.

General Databases

Government Statistics

 Actuarial Organisations

 The latter seems the most straightforward source to use. I will describe this source in more detail.

SOA Tables

The site contains a large number of tables organised by nation. The tables can be downloaded in a number of formats including Excel and CSV.

A more interesting format is XML. This is actual a standard format referred to as XTbML, which is defined by an ACORD standard.

This includes not only the rates, but also standard names and descriptions and related metadata, such as minimum and maximum ages.

Getting Tables

It is of course possible to simply download the tables of interest from the site. However, it would be useful to be able to automate this a little.

F# has a number of features that make loading data from the web simple and efficient, such as asynchronous workflows. To make this even easier, there is a client library for HTTP called Http.fs.

To download a range of files, you can then simply use this code:
 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
open HttpClient
/// get mortality tables in xml format from SOA site given range of integers 
let getSOAtabs ints = 
                  let targurl i = @"http://mort.soa.org/data/t" + i.ToString() + ".xml"
        
                  let resps = 
                  ints
                  |> List.map (fun i -> createRequest Get (targurl i) |> getResponseAsync)
                  |> Async.Parallel
                  |> Async.RunSynchronously
                  resps
                  |> List.ofArray
                  |> List.map (fun r -> r.EntityBody.Value)
let ints = [ 1..10 ]
let morts = getSOAtabs ints
//write files
let mortfol = @"C:\MortTabs"
List.iter2 (fun i m -> (File.WriteAllText(Path.Combine(mortfol, "t" + i.ToString() + ".xml"), m))) ints morts

The function getSOAtabs reads in a list of integers. It then loads in parallel all the corresponding mortality tables from the SOA site. This list of tables is then written to a folder C:\MortTabs. This produces these results:



No comments:

Post a Comment