Flexible Programming of Hierarchical Modeling Algorithms and Compilation of R Using NIMBLE

Similar documents
Beyond the black box: Using, programming, and sharing hierarchical modeling algorithms such as MCMC and Sequential MC using NIMBLE

Extensible so,ware for hierarchical modeling: using the NIMBLE pla>orm to explore models and algorithms

Programming with models: writing statistical algorithms for general model structures with NIMBLE

Extensible so,ware for hierarchical modeling: using the NIMBLE pla>orm to explore models and algorithms

NIMBLE User Manual. NIMBLE Development Team. Version 0.1

NIMBLE User Manual. NIMBLE Development Team. Version 0.4-1

Package nimble. November 22, 2017

Linear Modeling with Bayesian Statistics

Bayesian Modelling with JAGS and R

GNU MCSim Frederic Yves Bois Chair of mathematical modeling and system biology for predictive toxicology

winbugs and openbugs

BUGS: Language, engines, and interfaces

CS281 Section 9: Graph Models and Practical MCMC

Modeling and Reasoning with Bayesian Networks. Adnan Darwiche University of California Los Angeles, CA

Approximate Bayesian Computation. Alireza Shafaei - April 2016

Bayesian Statistics Group 8th March Slice samplers. (A very brief introduction) The basic idea

How to Use (R)Stan to Estimate Models in External R Packages. Ben Goodrich of Columbia University

Introduction to Applied Bayesian Modeling A brief JAGS and R2jags tutorial

Monte Carlo Techniques for Bayesian Statistical Inference A comparative review

Bayesian models and inferential methods for forecasting disease outbreak severity

Parallel Gibbs Sampling From Colored Fields to Thin Junction Trees

STAT 203 SOFTWARE TUTORIAL

From Bayesian Analysis of Item Response Theory Models Using SAS. Full book available for purchase here.

Discussion on Bayesian Model Selection and Parameter Estimation in Extragalactic Astronomy by Martin Weinberg

Package mederrrank. R topics documented: July 8, 2015

Clustering Relational Data using the Infinite Relational Model

Lab 7: Bayesian analysis of a dice toss problem using C++ instead of python

A Short History of Markov Chain Monte Carlo

Applied Bayesian Nonparametrics 5. Spatial Models via Gaussian Processes, not MRFs Tutorial at CVPR 2012 Erik Sudderth Brown University

Computer vision: models, learning and inference. Chapter 10 Graphical Models

Package DPBBM. September 29, 2016

Package BPHO documentation

Debugging MCMC Code. Charles J. Geyer. April 15, 2017

INLA: Integrated Nested Laplace Approximations

RevBayes: Introduction and Demonstration of the basic elements

GLM Poisson Chris Parrish August 18, 2016

1. Start WinBUGS by double clicking on the WinBUGS icon (or double click on the file WinBUGS14.exe in the WinBUGS14 directory in C:\Program Files).

PIANOS requirements specifications

Intermediate Programming in R Session 2: Loops. Olivia Lau, PhD

Particle Filters for Visual Tracking

Journal of Statistical Software

INLA: an introduction

GiRaF: a toolbox for Gibbs Random Fields analysis

WinBUGS Tutorial Outline

Issues in MCMC use for Bayesian model fitting. Practical Considerations for WinBUGS Users

A Co-Clustering approach for Sum-Product Network Structure Learning

Probabilistic multi-resolution scanning for cross-sample differences

Markov Chain Monte Carlo (part 1)

Overview. Monte Carlo Methods. Statistics & Bayesian Inference Lecture 3. Situation At End Of Last Week

Package LCMCR. R topics documented: July 8, 2017

Bayesian Estimation for Skew Normal Distributions Using Data Augmentation

Test Run to Check the Installation of JAGS & Rjags

Package beast. March 16, 2018

Approximate (Monte Carlo) Inference in Bayes Nets. Monte Carlo (continued)

Probabilistic Graphical Models

Learning Objects and Parts in Images

Statistical Matching using Fractional Imputation

Bayesian data analysis using R

A GENERAL GIBBS SAMPLING ALGORITHM FOR ANALYZING LINEAR MODELS USING THE SAS SYSTEM

Package BGPhazard. R topics documented: February 11, Version Date

Stochastic Simulation: Algorithms and Analysis

R package mcll for Monte Carlo local likelihood estimation

The GLMMGibbs Package

Lecture 13 Segmentation and Scene Understanding Chris Choy, Ph.D. candidate Stanford Vision and Learning Lab (SVL)

Package INLABMA. February 4, 2017

