New - Welcome to our new website! Please tell us on Ed if you find any teething issues.

M9 Simulations for Rates of Return

Topics in Insurance, Risk, and Finance

Author
Affiliation

Yuyu Chen

Department of Economics, University of Melbourne

Published

2024

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., Sn and Vn).
  • 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 (0,1), we can use the RAND() (or RANDARRAY) 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 F(x)yxF1(y). This is by the definition of F1.

Next, note that F1(y)x implies yF(F1(y))F(x). Here, the first inequality uses the fact that F is right-continuous. Then F(x)yxF1(y).

Thus we have \p(F1(U)x)=\p(UF(x))=F(x).

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 X, we first generate a sample of a standard uniform random variable u1,,un. Then, by inverse transformation, a sample of XF is F1(u1),,F1(un).
  • Note that not all distributions have nice formulas for their generalized inverse (e.g., normal distributions). One may need to rely on z-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,...): If p = F.DIST(x,...), then F.INV(p,...) = x.
  • Here, F.DIST is the distribution function.

Some inverse functions are summarized below

  • BETA.INV for Beta distribution
  • LOGNORM.INV for lognormal distribution
  • NORM.INV for normal distribution
  • NORM.S.INV for standard normal distribution

To see more inverse/probability functions Excel, go to Formulas More Functions Statistical.

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 (0,1). 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 XN(μ,σ2), from a standard normal random variable Z, we simply take X=μ+σZ.

Generating random numbers: example B

Generate random numbers from a uniform distribution F on the interval (a,b) where b>a.

  • We first find the inverse function of F, which is F1(p)=a+(ba)p.
  • To get a random number from the uniform distribution, we use a+(ba)×RAND().
  • To get a large sample of random numbers, one can use a+(ba)×RANDARRAY().
  • Plot a histogram of the sample.

Generating random numbers: example C I

