The MCMCModel class#

Aliases#

halerium.core.model.MCMCModel
class MCMCModel(graph, data=None, compiler=None, step_size=0.01, adapt_step_size=True, target_acceptance_rate=0.234, initial_values=None, initial_source_values=None, random_initial_value_scale=1, copy_graph=True, model_graph_options=None)#

Model providing markov chain monte carlo samples.

Model providing markov chain monte carlo samples.

The model implements the Metropolis Hastings sampling method, see https://en.wikipedia.org/wiki/Metropolis%E2%80%93Hastings_algorithm

Parameters:
  • graph (halerium.core.Graph) – The graph of the model.

  • data (halerium.core.DataLinker, dict, optional) – The data linker or dict containing data constraining the model. The default is None.

  • compiler (optional) – The compiler instance or class for compiling the model. The default is None, in which case a Tensorflow compiler is used.

  • step_size (float, optional) – The step size of the mcmc proposals The default is 0.01

  • adapt_step_size (bool, optional) – Whether to adapt the step size to reach the target acceptance rate The default is True

  • target_acceptance_rate (float, optional) – The target acceptance rate. A value between o and 1. Only has an effect if adapt_step_size is True. The default is 0.234

  • initial_values (dict, optional) – A dictionary containing (static) variables in the graph as keys and their initial values as values. The model then tries to convert these into appropriate initial values for the solver.

  • initial_source_values (dict, optional) – A dictionary containing (static) variables in the graph as keys and initial value for their source as values. In addition, strings are accepted as keys, but are ignored unless the model can interpret these. This attribute may be used to pass initial values for trainable model parameters obtained from training of earlier models with the same (or a sufficiently similar) graph. Other usage is not recommended (unless users know exactly how the model handles its sources and their initial values).

  • random_initial_value_scale (float, None, optional) – The scale of any randomly drawn initial values (relative to the scale of the respective variables as given by the graph). The default is 1.

  • copy_graph (bool, optional) – Whether the model should make a copy of the graph for its own use, or just keep the graph itself as attribute. Users should leave this set to the default True, unless they are certain that the graph won’t be altered by the user or other code. Such changes to a graph a model holds directly (i.e. not a copy) makes that model inconsistent and likely causes errors.

  • model_graph_options (dict, optional) – The options for creating the model graph. The default is None.

apply_to_samples(fetches, function, n_samples)#

Draw samples and apply a function to them.

Parameters:
  • fetches – The variables to generate sample data for.

  • function (callable) – The function to apply to the sample data.

  • n_samples (int) – The number of samples to draw from the model.

Returns:

The result of applying the function to the sampled data.

Return type:

result

assert_is_trained()#

Check if model is trained.

Return type:

None.

Raises:

RuntimeWarning – If model is not trained.

get_example(fetches)#

Draw an example from the model.

Parameters:

fetches – The variables to generate example values for.

Returns:

The example data.

Return type:

example

get_means(fetches, n_samples=100)#

Estimate mean values.

Parameters:
  • fetches – The variables to estimate mean values for.

  • n_samples (int) – The number of samples to estimate the means from.

Returns:

The estimated means of the variables.

Return type:

means

get_posterior_graph(name=None, n_samples=100)#

Create posterior graph from trained model.

Parameters:
  • name (str) – The name to give to the posterior graph.

  • n_samples (int) – The number of samples to estimate the posterior distributions from.

Returns:

post_graph – The posterior graph.

Return type:

halerium.core.Graph

get_samples(fetches, n_samples=1)#

Draw samples from the model.

Parameters:
  • fetches – The variables to generate sample data for.

  • n_samples (int) – The number of examples to draw from the model.

Returns:

The sampled data.

Return type:

samples

get_source_values(return_all=False)#

Get source values.

Prior to any training, these are just any initial values provided by the caller to the model upon construction. After training, these are the final values of the sources at the end of the training.

Parameters:

return_all (bool) – Whether to return all, or just the source values for static variables (default).

Returns:

The source values.

Return type:

source_values

get_standard_deviations(fetches, n_samples=100)#

Estimate standard deviations.

Parameters:
  • fetches – The variables to estimate standard deviations for.

  • n_samples (int) – The number of samples to estimate the standard deviations from.

Returns:

The estimated standard deviations of the variables.

Return type:

standard_deviations

get_variances(fetches, n_samples=100)#

Estimate variances.

Parameters:
  • fetches – The variables to estimate variances for.

  • n_samples (int) – The number of samples to estimate the variances from.

Returns:

The estimated variances of the variables.

Return type:

variances

property is_trained#

Whether the model has been trained.

property model_graph#

The model graph (don’t modify it yourself).

solve(n_iter=100000, n_burnin=10000, sample_interval=100, adapt_step_size=None, adapt_step_size_interval=1000)#

Solve the model.

Raises:

NotImplementedError – If not implemented in derived class.

Return type:

None