cpm.generators
cpm.generators.Value(value=None, lower=0, upper=1, prior=None, args=None, **kwargs)
The Value
class is a wrapper around a float value, with additional details such as the prior distribution, lower and upper bounds. It supports all basic mathematical operations and can be used as a regular float with the parameter value as operand.
Parameters: |
|
---|
Notes
We currently implement the following continuous distributions from scipy.stats
corresponding to the prior
argument:
- 'uniform'
- 'truncated_normal'
- 'beta'
- 'gamma'
- 'truncated_exponential'
- 'norm'
Because these distributions are inherited from scipy.stats
, see the scipy documentation for more details on how to update variables of the distribution.
Returns: |
|
---|
PDF(log=False)
Return the prior distribution of the parameter.
Returns: |
|
---|
copy()
Return a copy of the parameter.
Returns: |
|
---|
fill(value)
Replace the value of the parameter with a new value.
Parameters: |
|
---|
Notes
If the parameter is an array, it should be a list of values. If the parameter is an array, and the new value is a single value, it will be broadcasted to the shape of the array.
sample(size=1, jump=False)
Sample and update the parameter value from its prior distribution.
Returns: |
|
---|
update_prior(**kwargs)
Update the prior distribution of the parameter. Currently it is only implemented for truncated normal distributions.
Parameters: |
|
---|
cpm.generators.Parameters(**kwargs)
A class representing a set of parameters. It takes keyword arguments representing the parameters with their values and wraps them into a python object.
Parameters: |
|
---|
Returns: |
|
---|
Examples:
>>> from cpm.generators import Parameters
>>> parameters = Parameters(a=0.5, b=0.5, c=0.5)
>>> parameters['a']
0.1
>>> parameters.a
0.1
>>> parameters()
{'a': 0.1, 'b': 0.2, 'c': 0.5}
The Parameters class can also provide a prior.
>>> x = Parameters(
>>> a=Value(value=0.1, lower=0, upper=1, prior="normal", args={"mean": 0.5, "sd": 0.1}),
>>> b=0.5,
>>> weights=Value(value=[0.1, 0.2, 0.3], lower=0, upper=1, prior=None),
>>> )
>>> x.prior(log=True)
-6.5854290732499186
We can also sample new parameter values from the prior distributions.
>>> x.sample()
{'a': 0.4670755733417274, 'b': 0.30116207009111917}
PDF(log=False)
Return the prior probability density of the parameter values.
Returns: |
|
---|
bounds()
Returns a tuple with lower (first element) and upper (second element) bounds for parameters with defined priors.
Returns: |
|
---|
export()
Return all freely-varying parameters and their accompanying lower and upper bounds, and prior distributions.
Returns: |
|
---|
free()
Return a dictionary of all parameters with a prior distribution.
Returns: |
|
---|
keys()
Return a list of all the keys in the parameters dictionary.
Returns: |
|
---|
sample(size=1, jump=False)
Sample and update parameter values from their prior distribution.
Returns: |
|
---|
update(**kwargs)
Update the parameters with new values.
Parameters: |
|
---|
update_prior(**kwargs)
Update the prior distribution of all parameters.
Parameters: |
|
---|
cpm.generators.Wrapper(model=None, data=None, parameters=None)
A Wrapper
class for a model function in the CPM toolbox. It is designed to run a model for a single experiment (participant) and store the output in a format that can be used for further analysis.
Parameters: |
|
---|
Returns: |
|
---|
Notes
The model function should take two arguments: parameters
and trial
. The parameters
argument should be a Parameter object specifying the model parameters. The trial
argument should be a dictionary or pd.Series
containing all input to the model on a single trial. The model function should return a dictionary containing the model output for the trial. If the model is intended to be fitted to data, its output should contain the following keys:
- 'dependent': Any dependent variables calculated by the model that will be used for the loss function.
If a model output contains any keys that are also present in parameters, it updates those in the parameters based on the model output.
export()
Export the trial-level simulation details.
Returns: |
|
---|
reset(parameters=None, data=None)
Reset the model.
Parameters: |
|
---|
Notes
When resetting the model, and parameters
is None, reset model to initial state.
If parameter is array_like
, it resets the only the parameters in the order they are provided,
where the last parameter updated is the element in parameters corresponding to len(parameters).
Examples:
>>> x = Wrapper(model = mine, data = data, parameters = params)
>>> x.run()
>>> x.reset(parameters = [0.1, 1])
>>> x.run()
>>> x.reset(parameters = {'alpha': 0.1, 'temperature': 1})
>>> x.run()
>>> x.reset(parameters = np.array([0.1, 1, 0.5]))
>>> x.run()
Returns: |
|
---|
run()
Run the model.
Returns: |
|
---|
save(filename=None)
Save the model.
Parameters: |
|
---|
Returns: |
|
---|
Examples:
>>> x = Wrapper(model = mine, data = data, parameters = params)
>>> x.run()
>>> x.save('simulation')
If you wish to save a file in a specific folder, provide the relative path.
>>> x.save('results/simulation')
>>> x.save('../archives/results/simulation')
cpm.generators.Simulator(wrapper=None, data=None, parameters=None)
A Simulator
class for a model in the CPM toolbox. It is designed to run a model for multiple participants and store the output in a format that can be used for further analysis.
Parameters: |
|
---|
Returns: |
|
---|
export(save=False, path=None)
Return the trial- and participant-level information about the simulation.
Parameters: |
|
---|
Returns: |
|
---|
generate(variable='dependent')
Generate data for parameter recovery, etc.
Parameters: |
|
---|
reset()
Resets the simulation.
run()
Runs the simulation.
Note
Data is sorted according to the group IDs as ordered by pandas.
save(filename=None)
Saves the simulation results.
Parameters: |
|
---|
update(parameters=None)
Updates the parameters of the simulation.