Drawing Random Samples From Statistical Distributions
Drawing Random Samples From Statistical Distributions
Drawing Random Samples From Statistical Distributions
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
<strong>Drawing</strong> <strong>Random</strong> <strong>Samples</strong> <strong>From</strong> <strong>Statistical</strong><br />
<strong>Distributions</strong><br />
Paul E. Johnson 1 2<br />
1 Department of Political Science<br />
2 Center for Research Methods and Data Analysis, University of Kansas<br />
2011<br />
Descriptive<br />
K.U.
Where do random samplings come from<br />
Analytical solutions for some workable distributions<br />
Approximate computational solutions for the rest<br />
Descriptive<br />
K.U.
Where Do We Start?<br />
Assume we have formulae for distributions from which we<br />
want to draw samples<br />
Assume we have a random generator that can give us random<br />
integers on [0, MAX]<br />
Assume that the random generator is a good one, either<br />
MT19937 or one of L’Ecuyer’s parallel stream generators.<br />
Of course, do whatever book keeping necessary to assure<br />
perfect replication<br />
Descriptive<br />
K.U.
PDF<br />
Probability Density<br />
Function, f (x)<br />
PDF<br />
Note small letter<br />
used for PDF<br />
f(x)<br />
0.0 0.1 0.2 0.3 0.4<br />
−3 −2 −1 0 1 2 3<br />
x<br />
Descriptive<br />
K.U.
CDF: Always S-Shaped<br />
Cumulative<br />
Distribution<br />
Function,<br />
F (x)<br />
CDF: Area on<br />
left of point x<br />
F (x) =<br />
ˆ x<br />
−∞<br />
F(x)<br />
f (e)de<br />
0.0 0.2 0.4 0.6 0.8 1.0<br />
−3 −2 −1 0 1 2 3<br />
Descriptive<br />
Note<br />
CAPITAL<br />
letter used for<br />
CDF<br />
CDF is always “S shaped”<br />
x<br />
Notation Confusing because x in F (x) is the<br />
upper limit<br />
K.U.
The U[0,1] is Obtained Easily from Generator<br />
Draw from Uniform on 0, 1:<br />
x ∼ random integer from [0, MAX ] (1)<br />
y =<br />
x<br />
MAX<br />
(2)<br />
So, like Bogart said in the end of Casablanca, “We’ll always<br />
have the Uniform Distribution”<br />
Descriptive<br />
K.U.
Other <strong>Distributions</strong> More Difficult<br />
Inversion method:<br />
Easiest to conceptualize<br />
Works easily if we can calculate “quantiles” (meaning the CDF<br />
is calculatable).<br />
Many propose use of approximate CDF<br />
Rejection Sampling<br />
Find some other similar PDF that is easier to calculate<br />
Use algorithm to test “candidates” and keep ones that fit<br />
Composition<br />
MCMC, and others<br />
Descriptive<br />
K.U.
Inversion<br />
Consider a CDF.<br />
What does the left<br />
hand side mean?<br />
Fraction of cases<br />
smaller than<br />
that point.<br />
Think<br />
“backwards” to<br />
find x that<br />
corresponds.<br />
F(x)<br />
0 1<br />
0.21 0.58 0.82<br />
−0.8<br />
x<br />
0.2<br />
0.9<br />
Descriptive<br />
K.U.
Concept behind Inversion<br />
An “equally likely”<br />
draw from f (x)<br />
would have this<br />
property:<br />
All points on the<br />
vertical axis<br />
between [0,1]<br />
are going to be<br />
equally likely.<br />
Right?<br />
F(x)<br />
0 1<br />
0.21 0.58 0.82<br />
−0.8<br />
x<br />
0.2<br />
0.9<br />
(Otherwise, a<br />
randomly drawn x<br />
wouldn’t really be<br />
random, eh?)<br />
Descriptive<br />
K.U.
Inversion Algorithm<br />
Inversion method<br />
draw a random<br />
u ∼Uniform[0, 1]<br />
“Think<br />
Backwards” to<br />
get<br />
corresponding<br />
x = F −1 (u)<br />
F(x)<br />
0 1<br />
0.21 0.58 0.82<br />
−0.8<br />
0.2<br />
0.9<br />
Collect a lot of<br />
those, and you’ve<br />
got a sample from<br />
f (x)<br />
x<br />
Descriptive<br />
K.U.
Logistic: Easy Inversion<br />
Recall the Logistic PDF, with “location” parameter µ and “scale”<br />
parameter σ:<br />
f (x) = 1 exp(− x−µ<br />
σ )<br />
σ (1 + exp(− x−µ<br />
(3)<br />
σ ))2<br />
In the usual cases we discuss, µ = 0 and σ = 1.0, so the pdf in<br />
question is simpler.<br />
And CDF:<br />
F (x) =<br />
x−µ<br />
exp(<br />
σ )<br />
1 + exp( x−µ<br />
σ ) = 1<br />
. (4)<br />
1 + e−(x−µ)/σ Descriptive<br />
K.U.
Calculate Logistic Inverse CDF<br />
Figure for each probability density output value y, find the x<br />
that corresponds to it via F:<br />
y =<br />
1<br />
1 + e −(x−µ)/σ<br />
Through the same simple algebra used in derivation of<br />
Logistic Regression<br />
[ ]<br />
y<br />
ln = (x − µ)/σ<br />
1 − y<br />
So<br />
[ ]<br />
y<br />
x∗ = µ + σ · ln<br />
1 − y<br />
Descriptive<br />
K.U.
Use That For Inversion Sampling<br />
Draw u ∼ U[0, 1]<br />
[ ]<br />
Calculate x∗ = µ + σ · ln u<br />
1−u<br />
And, as they say on Shampoo instructions, Repeat.<br />
Descriptive<br />
K.U.
Limitations of Inversion<br />
Inversion is only practical when we have a formula for F −1<br />
that is easy to calculate.<br />
Logistic distribution, for example, has an easy formula.<br />
Normal, and many others, DO NOT.<br />
So we must either<br />
Approximate F −1 in order to conduct inversion<br />
Find some other algorithm.<br />
Descriptive<br />
K.U.
Rejection Sampling*<br />
ϕ(x): The pdf from which we want to draw a sample<br />
ϕ(x): is some complicated formula, we can’t calculate its<br />
CDF or inverse CDF. That means we have no obvious way to<br />
sample from ϕ(x)<br />
But we can calculate the PDF at any given point, and that<br />
turns out to be the magic bullet.<br />
*Notation Change, can’t remember why I changed from f to ϕ.<br />
Descriptive<br />
K.U.
General Idea behind Rejection Sampling<br />
r(x) is different from ϕ(x) in some undertandable way.<br />
So, draw samples from r(x)<br />
then keep the ones you need.<br />
How do you know when to throw away a draw from r(x)?<br />
That’s the trick!<br />
Descriptive<br />
K.U.
Start with an Easy Case<br />
Suppose<br />
When x < 0, r(x) = ϕ(x).<br />
When x ≥ 0, r(x) = 1.1 · ϕ(x)<br />
For now, don’t worry if such an r(x) exists, because it<br />
doesn’t. But it really makes the point clear.<br />
Draw a “candidate” random number x∗ from r. Should we<br />
keep it?<br />
Descriptive<br />
K.U.
If x∗ < 0, accept it<br />
as a representation<br />
of ϕ(x)<br />
Because when<br />
x < 0, r and ϕ<br />
coincide.<br />
probability density<br />
0.0 0.2 0.4 0.6 0.8<br />
Throw away this fraction<br />
−2 −1 0 1 2 3<br />
x<br />
If x∗ ≥ 0.<br />
Most of the time we want to keep that observation, since ϕ<br />
and r coincide most of the time.<br />
Where r and ϕ “overlap”, we want to keep x∗<br />
That happens with probability<br />
ϕ(x∗)/r(x∗) = ϕ(x∗)/(1.1 ∗ ϕ(x∗)) = 1/1.1<br />
So, with probability 1/1.1 = 0.9090909 . . ., we keep x∗<br />
Otherwise, throw it away and draw another.
More Realistic Case<br />
r(x) is always<br />
bigger than ϕ(x)<br />
(by definition)<br />
But if we can take<br />
a draw from r(x), it<br />
might be something<br />
like ϕ(x) would be.<br />
f(x)<br />
0.0 0.2 0.4 0.6 0.8 1.0<br />
ϕ<br />
r<br />
0.0 0.5 1.0 1.5 2.0 2.5 3.0<br />
x<br />
Descriptive<br />
K.U.
Check Out The Size of That Gap!<br />
The probability of<br />
drawing x∗ = 1.9<br />
can be calculated<br />
from r(x) and ϕ(x)<br />
Keep x∗ with<br />
probability<br />
ϕ(1.9)/r(1.9).<br />
Amounts to<br />
“throwing away” a<br />
“gap sized fraction”<br />
of candidate draws<br />
equal to 1.9<br />
f(x)<br />
0.0 0.2 0.4 0.6 0.8 1.0<br />
r(1.9)<br />
gap= 0.09<br />
ϕ(1.9)<br />
0.0 0.5 1.0 1.5 2.0 2.5 3.0<br />
x<br />
Descriptive<br />
K.U.
Realism and Rejection<br />
This procedure<br />
wastes<br />
computational<br />
effort<br />
“Works” even if r is<br />
not like ϕ at all,<br />
but is just more<br />
probability density<br />
0.0 0.5 1.0 1.5<br />
wasteful<br />
0.0 0.2 0.4 0.6 0.8 1.0<br />
If we draw a candidate x∗ = 0.2, we are likely to keep it<br />
x<br />
r(x) = 1.53<br />
ϕ(x) = β(1.2, 1.9)<br />
If we draw a candidate x∗ = 0.9, we are almost always going<br />
to throw it away because r(0.9)/ϕ(0.9) is very large.<br />
Descriptive<br />
K.U.