Putting Mathematica to work:
Integrated Value-at-Risk Modeling

Statement of the Problem

To demonstrate some useful features of Mathematica's design, we turn to Benninga and Wiener's short paper on Value-at Risk (VaR), the modern risk-measuring index. VaR is defined as the lowest quantile of the potential losses that can occur within a given portfolio during a specific time period. The time period T and the confidence level q are the two major parameters, which are chosen carefully and are dependent upon the goal of the risk management (regulatory reporting, corporate risk management, etc.). For example, suppose that a portfolio manager has a daily VaR equal to $1 million at 1%.  This means that, assuming normal market conditions, there is only one chance in a hundred that there will be a daily loss bigger than $1 million. While this problem can be easily stated, finding a solution can frequently be computationally difficult.

Needs["Statistics`Master`"]
Needs["Statistics`MultiDescriptiveStatistics`"]

The Single Asset Problem: Loading Mathematica Packages and Constructing a Model

First, we load the relevant add-on packages and use Mathematica to find the 5% VaR of $100 million invested in a single asset, lognormally distributed with expected return of 10% and variance of 30%. This is stated formally below (Hull, 2000).

	Log((Subscript[v, T])) ∼ 
        Normal [(μ - ((σ^2)/2)) T + Log(v), σT]

100 - 
      Exp[Quantile[
          NormalDistribution[
            Log[100] + ( .1 -  .((3^2)/2)),  .3],  .05]]

In other words, there is a 5% chance that $100 million invested in this single asset will lose more than $35,496,800.

PDF = 
        D[CDF[NormalDistribution[Log[100] + ( .1 -  .((3^2)/2)),  .3], 
            Log[x]], x];
\(\(a = 
        FilledPlot[{pdf}, {x, 0, 200}, PlotRange -> \ All, 
          RGBColor[0.312505, \ 0.203128, \ 0.445319] 
              AxesLabel -> {\*""(Subscript[Portofolio, t + 1])"", \*""P[(Subscript[Portofolio, t + 1])]""}];\)\)
b = 
        Graphics[
          Line[{{100 - 35.4968,  .0055}, {65, 
                0}}]];
Show[a, b]

Accounting for Correlations: Matrix Algebra with Mathematica

Of course, real portfolios have more than one asset. One key difficulty in modeling the behavior of portfolios with more than one asset is measuring, and then accounting for, correlations in the movement of asset prices. The above model is now generalized to accommodate three assets, the prices of which are lognormally distributed (meaning that returns are normally distributed).

	Portfolio size = $100 million
	
	Portfolio allocation = x = (({{(Subscript[x, 1])}, {(Subscript[x, 2])}, {(Subscript[x, 3])}}))= (({{0.30}, {0.25}, {0.45}})),
	
	Mean returns =μ = (({{(Subscript[μ, 1])}, {(Subscript[μ, 2])}, {(Subscript[μ, 3])}})),
	
	Variance-covariance = S = (({{(Subscript[σ, 11]), (Subscript[σ, 12]), (Subscript[σ, 13])}, {(Subscript[σ, 21]), (Subscript[σ, 22]), (Subscript[σ, 23])}, {(Subscript[σ, 31]), (Subscript[σ, 32]), (Subscript[σ, 33])}})).

This program will work in three pieces.  First, some preliminary definitions are made.

X = Table[(Subscript[x, i]), {i, 3}];
S = 
        Table[(Subscript[σ, i, j]), {i, 3}, {j, 
            3}];
means = Table[(Subscript[μ, k]), {k, 3}];
portfolioMean[X_, means_, initial_] := 
        X . means*initial;
portfolioSigma[X_, S_, initial_] := 
        Sqrt[X . S . X]*initial;

portfolioMean[X, means, initial]
portfolioSigma[X, S, initial]

Then, we can add in some numerical data and recalculate the portfolio's expected return and variance.

X = {0.3, 0.25, 
          0.45};
means = {0.1, 0.12, 
          0.13};
initial = 
        100;
S=(({{0.10, 0.04, 0.03}, {0.04, 0.20, -0.04}, {0.03, -0.04, 0.60}}));

portfolioMean[X, means, initial]
portfolioSigma[X, S, initial]

Bringing Everything Together: Writing Mathematica Functions

Finally, all of the pieces above are combined to write the new multivariate VaR for the portfolio.

VaR[X_, s_, initial_, level_] := 
initial - 
          Quantile[
NormalDistribution[
portfolioMean[X, means, initial], 
portfolioSigma[X, s, 
                initial]], level];

We can now look at VaR calculations for 1%, 5%, and 10%.

level = { .01,  .05,  .10};
VaR[X, S, initial,  level]

Given the stated parameters, we can say that for a one-year horizon, there is a 1% chance of losing more than $177 million, a 5% chance of losing more than $151 million, and a 10% chance of losing more than $137 million.

The Next Step: Databases, Monte Carlo Simulations, and Building a Real System

While these two problems demonstrate the basic setup of a VaR calculation in Mathematica, modeling of real-world returns requires more-involved calculations of the asset correlations from empirical data. Benninga and Wiener's paper delves more deeply into this process, including the techniques of risk mapping, historical simulation, variance-covariance techniques, and the Monte Carlo approach. Interested readers are strongly encouraged to download this and other white papers. To assist in the implementation of Mathematica in your firm, Wolfram Research Account Executives can recommend Mathematica consultants in your region, tools to link Mathematica to your database, and tools to value derivative instruments.

Note: All of the code used in this example was modified from the article "Value-at-Risk (VaR)" by Simon Benninga and Zvi Wiener. The article is a more thorough introduction to this subject and may be downloaded in PDF file format from http://finance.wharton.upenn.edu/~benninga/wiener.html.