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
Declaration of one model parameter. |
|
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:
objectDeclaration 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.
- 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:
objectBase class of every quantum-like and classical model.
Subclasses define three class attributes and one method:
PARAMS: list ofParam;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
optionsas a structural (non-fitted) option, for example a projector rank.- Variables:
options (
dict) – Structural options passed to the constructor.
- to_vector()[source]#
Unconstrained vector of the fitted parameters.
- Returns:
numpy.ndarray– One entry perfree_params()element; the inverse offrom_vector().
- classmethod from_vector(x, **options)[source]#
Build a model from an unconstrained parameter vector.
- Parameters:
x (
array_like) – Unconstrained values, in the order offree_params().**options – Structural options passed to the constructor.
- Returns:
Model– New instance of the class.
- 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.
- 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.