M9 Simulations for Rates of Return
Topics in Insurance, Risk, and Finance
Introduction
Learning outcomes
- Understand why Excel is needed for evaluating cash series.
- Can describe the process of simulation.
- Understand the theoretical basis for simulations and the Monte Carlo estimator.
- Can compute the Monte Carlo estimator and its confidence interval.
- Implement simulations to model rates of return and calculate related risk metrics in Excel.
Why Excel I
- As we have seen previously, besides moments of the accumulated/present value of cash flows, we want to learn more about these quantities (like VaR), mainly for risk management purposes.
- To do so, we need to know the corresponding distribution functions.
- However, deriving an explicit expression of the distributions is extremely challenging. Given lognormal rates, explicit results are available only in the case of a single cash flow (i.e.,
and ). - In practice, cash flows can be much more complicated.
Why Excel II
- We can use Excel (also other programming languages) to simulate random variables, and use these to generate random observations of cash flow series.
- These generations will result in an empirical distribution of the cash flow series. We can then use this distribution to estimate moments, quantiles, ranges, and probabilities.
- Simulation is widely used in the work by actuaries.
- Watching someone else use Excel has limited value, however, and so you must attempt these in your own time.
Theoretical preparation
Generating random numbers: uniform distribution
- A computer cannot generate true random numbers.
- Instead, Excel (and other programs) generate approximations that are called pseudorandom numbers.
- Once the parameter of the simulation is fixed, the same random samples will be generated each time.
- We will refer to pseudorandom numbers as random for brevity.
- There are multiple ways to generate random numbers in Excel.
- To generate random numbers from a uniform distribution on
, we can use theRAND()
(orRANDARRAY
) function, which gives us realizations of independent and identically distributed uniform random variables. - Generating uniform random numbers is at the core of simulations.
Inverse transformation
Questions: How to show probability transformation? Does the result hold for discrete distributions?
Inverse transformation: proof
We have that
Next, note that
Thus we have
Generating random numbers: inverse transformation
- Therefore, we can use inverse transformation to generate random numbers of a distribution as long as the (generalized) inverse of the distribution is known (i.e., VaR).
- That is, for a random variable
, we first generate a sample of a standard uniform random variable . Then, by inverse transformation, a sample of is - Note that not all distributions have nice formulas for their generalized inverse (e.g., normal distributions). One may need to rely on
-table for normal distributions.
Generating random numbers: inverse functions in Excel
- The inverse functions of many commonly used distributions are available in Excel.
- The general form of such inverse function is
F.INV(p,...)
: Ifp = F.DIST(x,...)
, thenF.INV(p,...) = x
. - Here,
F.DIST
is the distribution function.
Some inverse functions are summarized below
BETA.INV
for Beta distributionLOGNORM.INV
for lognormal distributionNORM.INV
for normal distributionNORM.S.INV
for standard normal distribution
To see more inverse/probability functions Excel, go to Formulas
Generating random numbers: example A
Generate random numbers from normal distributions.
- To get a random number of standard normal random variable, we take the inverse cumulative normal of a uniform random variable in the range
. In Excel,NORMSINV(RAND())
will generally do. - To get a large sample of random numbers, one can use
NORMSINV(RANDARRAY())
. - Plot a histogram of the sample.
- To get a general normal random variable
, from a standard normal random variable , we simply take
Generating random numbers: example B
Generate random numbers from a uniform distribution
- We first find the inverse function of
, which is - To get a random number from the uniform distribution, we use
RAND().
- To get a large sample of random numbers, one can use
RANDARRAY()
. - Plot a histogram of the sample.
Generating random numbers: example C I
Generate random numbers from the following distribution
Generating random numbers: example C II
We first find the inverse function
Generating random numbers: example C III
- We first generate a random number from a standard uniform distribution using
RAND
(or a sample usingRANDARRAY
). Store it in A1. - To write the inverse function, we use
IF
function:IF(condition, value1 if the condition is true, value2 if the condition is false)
- Logic operator:
AND(condition1,condition2)
,OR(condition1,condition2)
- The inverse function can be written as
IF(A1<=0.9,1/(1-A1)-1,IF(0.9< A1<=0.95,9,10))
orIF(A1<=0.9,1/(1-A1)-1,IF(AND(A1>0.9, A1<=0.95),9,10))
Monte Carlo estimator
- Given a sample of a random variable
, we are now able to approximate - Let
be iid copies of . The Monte Carlo estimator of can be obtained by - Note that this estimator also works for functions of random variables, i.e.,
. - If
, we can approximate the th moment. - If
, we can approximate the probability landing in .
Theoretical basis of simulation: Why does it work?
is unbiased, i.e., .- The Law of Large Numbers: for iid random variables
, with probability 1. Moreover, for iid random variables , with probability 1. - Also
Hence, if
Theoretical basis of simulation: standard error
- By the Central Limit theorem, the distribution of
can be approximated by . - So the error of the simulation, i.e.,
is normally distributed with mean and variance . The standard deviation is called the standard error. - A problem is that we do not know the variance of
(even the expectation is unknown, otherwise simulation is redundant). - For this, we can replace
by sample variance which is an unbiased estimator of .
Theoretical basis of simulation: confidence interval I
- By the Law of Large numbers,
with probability 1. (why?) - Then with the Central Limit Theorem and Slutsky’s Theorem, we have the following result.
Theoretical basis of simulation: confidence interval II
By the previous theorem, a
Example: MC estimate I
Suppose that interest rates
Compute a Monte Carlo estimate of the mean of
Example: MC estimate II
The simulated values of
Hence
Example: MC estimate III
Therefore, the Monte Carlo estimate of
Question: Is this a good confidence interval?
Some basic Excel functions
Summary statistics
Some functions:
- mean:
AVERAGE()
- variance of a sample:
VAR()
- variance of a population:
VARP()
- standard deviation of a sample:
STDEV()
- standard deviation of a population:
STDEVP()
- percentile:
PERCENTILE()
Analysis ToolPak
Load the Analysis ToolPak in Excel for Mac
- Click the Tools menu, and then click Excel Add-ins.
- In the Add-Ins available box, select the Analysis ToolPak check box, and then click OK.
- If Analysis ToolPak is not listed in the Add-Ins available box, click Browse to locate it.
- If you get a prompt that the Analysis ToolPak is not currently installed on your computer, click Yes to install it.
- Quit and restart Excel.
Now the Data Analysis command is available on the Data tab.
Analysis ToolPak
Data Analysis can complete basic statistical tasks including:
- generate random numbers (we can use seed here)
- summary statistics
- frequency table
Exercise A: I
- Recall that if rates are iid, even if the accumulation factors do not follow a lognormal distribution, one can still use lognormal distribution to approximate
or . - This can be seen by noting that
By central limit theorem, can be approximated by a normal distribution for large . - Next, we will assess the performance of using lognormal distribution as an approximation.
Exercise A: II
Suppose that in a varying rate model,
- Generate a random sample of
with size . - Compute the first and second moments of
numerically. - Plot the mean of
against the simulation number . - Compute the first and second moments of
analytically and compare them with the numerical result. - Use the analytical results for moments of
as the corresponding parameters of a lognormal distribution. Compare the histogram of the sample of and the density of the lognormal distribution. Explain the observation. - Use
FREQUENCY
to generate frequency table given Bin series.
Compare the simulated results with lognormal distribution.
Exercise A: III
We compute the first and second moments of
Exercise A: IV
Some observations:
- The mean and second moment of the sample are close to the analytical solution.
- As the simulation number increases, the mean of
becomes more and more stable and it is closer to the true value. - The simulated distribution of
is very close to the lognormal distribution. This is due to the central limit theorem: as is close to a normal distribution, is close to a lognormal distribution.
Exercise A: Remarks
Some remarks:
- To fill a column/row, use the fill function (Home
Editing Fill Series). - To select a large range of cells, use the name box in the top left of the workbook (e.g., A1:B2).
- When use the autofill function the columns should be adjacent.
- Although Excel states the lognormal function as
LOGNORM.DIST(x,mean,standard dev)
, the mean and standard deviation in this function refer to the equivalent normal distribution mean and standard deviation.
Exercise B: I
Suppose that
- Calculate the mean and standard deviation of
using recursive formulae. - Calculate the mean and standard deviation of
numerically using 5000 simulations. - Estimate the
and percentiles of . - Estimate
. - Get summary statistics of
.
Exercise B: II
The recursive formulae: Since
Simulation in VBA
VBA I
We can use VBA to automate the process of simulation (i.e., the simulation can be done automatically for a different set of parameters).
- To do so, we need to put our code in a marco.
- We first enable the Developer tab in Excel.
- Then go to Developer
Visual Basic project folder to open module. - If there is no module, right click the project folder to insert a module.
- We will learn some basic functions to complete simulation tasks.
VBA II
- Each automated task starts with
Sub taskName()
and ends withEnd Sub
- To declare variables in the task:
Dim variableName As variableType
- Types of variables can be
Long
(usually for large integer values),Double
(usually for values with decimals),Range
, and so on. We mainly consider numeric variables. - To assign a value from cell, e.g., A1 from worksheet B to a variable C:
C = Sheets("B").Range("A1").Value
- To output the value of variable C to cell A1 from worksheet B:
Sheets("B").Range("A1").Value = C
- The above
Range("A1")
can be replaced byCells(1,1)
- To assign a range, use
Set variableName = Range("")
VBA III
We can call Excel worksheet function by WorksheetFunction
:
- sum and average:
WorksheetFunction.Sum
andWorksheetFunction.Average
- max and min:
WorksheetFunction.Max
andWorksheetFunction.Min
- count and countif:
WorksheetFunction.Count
andWorksheetFunction.CountIf
VBA IV
Simulations:
- To initialize random number generator:
Randomize
(each run gives a different simulation). - To simulate a number from a uniform distribution on
:Rnd
- To simulate a number from a normal distribution
:WorksheetFunction.Norm_Inv(Rnd,
,)
- To simulate a number from a lognormal distribution
:WorksheetFunction.LogNorm_Inv(Rnd,
,)
Loop: start with For i = 1 To maxNumberOfLoops
and end with Next i
.
Exercise C
Let
- Generate
random numbers in a worksheet. When running the task, the cells containing the previous simulations should be empty. - Calculate the average of the random numbers.
Exercise D
Suppose that in a varying rate model,
- Generate
, , and given arbitrary , , , and . - Numerically calculate the first two moments of
and . - Enable one button to run the above two tasks and another button to erase the simulation results for
, , and .