Simulation Notes

Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

AN INTRODUCTION TO SIMULATION OF

OBSERVED VALUE OF A RANDOM NUMBER

MAYANK GOEL AND SHILPA GONDHALI

Abstract. We are interested in simulating observed values of a


given random variable using random numbers. In this note we dis-
cuss methods and mathematical logic behind the classical method
in detail.

1. Introduction
If we look at the dictionary, following is given as definition of simu-
lation.

Simulation is becoming as popular as sample analysis in recent times.


It won’t be an exaggeration to say that in a decade or two, simulation
would be one of the most popular method in any branch of science and
engineering. We already see examples of it being used. Most car com-
panies do simulations of various possible accidents to see whether new
design of car would give desired results. Here, we discuss simulation of
an observed value of a random variable.

Consider the following situation. Suppose as a practice of program-


ming, I and my friends are developing a computer game, say Tetres.
We divide program in various parts and I am supposed to decide which
of the following symbol would appear on the screen at the center of the
Date: September 13, 2024.
1
2 M. GOEL AND S. GONDHALI

top after previous symbol touches the ground (of the screen). There
are 12 symbols. I decide to use probability theory and random
numbers. I fix a random variable with 12 observed values. As an
example assume that my fixed variable is X which has binomial dis-
tribution, say Bin(n = 11, p = .35). Now my job is to write algorithm
in such a way that every time given a random number u in between
0 and 1 (such number is generated using Random Number Generator
by computer), one value in between 0 and 11 would be generated and
accordingly, one of the following symbol would be displayed on the
computer screen. For example, if value 3 is generated then N would
appear on the screen. If value 7 is generated then V would appear on
the screen and so on.

Suppose I want to do similar thing for the game Duck hunt. Suppose
length of the rectangle is L and every time one duck comes out from
a ground, I am supposed to decide from what point (from left) duck
would fly out. I again fix a random variable. Let’s fix X ∼ U ((0, L)).
In this case given a random number u ∈ [0, 1], we want to generated a
real number r ∈ [0, L].
SIMULATION 3

We can make this interesting as follows. Suppose in the game of


Tetres, instead of center, I allow symbol to appear on screen on top at
any point from left to right. Suppose I want more than one duck to
appear on screen and all ducks should come out from different places
then these would be done by considering one discrete and one contin-
uous variable simultaneously. We can make this more interesting than
this. You try it!

We divide the document in following manner. We describe methods


that we use in §2. In §3, we discuss a few examples. Section following it
is about mathematical justification/motivation for methods described.
Finally we mention some practice problems in §5.

Although mathematical justification is not necessary to un-


derstand method and example, it is advised to read §4 for
getting sound base for the topic.

2. Method
Following are the methods that we would follow

2.1. X is a real random variable and (the CDF FX is a strictly


increasing function).

• Find the CDF function F for the random variable and its inverse
F −1 .

• Select a random number u from the table and interpret it as a


probability , that is, as a number between 0 and 1.

• Evaluate F −1 at this randomly selected point to obtain a ran-


domly generated value for the random variable X.
Some authors call it the Inverse Transformation Method.
4 M. GOEL AND S. GONDHALI

2.2. X is a discrete random variable.


• Let Ob(X) = {x1 , x2 , · · · , xn } with x1 < x2 < · · · < xn and let
PDF of X be fX . Let corresponding CDF be denoted by FX .

• Choose a random number u. Interpret them as a probabil-


ity. This represents our entry corresponding to U where U ∼
U ((0, 1)).

• Choose j such that


FX (xj−1 ) ≤ u < FX (xj ).

• Assign xj as the observed value to u. Here we follow the con-


vention that if u < FX (x1 ) = fX (x1 ) then assigned value is
x1 .

• We follow exactly same procedure for infinite (discrete) ob-


served values.
Here we wonder why can’t output be xj−1 or some other observed
value. Also, why do we view random number as an observed value of
a uniform random variable U ∼ U ((0, 1))? We will see reasons in §4.

3. Examples
1. Problem. Let X be a (discrete) uniform over {1, 2, . . . , 1389}. Use
random number .674 to simulate a value of X.
Hint: (n − 1) ≤ 1389(u) < n then simulated value corresponding to u
is n.
2. Problem. Simulate observations from the exponential distribution
using numbers 0.532, 0.345, 0.987, 0.123.
3. Problem. Let X has geometric distribution with parameter p = .20.
Use random numbers .345, .834, .243 in the given order to simulate
values of X.
Hint: n − 1 ≤ ln(1−u)
ln(q)
< n then simulated value corresponding to u is
n.
4. Problem. Suppose that the probabilities are 0.2466, 0.3452, 0.2417,
0.1128, 0.0395, 0.0111, 0.0026 and 0.0005 that there will be 0, 1, 2, 3, 4, 5, 6
or 7 polluting spills in the Great lakes on any one day. Simulate this
model for the four digit numbers 1189, 2431, 2022, 6541, 6937, 7851, 8551,
4183, 9642, 6799, 9969. Find the total number of polluting spills in the
Great lakes in 11 days.
Solution:
We have rule which says that we read random number given, say u as
probability and simulated valus is n where F (n − 1) ≤ u < F (n). After
SIMULATION 5

