MCMC

This is a sample of a gaussian density using the "native" method:

μ
1x2
1  1
cov
2x2
1.000  0.800
0.800  1.000










 -5 
 -4 
 -3 
 -2 
 -1 
 0 
 1 
 2 
 3 
 4 
 5 

 x 




 -5 
 -4 
 -3 
 -2 
 -1 
 0 
 1 
 2 
 3 
 4 
 5 







 y 











































































 

 


























































































































 

 


















 output 




disp 3 (asRow $ fst okt)
1x2
1.038  1.034
disp 3 (snd okt)
2x2
0.815  0.660
0.660  0.876




 marginal density 









 -4 
 -2 
 0 
 2 
 4 

 x 




 0.0 
 0.1 
 0.2 
 0.3 
 0.4 







 y 



















































 

 

































 

 


























 empirical 
 true 




Metropolis

metropolis' :: Seed -> Int -> Int ->  -> V -> (V -> ) -> [MCMC] 
metropolis' seed step burn σ x0 lprob = g $ scanl f mc0 deltas
  where
    f (MCMC x lp _) (δ,r)
        | accept    = MCMC x' lp' True
        | otherwise = MCMC x  lp  False
      where
        x' = x+δ
        lp' = lprob x'
        ratio = lp' - lp
        accept = ratio > 0 || log r < ratio

    deltas = zip (infn seed (size x0) σ) (infu seed)
    g = drop burn . map head . chunksOf step
    mc0 = MCMC x0 (lprob x0) True

(deltas is an infinite list of tuples of gaussian perturbations of the appropriate dimensions and uniform values)

The initial raw samples (including the "burn-in" time and without any thining):











 -5 
 -4 
 -3 
 -2 
 -1 
 0 
 1 
 2 
 3 
 4 
 5 

 x 




 -5 
 -4 
 -3 
 -2 
 -1 
 0 
 1 
 2 
 3 
 4 
 5 







 y 











































































 

 


























































































































 

 


















 output 




The resulting "good" sample:











 -5 
 -4 
 -3 
 -2 
 -1 
 0 
 1 
 2 
 3 
 4 
 5 

 x 




 -5 
 -4 
 -3 
 -2 
 -1 
 0 
 1 
 2 
 3 
 4 
 5 







 y 











































































 

 


























































































































 

 


















 output 








 marginal density 









 -4 
 -2 
 0 
 2 
 4 

 x 




 0.0 
 0.1 
 0.2 
 0.3 
 0.4 







 y 



















































 

 

































 

 


























 empirical 
 true 




disp 3 (asRow $ fst ok)
1x2
1.341  1.180
disp 3 (snd ok)
2x2
1.272  1.017
1.017  1.288

(Convergence to the true parameters can be confirmed with a larger sample size.)

Another example

(test2,ar2) = info $ take 300 $ metropolis' seed step burn σ x0 logProb
  where
    seed = 888
    step = 10
    burn = 1000
    σ = 1
    x0 = vector [-3,3]
    logProb = lgauss 2 (0.3^2) . scalar . norm_2

The acceptance ratio is 0.35 .











 -5 
 -4 
 -3 
 -2 
 -1 
 0 
 1 
 2 
 3 
 4 
 5 

 x 




 -5 
 -4 
 -3 
 -2 
 -1 
 0 
 1 
 2 
 3 
 4 
 5 







 y 











































































 

 


































































































































































































































































































































 

 


















 output 




bayesian billiards game

This is the example in the excellent post frequentism vs bayesianism.

The 5-3 advantage gives a maximum likelihood estimate $\hat p = 3/8= 0.375 $, and then a probability of three consecutive wins $\hat p^3$ = 0.05.

The full bayesian analisis gives a different result. For a uniform prior on $p$ the posterior is:

$$P(p|3-5)=\frac{p^3(1-p)^5}{B(4,6)}$$





 P(p|3-5) 









 0.0 
 0.1 
 0.2 
 0.3 
 0.4 
 0.5 
 0.6 
 0.7 
 0.8 
 0.9 
 1.0 

 p 




 0.0 
 0.2 
 0.4 
 0.6 
 0.8 
 1.0 
 1.2 
 1.4 
 1.6 
 1.8 
 2.0 
 2.2 
 2.4 
 2.6 







 P(p) 

















































































 

 






















 

 















 output 




The probability of tree consecutive wins with this distribution is

$$P(6-5) = \frac{B(7,6)}{B(4,6)} \simeq 0.091$$

We can try to replicate this result using Metropolis' MCMC. First we compute samples from the posterior distribution of $p$. This is a very simple one-dimensional case.

mapM_ (printf "%5.2f\n") $ take 10 $ map ((!0)) test4
 0.36
 0.28
 0.36
 0.12
 0.24
 0.28
 0.77
 0.35
 0.46
 0.47

The acceptance ratio is 0.202 . The histogram is consistent with the analytic solution derived above:





 P(p) 









 0.0 
 0.2 
 0.4 
 0.6 
 0.8 
 1.0 

 p 




 0.0 
 0.2 
 0.4 
 0.6 
 0.8 
 1.0 
 1.2 
 1.4 
 1.6 
 1.8 
 2.0 
 2.2 
 2.4 
 2.6 
 2.8 







 P(p) 









































































 

 

































 

 


























   
 output 




And then we compute the empirical expectation of the probability of three more wins:

printf "%.3f" $ average (map ((**3).(!0)) test4)
0.094