I wrote the code for Gibbs sampling

It describes almost the same content (and the explanation is simple!) [[MH method] I wrote MCMC in Python [Gibbs-Sampler]](http://www.fisproject.jp/2015/12/mcmc I wrote a sample code for Gibbs sampling even though there is -in-python /). (Although it is full of reinventing the wheel (tears))

Why is it public?

There is python code published at the beginning, why is it published? Since MCMC is a complicated story in both rabbits and corners, I thought it would be a good idea to think that there is a "code that works for the time being" for beginners.

So, rather than "I wrote it", the amount of code I wrote was surprisingly short, so I have the frustration of "I don't know what it is, but what is it after all!" If you can add even a little, ...

What is MCMC?

I also tried to organize the [MCMC] of my predecessor. ](Http://qiita.com/shogiai/items/bab2b915df2b8dd6f6f2).

Distribution generated this time

It's a two-dimensional normal distribution: $ p(x,y)=\frac{1}{Z}\exp\big(-\frac{x^2-2bxy+y^2}{2}\big), Z=\frac{2\pi}{\sqrt{1-b^2}} $ Matched to the variables used in the python code below. $ Z $ is a normalization constant.

python code

##Preparation
import seaborn as sns
import numpy as np
from scipy.stats import norm

##Constants that determine the correlation of the two-dimensional normal distribution
b = 0.8
##Determining sample size
N = 10000

##List to put what you can sample
x = []
y = []

##The initial point is like this
x_init = 3.0
y_init = 9.0

##Random number generation according to a two-dimensional normal distribution.
##The place where the else and after of the for statement move, as it is commonly called "Kakukaku"
for i in range(N):
    if i==0:
        x.append(x_init) ##Initial point(x coordinate)
        y.append(y_init) ##Initial point(y coordinate)
    else:
        x.append(norm(loc=y[i-1]*b, scale=1.0).rvs(size=1.0)[0]) #Fix the y coordinate and set the x coordinate to N(Center point=Fixed y coordinate,1)Select from
        y.append(norm(loc=x[i]*b, scale=1.0).rvs(size=1.0)[0]) #Fix the x coordinate and set the x coordinate to N(Center point=Fixed x coordinate,1)Select from

%matplotlib inline ##If you want to draw with Jupyter Notebook, this is awesome!
sns.jointplot(np.array(x),np.array(y))

Unknown.png

A word related to the initial value

Even if you change the initial value, it will eventually become a steady distribution, so if you want only the "steady distribution", it is also a point to consider cutting off the sampling around the initial value a little later. This time, the motto was "Rabbit horns, simple code!"

Reference -MCMC Lecture (Yukito Iba) Difficulty ★★: This is a video that explains MCMC from a really simple place. -[Statistics] Let's explain sampling by Markov chain Monte Carlo method (MCMC) with animation. : The same distribution is carefully added to the animation.

Recommended Posts

I wrote the code for Gibbs sampling
I wrote the code for Japanese sentence generation with DeZero
I just wrote the original material for the python sample code
I tried porting the code written for TensorFlow to Theano
[Python] I implemented peripheral Gibbs sampling
I wrote the queue in Python
I wrote the stack in Python
I wrote the code to write the code of Brainf * ck in python
I tried tensorflow for the first time
I wrote the selection sort in C
I wrote unit tests for various languages
[Python beginner] I collected the articles I wrote
I wrote the sliding wing in creation.
I tried using scrapy for the first time
I checked the library for using the Gracenote API
vprof --I tried using the profiler for Python
I searched for railway senryu from the data
Somehow the code I wrote worked and I was impressed, so I will post it
I played with Floydhub for the time being
I tried python programming for the first time.
[Python] I searched for the longest Pokemon Shiritori
I tried Mind Meld for the first time
I wrote the hexagonal architecture in go language
Code for checking the operation of Python Matplotlib
A story that I was very convinced when I wrote the code for the Monty Hall problem and calculated the winning percentage
[CodeIQ] I wrote the probability distribution of dice (from CodeIQ math course for machine learning [probability distribution])
What I got into Python for the first time
I tried Python on Mac for the first time.
I wrote an automatic kitting script for OpenBlocks IoT
Create a QR code for the URL on Linux
I tried python on heroku for the first time
For the first time, I learned about Unix (Linux).
I wrote an automatic installation script for Arch Linux
I will install Arch Linux for the time being.
I felt that I ported the Python code to C ++ 98.
AI Gaming I tried it for the first time
I searched for the contents of CloudWatch Logs Agent
I tried running the sample code of the Ansible module
I wrote you to watch the signal with Go
I installed the retro game engine pyxel for Python on Mac and started the sample code
Techniques for code testing?
I counted the grains
[TensorFlow] I want to master the indexing for Ragged Tensor
I was hooked for 2 minutes with the Python debugger pdb
Test code to check for broken links in the page
I touched the latest automatic test tool "Playwright for Python"
I want to move selenium for the time being [for mac]
I tried running PIFuHD on Windows for the time being
Convenient for training newcomers? I wrote a Telnet practice server
I wrote the basic grammar of Python with Jupyter Lab
I wrote a demo program for linear transformation of a matrix
I wrote the basic operation of Seaborn in Jupyter Lab
I tried to summarize the code often used in Pandas
I wrote it in Go to understand the SOLID principle
I didn't know how to use the [python] for statement
I wrote a script to combine the divided ts files
Miscellaneous notes that I tried using python for the matter
I wrote the basic operation of Numpy in Jupyter Lab.
I tried the Google Cloud Vision API for the first time
I want to map the EDINET code and securities number
I wrote the basic operation of matplotlib with Jupyter Lab