Model base class#

Base class shared by every model in quantum_mind.

A model has named parameters with declared kinds, which map them to an unconstrained vector for fitting, and a Model.predict() method. For every condition of a design, predict returns a probability vector over the outcomes of that condition, or, for judgement models, a vector of predicted values.

Data are dictionaries with the same condition keys as the design: outcome counts for multinomial models (fitted by maximum likelihood) or observed values for judgement models (fitted by least squares).

Examples

A minimal model with one probability parameter:

>>> import numpy as np
>>> from quantum_mind.core import Model, Param
>>> class Coin(Model):
...     PARAMS = [Param('p', 'prob', 0.5)]
...     def predict(self, design):
...         return {c: np.array([self.p, 1 - self.p]) for c in design}
>>> Coin(p=0.3).predict(['toss'])['toss']
array([0.3, 0.7])

Classes

Param

Declaration of one model parameter.

Model

Base class of every quantum-like and classical model.

class Param(name, kind='real', default=0.0, lo=0.0, hi=1.0)[source]#

Bases: object

Declaration of one model parameter.

Parameters:
  • name (str) – Attribute name on the model.

  • kind ({'prob', 'angle', 'real', 'positive', 'bounded', 'fixed'}) – Constraint of the parameter. 'prob' lies in (0, 1) (logit transform), 'angle' is a free periodic real, 'real' is unconstrained, 'positive' is > 0 (log transform), 'bounded' lies in (lo, hi) and 'fixed' is never fitted.

  • default (float) – Value used when the model is constructed without this parameter.

  • lo (float) – Bounds of a 'bounded' parameter.

  • hi (float) – Bounds of a 'bounded' parameter.

to_free(v)[source]#

Map a parameter value to the unconstrained real line used by the optimiser.

Parameters:

v (float) – Parameter value.

Returns:

float – Unconstrained value (logit, log or identity, depending on kind).

from_free(x)[source]#

Inverse of to_free().

Parameters:

x (float) – Unconstrained value.

Returns:

float – Parameter value inside its constraint set.

random_free(rng)[source]#

Random unconstrained starting value for multi-start fitting.

Parameters:

rng (numpy.random.Generator) – Random number generator.

Returns:

float – Uniform on [0, pi) for angles, standard normal otherwise.

class Model(**params)[source]#

Bases: object

Base class of every quantum-like and classical model.

Subclasses define three class attributes and one method:

  • PARAMS: list of Param;

  • LOSS: 'multinomial' (outcome counts) or 'sse' (observed values);

  • name: label used in comparison tables;

  • predict(): design -> {condition: vector}.

Parameters:

**params – Parameter values by name; missing parameters take their default. Any other keyword is kept in options as a structural (non-fitted) option, for example a projector rank.

Variables:

options (dict) – Structural options passed to the constructor.

classmethod free_params()[source]#

Parameters that are fitted.

Returns:

list of Param – Every parameter whose kind is not 'fixed'.

property params#

current parameter values by name.

Type:

dict

to_vector()[source]#

Unconstrained vector of the fitted parameters.

Returns:

numpy.ndarray – One entry per free_params() element; the inverse of from_vector().

classmethod from_vector(x, **options)[source]#

Build a model from an unconstrained parameter vector.

Parameters:
  • x (array_like) – Unconstrained values, in the order of free_params().

  • **options – Structural options passed to the constructor.

Returns:

Model – New instance of the class.

property n_params#

number of fitted parameters (used by BIC and AIC).

Type:

int

predict(design)[source]#

Predictions for every condition of a design.

Parameters:

design (iterable) – Condition keys (their meaning depends on the model family).

Returns:

dict – {condition: numpy.ndarray}: a probability vector over outcomes, or predicted values.

loglik(data, design)[source]#

Multinomial log-likelihood of outcome counts.

Parameters:
  • data (dict) – {condition: counts} with the outcome order of predict().

  • design (iterable) – Conditions passed to predict().

Returns:

float – \(\sum_c \sum_k n_{ck} \log p_{ck}\) (constant terms omitted; probabilities are clipped at 1e-12).

sse(data, design)[source]#

Sum of squared errors between predicted and observed values.

Parameters:
  • data (dict) – {condition: observed values}.

  • design (iterable) – Conditions passed to predict().

Returns:

float – Sum over conditions of the squared differences.

sample(design, n, rng=None)[source]#

Simulate outcome counts from the model.

Parameters:
  • design (iterable) – Conditions.

  • n (int) – Number of observations per condition.

  • rng (numpy.random.Generator, optional) – Random number generator.

Returns:

dict – {condition: counts}, multinomial draws from the predicted probabilities.