The goal of pysmc is to implement Sequential Monte Carlo (SMC) techniques on top of the Monte Carlo (MC) package PyMC. The manual assumes that the user is already familiar with the way PyMC works. You are advised to read their tutorial before going on. A nice place to start with pysmc is our Tutorial.
Here is complete reference of all the classes included in pysmc.
pysmc.MCMCWrapper provides exactly the same functionality as pymc.MCMC. The only new thing is that it has the ability to get and set the current state of MCMC from a dictionary describing the state. Basically, you should not construct this class your self, pymc.SMC will do it automatically.
Note
It does not inherit from pymc.MCMC. It simply stores a reference to a pymc.MCMC object internally.
Here is a complete reference of the public members:
This is a wrapper class for pymc.MCMC.
Parameters: | mcmc_sampler (pymc.MCMC) – The underlying MCMC sampler. If None, then it must be specified before using an object of this class. |
---|
The database of the MCMC sampler.
The deterministic variables of the model.
Draw from the prior of the model.
Raises: | exceptions.AttributeError if the action is not possible. |
---|
Get a dictionary describing the state of the sampler.
Keep in mind that we do not copy the internal parameters of the sampler (i.e., the step methods, their parameters, etc.). We only copy the values of all the Stochastics and all the Deterministics. On contrast pymc.MCMC.get_state() copies the internal parameters and the Stochastics. It does not copy the Deterministics. The deterministics are needed because in most of our examples they are going to be very expensive to revaluate.
Returns: | A dictionary state containing the current state of
MCMC. The keys of the dictionary are as follows:
|
---|---|
Return type: | dict |
The log of the probability of the current state of the MCMC sampler.
The underlying pymc.MCMC object.
Getter: | Get the underlying MCMC object. |
---|---|
Setter: | Set the underlying MCMC object. |
Raises: | exceptions.TypeError |
Type: | pymc.MCMC |
The nodes of the model.
Sample iter times from the model.
This is basically exactly the same call as pymc.MCMC.sample() but with defaults that are more suitable for pysmc.SMC. For example, you typically do not want to allow pymc.MCMC.sample() to tune the parameters of the step methods. It is SMC that should do this. This is because the transition kernels need to retain their invariance properties. They can’t have parameters that change on the fly.
Parameters: |
|
---|
Set the state of the sampler.
Parameters: | state (dict) – A dictionary describing the state of the sampler. Look at pysmc.MCMCWrapper.get_state() for the appropriate format. |
---|
The step methods of the MCMC sampler.
The stochastic variables of the model.
The pysmc.DistributedObject class implements some basic functionality that is used by every distributed object in pysmc.
Here follows its complete reference:
This is a class that represents an object that is (potentially) distributed in parallel.
The MPI communicator.
Getter: | Get the MPI communicator. |
---|---|
Type: | mpi4py.COMM |
The MPI class.
Getter: | Get the MPI class. |
---|---|
Type: | mpi4py.MPI |
The rank of this process.
Getter: | Get the rank of this process. |
---|---|
Type: | int |
The size of the MPI pool.
Getter: | Get the size of MPI pool. |
---|---|
Type: | int |
Check if MPI is being used.
Returns: | True if MPI is used and False otherwise. |
---|---|
Return type: | bool |
pysmc.ParticleApproximation is a class that implements a a particle approximation.
Here is the complete reference of the public members:
Initialize a particle approximation.
If denotes collectively all the variables involved in the particle approximation, then this object represents as discussed in the Tutorial.
Base class: pysmc.DistributedObject
Parameters: |
|
---|
Note
When creating a distributed object, the particles must already be scattered.
Get a particle approximation on every process.
If we are not using MPI, it will simply return a copy of the object.
Returns: | A fully functional particle approximation on a single process. |
---|---|
Return type: | pysmc.ParticleApproximation |
Compute all the means associated with the particle approximation.
Parameters: | force_calculation (bool) – Computes the statistics even if a previous calculation was already made. |
---|
Compute the means of every variable of a type type_of_var.
Parameters: |
---|
Compute all the statistics of the particle approximation.
Parameters: | force_calculation (bool) – Computes the statistics even if a previous calculation was already made. |
---|
Compute all the variances.
Parameters: | force_calculation (bool) – Computes the statistics even if a previous calculation was already made. |
---|
Compute all variances of type type_of_var.
Parameters: |
---|
Compute the mean of the particle approximation.
Parameters: |
---|
Compute the variance of var_name.
Parameters: |
---|
Copy the particle approximation.
Returns: | A copy of the current particle approximation. |
---|---|
Return type: | pysmc.ParticleApproximation |
Get the mean of the func applied on var_name which is of type type_of_var.
Let the variable and the function we are referring to be and , respectively. Then the method computes and returns:
Parameters: | |
---|---|
Returns: | The mean of the random variable . |
Return type: | unknown |
Returns the particle approximation of a function of var_name variable of type type_of_var of the particle approximation.
Let the variable and the function we are referring to be and , respectively. Then, let denote the induced random variable when we pass through the function. The method returns the following particle approximation to the probability density of :
Parameters: | |
---|---|
Returns: | A particle approximation representing the random variable func(var_name). |
Return type: |
The logarithms of the weights of the particle approximation.
Getter: | Get the logarithms of the weights of the particle approximation. |
---|---|
Type: | 1D numpy.ndarray |
The mean of the variables of all types of the particle approximation.
The mean of a variable is computed as:
(1)
Getter: | Get the mean of the particles. |
---|---|
Type: | dict |
The number of particles owned by this process.
Getter: | Get the number of particles owned by this process. |
---|---|
Type: | int |
The number of particles.
Getter: | Get the number of particles. |
---|---|
Type: | int |
The particles of the particle approximation.
Getter: | Get the particles of the particle approximation. |
---|---|
Type: | A list of whatever types the approximation has. |
Resample the particles. After calling this, all particles will have the same weight.
The variance of all the variables of all types of the particle approximation.
The variance of a variable is computed as:
(2)
where is given in (1).
Getter: | Get the variance of all particles. |
---|---|
Type: | dict |
The weights of the particle approximation.
Getter: | Get the weights of the particle approximation. |
---|---|
Type: | 1D numpy.ndarray |
pysmc.SMC is the class that makes everything happen.
Here is a complete reference of the public members:
Use Sequential Monte Carlo (SMC) to sample from a distribution.
Base class: pysmc.DistributedObject
Parameters: |
|
---|
If the adapt proposal step is set to True, each of the step methods of the underlying pymc.MCMC class are adaptively tuned by monitoring the acceptance rate.
Getter: | Get the adapt flag. |
---|---|
Setter: | Set the adapt flag. |
Type: | bool |
Commit the current state to the data base.
The database containing info about all the particles we visited.
Getter: | Get the database. |
---|---|
Type: | dict |
Getter: | The current Effective Sample Size. |
---|---|
Type: | float |
It is a number between 0 and 1 representing the desired percent reduction of the effective sample size when we perform a SMC step. The next gamma will be selected adaptively so that the prescribed reduction is achieved.
Getter: | Get the reduction of the Effective Sample Size per SMC step. |
---|---|
Setter: | Set the reduction of the Effective Sample Size per SMC step. |
Type: | float |
Raises: | exceptions.ValueError |
The threshold of the Effective Sample Size is a number between 0 and 1 representing the percentage of the total number of particles. If the Effective Sample Size falls bellow ess_threshold * num_particles, then the particles are automatically resampled.
Getter: | Get the threshold of the Effective Sample Size. |
---|---|
Setter: | Set the threshold of the Effective Sample Size. |
Type: | float |
Raises: | exceptions.ValueError |
The value of the gamma_name parameter.
Getter: | Get the value of the gamma_name parameter. |
---|---|
Setter: | Set the value of the gamma_name parameter. |
Type: | float |
The Flag that determines if gamma is an exponent in the probability density.
Getter: | Get the flag. |
---|---|
Setter: | Set the flag. |
Type: | bool |
The true name of the gamma parameter in the pymc model.
Getter: | Get the name of the gamma parameter. |
---|---|
Setter: | Set the name of the gamma parameter. |
Type: | str |
The random variable of the pymc model that has a parameter named gamma_name.
Getter: | Get the value of the gamma_name parameter. |
---|---|
Type: | pymc.Stochastic |
Get a pysmc.ParticleApproximation representing the current state of SMC.
Returns: | A particle approximation of the current state. |
---|---|
Return type: | pysmc.ParticleApproximation |
Initialize SMC at a particular gamma.
The method has basically three ways of initializing the particles:
Parameters: |
|
---|
Getter: | The logarithm of the weights of the particles. |
---|---|
Type: | 1D numpy.ndarray |
The underlying pymc.MCMC object.
Getter: | Get the underlying MCMC object. |
---|---|
Setter: | Set the underlying MCMC object. |
Raises: | exceptions.TypeError |
Type: | pymc.MCMC |
Move the current particle approximation to gamma.
Parameters: | gamma (float) – The new gamma you wish to reach. |
---|
Note
There must already be a valid particle approximation. See pysmc.SMC.initialize() for ways of doing this.
Getter: | The number of particles associated with each process. |
---|---|
Type: | int |
Note
If not using MPI, then it is the same as pysmc.SMC.num_particles(). Otherwise is it is equal to num_particles / size, where size is the total number of MPI processes.
Getter: | Get the number of MCMC steps per SMC step. |
---|---|
Setter: | Set the number of MCMC steps per SMC step. |
Type: | int |
Raises: | exceptions.ValueError |
Getter: | Get the number of particles. |
---|---|
Type: | int |
The SMC particles.
Getter: | Get the SMC particles. |
---|---|
Type: | list of whatever objects your method supports. |
The total number of MCMC steps performed so far. This is zeroed, everytime you call pysmc.SMC.initialize().
Getter: | The total number of MCMC steps performed so far. |
---|---|
Type: | int |
Update the database or not.
Getter: | Get the update_db flag. |
---|---|
Setter: | Set the update_db flag. |
Type: | bool |
Specify the amount of output printed by the class. There are three levels:
- 0: Print nothing.
- 1: Print info from methods you call.
- 2: Print info from methods the methods you call call...
- 3: Guess what...
Getter: | Get the verbosity flag. |
---|---|
Setter: | Set the verbosity flag. |
Type: | int |
The class pysmc.DataBase implements a simple database for dumping SMC steps.
A data base storing the evolution of SMC particles as gamma changes.
Parameters: |
---|
Add the particle_approximation corresponding to gamma to the database.
Parameters: |
---|
Commit everything we have so far to the database.
The filename you have selected for dumping the data.
Getter: | Get the filename you have selected for dumping the data. |
---|---|
Type: | str |
The current gamma of the database.
Getter: | Get the current gamma of the database. |
---|---|
Type: | unknown |
The name we used for gamma.
Getter: | Get the name we used for gamma. |
---|---|
Type: | str |
The list of gammas we have visited.
Getter: | Get the list of gammas we have visited. |
---|---|
Type: | list |
This is a static method. It loads a database from filename.
The number of gammas added to the database.
Getter: | Get the number of gammas added to the data base. |
---|---|
Type: | int |
The current particle approximation of the database.
Getter: | Get the current particle approximation of the database. |
---|---|
Type: | unknown |
The particle approximations associated with each gamma.
Getter: | Get the particle approximations associated with each gamma. |
---|---|
Type: | list |
True if the class writes data to disk, False otherwise.
In PySMC we define a few step methods for the Metropolis-Hastings algorithm that extend the capabilities of PyMC.
Here is a list of what we offer:
This is a step method class that is good for positive random variables. It is a essentially a random walk in the logarithmic scale.
Base class: pymc.Metropolis
Tell PyMC that this step method is good for Lognormal, Exponential and Gamma random variables. In general, it should be good for positive random variables.
Compute the hastings factor.
Propose a move.
Plot the histogram of variable of a particle approximation.
Parameters: |
|
---|
This contains methods that do not fit into the other sections of the reference manual.
Try to turn the data into a numpy array.
Returns: | If possible, a numpy.ndarray containing the data. Otherwise, it just returns the data. |
---|---|
Return type: | numpy.ndarray or type(data) |
Plot the histogram of variable of a particle approximation.
Parameters: |
|
---|
Make a movie from a database.
Sample the multinomial according to p.
Parameters: | p (1D numpy.ndarray) – A numpy array of positive numbers that sum to one. |
---|---|
Returns: | A set of indices sampled according to p. |
Return type: | 1D numpy.ndarray of int |
Construct a kernel density approximation of the probability density of var_name of particle_approximation.
Parameters: | particle_approximation (pysmc.ParticleApproximation) – A particle approximation. |
---|---|
Returns: | A kernel density approximation. |
Return type: | scipy.stats.gaussian_kde |