Learn R Programming

sgmcmc: a stochastic gradient MCMC package for R

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. More details can be found at the TensorFlow R library webpage, also see the TensorFlow API for full documentation.

Citing sgmcmc

To cite the sgmcmc package, please reference the accompanying paper. Sample Bibtex is given below:

@article{sgmcmc-package,
  title        = "{sgmcmc}: An {R} Package for Stochastic Gradient {M}arkov Chain {M}onte {C}arlo",
  author       = c(person(given = "Jack",
                          family = "Baker",
                          email = "jackbaker92@mail.com"),
                   person(given = "Christopher",
                          family = "Nemeth",
                          email = "c.nemeth@lancaster.ac.uk"),
                   person(given = "Paul",
                          family = "Fearnhead",
                          email = "p.fearnhead@lancaster.ac.uk"),
                   person(given = c("Emily", "B."),
                          family = "Fox")),
  journal      = "Journal of Statistical Software",
  year         = "2019",
  volume       = "91",
  number       = "3",
  pages        = "1--27",
  doi          = "10.18637/jss.v091.i03"
}

Installation

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 the sgmcmc R package: install.packages("sgmcmc").
  • Install the required python packages (including TensorFlow and TensorFlow Probability) by running: sgmcmc::installTF().

If you already have the TensorFlow and TensorFlow probability packages installed, then this should be autodetected by the package and you can skip the final step. Make sure these are up to date though, as the TensorFlow API is under active development and still changes quite regularly. Especially ensure that your TensorFlow and TensorFlow probability modules are compatible.

Documentation

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.

Copy Link

Version

Install

install.packages('sgmcmc')

Monthly Downloads

90

Version

0.2.5

License

GPL-3

Issues

Pull Requests

Stars

Forks

Maintainer

Last Published

October 24th, 2019

Functions in sgmcmc (0.2.5)

installTF

Install TensorFlow and TensorFlow Probability
sgldSetup

Create an sgld object
getParams

Get current parameter values
sghmcSetup

Create an sghmc object
sghmc

Stochastic Gradient Hamiltonian Monte Carlo
getDataset

Load example datasets
sgld

Stochastic Gradient Langevin Dynamics
initSess

Initialise TensorFlow session and sgmcmc algorithm
sghmccv

Stochastic Gradient Hamiltonian Monte Carlo with Control Variates
sghmccvSetup

Create an sghmccv object
sgmcmc

sgmcmc: A package for stochastic gradient MCMC
sgmcmcStep

Single step of sgmcmc
sgnhtcvSetup

Create an sgnhtcv object
sgnhtcv

Stochastic Gradient Nose Hoover Thermostat with Control Variates
sgnhtSetup

Create an sgnht object
sgnht

Stochastic Gradient Nose Hoover Thermostat
sgldcvSetup

Create an sgldcv object
sgldcv

Stochastic Gradient Langevin Dynamics with Control Variates