Stochastic approximate inference

Download Report

Transcript Stochastic approximate inference

Stochastic approximate inference
Kay H. Brodersen
Computational Neuroeconomics Group
Department of Economics
University of Zurich
Machine Learning and Pattern Recognition Group
Department of Computer Science
ETH Zurich
http://people.inf.ethz.ch/bkay/
When do we need approximate inference?
๏‚ค
How to evaluate the posterior distribution of the model parameters?
๐‘ ๐’ด๐œƒ ๐‘ ๐œƒ
1
๐‘ ๐œƒ๐’ด =
= ๐‘ ๐’ด ๐œƒ ๐‘ ๐œƒ sample from an arbitrary distribution
๐‘ ๐’ด
๏‚ค
๏‚ค
๐‘
How to compute the evidence term?
๐‘ ๐’ด = โˆซ ๐‘ ๐’ด ๐œƒ ๐‘ ๐œƒ ๐‘‘๐œƒ = E๐œƒ ๐‘ ๐’ด ๐œƒ
compute an expectation
How to compute the expectation of the posterior?
๐ธ ๐œƒ ๐’ด = โˆซ ๐œƒ ๐‘ ๐œƒ ๐’ด ๐‘‘๐œƒ
compute an expectation
๏‚ค
How to make a point prediction?
โˆซ ๐‘ฆ ๐‘ ๐‘ฆ ๐’ด ๐‘‘๐‘ฆ = E ๐‘ฆ ๐’ด
compute an expectation
2
Which type of approximate inference?
Deterministic approximations
through structural assumptions
Stochastic approximations
through sampling
๏ณ application often requires mathematical
๏ณ computationally expensive
๏ณ storage intensive
๏ฒ asymptotically exact
๏ฒ easily applicable general-purpose
derivations (hard work)
๏ณ systematic error
๏ฒ computationally efficient
๏ฒ efficient representation
๏ฒ learning rules may give additional insight
algorithms
3
Themes in stochastic approximate inference
Sampling from a desired target distribution
we need to find a way of drawing random numbers from some target distribution ๐‘(๐‘ง)
๐ธ[๐‘ง]
p(z)
sampling
f(z)
z
z
๐‘ง
๐‘ง
1
2
= 0.201
= 1.807
โ‹ฎ
10,000
๐‘ง
= 0.870
summary
Computing an expectation w.r.t. that target distribution
we can approximate the expectation of ๐‘ง using the sample mean:
1
๐ธ ๐‘ง โ‰ˆ ๐‘‡ ๐‘‡๐œ=1 ๐‘ง ๐œ
4
1
Transformation method
Transformation method for sampling from ๐‘(๐‘ง)
๏‚ค
Idea: we can obtain samples from some distribution ๐‘ ๐‘ง by first sampling from
the uniform distribution and then transforming these samples.
1.4
2
1.2
1.5
0.8
p(z)
p(u)
1
0.6
1
0.4
0.5
0.2
0
๐‘ข = 0.9
0
0.5
1
0
๐‘ง = 1.15
0
1
2
3
4
transformation
6
Transformation method: algorithm
๏‚ค
Algorithm for sampling from ๐‘(๐‘ง)
๏‚€
Draw a random number from the uniform distribution:
๐‘ข ๐œ ~๐‘ˆ 0,1
Transform ๐‘ข by applying the inverse cumulative density function (cdf) of the
desired target distribution:
๐‘ง ๐œ = ๐น โˆ’1 ๐‘ข ๐œ
๏‚€
Repeat both steps for ๐œ = 1 โ€ฆ ๐‘‡.
๏‚€
7
Transformation method: example
๏‚ค
๏‚ค
Example: sampling from the exponential distribution
๏‚€
The desired pdf is: ๐‘ ๐‘ง|๐œ† = ๐œ† exp(โˆ’๐œ†๐‘ง)
๏‚€
The corresponding cdf is: ๐น ๐‘ง = 1 โˆ’ exp โˆ’๐œ†๐‘ง
๏‚€
The inverse cdf is: ๐น โˆ’1 ๐‘ข = โˆ’ ๐œ† ln 1 โˆ’ ๐‘ข
๏‚€
Thus, ๐‘ง
1
๐œ
1
= โˆ’ ๐œ† ln 1 โˆ’ ๐‘ข
๐œ
is a sample from the exponential distribution.
Implementation in MATLAB
for t=1:10000
z(t) = -1/lambda*log(1-rand);
end
hist(z)
mean(z)
8
Transformation method: summary
๏‚ค
Discussion
๏ฒyields high-quality samples
๏ฒeasy to implement
๏ฒcomputationally efficient
๏ณ obtaining the inverse cdf can be difficult
9
2
Rejection sampling
and importance sampling
Rejection sampling
๏‚ค
Idea: when the transformation method cannot be applied, we can resort to a
more general method called rejection sampling. Here, we draw random numbers
from a simpler proposal distribution ๐‘ž(๐‘ง) and keep only some of these samples.
The proposal distribution ๐‘ž(๐‘ง),
scaled by a factor ๐‘˜, represents
an envelope of ๐‘(๐‘ง).
11
Rejection sampling: algorithm
๏‚ค
Algorithm for sampling from ๐‘(๐‘ง)
๏‚€
๏‚€
๏‚€
๏‚€
๏‚€
Sample ๐‘ง0 from ๐‘ž(๐‘ง)
Sample ๐‘ข0 from ๐‘ˆ(0,1)
If ๐‘ข0 โ‰ค ๐‘(๐‘ง0 )/๐‘˜๐‘ž ๐‘ง0 , then accept the sample:
๐‘ง ๐œ = ๐‘ง0
Otherwise, discard ๐‘ง0 and ๐‘ข0 .
Repeat until we have obtained ๐‘‡ accepted samples.
12
Importance sampling
๏‚ค
๏‚ค
Idea: if our goal is to compute the expectation ๐ธ[๐‘ง], we can outperform rejection
sampling by bypassing the generationg of random samples.
Naïve approach
๏‚€
A naïve approach would be to approximate the expectation as follows. Rather than
sampling from ๐‘(๐‘ง), we discretize ๐‘ง-space into a uniform grid and evaluate:
๐ฟ
๐‘(๐‘ง ๐‘™ ) ๐‘ง (๐‘™)
๐”ผ๐‘ง โ‰ˆ
๐‘™=1
๏‚€
There are two problems with this approach:
๏‚€
๏‚€
The number of terms in the summation grows exponentially with the dimensionality of ๐‘ง.
Only a small proportion of the samples will make a significant contribution to the sum.
Uniform sampling clearly is very inefficient.
13
Importance sampling
๏‚ค
Addressing the two problems of the naive approach above, given a proposal
distribution ๐‘ž(๐‘ง), we can approximate the expectation as
๐”ผ ๐‘ง = โˆซ ๐‘ง ๐‘ ๐‘ง ๐‘‘๐‘ง =
๐‘ ๐‘ง
1
๐‘ง
๐‘ž ๐‘ง ๐‘‘๐‘ง โ‰ˆ
๐‘ž ๐‘ง
๐ฟ
where the samples ๐‘ง (๐‘™) are drawn from ๐‘ž.
๏‚ค
The quantities ๐‘Ÿ๐‘™ =
๐‘ ๐‘ง (๐‘™)
๐‘ž ๐‘ง (๐‘™)
๐ฟ
๐‘™=1
๐‘ ๐‘ง
๐‘ž ๐‘ง
๐‘™
๐‘™
๐‘ง
๐‘™
are known as importance weights, and they correct the
bias introduced by sampling from the wrong distribution.
๏‚ค
Unlike in the case of rejection sampling, all of the generated samples are
retained.
14
3
Markov Chain Monte Carlo
Markov Chain Monte Carlo (MCMC)
๏‚ค
Idea: we can sample from a large class of distributions and overcome the
problems that previous methods face in high dimensions using a framework
called Markov Chain Monte Carlo.
16
Background on Markov chains
๏‚ค
๏‚ค
A first-order Markov chain is defined as a series of random variables ๐‘ง
such the following conditional-independence property holds:
๐‘ ๐‘ง ๐‘š+1 ๐‘ง 1 , โ€ฆ , ๐‘ง ๐‘š = ๐‘ ๐‘ง ๐‘š+1 ๐‘ง ๐‘š
,โ€ฆ,๐‘ง
๐‘€
Thus, the graphical model of a Markov chain is a chain:
๐‘ง
๏‚ค
1
1
๐‘ง
2
๐‘ง
โ€ฆ
3
๐‘ง
๐‘€
A Markov chain is specified in terms of
๏‚€
the initial probability distribution ๐‘ ๐‘ง
๏‚€
the transition probabilities ๐‘ ๐‘ง
๐‘š+1
0
๐‘ง
๐‘š
17
Background on Markov chains
Markov chain: state diagram
2
Equlibrium distribution
๐‘(๐‘ง)
0.5
1
0.5
0.5
3
๐‘ง
1
2
3
18
Background on Markov chains
Markov chain: state diagram
2
Equlibrium distribution
๐‘(๐‘ง)
1
3
๐‘ง
1
2
3
19
The idea behind MCMC
p(z)
Desired target distribution
z
f(z)
Empirical distribution of samples
1
2
3
4
5
6
7
8
z
Markov chain whose
equilibrium distribution is
๐‘(๐‘ง)
20
Metropolis algorithm
๏‚ค
Algorithm for sampling from ๐‘(๐‘ง)
๏‚€
๏‚€
๏‚€
Initialize by drawing ๐‘ง (1) somehow.
At cycle ๐œ + 1, draw a candidate sample ๐‘ง โˆ— from ๐‘ž ๐‘ง ๐‘ง ๐œ .
Importantly, ๐‘ž needs to be symmetric, i.e., ๐‘ž ๐‘ง1 ๐‘ง2 = ๐‘ž ๐‘ง2 ๐‘ง1 .
Accept ๐‘ง (๐œ+1) โ† ๐‘ง โˆ— with probability
โˆ—
๐ด ๐‘ง ,๐‘ง
๐œ
= min
and otherwise set ๐‘ง
๏‚ค
๐‘(๐‘ง โˆ— )
1,
= min
๐‘(๐‘ง ๐œ )
(๐œ+1)
(๐œ)
โ†๐‘ง
๐‘(๐‘ง โˆ— )
1,
๐‘(๐‘ง ๐œ )
,
.
Notes
๏‚€
๏‚€
In contrast to rejection sampling, each cycle leads to a new sample, even
when the candidate ๐‘ง โˆ— is discarded.
Note that the sequence ๐‘ง 1 , ๐‘ง 2 , โ€ฆ is not a set of independent samples from
๐‘(๐‘ง) because successive samples are highly correlated.
21
Metropolis-Hastings algorithm
๏‚ค
Algorithm for sampling from ๐‘(๐‘ง)
๏‚€
๏‚€
๏‚€
Initialize by drawing ๐‘ง (1) somehow.
At cycle ๐œ + 1, draw a candidate sample ๐‘ง โˆ— from ๐‘ž(๐‘ง|๐‘ง ๐œ ).
In contrast to the Metropolis algorithm (see previous slide), ๐‘ž no longer needs
to be symmetric.
Accept ๐‘ง (๐œ+1) โ† ๐‘ง โˆ— with probability
๐‘ ๐‘ง โˆ— ๐‘ž๐‘˜ ๐‘ง ๐œ ๐‘ง โˆ—
โˆ—
๐œ
๐ด ๐‘ง ,๐‘ง
= min 1,
,
๐œ
โˆ—
๐œ
๐‘(๐‘ง
๐‘ž๐‘˜ (๐‘ง |๐‘ง
and otherwise set ๐‘ง (๐œ+1) โ† ๐‘ง (๐œ) .
22
Metropolis: accept or reject?
Inrease in density:
p(z*)
p(zฯ„)
๏ก ๏€ฝ1
zฯ„ z*
Decrease in density:
p(zฯ„)
p(z*)
~
p ( z*)
๏ก๏€ฝ~ ๏ด
p( z )
zฯ„ z*
23
Gibbs sampling
๏‚ค
๏‚ค
๏‚ค
Idea: as an alternative to the Metropolis-Hastings algorithm, Gibbs sampling is
less broadly applicable but does away with acceptance tests and can therefore be
more efficient.
Suppose we wish to sample from a multivariate distribution ๐‘ ๐‘ง = ๐‘(๐‘ง1 , โ€ฆ , ๐‘ง๐‘€ ),
e.g., representing several variables in a model. For example, we might be
interested in their joint posterior distribution.
In Gibbs sampling, we update one component at a time.
๐‘ง1
๐‘ง1
๐‘ง1
๐‘ง2
๐‘ง2
๐‘ง2
๐‘ง3
๐‘ง3
๐‘ง4
๐‘ง4
๐‘ง5
๐‘ง5
๐‘ง5
๐‘ง6
๐‘ง6
๐‘ง6
โ€ฆ
๐‘ง3
๐‘ง4
24
Gibbs sampling
๏‚ค
Algorithm for sampling from ๐‘(๐‘ง)
๏‚€
๏‚€
๏‚€
Initialize {๐‘ง๐‘– : ๐‘– = 1, โ€ฆ , ๐‘€} somehow.
๐œ
๐œ
At cycle ๐œ + 1, sample ๐‘ง๐‘– ~๐‘ ๐‘ง๐‘– ๐‘ง\๐‘– , i.e., replace the ๐‘– ๐‘กโ„Ž variable by a new sample,
drawn from a distribution that is conditioned of the current values of all other
variables. The resulting new vector is our new sample.
In the next cycle, replace a different variable ๐‘–. The simplest procedure is to go round
๐‘– = 1, โ€ฆ , ๐‘€, 1, โ€ฆ , ๐‘€, โ€ฆ Alternatively, ๐‘– could be chosen randomly.
25
Summary
๏‚ค
๏‚ค
๏‚ค
Throughout Bayesian statistics, we encounter intractable problems.
Most of these problems are: (i) evaluating a distribution; or (ii)
computing the expectation of a distribution.
Sampling methods provide a stochastic alternative to deterministic
methods. They are usually computationally less efficient, but are
asymptotically correct, broadly applicable, and easy to implement.
We looked at three main approaches:
๏‚€
๏‚€
๏‚€
Transformation method: efficient sampling from simple distributions
Rejection sampling and importance sampling: sampling from arbitrary
distributions; direct computation of an expected value
Monte Carlo Markov Chain (MCMC): efficient sampling from high-dimensional
distributions through the Metropolis-Hastings algorithm or Gibbs sampling
26
Supplementary slides
27
Transformation method: proof
๏‚ค
๏‚ค
Procedure: ๐‘ง
๐‘™
= ๐น โˆ’1 ๐‘ข
๐‘™
Proof:
๐‘‹ โ‰” ๐น โˆ’1 (๐‘ˆ). Is ๐‘‹~๐น?
๐‘ƒ ๐‘‹ โ‰ค ๐‘ก = ๐‘ ๐น โˆ’1 ๐‘ˆ โ‰ค ๐‘ก = ๐‘ƒ ๐‘ˆ โ‰ค ๐น ๐‘ก = ๐น(๐‘ก)
Moreover: ๐‘ˆ~๐‘… 0,1 โŸน ๐‘ƒ ๐‘ˆ โ‰ค ๐‘˜ = ๐‘˜, 0 โ‰ค ๐‘˜ โ‰ค 1
Therefore: ๐‘ƒ ๐‘ˆ โ‰ค ๐น ๐‘ก = ๐น ๐‘ก โŸน ๐‘‹~๐น
28
Background on Markov chains
๏‚ค
๏‚ค
Ergodicity: A Markov chain is ergodic if the distribution ๐‘ ๐‘ง ๐‘š converges to an
invariant distribution ๐‘โˆ— (๐‘ง) irrespective of the choice of initial distribution
๐‘ ๐‘ง 0 . A homogeneous Markov chain will be ergodic, subject to some weak
restrictions on the invariant distribution and the transition probabilities.
Equilibrium distribution: Given an ergodic Markov chain, that invariant
distribution which any initial distribution converges to is called the equilibrium
distribution. (An ergodic Markov chain may have several invariant distributions
but only one of these is the equilibrium distribution.)
29