You are on page 1of 34

Simulation

VBA

Giovanni Zucchinetti
giovanni.zucchinetti@gitec.ch

Adapted from the course « Notions informatiques – Modèles de calcul »


with kind permission from Prof. Yves Pigneur and Dr. Gabor Maksay

HEC Lausanne
Computanional Tools for Actuaries (CTA)
1
Agenda

1. General approach
2. Case study
3. Second example

2
Introduction
• Monte Carlo methods are a class of computational algorithms that rely on
repeated random sampling to compute their results.
• Monte Carlo methods are often used in computer simulations of
mathematical systems.
• These methods are most suited to calculation by a computer and tend to
be used when it is infeasible to compute an exact result with a
deterministic algorithm.
• They are used to model phenomena with uncertainty in inputs, such as
the calculation of risk in business.
[Source: Wikipedia]

CTA - G. Zucchinetti 3
Defining Simulation
• Simulation is a software-generated display of a process which one
wants to observe.

• Simulation is a useful tool for analyzing how a set of actions functions


under different parameters or settings.

• It benefits from trial and error without the cost of materials, labor and
time that would be necessary to observe the same results on the
original process.

• Simulation differs from optimization in that it does not aim to find the
best result for some given inputs.

• Rather, it seeks to find the best inputs given a desired result by


running several trials of a particular scenario.

CTA - G. Zucchinetti 4
Working with Distributions
• Simulation is useful because it can handle variability of
parameters in a model.

• That is, we cannot know many settings in a process with full


certainty ; there is usually some range of numbers into which
values fall but we can not know exact numbers.

• Thus, we use distributions and random numbers.

CTA - G. Zucchinetti 5
Monte Carlo simulation method
Monte Carlo methods vary, but tend to follow this general pattern :

1. Define a model representing the problem to solve

2. Define a domain of possible values for the uncertain inputs.

3. Generate inputs randomly from a probability distribution over the


domain.

4. Perform multiple deterministic computations of the model based


on a large number of randomly generated inputs.

5. Aggregate the results and display useful indicators to the


“decision-maker”

CTA - G. Zucchinetti 6
Illustration
3 dices game

- Model :
- Possible values for the uncertain input :
- Random number generation :
- Multiple computation of the model :
- Aggregation of results :

Decision :

CTA - G. Zucchinetti 7
1st example

HEC Lausanne
Computanional Tools for Actuaries (CTA)
Case study - Eearly retirement
• Mr X has stopped working at age 55.
• He will be entitled to a pension only from 65
• Inbetween, he will live on a termination capital of CHF 500’000 he
received from his employer.
• His banker advised him to invest it in a « balanced » fund, which has
an expected return of 4%. His banker told that the expected volatility
(standard deviation) is 5% ; the distribution of the returns is
supposed to follow a Normal distribution.
• Mr X wants to withdraw a constant yearly amount.
• He wants help in his decision making on the yearly withdrawal, with
regards to the risk of being short of money before 65.

CTA - G. Zucchinetti 9
Case study - Model

• Inputs

• Formulas

• Results

CTA - G. Zucchinetti 10
Uncertain input
Normal distribution (4% ; 5%)

Random input generation :

1.00
1.00
Random numbers

0.90
0.80
0.70
0.60
0.50
0.40
0.30
0.20
0.10

0.00 -
-20%

-8.00%

-5.00%

-2.00%

1.00%

4.00%

7.00%

10.00%

13.00%

16.00%

19.00%
-17.00%

-14.00%

-11.00%

CTA - G. Zucchinetti 11
Generating Random Numbers
• To generate a random number from a particular distribution,
we can use Excel’s inverse distribution functions.

• The general distribution Excel function is :


=DIST(x_value, distribution_parameters, cumulative_value)

• The general inverse distribution Excel function is :


=DISTINV(probability, distribution_parameters)

CTA - G. Zucchinetti 12
Rand Num and Dist
• To generate a random number within a given distribution, we
must use the inverse distribution functions.

• The format of these inverse distribution functions is the


following :
=NORMINV(probability, mean, std_dev)

• The probability parameter is a number between 0 and 1


associated with the given distribution.

CTA - G. Zucchinetti 13
Generating Random Numbers
We will use the RAND function as our value for the probability
parameter to generate some number between 0 and 1.

For example, to generate random numbers from the Normal


distribution, in the Excel Sheet we would follow the format :
- =NORMINV(RAND(), mean, std dev)
- in VBA : RND()

This inverse distribution function can be similarly used for other


distributions:
- BETAINV
- BINOMINV
- LOGINV

CTA - G. Zucchinetti 14
Excel formula implementation