Generate random numbers from the following distribution F(x)={11x+1,   0x<9,0.95,   9x<10,1,   x10.

Generating random numbers: example C II

We first find the inverse function F1(p)={11x1,   0<p0.9,9,   0.9<x0.95,10,   0.95<x1.

Generating random numbers: example C III

  • We first generate a random number from a standard uniform distribution using RAND (or a sample using RANDARRAY). 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)) or IF(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 X, we are now able to approximate θ=\E(X).
  • Let X1,,Xn be iid copies of X. The Monte Carlo estimator of θ can be obtained by θ^=1ni=1nXi.
  • Note that this estimator also works for functions of random variables, i.e., f(X).
  • If f(x)=xk, we can approximate the kth moment.
  • If f(x)=\id{x[a,b]} , we can approximate the probability landing in [a,b].

Theoretical basis of simulation: Why does it work?

  • θ^ is unbiased, i.e., \E(θ^)=θ.
  • The Law of Large Numbers: for iid random variables X1,,Xn, limn1ni=1nXi\E(X1) with probability 1. Moreover, for iid random variables Y1,,Yn, limn1ni=1nf(Yi)\E(f(Y1)) with probability 1.
  • Also \var(θ^)=\var(1ni=1nXi)=1n\var(X).

Hence, if n is large, θ^ can be very close to θ.

Theoretical basis of simulation: standard error

  • By the Central Limit theorem, the distribution of i=1nXi/n can be approximated by N(\E(X1),\var(X1)/n).
  • So the error of the simulation, i.e., i=1nXi/n\E(X1) is normally distributed with mean 0 and variance \var(X1)/n. The standard deviation \var(X1)/n is called the standard error.
  • A problem is that we do not know the variance of X (even the expectation is unknown, otherwise simulation is redundant).
  • For this, we can replace \var(X) by sample variance sn2=1n1i=1n(Xiθ^)2, which is an unbiased estimator of \var(X).

Theoretical basis of simulation: confidence interval I

  • By the Law of Large numbers, sn2\var(X) 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 1α confidence interval of θ is (θ^zcsnn,θ^+zcsnn) where \p(zc<Z<zc)=1α. Here ZN(0,1).

Example: MC estimate I

Suppose that interest rates i1,i2,i3U(0,0.5) are independent. An agent simulates 3 paths of i1,i2,i3 using inverse transformation. The random numbers, generated from a standard uniform distribution, for i1,i2,i3 are documented sequentially below:

Compute a Monte Carlo estimate of the mean of S3 with a 90% confidence interval.

Example: MC estimate II

The simulated values of i1,,i3 are:

Hence S3 in each path is:

Example: MC estimate III

Therefore, the Monte Carlo estimate of \E(S3) is 13(1.629+1.679+1.687)=1.665, and sn2=0.000988. The confidence interval is (1.6651.650.01815,1.665+1.650.01815).

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: Sn 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 Sn or Vn.
  • This can be seen by noting that logSn=t=1nlog(1+it). By central limit theorem, logSn can be approximated by a normal distribution for large n.
  • Next, we will assess the performance of using lognormal distribution as an approximation.

Exercise A: Sn II

Suppose that in a varying rate model, i1,,i30U(0.06,0.10). Complete the following tasks:

  • Generate a random sample of (1+it) with size 6000×30.
  • Compute the first and second moments of S30 numerically.
  • Plot the mean of S30 against the simulation number k=1,,6000.
  • Compute the first and second moments of S30 analytically and compare them with the numerical result.
  • Use the analytical results for moments of logS30 as the corresponding parameters of a lognormal distribution. Compare the histogram of the sample of S30 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: Sn III

We compute the first and second moments of S30 analytically. We have \E(1+it)=1+0.06+0.12=1.08 and \E((1+it)2)=1+2\E(it)+\E(it2)=1+20.06+0.12+0.060.1x210.10.06dx=1.6665. Then \E(S30)=1.0830=10.0626 and \E(S302)=1.666530=101.6049.

Exercise A: Sn 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 S30 becomes more and more stable and it is closer to the true value.
  • The simulated distribution of S30 is very close to the lognormal distribution. This is due to the central limit theorem: as logS30 is close to a normal distribution, S30 is close to a lognormal distribution.

Exercise A: Sn 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: An I

Suppose that 1+i1,,1+i10LN(0.09,0.042) are independent. Complete the following tasks.

  • Calculate the mean and standard deviation of A10 using recursive formulae.
  • Calculate the mean and standard deviation of A10 numerically using 5000 simulations.
  • Estimate the 5% and 95% percentiles of A10.
  • Estimate \p(A10<14.75).
  • Get summary statistics of A10.

Exercise B: An II

The recursive formulae: Since An=(1+in)(1+An1), we have \E(An)=\E((1+in))(1+\E(An1)), and \E(An2)=\E((1+in)2)(1+2\E(An1)+\E(An12)).

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 with End 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 aboveRange("A1") can be replaced by Cells(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 and WorksheetFunction.Average
  • max and min: WorksheetFunction.Max and WorksheetFunction.Min
  • count and countif: WorksheetFunction.Count and WorksheetFunction.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 (0,1): Rnd
  • To simulate a number from a normal distribution N(μ,σ2): WorksheetFunction.Norm_Inv(Rnd,μ,σ)
  • To simulate a number from a lognormal distribution LN(μ,σ2): WorksheetFunction.LogNorm_Inv(Rnd,μ,σ)

Loop: start with For i = 1 To maxNumberOfLoops and end with Next i.

Exercise C

Let n be a the number of simulations. Automate the following task.

  • Generate n N(0,1) 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, i1,,itU(a,b). Let n be the number of simulations. Automate the following tasks.

  • Generate St, St2, and logSt given arbitrary t, a, b, and n.
  • Numerically calculate the first two moments of St and logSt.
  • Enable one button to run the above two tasks and another button to erase the simulation results for St, St2, and logSt.
Back to top