2. Intro to MonteCarlo Experiment 2
The last topic that I want to cover with you is Montecarlo
Simulation in STATA.
What is a Simulation by Monte Carlo? In broad terms is a
useful and powerful methodology for investigating the
properties of economic estimators and tests.
The power comes from the fact that we can define the
characteristics of the Data Generating Process (DGP) and thus
study its properties like if we were in a laboratory.
Monte Carlo experiments employ pseudo random numbers that
mimic the theoretical properties of realizations of random
variables
3. Subject of the lecture 3
We learn how to generate pseudo random numbers using Stata.
Then we learn how to create simple programs in STATA. Then
we set Monte Carlo experiment to verify
1. the Weak Law of Large Numbers
2. the Central Limit Theorem
3. Properties of the OLS estimator
After this lecture you will learn how to use some other very
important STATA commands:
1. seed to set the pseudo random generator (important to
reproduce your findings)
2. postfile
3. program and simulate
4. Generating Random Numbers (1) 4
I am quite sure that you have already encountered the Weak
Law of Large Numbers (If you are interested in the
mathematical proof please let me know). I report the definition
in the next slides. Now we prove the Theorem using the Monte
Carlo method.
First we need to set the seed to generate random numbers in
STATA
set seed 12345
Pseudorandom-number generators use deterministic devices to
produce long chains numbers, with the Seed we can reproduce
the generation of our random numbers in any other pc with
STATA installed (same version).
5. Law of Large numbers 5
Let f (., θ) the density of a given random variable X. We don’t
know the exact value of θ but we assume that the random
variable X follows the distribution f(, ).
Question: can a random sample give us reliable information
about the value θ?
Yes! We can make this claim thanks to the Weak Law of
Large Numbers. This theorem allows to make reliable
inference about the unknown parameter θ. Stated differently:
Denote by µ the expected value E [X] of X. Using only a finite
number values of X we can make inference about the value of
E (X) because the LLN allows us to construct confidence
intervals.
6. Weak Law of Large Numbers 6
Let’s state in words the claim of the Weak Law of Large
numbers. A positive integer n can be determined such that if a
random sample of size n, or larger, is draw the probability that
the sample mean ¯X will deviate from µ less than a previously
specified quantity can be made as much close to 1 as desired.
Let’s try to state the claim using the mathematical language.
Taken two numbers ε > 0 and 0 < δ < 1 there exists an integer
n such that if a random sample of size n is taken the sample
mean ¯Xn will deviate from µ by less than ε with probability
larger than 1 − δ. In symbols:
For any ε > 0 and 0 < δ < 1 there exists an integer n such that
for any m ≥ n
P | ¯Xm − µ| < ε ≥ 1 − δ
7. Weak Law of Large numbers 7
Theorem Weak Law of Large numbers
Let f (.) be a density with mean µ and finite variance σ2. Let
¯Xn be the sample mean of a random sample of size n.
Let ε and δ be any two specified numbers satisfying ε > 0 and
0 < δ < 1.
If n is any integer greater than
σ2
ε2δ
Then
P −ε < ¯Xn − µ < ε ≥ 1 − δ
8. Generating Random Numbers (2) 8
Before proving the LLN through a Monte Carlo experiment we
need to generate some random numbers. Remind that we
already generate numbers during our previous lecture (go back
to the first set of slides). Imagine that we want to generate 30
observations draw from the uniform distribution.. it is very easy
in stata
clear all
set seed 123456
set obs 30
generate x=runiform()
As you can check in STATA now you have 30 observations
stored in the variable x which are independents draws from the
Uniform distirbution.
9. Coin tosses (1) 9
Consider a random variable that takes value 1 with probability
1
2 and 0 with probability 1
2 . Let’s generate 30, indipendent,
random variables of such type:
clear
set obs 30
quietly generate y = runiform()
quietly generate x=1 if (y > 0.5)
quietly replace x=0 if (y <= 0.5)
You can check that x takes either value 1 or value 0.
10. Coin Tosses (2) 10
You can check that x takes either value 1 or value 0. Notice
that, the theoretical mean and variance of x are given by:
E[X] = 1 ∗ 1
2 + 0 ∗ 1
2 = 1
2
and V [X] = E X2 − E [X]2
= 12 ∗ 1
2 + 0 ∗ 1
2 − 1
2
2
=
1
2 − 1
4 = 1
4
If you are in trouble understanding please let me know. Remind
that, in general terms the weak law of large numbers tells you
that the empirical mean approaches the theoretical one as long
as we increase the sample size. Namely, as long as we increase
the number of coin tosses the empirical mean shall converge to 1
2
11. Postfile 11
Postfile command: declares the memory object in which the
results are stored, the names of the variables in the result
dataset and the name of the results dataset file. In our example
we write:
postfile sim mean1 trials using sim1000, replace
Everyting will be clearer shortly. We generate a new dataset
called sim1000 where we store the results of our experiments.
12. Simulation 12
forvalues i = 1/100000 {
quietly capture drop y x
quietly set obs ‘i’
quietly generate y =runiform()
quietly generate x=1 if (y > 0.5)
quietly replace x=0 if (y <= 0.5)
summarize x
quietly post sim (r(mean)) (r(N))
}
postclose sim clear
use sim1000
13. forvalues 13
forvalues: i = 1/100000
means that Stata repeats the instructions between the curly
brackets 10000 times
In our example for every i we construct a new dataset that is
made by i observations.
Finally:
quietly post sim (r(mean)) (r(N))
saves in our external dataset, for each simulation trial, both
value of the mean and the number of observations.
15. Weak Law of Large Numbers 15
The law of large numbers tells us that knowing we can
determine a confidence interval such that for a given sample size
the empirical mean belongs to an interval centered at the
theoretical mean with a certain probability. Setting interval
equal to 0.1 and the confidence interval equal to 95% and
taking advantage of the theorem
1
4
1
(0.1)2
(0.05)
= 500
So the theorem tells that at least the 95% of the means shall
fall in this range....Let’s check making another experiment
16. Code 16
forvalues i = 1/1000 {
quietly capture
drop y x
quietly set obs 500
quietly generate y = runiform()
quietly generate x=1 if (y>0.5)
quietly replace x=0 if (y<=0.5)
summarize x
quietly post sim (r(mean))
} postclose sim
17. Check 17
clear
use weaklaw2.dta
* here we generate one variable that take gen test=1 if
((mean1>0.6) — (mean1<0.4))
summarize test
gen obs1 =n
scatter mean1 obs1
19. Weak Law of Large number 19
Please refer to the dofile for practical clarifications.
In our experiment we could not find any experiment for which
the sample mean does not belong to the bounds predicted by
the theorem.
Therefore, our experiments does not reject the theorem!
To check that is not artifact change number of observations
(trial) and make some experiments by yourself.
20. Central Limit Theorem (1) 20
Theorem: Central-limit theorem: Let f (.) be a density
with mean µand finite variance σ2. Let ¯Xn be the sample mean
of a random sample of size n from f (.). Let the random
variable Zn be defined by
Zn =
¯Xn − E ¯Xn
var ¯Xn
=
¯Xn − µ
σ√
n
The distribution of Zn approaches the standard normal
distribution as n approaches to infinity
21. Central Limit theorem (2) 21
The CLT says that the limiting distribution of Zn which is ¯Xn
standardized is the standard normal distribution. Namely, this very
important theorem tells us that ¯Xn itself is approximately, or asymptotically
distributed as a normal distribution with mean µ and variance σ2
n
Nothing is assumed about the form of the original density function
The degree of approximation depends on the sample size and on the
particular density f(.)
The idea is that you take many samples of size n. Then the central limit
theorem shows that the distribution of the mean of such samples will
approximate to a normal as the sample size increases
Homework: determine the exact distribution of the sample mean for a
sample from a Poisson distribution (you should know what is a Poisson
distribution, and its properties, feel free to come back to me to have
references that will help you to solve the exercises)
22. CLT and LLN 22
From Angrist Pischke book (Mastering Metrics):
How can we decide wether statistical result constitute strong
evidence or merely a lucky draw, unlikely to be replicated in
repeated samples?
LLN and CLT are the foundations of formal statistical
inference. As Angrist and Pischke write:
Quantifying sampling uncertainty is a necessary step in any
empirical projects and on the road to understanding statistical
claims made by others
23. Central Limit Theorem. Simulation 23
Now we try to prove the CLT by means of a Monte Carlo
Experiment:
Central Limit theorem: the sample mean is approximately
normally distribute as N approaches infinity. Let consider a
random variable that has the uniform distribution and a sample
size of 30. Let’s check that when we draw a single sample of
uniform draws the graph that we obtain as nothing to do with a
bell shaped curve:
quietly set obs 30
set seed 10101
generate x=runiform()
summarize x
quietly histogram x, width(0.1)
xtitle(”x from one sample”)
24. Recap: Uniform Distribution (1) 24
A random variable X has with values a ≤ X ≤ b has a unform
distirbution if its probability density function is given by
f (x|a, b) =
1
b − a
for a ≤ x ≤ b and zero otherwise
now, let a = 0 and b = 1
E [X] =
1
0
x
1
1 − 0
dx =
x2
2
1
0
=
1
2
V (X) =
1
0
x2 1
1 − 0
dx −
1
2
2
=
x3
3
1
0
−
1
4
=
1
3
−
1
4
=
1
12
26. Program 26
We will do simulation but using a different command. In place
of postfile we write a program. Notice that we put rclass. This
tell STATA that we want that the program return belongs to
the rclass .
program onesample, rclass
drop all
quietly set obs 30
generate x=runiform()
summarize x
return scalar meanforonesample=r(mean)
end
onesample is our first user written program. It generates 30
observations drawn fro the uniform distribution. It returns the
mean of this sample as an rclass object.
27. Simulate 27
set seed 10101
onesample
return list
We employ the command simulate to run the program as many
time as we wish. Understand that our r.v. has expected value
equal to 0.5 and s.d. equal to 0.0527. Each round we obtain a
new value of the sample mean stored in the xbar variable.
simulate xbar=r(meanforonesample), seed(10101)
reps(1000) nodots:onesample
Reps tells STATA how many simulations we want to perform.
In our case is 1000. We can check now our experiment.
summarize xbar quietly histogram xbar, normal xtitle(”xbar
from many samples”)
29. OLS regression 29
When we do OLS we employ the OLS method to determine
estimates of the True parameters.
Monte Carlo experiment can be used to estimate the reliability
of our estimators.
Let’s apply the MonteCarlo methodology to check some of the
well known OLS properties.
We verify that when the DGP meets the OLS assumptions the
OLS estimates are unbiased.
So what we do? We define a DGP that meets OLS assumptions
and then we veritfy that OLS estimator will have all that good
properties that makes it so widespread used!
30. Unbiasdness 30
Unbiasdness of the Sample Mean:
E ¯Yn = E [Yi] = µ
What it means: The sample mean should not be expected to
bang on the corresponding population mean: the sample
average in one sample can be too big; while in another sample
can be too small.
Unbiasdness tells us that deviations are not systematically up
or down; rather; in average samples they average out to zero.
Unbiasdness is distinct from the LLN, which says that the
sample mean gets closer and closer to the population mean as
the sample size grows. Unbiasdness of the sample mean holds
for samples of any size, for any n!
31. Model 31
Let assume that we have some data such that:
E [Y |X] = 100 + 10 ∗ 10 for the first 20 observations
and
E [Y |X] = 100 + 10 ∗ 20 for the remaining 20 observations
We assume that the error term follows a Normal distribution
with mean zero and variance equal to 2500, N (0, 50), where 50
is the standard deviation.
This equation define the theoretical model of our process. In
the real world once we do OLS we just have 1 sample! and we
want to estimate both the parameters, in our case equal to 100,
b1 and 10, b2
32. Setting the experiment 32
Before to define the simulation we save in global macros the key
input of our DGP.
clear all
global numobs 40 // sample size
global beta1 100 // intercept parameter
global beta2 10 // slope parameter
global sigma 50 // standard error
Now we can define the DGP
33. Data Generating Process 33
set seed 1234567
set obs $numobs
generate x=10
replace x=20 if n > $numobs/2
generate y = $beta1 + $beta2*x + rnormal(0,$sigma)
Now, given our data we can estimate the parameters through
OLS regression
regress y x
Notice that we do not get neither 1000 nor 10. The result of
one regression is not enough to say something about
the OLS properties!
34. OLS experiment 34
Look at the program in the dofile. we store for each experiment
the estimate of:
intercept
slope
variance.
return scalar b2 = b[x]
return scalar b1 = b[ cons]
return scalar sig2 = (e(rmse))2
35. Making a series of experiment 35
Taking advantage of STATA we can generate as many samples
of the DGP process. Then we can evaluate whether OLS
estimates are good or bad:
simulate b1r=r(b1) b2r=r(b2) sig2r=r(sig2), reps (1000) nodots
nolegend seed(1234567): regression1
We have just run 1000 regression. We can compare the
average of our estimates with the actual values.
38. Exercise 38
Consider a biased coin, probability of getting head =0.3.
Define and perform a MonteCarlo Experimento to obtain a
reliable estimate of the empirical mean
Consider a random variable that follows a Poisson
distribution with mean 1.5 (rpoisson). Determine the
expected value analytically (1.5) and by means of a
MonteCarlo Experiment evaluate the distribution of the
empirical mean considering 10000 samples for each one of
the following 3 cases: sample size=5, sample size=15,
sample size=50. Discuss your result in light of the CLT
consider the OLS simulation. Discuss how the empirical
estimate of V(b2), from the MonteCarlo experiment, differ
from the actual one. What happens when n increases?