calculation We have following


Day Random number No. of polluting spills
X f(X) F(X) 1 1189 0
2 2431 0
0 0.2466 0.2466
3 2022 0
1 0.3452 0.5916
4 6541 2
2 0.2417 0.8335
5 6937 2
3 0.1128 0.9463
6 7851 2
4 0.0395 0.9852
7 8551 3
5 0.0111 0.9969
8 4183 1
6 0.0026 0.9995
9 9642 4
7 0.0005 1
10 6799 2
11 9969 6
Hence, total number of spills in 11 days are 22. 
5. Problem. Find c so that f (x) = c(x − 1)2 for 1 < x ≤ 2 becomes
a density function. Simulate X for random numbers .4358, .8764, .9092
in the given order.
6. Problem. A company ‘A’ plan to supply raw material to company
‘B’ on a daily basis according to a Poisson process at the rate of 1 unit
per day. Each day company ‘B’ is supposed to run a quality testing
before accepting the supplied raw material from ‘A’. If the acceptance
of each unit of supplied material is independent with probability 0.8,
using random numbers 0.20, 0.75, 0.66 exactly once in the given order,
simulate the number of units accepted in the 1st and the 2nd day.
Solution:
Xi denote Units of raw material on ith day. Then Xi ∼ P oisson(1).

Xi 0 1 2 3 4 5 6
F [Xi ] 0.368 0.736 0.920 0.981 0.996 0.999 1.000
Yi denote the number of units accepted on ith day. Then, Yi ∼ Bin(xi , .8).
Let U1 ∼ U ((0, 1)). We are given u1 = 0.20.
Now, 0.20 < F (X1 = 0) hence simulated value of X1 is 0.
Proceeding in similar manner, we see that u2 = 0.75 hence we get
x2 = 2 as F (X2 = 1) ≤ u2 < F (X2 = 2).
We have Y2 ∼ Bin(2, 0.8).

Y2 0 1 2
F [Y2 ] 0.04 0.36 1
Using u3 = 0.66, we get that y2 = 2.
Hence, total units accepted in first and second day= 0+ 2= 2.
7. Problem. Suppose the claims are made to an insurance company
according to a Poisson process with rate 3 per day. The amount of
the claim is a continuous variable with density U (($1000, $1500)). The
6 M. GOEL AND S. GONDHALI

insurance company receives payment of $2000 per day. At the end of


each day settlement is done and if the company runs short with the
money to repay the claim of that day, then company borrows remain-
ing money, else, in the case of excess money, the excess money will
be used to settle the next day claim rather than repaying its loan.
Use following random numbers exactly once in given order to simulate
the total amount at the end of the fifth day; the random numbers
are 0.22, 0.50, 0.75, 0.12, 0.20, 0.53, 0.10, 0.21, 0.32, 0.01, 0.31, 0.80, 0.71.
Also find the total amount borrowed by the insurance company during
the above mentioned period.
Solution:
Let Xi denote number of claims on ith day. Then, Xi ∼ P oisson(3).

Xi 0 1 2 3 4 5 6 7 8 9 10
F [Xi ] 0.0498 0.1991 0.4232 0.6472 0.8153 0.9161 0.966 0.988 0.996 0.999 1
Let Y denote amount of claim. Then, Y ∼ U (($1000, $1500)).
Let u = F (y). Then, we get F −1 (u) = 500u + 1000. We are supposed
to use following random numbers
u1 = 0.22 u2 = 0.50 u3 = 0.75 u4 = 0.12 u5 = 0.20
u6 = 0.53 u7 = 0.10 u8 = 0.21 u9 = 0.32 u10 = 0.01
u11 = 0.31 u12 = 0.80 u13 = 0.71
Let Ui ∼ U ((0, 1)) and we view ui as an observed value of Ui . We do
simulation as per instruction and we get following
Day Money available Number of Amount of Money after settlement
(i) claims xi claim y
1 2000 u1 2 u2 1250
u3 1375 1000- (1250+ 1375)= -625
2 2000 u4 1 u5 1100 2000- 1100= 900
3 900+ 2000= 2900 u6 3 u7 1050
u8 1105
u9 1160 2900- 3315= -415
4 2000 u10 0 2000
5 2000+ 2000= 4000 u11 2 u12 1400
u13 1355 4000- 2755= 1245
Hence, total amount at the end of day 5 is 1245.
Total money borrowed is 625 + 415 = 1040.