Capital
Simulation Year
0 1 2 3 4 5 6 7
1 500'000 468'553 444'261 426'399 359'381 298'771 262'951 213'018
2 500'000 480'348 443'924 373'187 353'212 323'840 288'182 239'913

CTA - G. Zucchinetti 15
Aggregating results
Useful Excel functions
- COUNTIF : counts the number of nonblank cells within a range
that meet the given criteria

- SUMIF : adds the cells specified by a given criteria

- PERCENTILE : returns the k-th percentile of values in a range

- FREQUENCY : returns a frequency distribution as a vertical


array

- https://support.office.com/en-us/article/Excel-functions-by-
category-5f91f4e9-7b42-46d2-9bd1-63f26a86c0eb

CTA - G. Zucchinetti 16
Decision criteria
• What’s the possible evolution of the remaining capital ?

• What are the confidence levels of you projections ?

• What is the shortfall / ruin probabilty at the end of each year ?

• What is the outlook for the remaining capital after 10 years ?

CTA - G. Zucchinetti 17
Simulations - Plot

CTA - G. Zucchinetti 18
Confidence levels

Confidence levels
600'000

500'000

400'000 2.50%

300'000 10.00%

200'000 50.00%

100'000 90.00%

- 97.50%
0 1 2 3 4 5 6 7 8 9 10
Years

CTA - G. Zucchinetti 19
Shortfall probability

CTA - G. Zucchinetti 20
CTA - G. Zucchinetti
0%
5%
10%
15%
20%
25%
30%
Histogram

0
9'369
18'737
28'106
37'474
46'843
56'211
65'580
74'948
84'317
93'685
103'054
112'423
121'791
131'160
140'528
149'897
Remaining capital a er 10 years

159'265
168'634
178'002
21
Other indicators that make sense ?

CTA - G. Zucchinetti 22
Simulation with VBA

There is no particular “simulation code” in VBA.

We will instead be defining a set of functions we have


already learned, and some new ones, which we can use
when programming a simulation application.

CTA - G. Zucchinetti 23
Steps
1. Getting the inputs from XL’s sheet
2. Defining the measured variables
3. Initializing the simulation
4. Generating the random element
5. Computing & storing
6. Aggregate results or transfer to XL sheet

→ see VBA file

CTA - G. Zucchinetti 24
Making Runs and Collecting Data
As seen in the code above, we can use For, Next loops to
generate multiple data values.

We may ask the user how many criteria they want to use for the
simulation.

We may also perform some calculations with the random data


we generate.

To simulate a system, we choose the number of runs to perform.


- A run is a single execution of a series of actions which model
the system.

CTA - G. Zucchinetti 25
Runs and Data
We then need to determine if we will be storing this data and the results
of the specified calculations in arrays or in a spreadsheet.

For speed reasons, it’s better to limit the transfers to the excel file

When multiple calculations of data are needed, which do not require


Excel functions, arrays can be easier structures to work with.
- They are easier in the since that range names do not need to be
defined and extra worksheet space does not need to be used.

If however, some other Excel functions, such as distribution functions,


will need to be used with the generated data, it may be better to store
this data in a worksheet.

Alternative solution : work with XL’s function on array data

CTA - G. Zucchinetti 26
2nd example

HEC Lausanne
Computanional Tools for Actuaries (CTA)
Simulation mechanism

1.00 qx 0.07 (dies)


Random numbers

px 0.93 (survives)

0.00

CTA - G. Zucchinetti 28
Simulation algorithm

Random number generation [0,1[

CTA - G. Zucchinetti 29
Simulation algorithm

Translate outcome in corresponding


event according to the distribution

CTA - G. Zucchinetti 30
Simulation algorithm

Perform actions depending


on the new state

CTA - G. Zucchinetti 31
Simulation algorithm

Repeat simulation in order to have a


significative sample

CTA - G. Zucchinetti 32
Simulation - Analyse the data with
Excel

CTA - G. Zucchinetti 33
References
• [Maksay 08] Maksay Gabor, Pigneur Yves, Modéliser par l'exemple, Presses
polytechniques et universitaires romande, 2008,
http://www.ppur.org/produit/290/9782880748951/Modeliser%20par%20lexemple
%20

• [Seref 07] Michelle M.H. Şeref, Ravindra K. Ahuja, and Wayne L. Winston ;
Developing Spreadsheet-Based Decision Support Systems Using Excel and
VBA for Excel ; Dynamic Ideas, Belmont, Massachusetts 2007

• [Hainault 02], Jean-Luc Hainault, Bases de données et modèles de calcul (3ème


édition), Dunod, 2002

CTA - G. Zucchinetti 34

You might also like