2. Initial Assumptions
We have a computational means of generating
random numbers.
We will handle only cases in which P(x) can be
evaluated for all possible x in the domain.
We know some basic mathematics.
(calculus, statistics, algebra)
We want to perform high dimensional integration
(without lengthy/impossible calculations)
3. The Challenge of Sampling P(x)
We wish to sample
Problem 1: We usually don't know the constant Z
Problem 2: If we know Z, how do we draw the
samples (in higher dimensions)?
P(x)=
ˇP(x)
Z
4. The Curse of Dimensionality
Determine the normalization constant
Now add more parameters... :(
5. Uniform Sampling
We can't feasibly sample everywhere in the
parameter space.
Instead, let's try to find it by drawing random
samples.
6. The Procedure
1) Draw random samples uniformly from
parameter space
2) Evaluate P*(x) at those points.
3) Evaluate
4) Estimate E[x] by ZR=∑
r=1
R
f (x
(r)
)
ˇP(x
(r)
)
ZR
ZR=∑
r=1
R
ˇP(x(r)
)
7. Great...but it still sucks.
If P(x) is uniform, this method will work very well.
However, for a nonuniform distribution the
probability is typically confined to a small volume
within the parameter space. Hence we will require
a large number of samples making this method
essentially useless.
What will we do?!
10. Goals
Generate random samples from a distribution
To estimate the expectation of functions under
such a distribution
sing the estimator:
^E [x]=
1
R
∑
r
f (x
(r)
)
E [x]=⟨f (x)⟩≡∫P (x)f (x)dx1...dxn
{x(r )
}r=1
R
11. Why should you care?
We can write the variance as:
The variance will decrease as Var[X]/R
The accuracy of this method is independent of the
dimensionality of the space sampled.
Var(x)=⟨⟨f (x)⟩−f (x)⟩2
∫P( x)f (x)⟨⟨f (x)⟩−f (x)⟩2
dx1...dxn
12. Importance Sampling
When P(x) is too complicated, we can choose an
alternative “proposal” density, Q(x).
1) Generate R samples from Q(x).
2) Weight them according to their “importance”
(i.e. their value relative to P(x))
3) Evaluate the Estimator
wr ≡
ˇP(x)
ˇQ (x)
^E[x]=
∑ wr f (x
(r)
)
∑ wr
13. Rejection Sampling
Again, we choose a proposal density, Q(x).
1) Determine a constant, c, such that
2) Generate a random number x from Q(x).
3) Evaluate and generate a uniformly
distributed sample, u, in the interval
4) Evaluate
5) accept if u < reject if u >
∀ x,c ˇQ (x)> ˇP(x)
c ˇQ(x)
[0,c ˇQ(x)]
ˇP(x)
ˇP(x) ˇP(x)
14. Metropolis-Hastings Method
What if we can't find a “nice” proposal density?
Instead, generate random samples from an
“evolving” proposal density that more closely
approximates P(x) with each additional sample.
This implies the need for an iterative process,
which will require a Markov Chain (hence MCMC).
15. Metropolis-Hastings Algorithm
Let f(x) be proportional to the desired P(x)
Choose an arbitrary first sample, x, and an arbitrary
density Q(x|y) (typically a Gaussian)
For each iteration:
1) Generate a candidate x' from Q(x|y).
2) Calculate the acceptance ratio a=f(x')/f(x).
3) If a>1, calculate a=f(x'')/f(x') and so on.
4) If a<1, generate another candidate x' from Q(x|y).
16. A Summary of the Algorithms
We can implement Monte Carlo Methods on any
distribution, P(x), that can be expressed as
Each converts integrals to sums as
∫f (x)P(x)dx=
1
R
∑
r
f (x
(r)
)
P(x)=
ˇP(x)
Z
17. An Elementary Application
TASK: Estimate Pi Using a Monte Carlo Method
Throw N of darts at a unit square.
(Randomly sample [0,1]x[0,1])
Count a “throw” as a “hit” if the sample lies within
a quarter unit circle.
Area of quarter unit circle = pi/4
pi/4~(hits/throws)
pi~4*(hits/throws)
18. Rejection Sampling Python Code
from random import random
from math import sqrt
darts = 10000000
hits = 0
throws = 0
for I in range (1,darts):
throws += 1
x = random()
y = random()
dist = sqrt(x*x+y*y)
if dist <= 1.0:
hits += 1
pi = 4*(hits/throws)
print “pi = %s” %(pi)
20. The Ising Model (Magnetization)
Goal: Minimize the energy
(i.e. the hamiltonian)
Note: the energy is lower when
spins are aligned.
We will use the mean field (nearest
neighbor) approximation