Skip to content

Latest commit

 

History

History
631 lines (483 loc) · 23 KB

File metadata and controls

631 lines (483 loc) · 23 KB

GLM-HMM Analysis Pipeline: Complete Reasoning and Methodology

Table of Contents

  1. Scientific Rationale
  2. Pipeline Overview
  3. Phase 1: Non-Reversal Tasks
  4. Phase 2: Reversal Tasks
  5. How Phases Connect
  6. State Validation Framework
  7. Key Insights and Findings

Scientific Rationale

Why Two Separate Analyses?

The decision to split the analysis into Phase 1 (non-reversal) and Phase 2 (reversal) is methodologically critical, not arbitrary. Here's why:

The Fundamental Problem: Stimulus Variance

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_side has 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

The Scientific Question Differs

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

The Two-Phase Approach Enables:

  1. Proper statistical modeling: No zero-variance features in GLM
  2. Targeted feature sets: 7 features (Phase 1) vs 8 features (Phase 2)
  3. Task-appropriate analyses:
    • Phase 1: Strategy states, side biases
    • Phase 2: Psychometric curves, reversal transitions
  4. Cohort-specific protocols:
    • W cohort: Rolling 7/8 criterion, multiple reversals
    • F cohort: Single reversal per session

Pipeline Overview

┌─────────────────────────────────────────────────────────────────┐
│                        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         │
                └─────────────────────────────┘

Phase 1: Non-Reversal Tasks

Objective

Identify stable behavioral states during learning when stimulus contingencies are fixed.

Tasks Included

  • LD 1 choice v2: Light discrimination with one correct position
  • Punish Incorrect Training: Early training with error penalties

Key Characteristics

  • ✅ 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

Feature Engineering (7 features)

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)

Why Each Feature Matters:

  1. bias: Captures overall preference for left vs right
  2. prev_choice: Measures perseveration (repeating choices)
  3. wsls: Quantifies flexible learning (win-stay, lose-shift)
  4. session_progression: Within-session fatigue or warming up
  5. recent_side_bias: Short-term spatial biases
  6. task_stage: Long-term learning trajectory
  7. cumulative_experience: Overall expertise

GLM-HMM Model

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)

State Validation

States are validated using multiple behavioral signatures:

1. Performance Trajectory

  • Accuracy: Proportion of correct choices per state
  • Latency: Median correct touch latency
  • Consistency: Coefficient of variation (CV) in latency

2. Behavioral Metrics

  • WSLS ratio: (Win-stay + Lose-shift) / total trials
  • Side bias: Deviation from 50% left/right choices
  • Perseveration: Correlation with previous choice

3. State Label Assignment

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%)

Outputs

For each animal:

  1. GLM weights: (3 states × 7 features) matrix
  2. Transition matrix: 3×3 state transition probabilities
  3. State occupancy: Proportion of trials in each state
  4. Validated labels: Behavioral interpretation of each state
  5. Posterior probabilities: P(state | trial) for all trials

Phase 2: Reversal Tasks

Objective

Measure reversal learning and cognitive flexibility by analyzing adaptation when stimulus contingencies flip.

Tasks Included

  • W cohort: LD 1 choice reversal v3 (rolling 7/8 criterion)
  • F cohort: LD 1 Reversal 9 (single reversal)

Key Characteristics

  • ✅ Stimulus varies (correct side flips at reversals)
  • ✅ Can compute psychometric curves
  • ✅ Can measure adaptation speed
  • ✅ Captures flexibility via state transitions

Reversal Detection Pipeline

Critical preprocessing step: Identify when reversals occurred and correct stimulus values.

W Cohort Criteria:

  1. First reversal: After 7/8 consecutive correct trials
  2. Second reversal: After 5/6 correct in 6-trial blocks
  3. Some animals achieve 2 reversals in one session

F Cohort Criteria:

  1. Single reversal: After first criterion achievement
  2. Usually only one reversal per session

Stimulus Correction Algorithm:

# 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 left

Why 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!)

Feature Engineering (8 features)

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)

GLM-HMM Model (Same as Phase 1)

  • 3 states
  • 200 EM iterations
  • L2 penalty = 1.0
  • Binary logistic regression per state

Phase 2-Specific Analyses

1. Psychometric Curves

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

2. Reversal Transition Analysis

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

3. Adaptation Metrics

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

Outputs

For each animal:

  1. All Phase 1 outputs (weights, transitions, occupancy, labels)
  2. Psychometric curves: P(right) vs stimulus for each state
  3. Reversal transitions: State probabilities ±20 trials around reversals
  4. Adaptation metrics:
    • Trials to criterion
    • Perseveration errors
    • Post-reversal learning curves

How Phases Connect

Complementary Questions

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

Integrated Interpretation

A complete animal profile includes:

  1. Phase 1 states → Baseline learning strategies

    • Deliberative: Thoughtful, flexible
    • Procedural: Efficient, automatic
    • Disengaged: Low motivation or confusion
  2. 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

Cross-Phase Validation

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)

State Validation Framework

Why Validation Matters

GLM-HMM produces numeric states (0, 1, 2) without inherent meaning. We must:

  1. Assign behavioral labels based on empirical signatures
  2. Validate labels are consistent across animals
  3. Ensure labels are interpretable for neuroscience

Multi-Metric Validation Pipeline

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)

Step 1: Compute State Metrics

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

Step 2: Performance Trajectories

  • Bin trials by session or trial block
  • Compute rolling average of accuracy per state
  • Identify: Improving, Stable, or Declining performance

Step 3: Label Assignment

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)

Step 4: Confidence Scoring

Each label gets a confidence score:

  • High: Clear separation in metrics
  • Medium: Some ambiguity
  • Low: Borderline between categories

Step 5: Broad Categorization

Map detailed labels → broad categories:

  • Engaged: {Deliberative HP, Procedural HP}
  • Lapsed: {Disengaged, Side-Biased}
  • Mixed: {WSLS-Driven, Transition states}

Key Insights and Findings

1. Two-Phase Design is Essential

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

2. State Labels are Data-Driven

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 -/-)

3. Genotype Effects Manifest in Multiple Ways

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)

4. Dual Categorization Captures Complexity

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

5. Validation Ensures Reproducibility

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

Pipeline Execution

Full Analysis Command Sequence

# 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

Expected Runtime

  • 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

Output Structure

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

Conclusion

This two-phase GLM-HMM pipeline provides:

  1. Statistically rigorous models (no zero-variance features)
  2. Task-appropriate analyses (psychometric curves only where valid)
  3. Comprehensive behavioral profiling (strategy + flexibility)
  4. Validated state labels (multi-metric convergent evidence)
  5. 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