Hint
Example code: https://github.com/cselab/korali/tree/master/examples/sampling/
Sampling
In this tutorial we show how to directly sample from a function using Metropolis Hastings (MCMC).
Problem Description
We are given the function \(g(\vartheta)=\exp(-\vartheta^2)\) for \(\vartheta\in[-10,10]\).
We assume that \(f\) represents the unnormalized density of a distribution. We want to draw samples from this distribution.
For the rest of the tutorial we will work with the function \(f(\vartheta)=\log g(\vartheta) = -\vartheta^2\) for numerical reasons. In general we advise users of Korali to work in log space.
The Objective Function
Create a folder named model. Inside, create a file with name directModel.py and paste the following code,
This is the computational model that represents our objective function.
Sampling with MCMC
First, open a file and import the korali module
#!/usr/bin/env python3
import korali
Import the computational model,
import sys
sys.path.append('./model')
from directModel import *
The Korali Experiment Object
Next we construct a korali.Experiment object and set the computational model,
e = korali.Experiment()
e["Problem"]["Objective Function"] = model
The Problem Type
Then, we set the type of the problem to Direct Evaluation
e["Problem"]["Type"] = "Evaluation/Direct/Basic"
The Solver
We choose the solver MCMC and set the initial mean and standard deviation of the parameter X.
e["Solver"]["Type"] = "MCMC"
e["Variables"][0]["Initial Mean"] = 0.0
e["Variables"][0]["Initial Standard Deviation"] = 1.0
e["Solver"]["Burn In"] = 500
e["Solver"]["Termination Criteria"]["Max Samples"] = 5000
We also set some settings for MCMC. For a detailed description of the MCMC settings, see MCMC
Configuring the output
To reduce the output frequency we write
e["File Output"]["Frequency"] = 500
e["Console Output"]["Frequency"] = 500
e["Console Output"]["Verbosity"] = "Detailed"
Running
Finally, we are ready to run the simulation,
k = korali.Engine()
k.run(e)
The results are saved in the folder _korali_result/.
Plotting
You can see a histogram of the results by running the command python3 -m korali.plot