aepsych.strategy

aepsych.strategy module

aepsych.strategy.ensure_model_is_fresh(f)[source]

Decorator to ensure that the model is up-to-date before running a method.

Parameters:

f (Callable) – The function to wrap.

Returns:

The wrapped function.

Return type:

Callable

class aepsych.strategy.Strategy(generator, lb, ub, stimuli_per_trial, outcome_types, dim=None, min_total_tells=0, min_asks=0, model=None, use_gpu_modeling=False, use_gpu_generating=False, refit_every=1, min_total_outcome_occurrences=1, max_asks=None, keep_most_recent=None, min_post_range=None, name='', run_indefinitely=False, transforms=ChainedInputTransform())[source]

Bases: object

Object that combines models and generators to generate points to sample.

Initialize the strategy object.

Parameters:
  • generator (AEPsychGenerator) – The generator object that determines how points are sampled.

  • lb (torch.Tensor) – Lower bounds of the parameters.

  • ub (torch.Tensor) – Upper bounds of the parameters.

  • stimuli_per_trial (int) – The number of stimuli per trial.

  • outcome_types (Sequence[Type[str]]) – The types of outcomes that the strategy will generate.

  • dim (int, optional) – The number of dimensions in the parameter space. If None, it is inferred from the size of lb and ub.

  • min_total_tells (int) – The minimum number of total observations needed to complete this strategy.

  • min_asks (int) – The minimum number of points that should be generated from this strategy.

  • model (ModelProtocol, optional) – The AEPsych model of the data.

  • use_gpu_modeling (bool) – Whether to move the model to GPU fitting/predictions, defaults to False.

  • use_gpu_generating (bool) – Whether to use the GPU for generating points, defaults to False.

  • refit_every (int) – How often to refit the model from scratch.

  • min_total_outcome_occurrences (int) – The minimum number of total observations needed for each outcome before the strategy will finish. Defaults to 1 (i.e., for binary outcomes, there must be at least one “yes” trial and one “no” trial).

  • max_asks (int, optional) – The maximum number of trials to generate using this strategy. If None, there is no upper bound (default).

  • keep_most_recent (int, optional) – Experimental. The number of most recent data points that the model will be fitted on. This may be useful for discarding noisy data from trials early in the experiment that are not as informative as data collected from later trials. When None, the model is fitted on all data.

  • min_post_range (float, optional) – Experimental. The required difference between the posterior’s minimum and maximum value in probablity space before the strategy will finish. Ignored if None (default).

  • name (str) – The name of the strategy. Defaults to the empty string.

  • run_indefinitely (bool) – If true, the strategy will run indefinitely until finish() is explicitly called. Other stopping criteria will be ignored. Defaults to False.

  • transforms (ReversibleInputTransform, optional) – Transforms to apply parameters. This is immediately applied to lb/ub, thus lb/ub should be defined in raw parameter space for initialization. However, if the lb/ub attribute are access from an initialized Strategy object, it will be returned in transformed space.

no_gpu_acqfs = (<class 'aepsych.acquisition.monotonic_rejection.MonotonicMCAcquisition'>, <class 'aepsych.acquisition.mutual_information.MonotonicBernoulliMCMutualInformation'>, <class 'aepsych.acquisition.mc_posterior_variance.MonotonicMCPosteriorVariance'>, <class 'aepsych.acquisition.monotonic_rejection.MonotonicMCLSE'>)
normalize_inputs(x, y)[source]

converts inputs into normalized format for this strategy

Parameters:
  • x (torch.Tensor) – training inputs

  • y (torch.Tensor) – training outputs

Returns:

training inputs, normalized y (torch.Tensor): training outputs, normalized n (int): number of observations

Return type:

x (torch.Tensor)

gen(*args, **kwargs)[source]
get_max(*args, **kwargs)[source]
get_min(*args, **kwargs)[source]
inv_query(*args, **kwargs)[source]
predict(*args, **kwargs)[source]
get_jnd(*args, **kwargs)[source]
sample(*args, **kwargs)[source]
finish()[source]

Finish the strategy.

Return type:

None

property finished: bool

Check if the strategy is finished.

Returns:

True if the strategy is finished, False otherwise.

Return type:

bool

property can_fit: bool

Check if the strategy can be fitted.

Returns:

True if the strategy can be fitted, False otherwise.

Return type:

bool

property n_trials: int

Get the number of trials.

Returns:

The number of trials.

Return type:

int

add_data(x, y)[source]

Adds new data points to the strategy, and normalizes the inputs.

Parameters:
  • x (Union[np.ndarray, torch.Tensor]) – The input data points. Can be a PyTorch tensor or NumPy array.

  • y (Union[np.ndarray, torch.Tensor]) – The output data points. Can be a PyTorch tensor or NumPy array.

Return type:

None

fit()[source]

Fit the model.

Return type:

None

update()[source]

Update the model.

Return type:

None

classmethod from_config(config, name)[source]

Create a strategy object from a configuration object.

Parameters:
  • config (Config) – The configuration object.

  • name (str) – The name of the strategy.

Returns:

The strategy object.

Return type:

Strategy

class aepsych.strategy.SequentialStrategy(strat_list)[source]

Bases: object

Runs a sequence of strategies defined by its config

All getter methods defer to the current strat

Parameters:

strat_list (list[Strategy]) – TODO make this nicely typed / doc’d

Initialize the SequentialStrategy object.

Parameters:

strat_list (List[Strategy]) – The list of strategies.

gen(num_points=1, **kwargs)[source]

Generate the next set of points to evaluate.

Parameters:

num_points (int) – The number of points to generate. Defaults to 1.

Returns:

The next set of points to evaluate.

Return type:

torch.Tensor

finish()[source]

Finish the strategy.

Return type:

None

property finished: bool

Check if the strategy is finished.

Returns:

True if the strategy is finished, False otherwise.

Return type:

bool

add_data(x, y)[source]

Add new data points to the strategy.

Parameters:
  • x (Union[np.ndarray, torch.Tensor]) – The input data points.

  • y (Union[np.ndarray, torch.Tensor]) – The output data points.

Return type:

None

classmethod from_config(config)[source]

Create a SequentialStrategy object from a configuration object.

Parameters:

config (Config) – The configuration object.

Returns:

The SequentialStrategy object.

Return type:

SequentialStrategy