Professional Documents
Culture Documents
Biostatistics 615/815
Lecture 19
Scheduling
z
2ndd Midterm
Mid
scheduled
h d l d ffor D
December
b 18
8:00-10:00 am
8:30-10:00 am
Optimization Strategies
z
Single Variable
Golden Search
Quadratic Approximations
z
Multiple Variables
Simplex Method
E-M Algorithm
Simulated Annealingg
Simulated Annealing
z
Stochastic Method
Gibbs Sampler
z
Alternative Algorithm
C id a particular
Consider
ti l choice
h i off parameter
t values
l (t )
Define the next set of parameter values by :
a. Update each component, 1 .. k , in turn
b. Sample value for 1(t +1) from p (1 | x, 2 , 3 ,...k )
c. Sample value for 2( t +1) from p (2 | x, 1 , 3 ,...k )
...
z. Sample value for k(t +1) from p (k | x, 1 , 3 ,...k 1 )
I
Increment
t t and
d repeatt previous
i
steps.
t
Key Property:
Stationary Distribution
S
Suppose
th t (1( t ) , 2( t ) ,..., k( t ) ) ~ p(1 , 2 ,..., k | x)
that
Then (1(t +1) , 2( t ) ,..., k( t ) ) is distributed as
p (1 | 2 ,..., k , x) p ( 2 ,..., k | x) = p (1 , 2 ,..., k | x)
In fact...
(t ) ~ p ( | x) ( t +1) ~ p( | x)
Eventually, we expect the Gibbs sampler to sample parameter values
from their posterior distribution
Sampling A Component
i f ( x j | i , )
Pr( Z j = i | x j , ,, ) =
l f ( x j | l , )
l
Before sampling a
new origin for an
observation
observation
update
pdate mixture
mi t re
parameters given
current assignments
Could be expensive!
j:Z j =i
pi = ni n
xi =
j:Z j =i
ni
2
2
si = x j ni xi ni
j:Z =i
C Code:
Updating Parameters
void update_estimates(int k, int n,
double * prob, double * mean, double * sigma,
double * counts, double * sum, double * sumsq)
{
int i;
for (i = 0; i < k; i++)
{
prob[i]
[i] = counts[i]/
[i]/ n;
mean[i] = sum[i] / counts[i];
sigma[i] = sqrt((sumsq[i] - mean[i]*mean[i]*counts[i])
/ counts[i] + 1e-7);
}
}
C Code:
Updating Mixture Parameters II
void remove_observation(double x, int group,
double * counts, double * sum, double * sumsq)
{
counts[group] --;
sum[group] -= x;
sumsq[group] -= x * x;
}
void
id add_observation(double
i (d bl x, int
i
group,
double * counts, double * sum, double * sumsq)
{
counts[group] ++;
sum[group]
[
] += x;
sumsq[group] += x * x;
}
C Code:
Starting State
void initial_state(int k, int * group,
double * counts, double * sum, double * sumsq)
{
int i;
for (i = 0; i < k; i++)
counts[i] = sum[i] = sumsq[i] = 0.0;
f
for
(i = 0;
0 i < n; i++)
i )
{
group[i] = Random() * k;
counts[group[i]]
[
[i]] ++;
sum[group[i]] += data[i];
sumsq[group[i]] += data[i] * data[i];
}
}
Select an element
Update conditional on other elements
z
If appropriate,
i t output
t t summary for
f each
h
run
C Code:
Core of The Gibbs Sampler
initial_state(k, probs, mean, sigma, group, counts, sum, sumsq);
for (i = 0; i < 10000000; i++)
{
int id = Random() * n;
if (counts[group[id]] < MIN_GROUP) continue;
remove_observation(data[id], group[id], counts, sum, sumsq);
update_estimates(k,
i
(
n - 1, probs, mean, sigma,
i
counts, sum, sumsq);
group[id] = sample_group(data[id], k, probs, mean, sigma);
add_observation(data[id], group[id], counts, sum, sumsq);
if ((i > BURN_IN) && (i % THIN_INTERVAL == 0))
/* Collect statistics */
}
Gibbs Sampler:
Memory Allocation and Freeing
void gibbs(int k, double * probs, double * mean, double * sigma)
{
int i, * group = (int *) malloc(sizeof(int) * n);
double * sum = alloc_vector(k);
double * sumsq = alloc_vector(k);
double * counts = alloc_vector(k);
/* Core of the Gibbs Sampler goes here */
free_vector(sum, k);
free_vector(sumsq, k);
free_vector(counts, k);
f
free(group);
(
)
}
Example Application
Old Faithful Eruptions (n = 272)
10
5
0
Frequency
15
20
1.5
2.0
2.5
3.0
3.5
Duration (mins)
4.0
4.5
5.0
Notes
z
Why?
Log-Lik
kelihood
-250
-300
-350
350
-400
-450
0
2500
5000
Iteration
7500
10000
Me
ean
1
0
2500
5000
Iteration
7500
10000
-270
-280
-2
290
-300
Likelihood
L
-260
-25
50
20
40
60
Iteration (Millions)
80
100
1.90
1.85
1.80
Compon
nent Mean
1.95
20
40
60
Iteration (Millions)
80
100
4
3
2
Compo
onent Mean
20
40
60
Iteration (Millions)
80
100
Forever!
Drawing Inferences
z
8
4
Density
Mean
Density
4
Density
10
10
Component Means
Mean
Mean
3
2
De
ensity
De
ensity
4
0.0
0.2
0.4
0.6
Probability
0.8
1.0
2
0
De
ensity
10
Component Probabilities
0.0
0.2
0.4
0.6
Probability
0.8
1.0
0.0
0.2
0.4
0.6
Probability
0.8
1.0
Frequencies
F
i off 0.073,
0 073 0
0.278
278 and
d0
0.648
648
Means of 1.85, 2.08 and 4.28
Variances of 0.001, 0.065 and 0.182
Components
C
t contributing
t ib ti .160,
160 0
0.195
195 and
d0
0.644
644
Component means are 1.856, 2.182 and 4.289
Variances are 0.00766, 0.0709 and 0.172
Joint Distributions
z
0.2
25
0.20
0.15
0.10
0.05
Group 3 Probability
0.30
Component Probabilites
0.05
0.10
0.15
0.20
Group 1 Probability
0.25
0.30
So far today
z
Highlight
g g connection between Simulated
Annealing and the Gibbs sampler
Metropolis-Hastings
Metropolis
Hastings
Acceptance Probability
L t qij = q (propose S n +1 = j | S n = i )
Let
Let i and j be the relative probabilities of each state
The Metropolis - Hastings acceptance probability is :
j q ji
aij = min1,
q
i ij
j
i
j
or aij = min1,
i
if qij = q ji
Metropolis-Hastings Equilibrium
If we use the Metropolis - Hastings algorithm to update
a Markov Chain, it will reach an equilibrium distribution
where Pr (S = i) = i
For this to happen, the proposal density must allow all
states to communicate.
e
Gibbs Sampler
Simulated Annealing
Given a temperature parameter ,
1
replace i with
( )
i
At high
g tempe
p ratures,, the pprobability distribution is flattened
At low temperatures, larger weights are given to high probability states
Additional Reading
z