Summary statistics reference#
Generated by docs/gen_stats_reference.py from behav_utils.stats — do not edit the tables by hand.
The contract#
A statistic is a float computed from a TrialArrays. Every producer has the signature
f(arrays, *, rng) -> float (or a tuple of floats for a fitter). Two decorators register them:
from behav_utils.stats import stat, fit
@stat('my_rate', requires=('binary',)) # one scalar; runs on tasks with 0/1 responses
def my_rate(arrays, *, rng):
return float(arrays.choice.mean())
@fit('my_fit', outputs=('a', 'b'), exchangeable=False) # one fitter, several named outputs
def my_fit(arrays, *, rng):
...
return a, b
compute_stats(arrays, names) returns a float pd.Series indexed by the requested names, in request
order; a fitter runs once however many of its outputs are requested. PSYCHOMETRIC expands to
('mu', 'sigma', 'lapse_low', 'lapse_high') and PSE_DYNAMICS to the pse_* outputs.
from behav_utils import (PsychometricModel, TrialArrays, compute_stats, filter_trials, generate_synthetic_animal,
list_stats, select_sessions)
from behav_utils.stats import PSYCHOMETRIC
animal = generate_synthetic_animal(PsychometricModel(sigma=0.3), n_sessions=4, rng=0)
clean = filter_trials(select_sessions(animal, min_trials=0))
arrays = TrialArrays.from_sessions(clean)
s = compute_stats(arrays, ['accuracy', 'recency', *PSYCHOMETRIC])
s['mu'], s['accuracy']
list_stats() # every scalar name; list_stats(task=...) those that apply
Requirements. A producer declares the task capabilities it needs (requires=; none means every task).
compute_stats refuses an explicitly requested statistic whose needs the arrays’ task lacks, raising
RequirementError with what is missing and, where one exists, the counterpart to use (instead=);
compute_stats(arrays) with no names computes everything that applies. The built-in names are in
behav_utils.tasks.CAPABILITIES; a task of your own can declare more, and a statistic can require them
(see EXTENDING.md).
Exchangeable means trial-level resampling (bootstrap, matched-n draws) is valid for the producer:
memoryless and lag-1 statistics use the frozen prev_* view and are safe; anything that depends on
trial order beyond lag 1 is not, and the resamplers refuse it (is_exchangeable(name)). Session-level
resampling (unit='sessions') is valid for every stat.
Array-valued readouts — the psychometric curve, update matrix, conditional psychometric, binned
curve, serial-dependence profile — are not statistics; they live in behav_utils.readouts as
dataclasses with one draw-only plotter each.
Stimulus is on the normalised axis ([-1, 1], boundary at 0 — the loader maps raw units onto it, see
docs/config_guide.md); category B is the positive side; choice is 1 for B. “Hard” trials are
|stimulus| < HARD_THRESHOLD (0.3 by default).
Statistics by task#
Capabilities: binary: responses are 0 or 1; two_choices: both responses are active choices, as in 2-AFC; withhold: response 0 is withholding, as in go/no-go; stimulus_axis: a signed stimulus whose category is set by its side of the boundary at 0; scored: the task defines a correct response; k_choices: responses are one of K > 2 categories, coded 0 to K-1; continuous: responses are continuous values on the stimulus scale.
task |
capabilities |
refused (counterpart) |
|---|---|---|
|
|
|
|
|
|
|
|
|
On go/no-go, one-trial history statistics (win_stay, lose_shift, choice_autocorr, perseveration) read go or withhold after a correct or an error trial; reaction times cover go trials only.
Index#
scalar |
producer |
module |
exchangeable |
needs |
|---|---|---|---|---|
|
|
basic |
yes |
|
|
|
basic |
yes |
|
|
|
basic |
yes |
|
|
|
basic |
yes |
|
|
|
basic |
yes |
|
|
|
basic |
yes |
|
|
|
basic |
yes |
|
|
|
basic |
yes |
|
|
|
basic |
yes |
|
|
|
basic |
yes |
|
|
|
continuous |
yes |
|
|
|
continuous |
yes |
|
|
|
continuous |
yes |
|
|
|
continuous |
yes |
|
|
|
continuous |
no |
|
|
|
dynamics |
no |
|
|
|
dynamics |
no |
|
|
|
dynamics |
no |
|
|
|
dynamics |
no |
|
|
|
dynamics |
no |
|
|
|
dynamics |
no |
|
|
|
dynamics |
no |
|
|
|
dynamics |
no |
|
|
|
history |
yes |
|
|
|
history |
yes |
|
|
|
history |
yes |
|
|
|
history |
yes |
|
|
|
history |
yes |
|
|
|
history |
yes |
|
|
|
history |
yes |
|
|
|
history |
no |
|
|
|
history |
no |
|
|
|
history |
no |
|
|
|
history |
no |
|
|
|
history |
no |
|
|
|
history |
no |
|
|
|
history |
no |
|
|
|
history |
no |
|
|
|
history |
no |
|
|
|
history |
no |
|
|
|
history |
no |
|
|
|
history |
no |
|
|
|
history |
no |
|
|
|
psychometric |
yes |
|
|
|
psychometric |
yes |
|
|
|
psychometric |
yes |
|
|
|
psychometric |
yes |
|
|
|
psychometric |
yes |
|
|
|
psychometric |
yes |
|
|
|
rt |
yes |
nothing (every task) |
|
|
rt |
yes |
nothing (every task) |
|
|
sdt |
yes |
|
|
|
sdt |
yes |
|
Performance and bias (stats/basic.py)#
accuracy (stat; exchangeable: yes)#
Needs: scored.
Proportion correct over responded trials.
side_bias (stat; exchangeable: yes)#
Needs: two_choices. Instead, on a task with withhold: go_bias.
P(choose B) − 0.5. Positive = biased toward B.
accuracy_b_minus_a (stat; exchangeable: yes)#
Needs: scored, two_choices. Instead, on a task with withhold: hit_minus_cr_rate.
Accuracy on category-B trials minus accuracy on category-A trials. Positive = more accurate on B.
Unlike side_bias, it does not depend on how often each category is presented (bias-correcting
trial selection, an asymmetric stimulus distribution); it does change when the two categories differ
in difficulty. NaN if either category has fewer than 3 responded trials.
go_bias (stat; exchangeable: yes)#
Needs: withhold. Instead, on a task with two_choices: side_bias.
Go/no-go: go rate − 0.5 over all non-aborted trials (a withhold is a choice of 0).
hit_minus_cr_rate (stat; exchangeable: yes)#
Needs: scored, withhold. Instead, on a task with two_choices: accuracy_b_minus_a.
Go/no-go: hit rate (correct on go trials) minus correct-rejection rate (correct on no-go trials).
stimulus_sensitivity (stat; exchangeable: yes)#
Needs: binary, stimulus_axis.
Pearson correlation between stimulus value and choice.
choice_entropy (stat; exchangeable: yes)#
Needs: binary, stimulus_axis.
Mean over stimulus bins of the binary entropy of choice, in bits ([0, 1]).
hard_accuracy (stat; exchangeable: yes)#
Needs: scored, stimulus_axis.
Accuracy on |stimulus| < HARD_THRESHOLD.
easy_accuracy (stat; exchangeable: yes)#
Needs: scored, stimulus_axis.
Accuracy on |stimulus| >= HARD_THRESHOLD.
hard_easy_ratio (stat; exchangeable: yes)#
Needs: scored, stimulus_axis.
hard_accuracy / easy_accuracy. NaN if either side has < 3 trials or easy accuracy ≈ 0.
Defined for this library; it follows no published measure.
Continuous responses (stats/continuous.py)#
response_bias (stat; exchangeable: yes)#
Needs: continuous.
Mean signed error, response − truth: positive means responses land above the truth.
error_sd (stat; exchangeable: yes)#
Needs: continuous.
Standard deviation of the signed error: the spread of responses around the truth.
mean_abs_error (stat; exchangeable: yes)#
Needs: continuous.
Mean absolute error, |response − truth|.
response_slope (stat; exchangeable: yes)#
Needs: continuous.
Slope of the response regressed on the truth: 1 is faithful, below 1 pulled towards the middle. It is
the slope the response curve shows, so a pull towards the previous trial flattens it too;
serial_bias measures that part on its own.
serial_bias (stat; exchangeable: no)#
Needs: continuous.
How far the error leans towards the previous trial’s stimulus: the coefficient of previous −
current stimulus when the error is regressed on it and on the current stimulus. Positive is
attraction, negative repulsion. The current stimulus is in the regression because a pull towards
the middle (response_slope below 1) also makes the error depend on it; without it, that pull
would show up here as a serial effect.
Source: the serial dependence of Fischer & Whitney (2014), measured here by a linear regression rather than a fitted curve; the docs site lists the references.
Within-block PSE dynamics (stats/dynamics.py)#
pse_dynamics (fitter; exchangeable: no)#
Outputs: pse_start, pse_end, pse_final, pse_tau, pse_trials_to_90, pse_censored, pse_shape_daic, pse_step_switch
Needs: binary, stimulus_axis.
Exponential PSE trajectory over the block, compared with a step trajectory (all six params free).
Defined for this library; it follows no published measure.
Trial history and serial dependence (stats/history.py)#
recency (stat; exchangeable: yes)#
Needs: binary, stimulus_axis.
P(choose B | prev category B) − P(choose B | prev category A).
stimulus_recency (stat; exchangeable: yes)#
Needs: binary, stimulus_axis.
P(choose B | prev stimulus > 0) − P(choose B | prev stimulus <= 0).
recency_divergence (stat; exchangeable: yes)#
Needs: binary, stimulus_axis.
stimulus_recency − recency (sensory vs categorical serial dependence).
Defined for this library; it follows no published measure.
win_stay (stat; exchangeable: yes)#
Needs: binary, scored.
P(repeat | correct) − P(repeat | error).
win_stay_rate (stat; exchangeable: yes)#
Needs: binary, scored.
P(repeat | correct).
lose_shift (stat; exchangeable: yes)#
Needs: binary, scored.
P(switch | error).
choice_autocorr (stat; exchangeable: yes)#
Needs: binary.
Lag-1 correlation between choice_t and choice_{t−1}.
perseveration (stat; exchangeable: no)#
Needs: binary, stimulus_axis.
Observed repeat rate minus the repeat rate predicted by P(B | stimulus bin) alone.
Defined for this library; it follows no published measure.
logistic_history (fitter; exchangeable: no)#
Outputs: w_stimulus, w_prev_choice_1, w_prev_choice_2, w_prev_choice_3, w_prev_outcome_1, w_prev_outcome_2, w_prev_outcome_3, history_decay
Needs: scored, stimulus_axis, two_choices.
L2-regularised logistic regression of choice on stimulus + N_BACK trials of history.
Outputs: stimulus weight, previous-choice weights 1..N_BACK, previous- outcome weights 1..N_BACK, and the exponential decay rate of the previous-choice weights across lags.
Source: a logistic regression of the choice on the stimulus and on recent choices and outcomes, the approach of Fründ, Wichmann & Macke (2014); the docs site lists the references.
history_interaction_r2 (stat; exchangeable: no)#
Needs: scored, stimulus_axis, two_choices.
McFadden pseudo-R² gained by adding N_BACK trials of history to a stimulus-only model.
Source: McFadden’s pseudo-R² (McFadden, 1974); the docs site lists the references.
sd_profile (fitter; exchangeable: no)#
Outputs: sd_slope, sd_curvature, sd_range
Needs: binary, scored, stimulus_axis.
Scalar shape features of the post-correct serial-dependence profile.
Linear slope, quadratic curvature and range of
behav_utils.readouts.compute_sd_profile across previous-stimulus
bins; NaN when fewer than 4 bins are populated (curvature needs 5).
Psychometric fit (stats/psychometric.py)#
psychometric (fitter; exchangeable: yes)#
Outputs: mu, sigma, lapse_low, lapse_high
Needs: binary, stimulus_axis.
Cumulative-Gaussian fit: (mu, sigma, lapse_low, lapse_high).
mu and sigma are NaN when the fit is unreliable (sigma > SLOPE_THRESHOLD or |mu| > MU_BOUND); the lapses are returned regardless.
Source: the psychometric function with lapse rates and its constrained maximum-likelihood fit follow Wichmann & Hill (2001); the docs site lists the references.
pse (stat; exchangeable: yes)#
Needs: binary, stimulus_axis.
Lapse-corrected point of subjective equality: where the full curve crosses 0.5.
No slope guard (unlike ‘mu’), so shallow early-learning windows survive. NaN when the fit fails, the corrected curve cannot cross 0.5, or |PSE| > 1.
psychometric_gof (stat; exchangeable: yes)#
Needs: binary, stimulus_axis.
R² between binned choice proportions and the fitted psychometric curve.
Reaction time (stats/rt.py)#
reaction_time (stat; exchangeable: yes)#
Needs: nothing (every task).
Median response latency (ms) over trials with a recorded RT; NaN if none.
reaction_time_jitter (stat; exchangeable: yes)#
Needs: nothing (every task).
Median RT (ms) after an independent U[0, RT_JITTER_MS) draw per trial.
Re-drawn on every call, so under bootstrap the recording slop folds into the interval: a condition difference barely moves while its CI widens.
Signal detection (stats/sdt.py)#
sdt (fitter; exchangeable: yes)#
Outputs: dprime, criterion
Needs: binary, stimulus_axis.
Equal-variance signal-detection summary with category B as the signal and “choose B” as a yes:
hit = P(choose B | B), false alarm = P(choose B | A), both log-linear corrected.
dprime = z(hit) − z(fa) — discriminability independent of bias; criterion c = −(z(hit) + z(fa)) / 2 —
positive = conservative about B, i.e. biased toward A. A manipulation that moves the criterion without
moving d′ shifts the decision rule, not the evidence.
Source: equal-variance signal detection theory (Green & Swets, 1966; Macmillan & Creelman, 1991), with the log-linear correction for extreme rates of Hautus (1995); the docs site lists the references.