Loading RiskMetrics™ data to Mathematica

Purpose

RiskMetrics™ datasets are provided daily by the RiskMetrics™ Group. They can be downloaded from  http://www.riskmetrics.com/rm/cde/index.cgi or ftp://riskmetrics.com/pub/riskmetrics/datasets and stored as files. Choosing the first adress you can select the datasets you need, whereas the second address provides only the complete datasets. However, the second address seems to be more stable for automatic downloads. The MathLink Add in  riskmetrics.exe provides the functionality to filter the relevant datasets from the complete datasets which speeds up the loading by several orders. This notebook documents the functionality and shows how Mathematica can be used to work with the RiskMetrics™ datasets.

SetUp

Before loading the MathLink Add in  riskmetrics.exe, set the current working directory to the directory of your RiskMetrics files.

\(SetDirectory["G:\temp\RiskMetrics"]; 

    link\  = \ Install["riskmetrics"]; \)

The RiskMetrics™ data contain  volatility files and correlation files. The volatility files contain price/yield, price volatility and yield volatility for the different series, whereas the correlation files show the correlation between the series. RiskMetrics™ provides daily, monthly and regulatory datasets. Accordingly, there are three volatility files and three correlation files for each date. The files for May 19, 1999 can be shown by the use of the Mathematica command FileNames.

allfiles = FileNames["19990519.*"]

The first character of the suffix distinguishes between B regulatory,  D daily and M monthly datasets. The last characters of the suffix denote CF correlation files and VF volatility files.

Loading volatility files

The command RMReadVol[FileName, Filter1, Filter2] reads RiskMetrics™ volatility datasets from file. Filter 1 is the pattern for the currency, Filter 2 is the pattern for the series.  See RiskMetrics™ Technical Dokument, Fourth Edition, Chapter 10, for format of records. 
This returns all datasets from the daily volatility file of May 19, 1999 where "EUR" matches the currency and "S" the series (i.e. the Euro swap data)

RMReadVol["19990519.DVF", "EUR", "S"]

A "." matches all sets in the file. Here, load all fx rates

RMReadVol["19990519.DVF",  ".",  "XS"]

Example: Definition and illustration of the german zero curve:

1: Load the relevant data:

(data = RMReadVol["19990519.DVF", "DEM", "Z"]) // 
      TableForm

2. Select the time (in years) and the corresponding zero rate from data:

timerates = 
      {ToExpression[StringTake[#[[2]], -2]], #[[3]]}& /@ 
        data

3. Define the german zero curve as a InterpolatingFunction of the given spot rates:

r = Interpolation[timerates]

4. Visualization of the german zero curve:

Plot[r[t], {t, 2, 30}, PlotRange -> {{0, 30}, {0, 6}}, 
      AxesLabel -> {"time", "rate"}]

Missing Data

The moneymarket yields are missing in the volatility datasets (NA not available):

data = RMReadVol["19990519.DVF", "EUR", "."]

Define a function that returns the missing moneymarketyield as a function of time, pricevolatility and yieldvolatility:

mmyield[t_, σp_, σy_] := 
      σ(p/(σy t (1 - σ(p/(σy t)))))

The following function inserts the missing data for moneymarket recordsets:

\(insertmmyield[dataset_List] := 
      \((\(If[StringMatchQ[#[\([2]\)], "R*"], 

              			{#[\([1]\)], #[\([2]\)], 
                mmyield[\ 
                  ToExpression[StringTake[#[\([2]\)], \(-3\)]]/360., #[
                    \([5]\)], #[\([6]\)]\ ], #[\([4]\)], #[\([5]\)], #[
                  \([6]\)]}, #\ ]&\)\ /@\ dataset)\)\)

insertmmyield[data]

Load correlation files

The command RMReadCorr[FileName, Filter1, Filter2,Filter3,Filter4] reads RiskMetrics™ correlation datasets from file. One complete correlation file contains more than 100,000 recordsets. When loading correlation data to Mathematica, it is recommended to use filters as selection criterias. One recordset contains two instruments. Filter 1 and 2 select the currency and the series of the first instrument, whereas Filter 3 and 4 specify the second instrument.  See RiskMetrics™ Technical Dokument, Fourth Edition, Chapter 10, for format of records. 
Load all correlations between and within the DEM equity index and CAD money market rates. The filter "R" matches the moneymarket series R030 - R360. When loading correlation data, be aware that a correlation matrix is symmetrical around the diagonal. A file comprises the upper triangular correlation matrix.

\(data = 
      Join[RMReadCorr["19990519.DCF", \ "CAD", \ "R", \ 
          "DEM", \ "SE"], 
        RMReadCorr["19990519.DCF", \ "DEM", \ "SE", \ "DEM", 
          \ "SE"], 
					
        RMReadCorr["19990519.DCF", \ "CAD", \ "R", \ "CAD", \ 
          "R"]\ ]\)

Example: Buildung a correlation matrix

1. Get a list of all series codes:

seriescode =  
      Union[{#[[1]], #[[2]]}& /@ data, 
        {#[[3]], #[[4]]}& /@ data]

2. Determine the position of the correlation in the matrix.

pos = 
      ({Position[seriescode, {#[[1]], #[[2]], ___}],  
                Position[seriescode,  {#[[3]], #[[4]], ___}], 
                 #[[5]]} // Flatten)& /@ data

3. Duplicate the correlations for the lower triangular correlation matrix and sort the data

datasort = 
      Join[pos, {#[[2]], #[[1]], #[[3]]}& /@ pos]  //  
          Union  //  Sort

4. Build the correlationmatrix

\(ρ\  = \ 
      Partition[\(#[\([3]\)]&\)\ /@\ datasort, Length[seriescode]]; 

    ρ\  // \ MatrixForm\)

Covarianz Matrix

For generating a covarianz matrix, volatility and correlation data is required. Both lists must comprise identical RiskMetrics™ series in the same order.

\(data = \ 
      Join[RMReadVol["19990519.DVF", \ "DEM", \ "SE"], 

        \ \ \ \ \ \ \ 		
        RMReadVol["19990519.DVF", \ "CAD", \ "R"]]\)

Adjust the order of the volatility data to the order of the correlation data

datasort  =  
      Table[Flatten[
          Select[data, {#[[1]], #[[2]]}  ==  
                seriescode[[i]]&]], {i, Length[seriescode]}]

Extract the price volatility

σ  =  #[[5]]& /@ datasort

and compute the covarianz matrix.

(Σ  =  
          ρ * Outer[Times,  σ,  σ])  //  MatrixForm

Cleaning up:

Uninstall[link]