• This repository has been archived on 12/May/2019
  • Stars
    star
    168
  • Rank 225,507 (Top 5 %)
  • Language
    Julia
  • License
    Other
  • Created over 11 years ago
  • Updated over 6 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

MCMC inference in Julia

Klara.jl

Build Status Klara Docs Stories in In Progress

Throughput Graph

The Julia Klara package provides a generic engine for Markov Chain Monte Carlo (MCMC) inference.

The documentation is out of date, but will be brought up-to-date upon completing milestone 0.12.0. In the meantime, this README file provides some elementary examples of the current interface. More examples can be found in doc/examples.

Example: sampling from an unnormalized normal target

using Klara

### Define the log-target as a function (generic or anonymous):

plogtarget(z::Vector{Float64}) = -dot(z, z)

### Define the parameter via BasicContMuvParameter (it is a continuous multivariate variable)
### The input arguments for BasicContMuvParameter are:
### 1) the variable key,
### 2) the log-target

p = BasicContMuvParameter(:p, logtarget=plogtarget)

### Define the model using the likelihood_model generator
### The second argument informs the likelihood_model generator that p.index has not been set

model = likelihood_model(p, false)

### Define a Metropolis-Hastings sampler with an identity covariance matrix

sampler = MH(ones(2))

### Set MCMC sampling range

mcrange = BasicMCRange(nsteps=10000, burnin=1000)

### Set initial values for simulation

v0 = Dict(:p=>[5.1, -0.9])

### Specify job to be run

job = BasicMCJob(model, sampler, mcrange, v0)

### Run the simulation

run(job)

### Get simulated values

chain = output(job)

chain.value

### Check that the simulated values are close to the zero-mean target

mean(chain)

To reset the job, using a new initial value for the targeted parameter, run

reset(job, [3.2, 9.4])

run(job)

chain = output(job)

To see how the acceptance rate changes during burnin, set the vanilla tuner in verbose mode

job = BasicMCJob(model, sampler, mcrange, v0, tuner=VanillaMCTuner(verbose=true))

run(job)

chain = output(job)

If apart from the simulated chain you also want to store the log-target, then pass an additional dictionary to the job to specify the output options. In particular, the :monitor key indicates which items will be monitored. In the example below, both :value and :logtarget will be monitored, referring to the chain and log-target respectively. These can then be accessed by the corresponding fields chain.value and chain.logtarget:

outopts = Dict{Symbol, Any}(:monitor=>[:value, :logtarget])

job = BasicMCJob(model, sampler, mcrange, v0, tuner=VanillaMCTuner(verbose=true), outopts=outopts)

run(job)

chain = output(job)

chain.logtarget

The acceptance ratio diagnostics can be stgored via the :diagnostics=>[:accept] entry of outopts:

outopts = Dict{Symbol, Any}(:monitor=>[:value, :logtarget], :diagnostics=>[:accept])

job = BasicMCJob(model, sampler, mcrange, v0, tuner=VanillaMCTuner(verbose=true), outopts=outopts)

run(job)

chain = output(job)

Instead of saving the output in memory, it can be written in file via the output option :destination=>:iostream:

outopts = Dict{Symbol, Any}(
  :monitor=>[:value, :logtarget],
  :diagnostics=>[:accept],
  :destination=>:iostream
)

job = BasicMCJob(model, sampler, mcrange, v0, tuner=VanillaMCTuner(verbose=true), outopts=outopts)

run(job)

The chain, log-target and acceptance ratio diagnostics of the above example are stored in the respective CSV files "value.csv", "logtarget.csv" and "diagnosticvalues.csv" of the current directory. To save the output in another directory, use the :filepath=>"myfullpath.csv", where "myfullpath.csv" is substituted by the full path of your choice:

outopts = Dict{Symbol, Any}(
  :monitor=>[:value, :logtarget],
  :diagnostics=>[:accept],
  :destination=>:iostream,
  :filepath=>"/Users/theodore/workspace/julia"
)

job = BasicMCJob(model, sampler, mcrange, v0, tuner=VanillaMCTuner(verbose=true), outopts=outopts)

run(job)

To run a sampler which requires the gradient of the log-target, such as MALA, try

using Klara

plogtarget(z::Vector{Float64}) = -dot(z, z)

pgradlogtarget(z::Vector{Float64}) = -2*z

p = BasicContMuvParameter(:p, logtarget=plogtarget, gradlogtarget=pgradlogtarget)

model = likelihood_model(p, false)

### Set driftstep to 0.9

sampler = MALA(0.9)

mcrange = BasicMCRange(nsteps=10000, burnin=1000)

v0 = Dict(:p=>[5.1, -0.9])

### Save grad-log-target along with the chain (value and log-target)

outopts = Dict{Symbol, Any}(:monitor=>[:value, :logtarget, :gradlogtarget], :diagnostics=>[:accept])

job = BasicMCJob(model, sampler, mcrange, v0, tuner=VanillaMCTuner(verbose=true), outopts=outopts)

run(job)

chain = output(job)

chain.gradlogtarget

mean(chain)

To adapt the MALA drift step empirically during burnin towards an intended acceptance rate of 60%, run

job = BasicMCJob(
  model,
  sampler,
  mcrange,
  v0,
  tuner=AcceptanceRateMCTuner(0.6, verbose=true),
  outopts=outopts
)

run(job)

chain = output(job)

The examples below demonstrates how to run MCMC using automatic differentiation (AD).

To use forward mode AD, try the following:

using Klara

plogtarget(z::Vector) = -dot(z, z)

p = BasicContMuvParameter(:p, logtarget=plogtarget, diffopts=DiffOptions(mode=:forward))