4. Mathematical justification
We must ask ourselves whether there is any mathematical reason for
making choices that we made in our method or is it an algorithm? In
later case, we will have to be careful as this method might fail for vari-
ous cases. We discuss mathematics which motivate us to make choices
SIMULATION 7

that we did.

Consider two variables X and Y be given with respective distributions


1
f1 (x) = for 0 ≤ x ≤ 22.
23
 
22
f2 (y) = (.35)y (.65)22−y for 0 ≤ y ≤ 22.
y
Now suppose that we are given number 7. How do we decide whether
7 should be treated as an observed value of X or Y or none? This is
1
done by calculating P [7]. If P [7] =23 then 7 should be treated as an
22
observed value of X. If P [7] = 7 (.35)7 (.65)22−7 then 7 is observed
value of Y and if P [7] is other than these two then 7 is observed value
of neither of X and Y. We deal with real case similarly. Only instead
of calculating P [x], we calculate P [X ≤ x] to analyse distribution of x.

We begin by considering following lemma which is simple but plays


a crucial role in the theory.
8. Lemma. Let X be a random continuous variable with PDF f. Let
F denote CDF of X. Then, F (X) ∼ U([0, 1]).
Proof. Checking that F (X) ∼ U([0, 1]) is equivalent to checking that
t
MGF of Y = F (x) is e −1
t
. By definition, we have that
mY (t) = E[eY t ]
= E[eF (X)t ]
Z∞
= eF (x)t f (x) dx
−∞

Substituting F (x) = u, we have the following


Z1  ut 1
ut e et − 1
mY (t) = e du = = .
t 0 t
0
This completes our proof. 
9. Lemma. Let Y ∼ U ((0, 1)). Let α(x) be a continuous function with
α(a) = 0 and α(b) = 1. Suppose α is strictly increasing on (a, b). (where
a, b could be −∞ and ∞ respectively.) Then, the random variable X
defined by W := α−1 (Y ) is continuous and FW (x) = α(x).
Proof. Consider FW (x) = P [W ≤ x] = P [α−1 (Y ) ≤ x]. Since, α is a
strictly increasing function, so is alpha−1 . Hence,
α(x)
Z
FW (x) = P [Y ≤ α(x)] = dy = α(x).
y=0
8 M. GOEL AND S. GONDHALI


10. Observation. Given a continuous variable X, take Y = FX (X) and
α = FX and we see that FW ≡ FX and hence as random variables X =
W. That is, the value that we obtain using the inverse transformation
method is indeed an observed value of the distribution that we started
with.
n
P
11. Lemma. Let 0 ≤ p1 , p2 , · · · pn be such that pi = 1. Fix x1 <
i=1
· · · < xn real numbers. Let U ∼ ((0, 1)). We define a function X of u
as
j−1 j
X X
X(u) = xj for pi ≤ u < pi .
i=1 i=1
Check that P [X = xj ] = pj .
Proof. By definition, we see that
j
P
pi
i=1
j−1
X j
X  Z
P [X = xj ] = P pi ≤ u < pi = du = pj .
i=1 i=1 j−1
P
u= pi
i=1


12. Observation. (i) Given a discrete variable X, take pj = fX (xj ) and
we see that the value that we obtain using our method is indeed an
observed value of the distribution that we started with.
(i) This lemma tells us that if we assign observed value c to u such
that F (xj−1 ) ≤ u < F (xj ) then c must have property that P [X = c] =
P [X = xj ] so naturally we take c = xj .
13. Remark. In the case of discrete random variable, we developed
method using U ∼ U ((0, 1)). This is classical method which is univer-
sally accepted. It might be possible to develop some other method for
simulation of discrete random variable by taking some other appropri-
ate distribution for U.
5. Practice problems
14. Problem. A car manufacturing produce 1 to 8 units daily accord-
ing to uniform distribution. The number of vehicles available to ship
the manufactured cars from the factory to market daily follows Poisson
distribution with mean 1. Capacity of a shipping vehicle is 2 cars. The
probability of a shipped car being sold in a day is .75 independent of
all sales. If each sold car gives net profit of 2 Lakhs, whereas each
unshipped or shipped but unsold car has a storage cost 2000 or 4000
per day resp. Using random numbers .80, .82 and .59 once in the given
order, Simulate the net profit for the manufacturing firm in a day.
SIMULATION 9

15. Problem. Buses arrive at a sporting event according to a Poisson


