Monte Carlo SIMULATION with EXCEL Montana Going Green Workshop October 2010

Preview:

Citation preview

Monte Carlo SIMULATIONwith EXCEL

Montana Going Green Workshop

October 2010

2

What we will discuss

• Background

• Monte-Carlo intro

• Examples

3

Simulation

• Simulation refers to a broad collection of methods and applications that we use to mimic complex real systems

• We “reproduce” the operational behavior of a system.

• We use simulation to evaluate a system through a model (numerically).

• We collect data from a simulation to support our analysis.

4

Simulations Support Risk Analysis

• Risk implies a potential for loss

• Uncertainty does not mean “loss” or “risky”

• Three tools to help us inform decision makers– Best case – worst case (no idea on how our

outcomes are distributed)– “What if” analysis (many required…)– Simulations (greater insights on outcomes)

5

Types we will see

• Static versus Dynamic– Static – time is not a player– Dynamic – we are interested in time

• Continuous versus Discrete– Continuous – state of the model can change continuously (water

flowing in a reservoir)– Discrete – change only occurs at given points (arrival and

departures)

• Deterministic versus stochastic– Deterministic – all inputs known, with fixed time periods– Stochastic – random inputs (random arrival times)

• Random Variable – a variable whose value cannot be predicted or set with certainty

6

Simulation• To simulate is to try to duplicate the features, appearance, and

characteristics of a real system because not all parameters are known with certainty.

• A large family of problems consist of random variables represented by probability distributions. “Probabilistic” models are a natural application of computerized simulation. Simulation = a systematic way of analyzing such problems.

– Can imitate a real-world situation with a mathematical model that does not affect operations

– Study the model’s properties and operating characteristics

– Draw conclusions and make decisions based on the simulation’s results

7

Where we find Simulations

• Manufacturing• Queuing – banks, stores, latrines,….• Business and finance – returns, futures, rates, …• Military with systems design or combat operations• Development of new items

8

When should we simulate?

• A decision is being made that needs additional information

• The process is well defined and repetitive• Activities in the system exhibit

interdependency and are repetitive• The cost impact of the decision is greater

than the cost of doing the simulation• The cost of experimentation is greater

than the cost to do a simulation

9

Disadvantages

• GOOD simulations may take months to develop and can be very expensive

• “Trial and error” approach – does not produce optimal results (descriptive)

• The model will not produce answers without adequate and realistic inputs.

• Each one is unique

10

Simulation Characteristics

• Captures system interdependencies• Accounts for variability• Versatile to model almost any system• Illustrates behavior over time• Less costly than using a real system for testing• Rich source of information on multiple measures• Interesting to the user and senior management• Results can be communicated and explained• Runs in compressed time• Different levels of detail• Avoids disruptive trial and error methods• Safety – think of flight simulators

11

Applications

• Work flow planning• Capacity planning• Cycle time reduction• Staff and resource

planning• Work prioritization• Bottleneck analysis• Quality improvement• Cost reduction• Inventory reduction

• Throughput analysis• Productivity

improvements• Layout analysis• Line balancing• Batch size• Production scheduling• Resource scheduling• Maintenance scheduling• System design

12

Algorithm

• An algorithm is a step-by-step process that traces the inputs through the major steps to the outputs.

• We need an algorithm for every simulation. Programming is not an algorithm.

13

Process in Monte Carlo Simulation

• Random numbersEventOutcomes

14

Algorithm Example 1:Flip of a Coin-Probability

• Inputs: Number of trials (flips), N• Output: P(H), P(T)• Step 1: Set Counter H and T at 0.• Step 2: For I = 1 to N (number of flips) do

• Step 3: Generate a random number, xi, between [0,1]

• Step 4: If 0 < xi < .5, then H=H+1, else T=T+1

• Step 5: Calculate P(H) = H/N and P(T)=T/N• Output P(H) and P(T)• Stop

15

Algorithm

• Generic enough to be coded by any simulation language or Excel.

Simulations with EXCEL

17

Monte Carlo SimulationsPieces

• Generate Random Numbers Events (logical statements for assignment) Output or Calculated Result