model = likelihood_model(p, false)

sampler = MALA(0.9)

mcrange = BasicMCRange(nsteps=10000, burnin=1000)

v0 = Dict(:p=>[5.1, -0.9])

outopts = Dict{Symbol, Any}(:monitor=>[:value, :logtarget, :gradlogtarget], :diagnostics=>[:accept])

job = BasicMCJob(model, sampler, mcrange, v0, tuner=VanillaMCTuner(verbose=true), outopts=outopts)

run(job)

chain = output(job)

Note that plogtarget takes an argument of type Vector instead of Vector{Float64}, as required by the ForwardDiff package. Furthermore, notice that in the definition of parameter p, the gradient of its log-target is not provided explicitly; instead, the optional argument diffopts=DiffOptions(mode=:forward) enables computing the gradient via forward mode AD.

To employ reverse mode AD, try

using Klara

plogtarget(z) = -dot(z, z)

p = BasicContMuvParameter(:p, logtarget=plogtarget, diffopts=DiffOptions(mode=:reverse))

model = likelihood_model(p, false)

sampler = MALA(0.9)

mcrange = BasicMCRange(nsteps=10000, burnin=1000)

v0 = Dict(:p=>[5.1, -0.9])

outopts = Dict{Symbol, Any}(:monitor=>[:value, :logtarget, :gradlogtarget], :diagnostics=>[:accept])

job = BasicMCJob(model, sampler, mcrange, v0, tuner=VanillaMCTuner(verbose=true), outopts=outopts)

run(job)

chain = output(job)

plogtarget takes an argument without specifying its type, as required by the ReverseDiff package. In the definition of parameter p, the gradient of its log-target is not provided explicitly; the optional argument diffopts=DiffOptions(mode=:reverse) enables computing the gradient via reverse mode AD.

Documentation

The user guide is currently being written up.

More Repositories

1

Distributions.jl

A Julia package for probability distributions and associated functions.
Julia
1,073
star
2

GLM.jl

Generalized linear models in Julia
Julia
574
star
3

StatsBase.jl

Basic statistics for Julia
Julia
565
star
4

Distances.jl

A Julia package for evaluating distances (metrics) between vectors.
Julia
410
star
5

MixedModels.jl

A Julia package for fitting (statistical) mixed-effects models
Julia
402
star
6

MultivariateStats.jl

A Julia package for multivariate statistics and data analysis (e.g. dimension reduction)
Julia
370
star
7

Clustering.jl

A Julia package for data clustering
Julia
343
star
8

TimeSeries.jl

Time series toolkit for Julia
Julia
342
star
9

HypothesisTests.jl

Hypothesis tests for Julia
Julia
287
star
10

StatsModels.jl

Specifying, fitting, and evaluating statistical models in Julia
Julia
245
star
11

StatsFuns.jl

Mathematical functions related to statistics.
Julia
224
star
12

MLBase.jl

A set of functions to support the development of machine learning algorithms
Julia
186
star
13

KernelDensity.jl

Kernel density estimators for Julia
Julia
169
star
14

RDatasets.jl

Julia package for loading many of the data sets available in R
R
160
star
15

Lasso.jl

Lasso/Elastic Net linear and generalized linear models
Julia
141
star
16

StatsKit.jl

Convenience meta-package to load essential packages for statistics
Julia
136
star
17

PDMats.jl

Uniform Interface for positive definite matrices of various structures
Julia
103
star
18

Loess.jl

Local regression, so smooooth!
Julia
99
star
19

GLMNet.jl

Julia wrapper for fitting Lasso/ElasticNet GLM models using glmnet
Julia
96
star
20

NMF.jl

A Julia package for non-negative matrix factorization
Julia
89
star
21

Survival.jl

Survival analysis in Julia
Julia
73
star
22

LogExpFunctions.jl

Julia package for various special functions based on `log` and `exp`.
Julia
73
star
23

Statistics.jl

The Statistics stdlib that ships with Julia.
Julia
69
star
24

TimeModels.jl

Modeling time series in Julia
Julia
57
star
25

DataArrays.jl

DEPRECATED: Data structures that allow missing values
Julia
53
star
26

PGM.jl

A Julia framework for probabilistic graphical models.
Julia
52
star
27

ConjugatePriors.jl

A Julia package to support conjugate prior distributions.
Julia
46
star
28

SVM.jl

SVM's for Julia
Julia
41
star
29

Roadmap.jl

A centralized location for planning the direction of JuliaStats
35
star
30

NullableArrays.jl

DEPRECATED Prototype of the new JuliaStats NullableArrays package
Julia
35
star
31

Distance.jl

Julia module for Distance evaluation
Julia
27
star
32

DimensionalityReduction.jl

Deprecated in favor of MultivariateStats.jl
Julia
27
star
33

Rmath-julia

The Rmath library from R
C
25
star
34

RegERMs.jl

DEPRECATED: Regularised Empirical Risk Minimisation Framework (SVMs, LogReg, Linear Regression) in Julia
Julia
17
star
35

StatsAPI.jl

A statistics-focused namespace for packages to share functions
Julia
17
star
36

Rmath.jl

Archive of functions that emulate R's d-p-q-r functions for probability distributions
Julia
16
star
37

JuliaStats.github.io

The home page of JuliaStats
CSS
11
star
38

NullableStats.jl

DEPRECATED: Statistical functionality for NullableArrays
Julia
4
star
39

RmathBuilder

Builder repository for rmath-julia
Julia
3
star
40

RmathDist.jl

Julia interface to Rmath distribution functions
Julia
3
star