-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathImportance.lhs
265 lines (201 loc) · 7.07 KB
/
Importance.lhs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
% Importance Sampling
% Dominic Steinitz
% 14th August 2014
---
bibliography: Kalman.bib
---
\newcommand{\condprob} [3] {#1\left(#2 \,\vert\, #3\right)}
Importance Sampling
===================
Suppose we have an random variable $X$ with pdf $1/2\exp{-\lvert
x\rvert}$ and we wish to find its second moment numerically. However,
the [random-fu](https://hackage.haskell.org/package/random-fu) package
does not support sampling from such as distribution. We notice that
$$
\int_{-\infty}^\infty x^2 \frac{1}{2} \exp{-\lvert x\rvert} \mathrm{d}x =
\int_{-\infty}^\infty x^2 \frac{\frac{1}{2} \exp{-\lvert x\rvert}}
{\frac{1}{\sqrt{8\pi}}{\exp{-x^2/8}}}
\frac{1}{\sqrt{8\pi}}{\exp{-x^2/8}}
\,\mathrm{d}x
$$
So we can sample from ${\cal{N}}(0, 4)$ and evaluate
$$
x^2 \frac{\frac{1}{2} \exp{-\lvert x\rvert}}
{\frac{1}{\sqrt{8\pi}}{\exp{-x^2/8}}}
$$
> {-# OPTIONS_GHC -Wall #-}
> {-# OPTIONS_GHC -fno-warn-name-shadowing #-}
> {-# OPTIONS_GHC -fno-warn-type-defaults #-}
> {-# OPTIONS_GHC -fno-warn-unused-do-bind #-}
> {-# OPTIONS_GHC -fno-warn-missing-methods #-}
> {-# OPTIONS_GHC -fno-warn-orphans #-}
> module Importance where
> import Control.Monad
> import Data.Random.Source.PureMT
> import Data.Random
> import Data.Random.Distribution.Binomial
> import Data.Random.Distribution.Beta
> import Control.Monad.State
> import qualified Control.Monad.Writer as W
> sampleImportance :: RVarT (W.Writer [Double]) ()
> sampleImportance = do
> x <- rvarT $ Normal 0.0 2.0
> let x2 = x^2
> u = x2 * 0.5 * exp (-(abs x))
> v = (exp ((-x2)/8)) * (recip (sqrt (8*pi)))
> w = u / v
> lift $ W.tell [w]
> return ()
> runImportance :: Int -> [Double]
> runImportance n =
> snd $
> W.runWriter $
> evalStateT (sample (replicateM n sampleImportance))
> (pureMT 2)
We can run this 10,000 times to get an estimate.
[ghci]
import Formatting
format (fixed 2) (sum (runImportance 10000) / 10000)
Since we know that the $n$-th moment of the exponential distribution
is $n! / \lambda^n$ where $\lambda$ is the rate (1 in this example),
the exact answer is 2 which is not too far from our estimate using
importance sampling.
The value of
$$
w(x) = \frac{1}{N}\frac{\frac{1}{2} \exp{-\lvert x\rvert}}
{\frac{1}{\sqrt{8\pi}}{\exp{-x^2/8}}}
= \frac{p(x)}{\pi(x)}
$$
is called the weight, $p$ is the pdf from which we wish to sample and
$\pi$ is the pdf of the importance distribution.
Importance Sampling Approximation of the Posterior
==================================================
Suppose that the posterior distribution of a model in which we are
interested has a complicated functional form and that we therefore
wish to approximate it in some way. First assume that we wish to
calculate the expectation of some arbitrary function $f$ of the
parameters.
$$
{\mathbb{E}}(f({x}) \,\vert\, y_1, \ldots y_T) =
\int_\Omega f({x}) p({x} \, \vert \, y_1, \ldots y_T) \,\mathrm{d}{x}
$$
Using Bayes
$$
\int_\Omega f({x}) \condprob{p}{x}{y_1, \ldots y_T} \,\mathrm{d}{x} =
\frac{1}{Z}\int_\Omega f({x}) \condprob{p}{y_1, \ldots y_T}{x}p(x) \,\mathrm{d}{x}
$$
where $Z$ is some normalizing constant.
As before we can re-write this using a proposal distribution $\pi(x)$
$$
\frac{1}{Z}\int_\Omega f({x}) \condprob{p}{y_1, \ldots y_T}{x}p(x) \,\mathrm{d}{x} =
\frac{1}{Z}\int_\Omega \frac{f({x}) \condprob{p}{y_1, \ldots y_T}{x}p(x)}{\pi(x)}\pi(x) \,\mathrm{d}{x}
$$
We can now sample $X^{(i)} \sim \pi({x})$ repeatedly to obtain
$$
{\mathbb{E}}(f({x}) \,\vert\, y_1, \ldots y_T) \approx \frac{1}{ZN}\sum_1^N
f({X^{(i)}}) \frac{p(y_1, \ldots y_T \, \vert \, {X^{(i)}})p({X^{(i)}})}
{\pi({X^{(i)}})} =
\sum_1^N w_if({X^{(i)}})
$$
where the weights $w_i$ are defined as before by
$$
w_i = \frac{1}{ZN} \frac{p(y_1, \ldots y_T \, \vert \, {X^{(i)}})p({X^{(i)}})}
{\pi({X^{(i)}})}
$$
We follow [Alex
Cook](http://blog.nus.edu.sg/alexcook/teaching/sph6004/) and use the
example from [@citeulike:5986027]. We take the prior as $\sim
{\cal{Be}}(1,1)$ and use ${\cal{U}}(0.0,1.0)$ as the proposal
distribution. In this case the proposal and the prior are identical
just expressed differently and therefore cancel.
Note that we use the log of the pdf in our calculations otherwise we
suffer from (silent) underflow, e.g.,
[ghci]
pdf (Binomial nv (0.4 :: Double)) xv
On the other hand if we use the log pdf form
[ghci]
logPdf (Binomial nv (0.4 :: Double)) xv
> xv, nv :: Int
> xv = 51
> nv = 8197
> sampleUniform :: RVarT (W.Writer [Double]) ()
> sampleUniform = do
> x <- rvarT StdUniform
> lift $ W.tell [x]
> return ()
> runSampler :: RVarT (W.Writer [Double]) () ->
> Int -> Int -> [Double]
> runSampler sampler seed n =
> snd $
> W.runWriter $
> evalStateT (sample (replicateM n sampler))
> (pureMT (fromIntegral seed))
> sampleSize :: Int
> sampleSize = 1000
> pv :: [Double]
> pv = runSampler sampleUniform 2 sampleSize
> logWeightsRaw :: [Double]
> logWeightsRaw = map (\p -> logPdf (Beta 1.0 1.0) p +
> logPdf (Binomial nv p) xv -
> logPdf StdUniform p) pv
> logWeightsMax :: Double
> logWeightsMax = maximum logWeightsRaw
>
> weightsRaw :: [Double]
> weightsRaw = map (\w -> exp (w - logWeightsMax)) logWeightsRaw
> weightsSum :: Double
> weightsSum = sum weightsRaw
> weights :: [Double]
> weights = map (/ weightsSum) weightsRaw
> meanPv :: Double
> meanPv = sum $ zipWith (*) pv weights
>
> meanPv2 :: Double
> meanPv2 = sum $ zipWith (\p w -> p * p * w) pv weights
>
> varPv :: Double
> varPv = meanPv2 - meanPv * meanPv
We get the answer
[ghci]
meanPv
But if we look at the size of the weights and the effective sample size
[ghci]
length $ filter (>= 1e-6) weights
(sum weights)^2 / (sum $ map (^2) weights)
so we may not be getting a very good estimate. Let's try
> sampleNormal :: RVarT (W.Writer [Double]) ()
> sampleNormal = do
> x <- rvarT $ Normal meanPv (sqrt varPv)
> lift $ W.tell [x]
> return ()
> pvC :: [Double]
> pvC = runSampler sampleNormal 3 sampleSize
> logWeightsRawC :: [Double]
> logWeightsRawC = map (\p -> logPdf (Beta 1.0 1.0) p +
> logPdf (Binomial nv p) xv -
> logPdf (Normal meanPv (sqrt varPv)) p) pvC
> logWeightsMaxC :: Double
> logWeightsMaxC = maximum logWeightsRawC
>
> weightsRawC :: [Double]
> weightsRawC = map (\w -> exp (w - logWeightsMaxC)) logWeightsRawC
> weightsSumC :: Double
> weightsSumC = sum weightsRawC
> weightsC :: [Double]
> weightsC = map (/ weightsSumC) weightsRawC
> meanPvC :: Double
> meanPvC = sum $ zipWith (*) pvC weightsC
> meanPvC2 :: Double
> meanPvC2 = sum $ zipWith (\p w -> p * p * w) pvC weightsC
>
> varPvC :: Double
> varPvC = meanPvC2 - meanPvC * meanPvC
Now the weights and the effective size are more re-assuring
[ghci]
length $ filter (>= 1e-6) weightsC
(sum weightsC)^2 / (sum $ map (^2) weightsC)
And we can take more confidence in the estimate
[ghci]
meanPvC
Bibliography
============