18

Random Number in EXCEL• RAND Show All• Returns an evenly distributed random real number greater

than or equal to 0 and less than 1. A new random real number is returned every time the worksheet is calculated.

• RAND( )• To generate a random real number between a and b, use: • RAND()*(b-a)+a• If you want to use RAND to generate a random number but

don't want the numbers to change every time the cell is calculated, you can enter =RAND() in the formula bar, and then press F9 to change the formula to a random number.

• Example•  

1.

1

2

3

A B

Formula Description (Result)

=RAND() A random number between 0 and 1 (varies)

=RAND()*100 A random number greater than or equal to 0 but less than 100 (varies)

19

Integers

• Use the Excel command =randbetween(#1,#2)

• To get a random integer between 1 & 6 use

• =Randbetween(1,6)

20

Logical “IF” Statements in EXCEL

• Returns one value if a condition you specify evaluates to TRUE and another value if it evaluates to FALSE.

• Use IF to conduct conditional tests on values and formulas.• IF(logical_test,value_if_true,value_if_false)• Logical_test   is any value or expression that can be evaluated to TRUE or FALSE. For

example, A10=100 is a logical expression; if the value in cell A10 is equal to 100, the expression evaluates to TRUE. Otherwise, the expression evaluates to FALSE. This argument can use any comparison calculation operator.

• Value_if_true   is the value that is returned if logical_test is TRUE. For example, if this argument is the text string "Within budget" and the logical_test argument evaluates to TRUE, then the IF function displays the text "Within budget". If logical_test is TRUE and value_if_true is blank, this argument returns 0 (zero). To display the word TRUE, use the logical value TRUE for this argument. Value_if_true can be another formula.

• Value_if_false   is the value that is returned if logical_test is FALSE. For example, if this argument is the text string "Over budget" and the logical_test argument evaluates to FALSE, then the IF function displays the text "Over budget". If logical_test is FALSE and value_if_false is omitted, (that is, after value_if_true, there is no comma), then the logical value FALSE is returned. If logical_test is FALSE and value_if_false is blank (that is, after value_if_true, there is a comma followed by the closing parenthesis), then the value 0 (zero) is returned. Value_if_false can be another formula.

21

Applied to Flip of a Coin Algorithm

• Random number [0,1]

• If RN < .5, call it a head

• Otherwise it is a tail.

• Count the number of heads and find P(Heads) = number of heads/ total experiments

22

EXCEL

• Column 1 will be our Random Numbers, =rand(). Copy it down to get N random numbers.

• Column 2 will be our IF statement;• =IF(A2<.5,1,0) Return a 1 if a heads and a 0 is a tail. • Sum column of 1 and 0 to get total number of heads.• Compute P (heads) = total number of heads/N • Also, P(tails)=1-P(heads)

23

EXCEL has limits

• Only 7 imbedded IF statements can be used.

• Only a limited number of columns and rows also so that limits variables and the number of trails, N.

24

Example

• You are investing for your retirement and want to choose a set of investments.

• Each has an “expected” return, but there is a min and a max return from each investment.

• You have a limited amount to invest.

• How do you decide?

25

Monte Carlo• Monte Carlo method is a technique that involves using random numbers

and probability to solve problems. • The system we are looking at depends on chance. • The basic experimentation on chance can be explained with random

sampling.• Our basic steps

– Set up a probability distribution for our important variables– Build a cumulative distribution for each variable– Establish random number intervals– Generate the random numbers– Obtain outcomes based on prob.– Simulate the system

26

Example -- Deterministic Model

• Deterministic – no random inputs (fixed time periods)

• Example: determine the return on a 5-year investment with an annual interest rate of 7%, compounded monthly. The model is just the equation :

ym

m

rPF )1(

27

Why simulate this?

• We can quickly do “what ifs”.

• What if we could invest more money

• What if the interest rate is now __%

• What if we can afford to invest for x or y years?

28

Example – Stochastic model Unlike our Deterministic model, model results

depend on a distribution…a stochastic model is one that involves probability or randomness.

29

Stochastic Simulations

• It is important to remember that since probability is based on the law of large numbers that we should run a stochastic simulations lots of times (thousands and thousands of iterations if possible).

30

First – Let’s Organize

• Use Rows and columns to your advantage • Frequency -- shows the number of times that the

event has occurred • Probability – the chance of an event occurring• Cumulative Probability – you add the

probabilities of the different events, all sum to “1”• Random Number Interval – use your cumulative

distribution to determine your random number range (first and last values within a range)

31

Basic Steps – An Example

Historical Calculate the

Probability

Calculate the

Cumulative

Assign Range

Demand for Ships Frequency Probability Cumulative

Random Number Interval

0 15 0.05 0.05 0-51 30 0.10 0.15 5-152 60 0.20 0.35 15-353 120 0.40 0.75 35-754 45 0.15 0.90 75-905 30 0.10 1.00 90-100

300 1.00

32

Example

Demand for Cumulative Random #

Item A Frequency Probability Probability Interval

0 50

10 75

20 100

30 125

40 75

50 50

60 25

500

33

Probability Table

Demand for Cumulative Random #Item A Frequency Probability Probability Interval

0 50 .1 .1 (0,.1]

10 75 .15 .25 (.1,.25]

20 100 .20 .45 (.25,.45]

30 125 .25 .70 (.45,.70]

40 75 .15 .85 (.7,.85]

50 50 .1 .95 (.85,.95]

60 25 .05 1.0 (.95,1.0]

500 1.0

34

Random Numbers• Pick a number between LO and HI – that is a

RANDOM number• In EXCEL:

– RAND() -- returns a random number between zero and one. – If you want numbers between zero and five, multiply by five:

=5*RAND(). If you want numbers between 3 and 8, use =3+5*RAND(). How would you get a random number between 0 and 50? 0 and 100?

– RANDBETWEEN(lo,hi) -- returns a random integer between lo and hi. (for example, RANDBETWEEN(1,9) returns an integer between 1 and 9.

35

Random NumbersIn EXCEL, here are commands to obtain random numbers To Simulate EXCEL Formula to use Random number, uniform [0,1] =rand( ) Random number between [a, b] =a+ (b-a)*rand( ) Discrete integer random number between [a, b]

=randbetween(a,b)

Normal random number =NORMINV(rand(),) Exponential random number with mean rate

=(-1/)* ln(rand( ))

Discrete general distribution with only two outcomes (like a flip of a coin): A and B Probability of outcome is p.

=if(rand( )<p,A,B)

Discrete general distribution for more than two outcomes. Range1= cell range for lower limits of the random number intervals Range2=cell range containing the variable values

=lookup(RAND( ), Range1,Range2)

Note: in the command: RAND( ) there is not space between the two parentheses.

36

Other Distributions

• Uniform as we have seen

• Normal norminv(rand(), mean, SD)

• Integer round(norminv(rand(), mean, SD))

• Exponential (-1/mean)*ln(rand())

37

The Basic Statistics• Sample Size (n): =COUNT(G:G)• Sample Mean: =AVERAGE(G:G)• Median: =MEDIAN(G:G)• Sample Standard Deviation (σ): =STDEV(G:G)• Maximum: =MAX(G:G)• Minimum: =MIN(G:G)• Skewness: =SKEW(G:G)• Kurtosis: =KURT(G:G)

A positive skewness indicates that the distribution has a longer right-hand tail (skewed towards more positive values).

Positive kurtosis indicates a more peaked distribution.

38

Sample Size

• The sample size, n, is the number of observations or data points from a single simulation.

• Because the Monte Carlo method is stochastic, if we repeat the simulation, we will end up calculating a different set of summary statistics.

• The larger the sample size, the smaller the difference will be between the repeated simulations. Better to have a large sample size.

39

More Stats Page• If you repeat your simulation and record the sample

mean each time, the distribution of the sample mean would end up following a Normal distribution. This is the Central Limit Theorem.

• Confidence Interval -- We can be 95% confident that the true mean of the population falls somewhere between the lower and upper limits."

Recommended