process with a rate 5 per hour. Each bus is equally likely to con-
tain either 20, 21, 22, 23 fans, with the numbers in different buses be-
ing independent. Using random numbers exactly once in given order
0.92, 0.20, 0.80 simulate the arrival of fans to the event by t = .1 hours.
16. Problem. Rockland Quarries Ltd. produces 10 tons of metal
stones per day for construction purpose. The produced metal stones
are transported to its retail outlet using trucks. Each day two trucks
will be available for transport. Each truck (independent of other truck)
will have capacity 3, 5, 7 or 9 tons equally likely. Each day’s unshipped
metal stones will be added to the next day’s production quantity. Using
random numbers 0.21, 0.01, 0.65, 0.29, 0.49 and 0.12 once in the given
order, simulate for six days and find average tons of metal stones trans-
ported per day.
17. Problem. A company has two machines. Each machine has 3
electrical components. Each electrical component works independently
2
and has a failure time distribution (in hours) f (t) = 2te−t , t > 0. The
components are placed in the machine such that until one fails, the
other does not start and machine does not fail until all the components
fail. Using the random numbers 0.1, 0.7, 0.2 (machine-I), 0.9, 0.3, 0.8
(machine II), simulate number of machines working more than 3 hours
(observations rounded off to 3 decimal places).
18. Problem. A T − 20 match is played between team B and team
C. Length of every over is fixed as 5 minutes. The time (in minutes)
between fall of wickets forboth the team follows
x

 450
, if 0 < x ≤ 15
1,

if 15 < x < 35
f (x) = 803x2
 , if 35 ≤ x < 60
 692500

 1
160
, if 60 ≤ x < 100
The partnership of runs in each wicket follows discrete uniform over
{0, 1, 2, · · · , n−1}, where n = b2tc if number of wickets fall in less than
or equal to 5, else n = btc and t is the time for which the partnership
occur. Using the following random numbers 0.375, 0.81, 0.25, 0.85,
0.75, 0.84, 0.5, 0.88, 0.375, 0.91, 0.8, 0.76 once in the given order
simulate the T − 20 match to find out whether team B won the match
with how many runs or did team C win the match with how many
wickets. Assume team B bat first.
19. Problem. Suppose the pricing of a particular product on an on-
line shopping site varies time to time according to the demand of the
that product. Every day initial price of the product (i.e. price at 12
midnight) is decided according to continuous uniform distribution with
minimum price $500 and maximum $600, then after every 6 hour pe-
riod, price is revised for next 6 hours based on the total units sold
10 M. GOEL AND S. GONDHALI

in last 6 hour period. For instance, price of the product for the time
interval 6AM to 12 noon is revised based on the total units sold during
12 midnight to 6AM. So, as per this strategy price will be decided at
12 midnight and will be revised at 6AM, 12 noon and 6PM of every
day. It is decided, if total units sold in a 6 hour period is more than 2
units, then the price per unit will increase by 10% else it will decrease
by 10%. From past experience, we know that the total number of units
sold during any 6 hour period follows Poisson process with λ = 0.2 per
hour. Every day, if you check the price of this product online on this
t
shopping site at a random hour T with density f (t) = 288 , 0 < t < 24,
then based on three days simulation find the average price you ob-
served. Use the following random numbers exactly once in the given
order: random numbers to generate the price process (i.e. price and
demand) are 0.50, 0.70, 0.76, 0.93, 0.75, 0.72, 0.91, 0.45, 0.45, 0.57. Ran-
dom numbers to generate the time at which you access the site are
0.73, 0.39 and 0.20.

20. Problem. Suppose two friends A and B are waiting for their friend
C. The waiting time for C follows uniform distribution with minimum
0 to maximum 2 hours. The two friends A and B decide to play a series
of “dice” game during this waiting period. In each “dice” game either
one (A or B) is owner and other one is called player. Owner always
bet over number 1 and throws an unbiased die, if 1 appears on the die,
owner wins and receives $6 from player else owner need to pay $3 to
the player. The winner of the game will be the next game’s owner and
looser will be the player. If the total number of games played during
waiting time follows Poisson distribution with mean 3 per hour and the
owner of the first game is A then simulate the total profit for A and B
using random numbers 0.50, 0.75, 0.30, 0.01, 0.10, 0.20 exactly once in
the given order.

If there is any typo, error, query or suggestion then please feel free
to contact us.

21. Acknowledgement. Authors would like to thank faulty of deprtment


of Mathematics, BITS Pilani, K K Birla Goa campus for the contribu-
tion of problems.

References
[1] Baron, Michel; Probability and Statistics for Computer Scientists, Second Edi-
tion Chapman & Hall/CRC (ISBN:1439875901 9781439875902).
[2] Sheldon M. Ross; Simulation 6th Edition.
SIMULATION 11

Department of Mathematics, Birla Institute of Technology and


Science (BITS)-Pilani, K K Birla Goa Campus, 403726 Goa, India.
Email address: mayankgoel@goa.bits-pilani.ac.in

Department of Mathematics, Birla Institute of Technology and


Science (BITS)-Pilani, K K Birla Goa Campus, 403726 Goa, India.
Email address: shilpag@goa.bits-pilani.ac.in, shilpa.s.gondhali@gmail.com

You might also like