keanu.algorithm

Inference

keanu.algorithm.sample(net, sample_from, sampling_algorithm=None, draws=500, drop=0, down_sample_interval=1, plot=False, ax=None)[source]
Parameters:
  • net (BayesNet) – Bayesian Network containing latent variables.
  • sample_from (Iterable[Vertex]) – Vertices to include in the returned samples.
  • sampling_algorithm (Optional[PosteriorSamplingAlgorithm]) – The posterior sampling algorithm to use. Options are keanu.algorithm.MetropolisHastingsSampler, keanu.algorithm.NUTSSampler and keanu.algorithm.ForwardSampler If not set, keanu.algorithm.MetropolisHastingsSampler is chosen with ‘prior’ as its proposal distribution.
  • draws (int) – The number of samples to take.
  • drop (int) – The number of samples to drop before collecting anything. If this is zero then no samples will be dropped before collecting.
  • down_sample_interval (int) – Collect 1 sample for every down_sample_interval. If this is 1 then there will be no down-sampling. If this is 2 then every other sample will be taken. If this is 3 then 2 samples will be dropped before one is taken. And so on.
  • plot (bool) – Flag for plotting the trace after sampling. Call matplotlib.pyplot.show to display the plot.
  • ax (Axes) – matplotlib.axes.Axes. If not set, a new one is created.
Raises:

ValueError – If sample_from contains vertices without labels.

Return type:

Dict[Union[str, Tuple[str, str]], List[Union[int, integer, float, floating, bool_]]]

Returns:

Dictionary of samples at an index (tuple) for each vertex label (str). If all the vertices in sample_from are scalar, the dictionary is only keyed by label.

keanu.algorithm.generate_samples(net, sample_from, sampling_algorithm=None, drop=0, down_sample_interval=1, live_plot=False, refresh_every=100, ax=None)[source]
Parameters:
  • net (BayesNet) – Bayesian Network containing latent variables.
  • sample_from (Iterable[Vertex]) – Vertices to include in the returned samples.
  • sampling_algorithm (Optional[PosteriorSamplingAlgorithm]) – The posterior sampling algorithm to use. Options are keanu.algorithm.MetropolisHastingsSampler and keanu.algorithm.NUTSSampler. If not set, keanu.algorithm.MetropolisHastingsSampler is chosen with ‘prior’ as its proposal distribution.
  • drop (int) – The number of samples to drop before collecting anything. If this is zero then no samples will be dropped before collecting.
  • down_sample_interval (int) – Collect 1 sample for every down_sample_interval. If this is 1 then there will be no down-sampling. If this is 2 then every other sample will be taken. If this is 3 then 2 samples will be dropped before one is taken.
  • live_plot (bool) –

    Flag for plotting the trace while sampling. Call matplotlib.pyplot.show to display the plot.

  • refresh_every (int) – Period of plot updates (in sample number).
  • ax (Axes) –

    matplotlib.axes.Axes. If not set, a new one is created.

Raises:

ValueError – If sample_from contains vertices without labels.

Return type:

Generator[Dict[Union[str, Tuple[str, str]], Union[int, integer, float, floating, bool_]], None, None]

Returns:

Dictionary of samples at an index (tuple) for each vertex label (str). If all the vertices in sample_from are scalar, the dictionary is only keyed by label.

Samplers

class keanu.algorithm.MetropolisHastingsSampler(proposal_distribution, latents, proposal_listeners=[], proposal_distribution_sigma=None)[source]
Parameters:
  • proposal_distribution (str) – The proposal distribution for Metropolis Hastings. Options are ‘gaussian’ and ‘prior’.
  • latents (Iterable[Vertex]) – All latent vertices.
  • proposal_listeners – Listeners for proposal creation and rejection. Options are keanu.algorithm.AcceptanceRateTracker.
  • proposal_distribution_sigma (Union[int, integer, float, floating, bool_, Series, DataFrame, ndarray, List[Union[int, integer, float, floating, bool_, Series, DataFrame, ndarray]], None]) – Parameter sigma (a single sigma or a list of sigmas for each latent) for ‘gaussian’ proposal distribution
Raises:
  • TypeError – If you pass proposal_listener without specifying proposal_distribution.
  • TypeError – If you choose ‘gaussian’ as proposal_distribution but did not specify latents.
  • TypeError – If you choose ‘gaussian’ as proposal_distribution but did not specify proposal_distribution_sigma.
  • TypeError – If you choose ‘prior’ as proposal_distribution but did not pass latent vertices.
class keanu.algorithm.NUTSSampler(adapt_count=None, adapt_step_size_enabled=None, adapt_potential_enabled=None, target_acceptance_prob=None, initial_step_size=None, potential_adapt_window_size=None, max_energy_change=None, max_tree_height=None)[source]
Parameters:
  • adapt_count (Optional[int]) – The number of samples for which the step size will be tuned. For the remaining samples in which it is not tuned, the step size will be frozen to its last calculated value. Defaults to 1000.
  • adapt_step_size_enabled (Optional[bool]) – enable the step size adaption. Defaults to true.
  • adapt_potential_enabled (Optional[bool]) – enable the potential adaption. Defaults to true.
  • target_acceptance_prob (Optional[float]) – The target acceptance probability. Defaults to 0.8.
  • initial_step_size (Optional[float]) – Sets the initial step size. If none is given then a heuristic will be used to determine a good step size.
  • max_energy_change (Optional[float]) – the maximum energy change before a step is considered divergent.
  • max_tree_height (Optional[int]) – The maximum tree size for the sampler. This controls how long a sample walk can be before it terminates. This will set at a maximum approximately 2^treeSize number of logProb evaluations for a sample. Defaults to 10.