cpm.models#
The components that models are built from. Combine them inside a model
function and pass that to Wrapper. Their formulas are
in cpm.models.kernels, as functions that the classes compute with and
that loops compiled with numba can call.
Learning rules#
DeltaRule class computes the prediction error for a given input and target value. |
|
A class representing a learning rule based on the separable error-term of Bush and Mosteller (1951). |
|
Q-learning rule (Watkins, 1989) for a one-dimensional array of Q-values. |
|
A humble teacher learning rule (Kruschke, 1992; Love, Gureckis, and Medin, 2004) for multi-dimensional outcome learning. |
|
SARSA learning rule with an eligibility trace for a two-stage Markov decision task (Sutton & Barto, 2018; Kool et al., 2016). |
Decision rules#
Softmax class for computing policies based on activations and temperature (Bridle, 1990). |
|
A class representing a sigmoid function that takes an n by m array of activations and returns an n array of outputs, where n is the number of output and m is the number of inputs. |
|
A class representing an ε-greedy rule based on Daw et al. (2006). |
|
A class representing a choice kernel based on a softmax function that incorporates the frequency of choosing an action. |
Activation functions#
Represents a sigmoid activation function. |
|
A competitive attentional gating function, an attentional activation function, that incorporates stimulus salience in addition to the stimulus vector to modulate the weights. |
|
A class for computing choice utilities based on prospect theory. |
|
A class for adding a scalar to one element of an input array. |
Attention#
RapidAttentionShift implements the rapid attention shift from models such as EXIT (Kruschke, 2001) and its simplified version (Paskewitz & Jones, 2020). |
Utilities#
Converts a target value to a binary representation using a specified number of bits. |
Kernels for compiled models#
The computations of the building blocks in cpm.models, as functions.
The classes in cpm.models.decision, cpm.models.learning,
cpm.models.activation and cpm.models.attention compute one step
of a model and are convenient to write per-trial models with. Their formulas
live here, once, as functions on numpy arrays and numbers, and the classes
compute with them: each class converts and checks its input, calls the function
it is named after, and keeps the result in its attributes.
The functions are written with numpy operations that numba can also compile, so
that loops over trials that are compiled as a whole, such as the models of the
built-in applications (see cpm.generators.SessionWrapper), can call
them too. With numba installed, the functions of this module are compiled on
first use (and cached on disk); the classes use a plain-Python copy of them
(see cpm.core._jit.kernels), which never needs numba. Both give the same
results.
logistic, p_second, log_softmax and choose have no class counterpart:
they are for compiled loops. The softmax switches to an equivalent form that
cannot overflow where the largest scaled activation is beyond ±700; logistic
and p_second cannot overflow.
Functions that update values in place say so; all others return new arrays.
Examples#
A Rescorla-Wagner learner with a softmax policy, compiled as a whole:
>>> import numpy as np
>>> from numba import njit
>>> from cpm.models import kernels
>>> @njit
... def session(alpha, beta, rewards, choices):
... values = np.zeros(2)
... p_right = np.empty(len(choices))
... for t in range(len(choices)):
... p_right[t] = kernels.p_second(beta, values[0], values[1])
... values[choices[t]] += alpha * (rewards[t] - values[choices[t]])
... return p_right
>>> session(0.3, 2.0, np.array([1.0, 0.0, 1.0]), np.array([1, 1, 0]))
array([0.5 , 0.64565631, 0.60348325])
The softmax policy, \(e^{\beta x_i} / \sum_j e^{\beta x_j}\), of |
|
The logarithm of |
|
The probability of the second of two options under a softmax. |
|
The logistic function, 1 / (1 + exp(-x)), without overflow. |
|
A policy with irreducible noise |
|
The softmax with irreducible noise, as |
|
The policy of |
|
The policy of |
|
The policy of |
|
Sample an option from a policy, given a uniform random number in [0, 1). |
|
The change in the weights and the prediction errors of |
|
The change in the weights and the prediction errors of |
|
The updated values of |
|
The change in the weights of |
|
The updated weights (a new array) of |
|
The prediction errors and value changes of |
|
|
|
The activations of |
|
The normalised attentional gain of |
|
The values of each stimulus scaled by its attentional gain (a new array). |
|
The gated values (a new array) of |
|
The power utility of |
|
The decision weights of outcomes, as |
|
The Tversky & Kahneman (1992) weighting function with curvature |
|
The power weighting function, p^gamma. |
|
The Prelec (1998) weighting function. |
|
The Gonzalez & Wu (1999) weighting function. |
|
The expected utility of each option: the sum of its weighted utilities. |
|
Add |
|
The output of |
|
The change in the attention gain of |