7.1.1.2.1.1. pta.sampling.commons

Common classes and methods for sampling algorithms.

7.1.1.2.1.1.1. Module Contents

exception pta.sampling.commons.SamplingException[source]

Bases: Exception

Raised when an exception occurs during sampling.

class pta.sampling.commons.SamplingResult(samples: pandas.DataFrame, psrf: pandas.Series, basis_samples: pandas.DataFrame = None, chains: numpy.ndarray = None)[source]

Encapsulates the result of a sampler.

Parameters:
  • samples (pd.DataFrame) – Data frame containing the samples as rows.

  • psrf (pd.Series) – Series containing the PSRF of each variable.

  • basis_samples (pd.DataFrame, optional) – The samples in the basis.

  • chains (np.ndarray, optional) – The simulated chains.

property basis_samples: pandas.DataFrame[source]

Gets a data frame containing the samples in the basis.

property samples: pandas.DataFrame[source]

Gets a data frame containing the samples.

property chains: numpy.ndarray[source]

Get the simulated chains.

property psrf: pandas.Series[source]

Gets the Potential Scale Reduction Factor (PSRF) of each variable.

check_convergence(max_psrf: float = default_max_psrf) bool[source]

Checks whether the chains converged according to the given criteria.

Parameters:

max_psrf (float, optional) – Maximum PSRF value for convergence.

Returns:

True if the test succeeded, false otherwise.

Return type:

bool

class pta.sampling.commons.SamplerInterface[source]

Interface for an MCMC sampler.

abstract property dimensionality: int[source]

Get the dimensionality of the sampling space.

abstract simulate(settings: _pta_python_binaries.SamplerSettings, initial_points: numpy.ndarray, directions_transform: numpy.ndarray) Any[source]

Run the sampler with the given parameters.

Parameters:
  • settings (pb.SamplerSettings) – Sampling settings.

  • initial_points (np.ndarray) – The initial points for the chains.

  • directions_transform (np.ndarray) – The transform for the directions sampler.

abstract compute_psrf(result: Any) pandas.Series[source]

Compute the potential scale reduction factors for the variables of interest on a given set of chains.

Parameters:

result (Any) – The result of the sampling function.

Returns:

The computed potential scale reduction factors.

Return type:

pd.Series

abstract get_chains(result: Any) numpy.ndarray[source]

Extract the simulated chains from a given result.

Parameters:

result (Any) – The result of the native sampling function.

Returns:

The simulated chains.

Return type:

np.ndarray

pta.sampling.commons.sample_from_chains(chains: numpy.ndarray, num_samples: int) numpy.ndarray[source]

Draws samples from a given set of chains.

Parameters:
  • chains (np.ndarray) – Numpy 3D array containing the chains.

  • num_samples (int) – Number of samples to draw.

Returns:

Array of samples.

Return type:

np.ndarray

pta.sampling.commons.split_chains(chains: numpy.ndarray) numpy.ndarray[source]

Split the chains in two, threating each half as a new chain. This is often used to detect systematic trends in a random walk.

Parameters:

chains (np.ndarray) – Numpy 3D array containing the input chains.

Returns:

Numpy 3D array containing the resulting chains.

Return type:

np.ndarray

pta.sampling.commons.apply_to_chains(chains: numpy.ndarray, f: Callable[[numpy.ndarray], numpy.ndarray]) numpy.ndarray[source]

Apply a function to each sample of a group of chains.

Parameters:
  • chains (np.ndarray) – Numpy 3D array containing the input chains.

  • f (Callable[[np.ndarray], np.ndarray]) – Function to apply to each sample.

Returns:

Numpy 3D array containing the transformed samples.

Return type:

np.ndarray

pta.sampling.commons.split_R(chains: numpy.ndarray) numpy.ndarray[source]

Compute the split-R (or Potential Scale Reduction Factor) of each variable in the given chains.

Parameters:

chains (np.ndarray) – Numpy 3D array containing the input chains.

Returns:

Vector containing the split-R value for each variable.

Return type:

np.ndarray

pta.sampling.commons.fill_common_sampling_settings(settings: _pta_python_binaries.SamplerSettings, num_samples: int, num_steps: int, num_chains: int = -1, num_warmup_steps: int = -1, max_threads: int = default_max_threads)[source]

Fills default values for common sampling settings.

Parameters:
  • settings (SamplerSettings) – The settings object to be filled.

  • log_directory (str) – Directory in which the sampling logs should be stored.

  • num_samples (int) – Number of samples to draw.

  • num_steps (int) – Number of steps to take with each chain.

  • num_chains (int, optional) – Number of chains to use. Will be set to the number of CPUs by default.

  • num_warmup_steps (int, optional) – Number of burn-in steps to discard. Will be set to half the number of steps by default.

  • log_interval (datetime.timedelta, optional) – Interval between each logging event.

Raises:

ValueError – If the inputs are inconsistent.