Derived Models¶
Introduction¶
QInfer provides several models which decorate other models, providing additional functionality or changing the behaviors of underlying models.
PoisonedModel
- Model corrupted by likelihood errors¶
-
class
qinfer.
PoisonedModel
(underlying_model, tol=None, n_samples=None, hedge=None)[source]¶ Bases:
qinfer.derived_models.DerivedModel
Model that simulates sampling error incurred by the MLE or ALE methods of reconstructing likelihoods from sample data. The true likelihood given by an underlying model is perturbed by a normally distributed random variable \(\epsilon\), and then truncated to the interval \([0, 1]\).
The variance of \(\epsilon\) can be specified either as a constant, to simulate ALE (in which samples are collected until a given threshold is met), or as proportional to the variance of a possibly-hedged binomial estimator, to simulate MLE.
Parameters: -
simulate_experiment
(modelparams, expparams, repeat=1)[source]¶ Simulates experimental data according to the original (unpoisoned) model. Note that this explicitly causes the simulated data and the likelihood function to disagree. This is, strictly speaking, a violation of the assumptions made about
Model
subclasses. This violation is by intention, and allows for testing the robustness of inference algorithms against errors in that assumption.
-
BinomialModel
- Model over batches of two-outcome experiments¶
-
class
qinfer.
BinomialModel
(underlying_model)[source]¶ Bases:
qinfer.derived_models.DerivedModel
Model representing finite numbers of iid samples from another model, using the binomial distribution to calculate the new likelihood function.
Parameters: underlying_model (qinfer.abstract_model.Model) – An instance of a two- outcome model to be decorated by the binomial distribution. Note that a new experimental parameter field
n_meas
is added by this model. This parameter field represents how many times a measurement should be made at a given set of experimental parameters. To ensure the correct operation of this model, it is important that the decorated model does not also admit a field with the namen_meas
.-
decorated_model
¶
-
expparams_dtype
¶
-
is_n_outcomes_constant
¶ Returns
True
if and only if the number of outcomes for each experiment is independent of the experiment being performed.This property is assumed by inference engines to be constant for the lifetime of a Model instance.
-
n_outcomes
(expparams)[source]¶ Returns an array of dtype
uint
describing the number of outcomes for each experiment specified byexpparams
.Parameters: expparams (numpy.ndarray) – Array of experimental parameters. This array must be of dtype agreeing with the expparams_dtype
property.
-
domain
(expparams)[source]¶ Returns a list of ``Domain``s, one for each input expparam.
Parameters: expparams (numpy.ndarray) – Array of experimental parameters. This array must be of dtype agreeing with the expparams_dtype
property, or, in the case wheren_outcomes_constant
isTrue
,None
should be a valid input.Return type: list of Domain
-
are_expparam_dtypes_consistent
(expparams)[source]¶ Returns
True
iff all of the given expparams correspond to outcome domains with the same dtype. For efficiency, concrete subclasses should override this method if the result is alwaysTrue
.Parameters: expparams (np.ndarray) – Array of expparamms of type expparams_dtype
Return type: bool
-
GaussianHyperparameterizedModel
- Model over Gaussian outcomes conditioned on two-outcome experiments¶
-
class
qinfer.
GaussianHyperparameterizedModel
(underlying_model)[source]¶ Bases:
qinfer.derived_models.DerivedModel
Model representing a two-outcome model viewed through samples from one of two distinct Gaussian distributions. This model adds four new model parameters to its underlying model, respectively representing the mean outcome conditioned on an underlying 0, the mean outcome conditioned on an underlying 1, and the variance of outcomes conditioned in each case.
Parameters: underlying_model (qinfer.abstract_model.Model) – An instance of a two- outcome model to be viewed through Gaussian distributions. -
decorated_model
¶
-
modelparam_names
¶
-
n_modelparams
¶
-
MultinomialModel
- Model over batches of D-outcome experiments¶
-
class
qinfer.
MultinomialModel
(underlying_model)[source]¶ Bases:
qinfer.derived_models.DerivedModel
Model representing finite numbers of iid samples from another model with a fixed and finite number of outcomes, using the multinomial distribution to calculate the new likelihood function.
Parameters: underlying_model (qinfer.abstract_model.FiniteOutcomeModel) – An instance of a D-outcome model to be decorated by the multinomial distribution. This underlying model must have is_n_outcomes_constant
asTrue
.Note that a new experimental parameter field
n_meas
is added by this model. This parameter field represents how many times a measurement should be made at a given set of experimental parameters. To ensure the correct operation of this model, it is important that the decorated model does not also admit a field with the namen_meas
.-
decorated_model
¶
-
expparams_dtype
¶
-
is_n_outcomes_constant
¶ Returns
True
if and only if the number of outcomes for each experiment is independent of the experiment being performed.This property is assumed by inference engines to be constant for the lifetime of a Model instance.
-
n_sides
¶ Returns the number of possible outcomes of the underlying model.
-
n_outcomes
(expparams)[source]¶ Returns an array of dtype
uint
describing the number of outcomes for each experiment specified byexpparams
.Parameters: expparams (numpy.ndarray) – Array of experimental parameters. This array must be of dtype agreeing with the expparams_dtype
property.
-
domain
(expparams)[source]¶ Returns a list of
Domain
objects, one for each input expparam. :param numpy.ndarray expparams: Array of experimental parameters. Thisarray must be of dtype agreeing with theexpparams_dtype
property.Return type: list of Domain
-
are_expparam_dtypes_consistent
(expparams)[source]¶ Returns
True
iff all of the given expparams correspond to outcome domains with the same dtype. For efficiency, concrete subclasses should override this method if the result is alwaysTrue
.Parameters: expparams (np.ndarray) – Array of expparamms of type expparams_dtype
Return type: bool
-
MLEModel
- Model for approximating maximum-likelihood estimation¶
-
class
qinfer.
MLEModel
(underlying_model, likelihood_power)[source]¶ Bases:
qinfer.derived_models.DerivedModel
Uses the method of [JDD08] to approximate the maximum likelihood estimator as the mean of a fictional posterior formed by amplifying the Bayes update by a given power \(\gamma\). As \(\gamma \to \infty\), this approximation to the MLE improves, but at the cost of numerical stability.
Parameters: likelihood_power (float) – Power to which the likelihood calls should be rasied in order to amplify the Bayes update.
RandomWalkModel
- Model for adding fixed random walk to parameters¶
-
class
qinfer.
RandomWalkModel
(underlying_model, step_distribution)[source]¶ Bases:
qinfer.derived_models.DerivedModel
Model such that after each time step, a random perturbation is added to each model parameter vector according to a given distribution.
Parameters: - underlying_model (Model) – Model representing the likelihood with no random walk added.
- step_distribution (Distribution) – Distribution over step vectors.
GaussianRandomWalkModel
- Model for adding gaussian random walk to parameters¶
-
class
qinfer.
GaussianRandomWalkModel
(underlying_model, random_walk_idxs='all', fixed_covariance=None, diagonal=True, scale_mult=None, model_transformation=None)[source]¶ Bases:
qinfer.derived_models.DerivedModel
Model such that after each time step, a random perturbation is added to each model parameter vector according to a zero-mean gaussian distribution.
The \(n\times n\) covariance matrix of this distribution is either fixed and known, or its entries are treated as unknown, being appended to the model parameters. For diagonal covariance matrices, \(n\) parameters are added to the model storing the square roots of the diagonal entries of the covariance matrix. For dense covariance matrices, \(n(n+1)/2\) parameters are added to the model, storing the entries of the lower triangular portion of the Cholesky factorization of the covariance matrix.
Parameters: - underlying_model (Model) – Model representing the likelihood with no random walk added.
- random_walk_idxs – A list or
np.slice
ofunderlying_model
model parameter indeces to add the random walk to. Indeces larger thanunderlying_model.n_modelparams
should not be touched. - fixed_covariance – An
np.ndarray
specifying the fixed covariance matrix (or diagonal thereof ifdiagonal
isTrue
) of the gaussian distribution. If set toNone
(default), this matrix is presumed unknown and parameters are appended to the model describing it. - diagonal (boolean) – Whether the gaussian distribution covariance matrix
is diagonal, or densely populated. Default is
True
. - scale_mult – A function which takes an array of expparams and
outputs a real number for each one, representing the scale of the
given experiment. This is useful if different experiments have
different time lengths and therefore incur different dispersion amounts.If a string is given instead of a function,
thee scale multiplier is the
exparam
with that name. - model_transformation – Either
None
or a pair of functions(transform, inv_transform)
specifying a transformation ofmodelparams
(of the underlying model) before gaussian noise is added, and the inverse operation after the gaussian noise has been added.
-
modelparam_names
¶
-
n_modelparams
¶
-
is_n_outcomes_constant
¶
-
est_update_covariance
(modelparams)[source]¶ Returns the covariance of the gaussian noise process for one unit step. In the case where the covariance is being learned, the expected covariance matrix is returned.
Parameters: modelparams – Shape (n_models, n_modelparams)
shape arrayof model parameters.