Stochastic Gradient Markov Chain Monte Carlo

Provides functions that performs popular stochastic gradient Markov chain Monte Carlo (SGMCMC) methods on user specified models. The required gradients are automatically calculated using 'TensorFlow' <>, an efficient library for numerical computation. This means only the log likelihood and log prior functions need to be specified. The methods implemented include stochastic gradient Langevin dynamics (SGLD), stochastic gradient Hamiltonian Monte Carlo (SGHMC), stochastic gradient Nose-Hoover thermostat (SGNHT) and their respective control variate versions for increased efficiency. References: M. Welling, Y. W. Teh (2011) <>; T. Chen, E. B. Fox, C. E. Guestrin (2014) ; N. Ding, Y. Fang, R. Babbush, C. Chen, R. D. Skeel, H. Neven (2014) <>; J. Baker, P. Fearnhead, E. B. Fox, C. Nemeth (2017) .

Travis-CI Build Status CRAN_Status_Badge

sgmcmc implements popular stochastic gradient Markov chain Monte Carlo (SGMCMC) methods including stochastic gradient Langevin dynamics (SGLD), stochastic gradient Hamiltonian Monte Carlo (SGHMC) and stochastic gradient Nosé-Hoover thermostat (SGNHT). The package uses automatic differentiation, so all the differentiation needed for the methods is calculated automatically. Control variate methods can be used in order to improve the efficiency of the methods as proposed in the recent publication.

The package is built on top of the TensorFlow library for R, which has a lot of support for statistical distributions and operations, which allows a large class of posteriors to be built. For more details can be found at the TensorFlow R library webpage, also see the TensorFlow API for full documentation.


sgmcmc requires TensorFlow for R to be installed, which requires packages that can't be automatically built by R, so has a few steps:

  • Install TensorFlow for R, first run install.packages("tensorflow"), then after run tensorflow::install_tensorflow().
  • Now run install.packages("sgmcmc").

The TensorFlow API changes a lot, so it's best to make sure your TensorFlow for python installation is up to date.


It's recommended you start here. This getting started page outlines the general structure of the package and its usage.

There's also worked examples for the following models (these will be extended as the package matures):

The SGMCMC algorithms can also be run step by step, which allows custom storage of parameters using test functions, or sequential estimates. Useful if your chain is too large to fit into memory! This requires a better knowledge of TensorFlow. An example of this is given in the neural network vignette.

Finally full details of the API can be found here.

For the source code, and bug reporting, see the Github page.

Issues Running Examples

If you are having issues running the examples, as a first port of call please make sure your TensorFlow installation is the most up to date version. A lot of issues are simply because the TensorFlow API has changed. If you're still having issues, please file a bug report.


sgmcmc 0.2.0

  • Added the ability to run algorithms step by step. This allows custom storage of parameters, useful when the full chain does not fit into memory!
  • Added new vignette to demonstrate step by step functionality -- a Bayesian neural network model.
  • Changed optimizer to TensorFlow SGDOptimizer for control variate methods.

sgmcmc 0.1.0

  • Initial release.

Reference manual

It appears you don't have a PDF plugin for this browser. You can click here to download the reference manual.


0.2.1 by Jack Baker, 4 days ago

Report a bug at

Browse source code at

Authors: Jack Baker [aut, cre, cph], Christopher Nemeth [aut, cph], Paul Fearnhead [aut, cph], Emily B. Fox [aut, cph], STOR-i [cph]

Documentation:   PDF Manual  

GPL-3 license

Imports utils

Depends on tensorflow

Suggests testthat, MASS, knitr, ggplot2, rmarkdown

System requirements: TensorFlow (

See at CRAN