CSC412: Stochastic Variational Inference. David Duvenaud

Illustration of Adaptive MCMC

Data Mining. Jeff M. Phillips. January 7, 2019 CS 5140 / CS 6140

Fitting Bayesian item response models in Stata and Stan

Markov chain Monte Carlo methods

Package sgmcmc. September 26, Type Package

MCMC for Bayesian Inference Metropolis: Solutions

Learning Global-to-Local Discrete Components with Nonparametric Bayesian Feature Construction

Human Upper Body Pose Estimation in Static Images

The Shogun Machine Learning Toolbox

Monte Carlo sampling

Introduction to Bayesian networks

Inference. Inference: calculating some useful quantity from a joint probability distribution Examples: Posterior probability: Most likely explanation:

Package ssa. July 24, 2016

Outline. S: past, present and future Some thoughts. The 80s. Interfaces - 60s & 70s. Duncan Temple Lang Department of Statistics UC Davis

COPULA MODELS FOR BIG DATA USING DATA SHUFFLING

Running WinBUGS from within R

Scalable Multidimensional Hierarchical Bayesian Modeling on Spark

Semiparametric Mixed Effecs with Hierarchical DP Mixture

Sum-Product Networks. STAT946 Deep Learning Guest Lecture by Pascal Poupart University of Waterloo October 15, 2015

The R statistical computing environment

Test Run to Check the Installation of OpenBUGS, JAGS, BRugs, R2OpenBUGS, Rjags & R2jags

Probabilistic Graphical Models

MCMC Methods for data modeling

Image analysis. Computer Vision and Classification Image Segmentation. 7 Image analysis

HBC: Hierarchical Bayes Compiler

Running R Faster. Tomas Kalibera

MCMC Diagnostics. Yingbo Li MATH Clemson University. Yingbo Li (Clemson) MCMC Diagnostics MATH / 24

Bayesian Computation with JAGS

Bayesian Analysis of Extended Lomax Distribution

Package nltt. October 12, 2016

Statistics (STAT) Statistics (STAT) 1. Prerequisites: grade in C- or higher in STAT 1200 or STAT 1300 or STAT 1400

Data Mining. Jeff M. Phillips. January 12, 2015 CS 5140 / CS 6140

UP School of Statistics Student Council Education and Research

{, } Probabilistic programming. John Winn and Tom Minka Machine Learning Summer School. September 2009

Transcription:

Flexible Programming of Hierarchical Modeling Algorithms and Compilation of R Using NIMBLE Christopher Paciorek UC Berkeley Statistics Joint work with: Perry de Valpine (PI) UC Berkeley Environmental Science, Policy and Managem t Daniel Turek Williams College Math and Statistics Nick Michaud UC Berkeley ESPM and Statistics Duncan Temple Lang UC Davis Statistics Bayesian nonparametrics development with: Claudia Wehrhahn Cortes UC Santa Cruz Applied Math and Statistics Abel Rodriguez UC Santa Cruz Applied Math and Statistics http://r-nimble.org October 2017 Funded by NSF DBI-1147230, ACI-1550488, DMS-1622444; Google Summer of Code 2015, 2017

Hierarchical statistical models A basic random effects / Bayesian hierarchical model Probabilistic model 2

Hierarchical statistical models A basic random effects / Bayesian hierarchical model BUGS DSL code Probabilistic model # priors on hyperparameters alpha ~ dexp(1.0) beta ~ dgamma(0.1,1.0) for (i in 1:N){ # latent process (random effects) # random effects distribution theta[i] ~ dgamma(alpha,beta) # linear predictor lambda[i] <- theta[i]*t[i] # likelihood (data model) x[i] ~ dpois(lambda[i]) } 3

Divorcing model specification from algorithm Your new method Variational Bayes Y(1) Y(2) Y(3) MCMC Flavor 1 MCMC Flavor 2 MCEM X(1) X(2) X(3) Particle Filter Quadrature Maximum likelihood Importance Sampler 4

What can a practitioner do with hierarchical models? Two basic software designs: 1. Typical R/Python package = Model family + 1 or more algorithms GLMMs: lme4, MCMCglmm GAMMs: mgcv spatial models: spbayes, INLA 5

What can a practitioner do with hierarchical models? Two basic software designs: 1. Typical R/Python package = Model family + 1 or more algorithms GLMMs: lme4, MCMCglmm GAMMs: mgcv spatial models: spbayes, INLA 2. Flexible model + black box algorithm BUGS: WinBUGS, OpenBUGS, JAGS PyMC INLA Stan 6

