aind_dynamic_foraging_models.logistic_regression package¶
Submodules¶
aind_dynamic_foraging_models.logistic_regression.model module¶
Logistic regression on choice and reward history for the foraging task
adapted from https://github.com/hanhou/map-ephys/blob/master/pipeline/model/descriptive_analysis.py
- aind_dynamic_foraging_models.logistic_regression.model.exp_func(trial_back, amp, tau)[source]¶
Exponential function for fitting
- aind_dynamic_foraging_models.logistic_regression.model.fit_logistic_regression(choice_history: List | ndarray, reward_history: List | ndarray, logistic_model: Literal['Su2022', 'Bari2019', 'Hattori2019', 'Miller2021'] = 'Su2022', n_trial_back: int = 15, selected_trial_idx: List | ndarray | None = None, solver: Literal['lbfgs', 'liblinear', 'newton-cg', 'newton-cholesky', 'sag', 'saga'] = 'liblinear', penalty: Literal['l1', 'l2', 'elasticnet', None] = 'l2', Cs: int = 10, cv: int = 5, C: int = 0.1, n_jobs_cross_validation: int = -1, n_bootstrap_iters: int = 1000, n_bootstrap_samplesize: int | None = None, fit_exponential: bool = True, **kwargs) Dict[source]¶
- Fit logistic regression model to choice and reward history.
use cross-validataion to determine the best L2 penality parameter, C
use bootstrap to determine the CI and std
- Parameters:
choice_history (Union[List, np.ndarray]) – Choice history (0 = left choice, 1 = right choice).
reward_history (Union[List, np.ndarray]) – Reward history (0 = unrewarded, 1 = rewarded).
logistic_model (Literal['Su2022', 'Bari2019', 'Hattori2019', 'Miller2021'], optional) – The logistic regression model to use. Defaults to ‘Su2022’. Supported models: ‘Su2022’, ‘Bari2019’, ‘Hattori2019’, ‘Miller2021’.
n_trial_back (int, optional) – Number of trials back into history. Defaults to 15.
selected_trial_idx (Union[List, np.ndarray], optional) – If None, use all trials; else, only look at selected trials for fitting, but using the full history.
solver (str, optional) – Algorithm to fit logistic regression, by default ‘liblinear’ Note that there are some restrictions on the choice of penalty and solver.
penalty (str, optional) – Penalty term for regularization, by default ‘l2’ Note that there are some restrictions on the choice of penalty and solver.
Cs (int, optional) – Number of hyperparameter C to do grid search on, by default 10
cv (int, optional) – Number of folds in cross validation, by default 10 if cv is 1, skip cross validation and use hyperparameter passed in
C (int, optional) – Logistic regression hyperparameter, only used if cv = 1 by default 0.1
n_jobs_cross_validation (int, optional) – Number of CPU cores used during the cross-validation loop, by default -1 (all CPU cores)
n_bootstrap_iters (int, optional) – Number of bootstrap iterations, by default 1000
n_bootstrap_samplesize (Union[int, None], optional) – Number of samples in each bootstrap iteration, by default None (use the same number of samples as the original dataset)
fit_exponential (bool, optional) – Whether to fit an exponential curve to the trial-delayed regression weights by default True (fit the exponential)
- Returns:
A dictionary containing logistic regression results.
- Return type:
Dict
- aind_dynamic_foraging_models.logistic_regression.model.prepare_logistic_design_matrix(choice_history: List | ndarray, reward_history: List | ndarray, logistic_model: Literal['Su2022', 'Bari2019', 'Hattori2019', 'Miller2021'] = 'Su2022', n_trial_back: int = 15, selected_trial_idx: List | ndarray | None = None) DataFrame[source]¶
Prepare logistic regression design matrix from choice and reward history.
- See discussion here:
https://github.com/AllenNeuralDynamics/aind-dynamic-foraging-models/discussions/10
- Parameters:
choice_history (Union[List, np.ndarray]) – Choice history (0 = left choice, 1 = right choice).
reward_history (Union[List, np.ndarray]) – Reward history (0 = unrewarded, 1 = rewarded).
logistic_model (Literal['Su2022', 'Bari2019', 'Hattori2019', 'Miller2021'], optional) – The logistic regression model to use. Defaults to ‘Su2022’. Supported models: ‘Su2022’, ‘Bari2019’, ‘Hattori2019’, ‘Miller2021’.
n_trial_back (int, optional) – Number of trials back into history. Defaults to 15.
selected_trial_idx (Union[List, np.ndarray], optional) – If None, use all trials; else, only look at selected trials for fitting, but using the full history.
- Returns:
df_design –
- A dataframe with index of (trial) and hierachical columns where
df_design.Y: Choice df_design.X: each column represents an independent variable
- Return type:
pd.DataFrame
aind_dynamic_foraging_models.logistic_regression.plot module¶
Plot functions for logistic regression
- aind_dynamic_foraging_models.logistic_regression.plot.plot_logistic_regression(dict_logistic_result, ax=None, ls='-o', alpha=0.3)[source]¶
Plot logistic regression results with the output dictionary from model.fit_logistic_regression
- Parameters:
dict_logistic_result (Dict) – The dictionary output from model.fit_logistic_regression
ax (, optional) – If None, create a new figure and axis, by default None
ls (str, optional) – Line style for the plot, by default ‘-o’
alpha (float, optional) – Transparency of the confidence interval band, by default 0.3
- Returns:
ax – a matplotlib axis
- Return type:
matplotlib.axes.Axes
Module contents¶
Logistic regression models for dynamic foraging