- Scientific Rationale
- Pipeline Overview
- Phase 1: Non-Reversal Tasks
- Phase 2: Reversal Tasks
- How Phases Connect
- State Validation Framework
- Key Insights and Findings
The decision to split the analysis into Phase 1 (non-reversal) and Phase 2 (reversal) is methodologically critical, not arbitrary. Here's why:
In non-reversal tasks (LD 1 choice v2, Punish Incorrect Training):
- The stimulus (correct side) is constant throughout training
- For example, position 8 (left) is always correct
- This means
stimulus_correct_sidehas zero variance - Including it in the GLM would cause:
- Matrix singularity issues
- Unidentifiable parameters
- Model convergence failure
In reversal tasks (LD 1 choice reversal v3, LD Reversal 9):
- The stimulus changes dynamically at reversal points
- Correct side flips from left → right (or vice versa)
- This creates essential variance for the stimulus feature
- The stimulus feature becomes the most important predictor of adaptation
Phase 1 asks: "What behavioral strategies do animals use when stimulus is stable?"
- Focus: Choice biases, perseveration, win-stay/lose-shift patterns
- Goal: Identify stable states (Deliberative, Procedural, Disengaged)
- Output: Strategy profiles independent of stimulus
Phase 2 asks: "How do animals adapt when stimulus contingencies reverse?"
- Focus: Stimulus following, flexibility, adaptation speed
- Goal: Measure reversal learning capacity via state transitions
- Output: Psychometric curves, adaptation trajectories
- Proper statistical modeling: No zero-variance features in GLM
- Targeted feature sets: 7 features (Phase 1) vs 8 features (Phase 2)
- Task-appropriate analyses:
- Phase 1: Strategy states, side biases
- Phase 2: Psychometric curves, reversal transitions
- Cohort-specific protocols:
- W cohort: Rolling 7/8 criterion, multiple reversals
- F cohort: Single reversal per session
┌─────────────────────────────────────────────────────────────────┐
│ RAW DATA INPUT │
│ • W LD Data 11.08 All_processed.csv (W cohort) │
│ • F LD Data 11.08 All_processed.csv (F cohort) │
│ • LDR 2025 data1_processed_withSecondCriterion.csv (reversals) │
└─────────────────────────────────────────────────────────────────┘
↓
┌─────────────────────┴─────────────────────┐
↓ ↓
┌──────────────────┐ ┌──────────────────┐
│ PHASE 1 │ │ PHASE 2 │
│ Non-Reversal │ │ Reversal │
│ Tasks │ │ Tasks │
└──────────────────┘ └──────────────────┘
↓ ↓
┌──────────────────┐ ┌──────────────────┐
│ Task Selection: │ │ Task Selection: │
│ • LD 1 choice v2 │ │ • LD reversal v3 │
│ • PI Training │ │ • LD Reversal 9 │
│ │ │ │
│ Stimulus: FIXED │ │ Stimulus: VARIES │
│ (zero variance) │ │ (essential!) │
└──────────────────┘ └──────────────────┘
↓ ↓
┌──────────────────┐ ┌──────────────────┐
│ Feature Matrix: │ │ Feature Matrix: │
│ 7 features │ │ 8 features │
│ (NO stimulus) │ │ (WITH stimulus) │
└──────────────────┘ └──────────────────┘
↓ ↓
┌──────────────────┐ ┌──────────────────┐
│ Reversal │ │ Reversal │
│ Detection │ │ Detection │
│ (N/A) │ │ + Correction │
└──────────────────┘ └──────────────────┘
↓ ↓
┌──────────────────┐ ┌──────────────────┐
│ GLM-HMM Fitting │ │ GLM-HMM Fitting │
│ 3 states │ │ 3 states │
│ 200 iterations │ │ 200 iterations │
└──────────────────┘ └──────────────────┘
↓ ↓
┌──────────────────┐ ┌──────────────────┐
│ State Validation │ │ State Validation │
│ • Performance │ │ • Performance │
│ • Metrics │ │ • Metrics │
│ • Labels │ │ • Labels │
└──────────────────┘ └──────────────────┘
↓ ↓
┌──────────────────┐ ┌──────────────────┐
│ Outputs: │ │ Outputs: │
│ • GLM weights │ │ • GLM weights │
│ • State metrics │ │ • Psychometric │
│ • Transitions │ │ curves │
│ • Occupancy │ │ • Reversal adapt │
└──────────────────┘ │ • Transitions │
└──────────────────┘
↓ ↓
└─────────────────────┬─────────────────────┘
↓
┌─────────────────────────────┐
│ CROSS-COHORT COMPARISON │
│ • W+ vs F+ (controls) │
│ • Genotype effects │
│ • State distributions │
└─────────────────────────────┘
↓
┌─────────────────────────────┐
│ COMPREHENSIVE ANALYSES │
│ • Mixed models │
│ • ANOVAs │
│ • Learning curves │
│ • RT distributions │
└─────────────────────────────┘
Identify stable behavioral states during learning when stimulus contingencies are fixed.
- LD 1 choice v2: Light discrimination with one correct position
- Punish Incorrect Training: Early training with error penalties
- ✅ Stimulus is constant (e.g., position 8 always correct)
- ✅ Focus on strategy development
- ❌ Cannot compute psychometric curves (no stimulus variance)
- ❌ Cannot measure stimulus-response relationships
design_matrix = [
'bias', # Constant term (intercept)
'prev_choice', # Previous choice (-1=left, +1=right)
'wsls', # Win-stay/lose-shift indicator
'session_progression', # Trial position within session (0 to 1)
'recent_side_bias', # Proportion right in last 10 trials
'task_stage', # Training progression index
'cumulative_experience' # Total trials across all sessions
]
# NOTE: 'stimulus_correct_side' is EXCLUDED (zero variance)- bias: Captures overall preference for left vs right
- prev_choice: Measures perseveration (repeating choices)
- wsls: Quantifies flexible learning (win-stay, lose-shift)
- session_progression: Within-session fatigue or warming up
- recent_side_bias: Short-term spatial biases
- task_stage: Long-term learning trajectory
- cumulative_experience: Overall expertise
Model specification:
P(choice_right | features, state) = logistic(β_state · features)
States: {0, 1, 2}
Transitions: 3×3 matrix (state_i → state_j probabilities)
Observations: Bernoulli (binary choices)
Fitting procedure:
- Algorithm: Expectation-Maximization (EM)
- Iterations: 200
- Regularization: L2 penalty (λ = 1.0)
- Random seed: 42 (reproducibility)
States are validated using multiple behavioral signatures:
- Accuracy: Proportion of correct choices per state
- Latency: Median correct touch latency
- Consistency: Coefficient of variation (CV) in latency
- WSLS ratio: (Win-stay + Lose-shift) / total trials
- Side bias: Deviation from 50% left/right choices
- Perseveration: Correlation with previous choice
Deliberative High-Performance (Engaged):
- Accuracy: ≥ 70%
- Latency CV: ≥ 0.65 (high variability = deliberation)
- WSLS: ≥ 0.6 (flexible learning)
Procedural High-Performance (Engaged):
- Accuracy: ≥ 70%
- Latency CV: < 0.65 (low variability = automaticity)
- Side bias: < 0.3 (balanced)
Disengaged (Lapsed):
- Accuracy: < 70%
- WSLS: < 0.5 (not learning from feedback)
WSLS-Driven (Mixed):
- High WSLS (≥ 0.7) but moderate accuracy (50-70%)
Side-Biased (Lapsed):
- Strong side bias (≥ 0.3)
- Poor accuracy (< 60%)
For each animal:
- GLM weights: (3 states × 7 features) matrix
- Transition matrix: 3×3 state transition probabilities
- State occupancy: Proportion of trials in each state
- Validated labels: Behavioral interpretation of each state
- Posterior probabilities: P(state | trial) for all trials
Measure reversal learning and cognitive flexibility by analyzing adaptation when stimulus contingencies flip.
- W cohort: LD 1 choice reversal v3 (rolling 7/8 criterion)
- F cohort: LD 1 Reversal 9 (single reversal)
- ✅ Stimulus varies (correct side flips at reversals)
- ✅ Can compute psychometric curves
- ✅ Can measure adaptation speed
- ✅ Captures flexibility via state transitions
Critical preprocessing step: Identify when reversals occurred and correct stimulus values.
- First reversal: After 7/8 consecutive correct trials
- Second reversal: After 5/6 correct in 6-trial blocks
- Some animals achieve 2 reversals in one session
- Single reversal: After first criterion achievement
- Usually only one reversal per session
# Initial stimulus (both cohorts start with left=correct)
stimulus_values = [-1, -1, -1, ...] # Position 8 (left) correct
# At first reversal (trial = first_reversal_trial)
stimulus_values[first_reversal_trial:] = +1 # Flip to right correct
# At second reversal (if applicable)
stimulus_values[second_reversal_trial:] = -1 # Flip back to leftWhy this matters: Without correction, GLM would see:
- Pre-reversal: Left choices → correct
- Post-reversal: Left choices → still marked as correct (WRONG!)
With correction:
- Pre-reversal: Left choices → correct
- Post-reversal: Left choices → incorrect (RIGHT!)
design_matrix = [
'stimulus_correct_side', # ⭐ NEW: Corrected stimulus (-1 or +1)
'bias',
'prev_choice',
'wsls',
'session_progression',
'recent_side_bias',
'task_stage',
'cumulative_experience'
]The stimulus feature:
- Most important predictor for reversal learning
- GLM weight on stimulus → "stimulus following strength"
- High weight = strong stimulus-response coupling
- Low weight = ignoring stimulus (lapsed/biased)
- 3 states
- 200 EM iterations
- L2 penalty = 1.0
- Binary logistic regression per state
What: P(choose right | stimulus) for each state
Why valid in Phase 2: Stimulus varies, so we can bin trials by stimulus value
Interpretation:
- Steep curve: Strong stimulus following (good reversal learning)
- Flat curve: Ignoring stimulus (side bias or disengagement)
Example:
Stimulus = Left correct → P(right) should be LOW
Stimulus = Right correct → P(right) should be HIGH
What: State probabilities around reversal points
Shows:
- Pre-reversal: Which state dominates?
- During reversal: Does state change occur?
- Post-reversal: New stable state?
Expected pattern:
- Flexible animals: Deliberative → Procedural → Deliberative (at reversal) → Procedural
- Inflexible animals: Stay in Disengaged or Side-Biased states
Trials to criterion: How many trials to reach performance threshold post-reversal?
Perseveration errors: How many trials does the animal continue with the old rule?
Post-reversal accuracy trajectory: Learning curve after each reversal
For each animal:
- All Phase 1 outputs (weights, transitions, occupancy, labels)
- Psychometric curves: P(right) vs stimulus for each state
- Reversal transitions: State probabilities ±20 trials around reversals
- Adaptation metrics:
- Trials to criterion
- Perseveration errors
- Post-reversal learning curves
| Aspect | Phase 1 | Phase 2 |
|---|---|---|
| Task type | Non-reversal (stable) | Reversal (dynamic) |
| Stimulus variance | Zero (excluded) | High (essential) |
| Primary question | What strategies? | How flexible? |
| State focus | Strategy states | Adaptation states |
| Key output | GLM weights on bias/WSLS | GLM weight on stimulus |
| Behavioral metric | Accuracy, WSLS, side bias | Reversal adaptation speed |
A complete animal profile includes:
-
Phase 1 states → Baseline learning strategies
- Deliberative: Thoughtful, flexible
- Procedural: Efficient, automatic
- Disengaged: Low motivation or confusion
-
Phase 2 adaptation → Cognitive flexibility
- Fast adaptation: Switches states at reversal
- Slow adaptation: Perseverates in old strategy
- No adaptation: Stays in Side-Biased/Disengaged
Example interpretations:
Animal A (WT control):
- Phase 1: Procedural state (70% accuracy, low latency CV)
- Phase 2: Fast adaptation (15 trials to criterion), strong stimulus following
Animal B (-/- knockout):
- Phase 1: Side-Biased state (80% right choices)
- Phase 2: No adaptation (perseverates on old side), flat psychometric curve
Consistency check: Do state labels agree across phases?
- Engaged in Phase 1 → Should show fast adaptation in Phase 2
- Lapsed in Phase 1 → Should show slow/no adaptation in Phase 2
If inconsistent: Possible explanations:
- Task-specific deficits
- Motivational changes
- Learning over time (Phase 1 → Phase 2)
GLM-HMM produces numeric states (0, 1, 2) without inherent meaning. We must:
- Assign behavioral labels based on empirical signatures
- Validate labels are consistent across animals
- Ensure labels are interpretable for neuroscience
state_metrics = compute_comprehensive_state_metrics(trial_df, model, metadata)
trajectory_df = compute_performance_trajectory(trial_df, model)
validated_labels = validate_state_labels(state_metrics, trajectory_df)For each state in each animal:
- Accuracy: % correct trials
- Median latency: Response time (ms)
- Latency CV: Coefficient of variation
- WSLS ratio: (Win-stay + Lose-shift) / total
- Side bias: |P(right) - 0.5|
- Perseveration: Autocorrelation of choices
- Bin trials by session or trial block
- Compute rolling average of accuracy per state
- Identify: Improving, Stable, or Declining performance
Decision tree:
IF accuracy ≥ 70%:
IF latency_CV ≥ 0.65:
→ Deliberative High-Performance
ELSE:
→ Procedural High-Performance
ELIF WSLS ≥ 0.7:
→ WSLS-Driven (flexible but not accurate yet)
ELIF side_bias ≥ 0.3:
→ Side-Biased (lapsed)
ELSE:
→ Disengaged (lapsed)
Each label gets a confidence score:
- High: Clear separation in metrics
- Medium: Some ambiguity
- Low: Borderline between categories
Map detailed labels → broad categories:
- Engaged: {Deliberative HP, Procedural HP}
- Lapsed: {Disengaged, Side-Biased}
- Mixed: {WSLS-Driven, Transition states}
Methodological necessity:
- Cannot include zero-variance features in GLM
- Different tasks require different feature sets
- Enables task-appropriate analyses (psychometric curves only in Phase 2)
Scientific value:
- Separates strategy (Phase 1) from flexibility (Phase 2)
- Allows targeted hypotheses for each cognitive domain
Not pre-defined:
- GLM-HMM discovers states from data
- Validation assigns labels based on behavioral signatures
- Labels may differ across animals (heterogeneity)
Consistent patterns emerge:
- Most animals show 2-3 interpretable states
- WT controls: predominantly Engaged states
- Knockouts: more Lapsed states (especially -/-)
State occupancy:
- -/- animals spend more time in Side-Biased state
Adaptation speed:
- -/- animals show slower reversal learning
GLM weights:
- -/- animals have lower stimulus weights (less stimulus following)
Broad categories (Engaged/Lapsed/Mixed):
- Clear group-level patterns
- Statistical power for genotype comparisons
Detailed labels (Deliberative/Procedural/Disengaged/etc.):
- Individual animal heterogeneity
- Mechanistic insights
Multi-metric approach:
- No single metric defines a state
- Convergent evidence from accuracy, latency, WSLS, side bias
Transparency:
- All thresholds documented
- Confidence scores provided
- Evidence listed for each label
# Phase 1: Non-reversal tasks (W and F cohorts)
python run_phase1_analysis.py
# Phase 2: Reversal tasks (W and F cohorts)
python run_phase2_analysis.py
# Cross-cohort comparison
python create_cross_cohort_comparison.py
# Comprehensive statistical analyses
python run_comprehensive_analyses.py- Phase 1: ~10-15 minutes per cohort (20-30 animals)
- Phase 2: ~15-20 minutes per cohort (reversal detection adds time)
- Cross-cohort: ~5 minutes
- Total: ~45-60 minutes for complete pipeline
results/
├── phase1_nonreversal/
│ ├── figures/
│ │ ├── {animal_id}_cohort{W/F}/
│ │ │ ├── {animal}_weights.png
│ │ │ ├── {animal}_transitions.png
│ │ │ └── {animal}_occupancy.png
│ ├── cohort_W_phase1_summary.txt
│ ├── cohort_F_phase1_summary.txt
│ └── {animal}_cohort{W/F}_phase1_model.pkl
│
├── phase2_reversal/
│ ├── figures/
│ │ ├── {animal_id}_cohort{W/F}/
│ │ │ ├── {animal}_weights.png
│ │ │ ├── {animal}_psychometric.png
│ │ │ ├── {animal}_reversal_transitions.png
│ │ │ └── {animal}_adaptation.png
│ ├── cohort_W_phase2_summary.txt
│ ├── cohort_F_phase2_summary.txt
│ └── {animal}_cohort{W/F}_phase2_model.pkl
│
└── cross_cohort/
├── cross_cohort_comparison_phase1.png
├── cross_cohort_comparison_phase2.png
└── genotype_summary_statistics.txt
This two-phase GLM-HMM pipeline provides:
- Statistically rigorous models (no zero-variance features)
- Task-appropriate analyses (psychometric curves only where valid)
- Comprehensive behavioral profiling (strategy + flexibility)
- Validated state labels (multi-metric convergent evidence)
- Interpretable results (broad categories + detailed labels)
The design choices are methodologically necessary and scientifically valuable, enabling deep insights into how genotype affects both stable learning strategies (Phase 1) and cognitive flexibility (Phase 2).
Authors: Claude (Anthropic AI) Date: November 2025 Version: 1.0