Existing software Examples: BUGS (WinBUGS, OpenBUGS, JAGS), INLA, Stan Model Algorithm Y(1) Y(2) Y(3) X(1) X(2) X(3) Widely used in various disciplines: environmental sciences, social sciences, biomedical/health sciences, statistics 7

Model NIMBLE: The Goal Algorithm language Y(1) Y(2) Y(3) X(1) X(2) X(3) + = 8

NIMBLE philosophy Combine flexible model specification with flexible algorithm programming, while Retaining BUGS DSL compatibility Providing a variety of standard algorithms Allowing developers to add new algorithms (including modular combination of algorithms) Allowing users to operate within R Providing speed via compilation to C++, with R wrappers 9

NIMBLE system components 1. Hierarchical model specification BUGS language è R/C++ model object 2. Algorithm library MCMC, Particle Filter/Sequential MC, MCEM, etc. 3. Algorithm programming via nimblefunctions NIMBLE programming language (DSL) within R è R/C++ algorithm object 10

NIMBLE: programming with models You give NIMBLE BUGS DSL code: You get a programmable model object: pumpcode <- nimblecode( { # priors on hyperparameters alpha ~ dexp(1.0) beta ~ dgamma(0.1,1.0) for (i in 1:N){ theta[i] ~ dgamma(alpha,beta) lambda[i] <- theta[i]*t[i] x[i] ~ dpois(lambda[i]) } } ) > pumpmodel$theta[1] <- 5 # set values in model > simulate(pumpmodel, theta') # simulate from prior > beta_deps <- pumpmodel$getdependencies( beta ) # model structure > calculate(pumpmodel, beta_deps) # calculate probability density > getlogprob(pumpmodel, theta') 11

User experience: specializing an algorithm to a model pumpcode <- nimblecode( { alpha ~ dexp(1.0) beta ~ dgamma(0.1,1.0) for (i in 1:N){ theta[i] ~ dgamma(alpha,beta) lambda[i] <- theta[i]*t[i] x[i] ~ dpois(lambda[i]) } } ) sampler_slice <- nimblefunction( setup = function((model, mvsaved, control) { calcnodes <- model$getdependencies(control$targetnode) discrete <- model$getnodeinfo()[[control$targetnode]]$isdiscrete() [ snip ] run = function() { u <- getlogprob(model, calcnodes) - rexp(1, 1) x0 <- model[[targetnode]] L <- x0 - runif(1, 0, 1) * width [ snip.] > pumpmcmcconf <- configuremcmc(pumpmodel) > pumpmcmcconf$printsamplers() [1] RW sampler: alpha [2] conjugate_dgamma_dgamma sampler: beta [3] conjugate_dgamma_dpois sampler: theta[1] [...snip...] > pumpmcmcconf$addsampler( alpha, slice, list(adaptinterval = 100)) > pumpmcmcconf$removesamplers( beta ) > pumpmcmcconf$addsampler( beta, slice, list(adaptinterval = 100)) > pumpmcmcconf$addmonitors( theta ) > pumpmcmc <- buildmcmc(pumpmcmcspec) > pumpmcmc_cpp <- compilenimble(pumpmcmc, project = pumpmodel) > pumpmcmc_cpp$run(20000) 12

NIMBLE system components 1. Hierarchical model specification BUGS language è R/C++ model object 2. Algorithm library MCMC, Particle Filter/Sequential MC, MCEM, etc. 3. Algorithm programming via nimblefunctions NIMBLE programming language (DSL) within R è R/C++ algorithm object 13

NIMBLE s algorithm library MCMC samplers: Conjugate, adaptive Metropolis, adaptive blocked Metropolis, slice, elliptical slice sampler, particle MCMC, specialized samplers for particular distributions (Dirichlet, CAR) Flexible choice of sampler for each parameter User-specified blocks of parameters Sequential Monte Carlo (particle filters) Various flavors MCEM Write your own 14

NIMBLE system components 1. Hierarchical model specification BUGS language è R/C++ model object 2. Algorithm library MCMC, Particle Filter/Sequential MC, MCEM, etc. 3. Algorithm programming via nimblefunctions NIMBLE programming language (DSL) within R è R/C++ algorithm object 15

Using nimblefunctions for algorithms Users can write nimblefunctions for use with statistical models to: Code their own algorithms Create user-defined MCMC samplers for use in NIMBLE s MCMC engine Write distributions and functions for use in BUGS code nimblefunctions that work with models have two components: setup function that is written in R and provides information to specialize an algorithm to a model run function that encodes generic execution of algorithm on arbitrary model 16

NIMBLE: programming with models sampler_mymetropolis_randomwalk <- nimblefunction( setup = function(model, mvsaved, targetnode, scale) { calcnodes <- model$getdependencies(targetnode) }, run = function() { model_lp_initial <- calculate(model, calcnodes) proposal <- rnorm(1, model[[targetnode]], scale) model[[targetnode]] <<- proposal model_lp_proposed <- calculate(model, calcnodes) log_mh_ratio<- model_lp_proposed - model_lp_initial if(decide(log_mh_ratio)) jump <- TRUE else jump <- FALSE #. Various bookkeeping operations # }) 2 kinds of functions 17

NIMBLE: programming with models sampler_myrw <- nimblefunction( setup = function(model, mvsaved, targetnode, scale) { calcnodes <- model$getdependencies(targetnode) }, run = function() { model_lp_initial <- calculate(model, calcnodes) proposal <- rnorm(1, model[[targetnode]], scale) model[[targetnode]] <<- proposal model_lp_proposed <- calculate(model, calcnodes) log_mh_ratio<- model_lp_proposed - model_lp_initial query model structure ONCE (R code) if(decide(log_mh_ratio)) jump <- TRUE else jump <- FALSE #. Various bookkeeping operations # }) 18

NIMBLE: programming with models sampler_myrw <- nimblefunction( setup = function(model, mvsaved, targetnode, scale) { calcnodes <- model$getdependencies(targetnode) }, run = function() { model_lp_initial <- calculate(model, calcnodes) proposal <- rnorm(1, model[[targetnode]], scale) model[[targetnode]] <<- proposal model_lp_proposed <- calculate(model, calcnodes) log_mh_ratio<- model_lp_proposed - model_lp_initial if(decide(log_mh_ratio)) jump <- TRUE else jump <- FALSE #. Various bookkeeping operations # }) the actual (generic) algorithm (NIMBLE DSL) 19

Using nimblefunctions to compile R R code for a Markov chain mc <- function(n, rho1, rho2) { path <- rep(0, n) # initialize path[1:2] <- rnorm(2) for(i in 3:n) # propagate forward in time path[i] <- rho1*path[i-1] + rho2*path[i-2] + rnorm(1) return(path) } NIMBLE code nim_mc <- nimblefunction( run = function(n = double(0), rho1 = double(0), rho2 = double(0)) { returntype(double(1)) path <- numeric(n, init = FALSE) path[1:2] <- rnorm(2) for(i in 3:n) path[i] <- rho1*path[i-1] + rho2*path[i-2] + rnorm(1) return(path) }) Compile to C++ (and then to executable) cnim_mc <- compilenimble(nim_mc) NIMBLE DSL 20

Using nimblefunctions to compile R cnim_mc<- compilenimble(nim_mc) #g++ -I/usr/share/R/include -DNDEBUG -DEIGEN_MPL2_ONLY=1 - I"/home/paciorek/R/x86_64/3.2/nimble/include" -fpic -g -O2 -fstack-protector -- param=ssp-buffer-size=4 -Wformat -Werror=format-security -D_FORTIFY_SOURCE=2 -g -c P_1_rcFun_4.cpp -o P_1_rcFun_4.o #g++ -shared -L/usr/lib/R/lib -Wl,-Bsymbolic-functions -Wl,-z,relro -o P_1_rcFun_09_02_02.so P_1_rcFun_4.o -L/home/paciorek/R/x86_64/3.2/nimble/CppCode - Wl,-rpath=/home/paciorek/R/x86_64/3.2/nimble/CppCode -lnimble -L/usr/lib/R/lib -lr n <- 1e6 rho1 <-.8; rho2 <-.1 set.seed(0) system.time( path1 <- mc(n, rho1, rho2) ) # original R version # user system elapsed # 3.883 0.001 3.883 set.seed(0) system.time( path2 <- cnim_mc(n, rho1, rho2) ) # compiled version # user system elapsed # 0.070 0.004 0.074 > identical(path1, path2) [1] TRUE 21

The NIMBLE compiler (NIMBLE DSL code) Feature summary: R-like matrix algebra (using Eigen library) R-like indexing (e.g. x[1:5,]) Use of model variables and nodes Model calculate (logprob) and simulate functions Sequential integer iteration If-then-else, do-while Access to much of Rmath.h (e.g. distributions) Automatic R interface / wrapper Call out to your own C/C++ or back to R Many improvements / extensions planned 22

How DSL code is compiled in NIMBLE DSL code within nimblefunction().cpp and.h files in R TMPDIR Parse tree of code Parse in R Abstract syntax tree Process in R Writing to files from R g++/llvm/etc. Shared library in R TMPDIR Generation of R wrapper functions that use.call Access via wrappers from R 23

Key steps in compiling R -> C++ nf <- nimblefunction( ) Generate custom class definition Evaluate setup code in R (possible for multiple cases) AST transformed and annotated: - Types inferred - Symbol table populated - Sizes tracked as expressions - Resizing and size-checking calls inserted - Intermediate variables inserted - Labeling for Eigen compatibility - Insertion of Eigen matrix / map setup Symbol table initiated from setup code results Run function and other member functions converted to Abstract Syntax Tree (AST). Partial evaluation of some functions (mostly for generic model uses). Creation of object to manage C++ function/class content. - Also creates AST for C function for.c() - Includes generic void* system to access any member data easily from R. Write.cpp and.h files and compile them Generate class definition to access function or object(s) of compiled code - creates natural R calls - allows natural access to C++ member data 24

Compilation steps 25

Basic example: calls from R > nim_mc function (n, rho1, rho2) { path <- nimnumeric(n, init = FALSE) path[1] <- rnorm(1) path[2] <- rnorm(1) for (i in 3:n) path[i] <- rho1 * path[i - 1] + rho2 * path[i - 2] + rnorm(1) return(path) } > cnim_mc function (n, rho1, rho2) { if (is.null(cnativesymbolinfo_)) { warning("trying to call compiled nimblefunction that does not exist (may have been cleared).") return(null) } ans <-.Call(CnativeSymbolInfo_, n, rho1, rho2) ans <- ans[[4]] ans } 26

Basic example: generated C++ code NimArr<1, double> rcfun_2 ( double ARG1_n_, double ARG2_rho1_, double ARG3_rho2_ ) { NimArr<1, double> path; double i; path.initialize(0, false, true, true, ARG1_n_); path[0] = rnorm(0, 1); path[1] = rnorm(0, 1); for(i=3; i<= static_cast<int>(arg1_n_); ++i) { path[(i) - 1] = (ARG2_rho1_ * path[(i - 1) - 1] + ARG3_rho2_ * path[(i - 2) - 1]) + rnorm(0, 1); } return(path); } SEXP CALL_rcFun_4 ( SEXP S_ARG1_n_, SEXP S_ARG2_rho1_, SEXP S_ARG3_rho2_ ) { //... } 27

Basic example using Eigen for vectorization Uncompiled nimblefunction (DSL) code example_vec <- nimblefunction( run = function(x = double(1)) { returntype(double(1)) out <- acos(tanh(x)) return(out) }) Compiled C++ code NimArr<1, double> rcfun_5 ( NimArr<1, double> & ARG1_x_ ) { NimArr<1, double> out; Map<MatrixXd> Eig_out(0,0,0); EigenMapStr Eig_ARG1_x_Interm_1(0,0,0, EigStrDyn(0, 0)); out.setsize(arg1_x_.dim()[0]); new (&Eig_out) Map< MatrixXd >(out.getptr(),arg1_x_.dim()[0],1); new (&Eig_ARG1_x_Interm_1) EigenMapStr(ARG1_x_.getPtr() + static_cast<int>(arg1_x_.getoffset() + static_cast<int>(0)),arg1_x_.dim()[0],1,eigstrdyn(0, ARG1_x_.strides()[0])); Eig_out = (((Eig_ARG1_x_Interm_1).array()).unaryExpr(std::ptr_fun<double, double>(tanh))).acos(); return(out); } 28

Compiler extensibility Compiler is written in R with extensibility in mind. Adding new functions requires/allows: Possible syntax modification A function to annotate AST with appropriate sizes and types (can be an existing function or a new one) Determination of C++ output format Other details Adding new types is more involved. Goal is to automate /isolate some extensibility steps. 29

Goals for extending NIMBLE Advanced math Automatic differentiation (generate code to use existing C++ CppAD library): well underway More linear algebra (sparsity and more) Advanced computing Parallelization via compilation to Tensorflow (in place of Eigen): initial steps done More modular compilation units More native use of R objects in C++ (less copying) Scalability Faster R processing of model and algorithm code Vectorization of algorithms for replicated model nodes More algorithms 30

Interested? Version 0.6-6 on R package repository (CRAN) Lots of information (manual, examples, etc.) on r- nimble.org Development: github.com/nimble-dev/nimble Announcements: nimble-announce Google site User support/discussion: nimble-users Google site 31