- Introduction to Python
- Getting started with Python and the IPython notebook
- Functions are first class objects
- Data science is OSEMN
- Working with text
- Preprocessing text data
- Working with structured data
- Using SQLite3
- Using HDF5
- Using numpy
- Using Pandas
- Computational problems in statistics
- Computer numbers and mathematics
- Algorithmic complexity
- Linear Algebra and Linear Systems
- Linear Algebra and Matrix Decompositions
- Change of Basis
- Optimization and Non-linear Methods
- Practical Optimizatio Routines
- Finding roots
- Optimization Primer
- Using scipy.optimize
- Gradient deescent
- Newton’s method and variants
- Constrained optimization
- Curve fitting
- Finding paraemeters for ODE models
- Optimization of graph node placement
- Optimization of standard statistical models
- Fitting ODEs with the Levenberg–Marquardt algorithm
- 1D example
- 2D example
- Algorithms for Optimization and Root Finding for Multivariate Problems
- Expectation Maximizatio (EM) Algorithm
- Monte Carlo Methods
- Resampling methods
- Resampling
- Simulations
- Setting the random seed
- Sampling with and without replacement
- Calculation of Cook’s distance
- Permutation resampling
- Design of simulation experiments
- Example: Simulations to estimate power
- Check with R
- Estimating the CDF
- Estimating the PDF
- Kernel density estimation
- Multivariate kerndel density estimation
- Markov Chain Monte Carlo (MCMC)
- Using PyMC2
- Using PyMC3
- Using PyStan
- C Crash Course
- Code Optimization
- Using C code in Python
- Using functions from various compiled languages in Python
- Julia and Python
- Converting Python Code to C for speed
- Optimization bake-off
- Writing Parallel Code
- Massively parallel programming with GPUs
- Writing CUDA in C
- Distributed computing for Big Data
- Hadoop MapReduce on AWS EMR with mrjob
- Spark on a local mahcine using 4 nodes
- Modules and Packaging
- Tour of the Jupyter (IPython3) notebook
- Polyglot programming
- What you should know and learn more about
- Wrapping R libraries with Rpy
Gibbs sampler
Suppose we have a vector of parameters \(\theta = (\theta_1, \theta_2, \dots, \theta_k)\), and we want to estimate the joint posterior distribution \(p(\theta | X)\). Suppose we can find and draw random samples from all the conditional distributions
\[\begin{split}p(\theta_1 | \theta_2, \dots \theta_k, X) \\ p(\theta_2 | \theta_1, \dots \theta_k, X) \\ \dots \\ p(\theta_k | \theta_1, \theta_2, \dots, X)\end{split}\]
With Gibbs sampling, the Markov chain is constructed by sampling from the conditional distribution for each parameter \(\theta_i\) in turn, treating all other parameters as observed. When we have finished iterating over all parameters, we are said to have completed one cycle of the Gibbs sampler. Where it is difficult to sample from a conditional distribution, we can sample using a Metropolis-Hastings algorithm instead - this is known as Metropolis wihtin Gibbs.
Gibbs sampling is a type of random walk thorugh parameter space, and hence can be thought of as a Metroplish-Hastings algorithm with a special proposal distribtion. At each iteration in the cycle, we are drawing a proposal for a new value of a particular parameter, where the propsal distribution is the conditional posterior probability of that parameter. This means that the propsosal move is always accepted. Hence, if we can draw ssamples from the ocnditional distributions, Gibbs sampling can be much more efficient than regular Metropolis-Hastings.
Advantages of Gibbs sampling
- No need to tune proposal distribution
- Proposals are always accepted
Disadvantages of Gibbs sampling
- Need to be able to derive conditional probability distributions
- need to be able to draw random samples from contitional probability distributions
- Can be very slow if paramters are coorelated becauce you cannot take “diagonal” steps (draw picture to illustrate)
Motivating example
We will use the toy example of estimating the bias of two coins given sample pairs \((z_1, n_1)\) and \((z_2, n_2)\) where \(z_i\) is the number of heads in \(n_i\) tosses for coin \(i\).
Setup
def bern(theta, z, N): """Bernoulli likelihood with N trials and z successes.""" return np.clip(theta**z * (1-theta)**(N-z), 0, 1)
def bern2(theta1, theta2, z1, z2, N1, N2): """Bernoulli likelihood with N trials and z successes.""" return bern(theta1, z1, N1) * bern(theta2, z2, N2)
def make_thetas(xmin, xmax, n): xs = np.linspace(xmin, xmax, n) widths =(xs[1:] - xs[:-1])/2.0 thetas = xs[:-1]+ widths return thetas
def make_plots(X, Y, prior, likelihood, posterior, projection=None): fig, ax = plt.subplots(1,3, subplot_kw=dict(projection=projection, aspect='equal'), figsize=(12,3)) if projection == '3d': ax[0].plot_surface(X, Y, prior, alpha=0.3, cmap=plt.cm.jet) ax[1].plot_surface(X, Y, likelihood, alpha=0.3, cmap=plt.cm.jet) ax[2].plot_surface(X, Y, posterior, alpha=0.3, cmap=plt.cm.jet) else: ax[0].contour(X, Y, prior) ax[1].contour(X, Y, likelihood) ax[2].contour(X, Y, posterior) ax[0].set_title('Prior') ax[1].set_title('Likelihood') ax[2].set_title('Posteior') plt.tight_layout()
thetas1 = make_thetas(0, 1, 101) thetas2 = make_thetas(0, 1, 101) X, Y = np.meshgrid(thetas1, thetas2)
Analytic solution
a = 2 b = 3 z1 = 11 N1 = 14 z2 = 7 N2 = 14 prior = stats.beta(a, b).pdf(X) * stats.beta(a, b).pdf(Y) likelihood = bern2(X, Y, z1, z2, N1, N2) posterior = stats.beta(a + z1, b + N1 - z1).pdf(X) * stats.beta(a + z2, b + N2 - z2).pdf(Y) make_plots(X, Y, prior, likelihood, posterior) make_plots(X, Y, prior, likelihood, posterior, projection='3d')
Grid approximation
def c2d(thetas1, thetas2, pdf): width1 = thetas1[1] - thetas1[0] width2 = thetas2[1] - thetas2[0] area = width1 * width2 pmf = pdf * area pmf /= pmf.sum() return pmf
_prior = bern2(X, Y, 2, 8, 10, 10) + bern2(X, Y, 8, 2, 10, 10) prior_grid = c2d(thetas1, thetas2, _prior) _likelihood = bern2(X, Y, 1, 1, 2, 3) posterior_grid = _likelihood * prior_grid posterior_grid /= posterior_grid.sum() make_plots(X, Y, prior_grid, likelihood, posterior_grid) make_plots(X, Y, prior_grid, likelihood, posterior_grid, projection='3d')
Metropolis
a = 2 b = 3 z1 = 11 N1 = 14 z2 = 7 N2 = 14 prior = lambda theta1, theta2: stats.beta(a, b).pdf(theta1) * stats.beta(a, b).pdf(theta2) lik = partial(bern2, z1=z1, z2=z2, N1=N1, N2=N2) target = lambda theta1, theta2: prior(theta1, theta2) * lik(theta1, theta2) theta = np.array([0.5, 0.5]) niters = 10000 burnin = 500 sigma = np.diag([0.2,0.2]) thetas = np.zeros((niters-burnin, 2), np.float) for i in range(niters): new_theta = stats.multivariate_normal(theta, sigma).rvs() p = min(target(*new_theta)/target(*theta), 1) if np.random.rand() < p: theta = new_theta if i >= burnin: thetas[i-burnin] = theta
kde = stats.gaussian_kde(thetas.T) XY = np.vstack([X.ravel(), Y.ravel()]) posterior_metroplis = kde(XY).reshape(X.shape) make_plots(X, Y, prior(X, Y), lik(X, Y), posterior_metroplis) make_plots(X, Y, prior(X, Y), lik(X, Y), posterior_metroplis, projection='3d')
Gibbs
a = 2 b = 3 z1 = 11 N1 = 14 z2 = 7 N2 = 14 prior = lambda theta1, theta2: stats.beta(a, b).pdf(theta1) * stats.beta(a, b).pdf(theta2) lik = partial(bern2, z1=z1, z2=z2, N1=N1, N2=N2) target = lambda theta1, theta2: prior(theta1, theta2) * lik(theta1, theta2) theta = np.array([0.5, 0.5]) niters = 10000 burnin = 500 sigma = np.diag([0.2,0.2]) thetas = np.zeros((niters-burnin,2), np.float) for i in range(niters): theta = [stats.beta(a + z1, b + N1 - z1).rvs(), theta[1]] theta = [theta[0], stats.beta(a + z2, b + N2 - z2).rvs()] if i >= burnin: thetas[i-burnin] = theta
kde = stats.gaussian_kde(thetas.T) XY = np.vstack([X.ravel(), Y.ravel()]) posterior_gibbs = kde(XY).reshape(X.shape) make_plots(X, Y, prior(X, Y), lik(X, Y), posterior_gibbs) make_plots(X, Y, prior(X, Y), lik(X, Y), posterior_gibbs, projection='3d')
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论