Bayesian LDA (RxLDA) - Bayesian Linear Discriminant Analysis
API Name:RxLDAModelMathematical Model: Pooled Gaussian Classifier (PGC) Bayesian LDA (also known as RxLDA in the codebase) is a Bayesian classification model with uncertainty quantification, implemented using RxInfer.jl’s reactive message passing framework. It uses a shared precision matrix across all classes, making it fast and efficient for BCI classification.
Bayesian LDA (RxLDA) is currently implemented in NimbusSDK.jl and ready for use in production BCI applications. LDA is widely recognized in the BCI community, and “Bayesian” signals our uncertainty quantification capabilities.
Overview
Bayesian LDA extends traditional Linear Discriminant Analysis with full Bayesian inference, providing:- ✅ Posterior probability distributions (not just point estimates)
- ✅ Uncertainty quantification for each prediction
- ✅ Probabilistic confidence scores
- ✅ Fast inference (<20ms per trial)
- ✅ Training and calibration support
- ✅ Batch and streaming inference modes
When to Use Bayesian LDA
Bayesian LDA is ideal for:- Motor Imagery classification (2-4 classes)
- Well-separated class distributions
- Fast inference requirements (<20ms)
- Interpretable results for medical applications
- When classes have similar covariance structures
- Classes have significantly different covariance structures
- Complex, overlapping distributions
- Need more flexibility in modeling per-class variances
Model Architecture
Mathematical Foundation (Pooled Gaussian Classifier)
Bayesian LDA implements a Pooled Gaussian Classifier (PGC), which models class-conditional distributions with a shared precision matrix:μ_k= mean vector for class k (learned from data)W= shared precision matrix (same for all classes)- Assumes classes have similar covariance structure
Model Structure
RxInfer Implementation
The Bayesian LDA model uses RxInfer.jl for variational Bayesian inference: Learning Phase:Usage
1. Load Pre-trained Model
2. Train Custom Model
iterations: Number of variational inference iterations (default: 50)- More iterations = better convergence but slower training
- 50-100 is typically sufficient
showprogress: Display progress bar during trainingname: Model identifierdescription: Model description for documentation
3. Subject-Specific Calibration
Fine-tune a pre-trained model with subject-specific data (much faster than training from scratch):- Requires only 10-20 trials per class (vs 50-100 for training from scratch)
- Faster: 20 iterations vs 50-100
- Better generalization: Uses pre-trained model as prior
- Typical accuracy improvement: 5-15% over generic model
4. Batch Inference
Process multiple trials efficiently:5. Streaming Inference
Real-time chunk-by-chunk processing:Training Requirements
Data Requirements
- Minimum: 40 trials per class (160 total for 4-class)
- Recommended: 80+ trials per class (320+ total for 4-class)
- For calibration: 10-20 trials per class sufficient
Bayesian LDA requires at least 2 observations to estimate class statistics and shared precision matrix. Single-trial training will raise an error.
Feature Requirements
Bayesian LDA expects preprocessed features, not raw EEG: ✅ Required preprocessing:- Bandpass filtering (8-30 Hz for motor imagery)
- Artifact removal (ICA recommended)
- Spatial filtering (CSP for motor imagery)
- Feature extraction (log-variance for CSP features)
- Raw EEG channels
- Unfiltered data
- Non-extracted features
Performance Characteristics
Computational Performance
| Operation | Latency | Notes |
|---|---|---|
| Training | 10-30 seconds | 50 iterations, 100 trials per class |
| Calibration | 5-15 seconds | 20 iterations, 20 trials per class |
| Batch Inference | 10-20ms per trial | 10 iterations |
| Streaming Chunk | 10-20ms | 10 iterations per chunk |
Classification Accuracy
| Paradigm | Classes | Typical Accuracy | ITR |
|---|---|---|---|
| Motor Imagery | 2 (L/R hand) | 75-90% | 15-25 bits/min |
| Motor Imagery | 4 (L/R/Feet/Tongue) | 70-85% | 20-35 bits/min |
| P300 | 2 (Target/Non-target) | 80-95% | 25-40 bits/min |
Accuracy is highly subject-dependent. Subject-specific calibration typically improves accuracy by 5-15%.
Model Inspection
View Model Parameters
Compare Models
Advantages & Limitations
Advantages
✅ Fast Training: Shared covariance estimation is efficient✅ Fast Inference: Analytical posterior computation (<20ms)
✅ Interpretable: Clear probabilistic formulation
✅ Memory Efficient: Single shared precision matrix
✅ Robust: Handles uncertainty naturally via Bayesian inference
✅ Production-Ready: Battle-tested in real BCI applications
Limitations
❌ Shared Covariance Assumption: May not fit well if classes have very different spreads❌ Linear Decision Boundary: Cannot capture non-linear class boundaries
❌ Gaussian Assumption: Assumes normal class distributions
❌ Not Ideal for Overlapping Classes: Use RxGMM for complex distributions
Comparison: Bayesian LDA vs Bayesian GMM
| Aspect | Bayesian LDA (RxLDA) | Bayesian GMM (RxGMM) |
|---|---|---|
| Precision Matrix | Shared across all classes | Class-specific |
| Mathematical Model | Pooled Gaussian Classifier (PGC) | Heteroscedastic Gaussian Classifier (HGC) |
| Training Speed | Faster | Slower |
| Inference Speed | Faster (~10-15ms) | Slightly slower (~15-20ms) |
| Flexibility | Less flexible | More flexible |
| Best For | Well-separated classes | Overlapping/complex distributions |
| Memory | Lower | Higher |
| Parameters | Fewer (n_classes means + 1 precision) | More (n_classes means + precisions) |
Next Steps
Bayesian GMM (RxGMM)
More flexible Gaussian Mixture Model with class-specific covariances
Training Tutorial
Complete training walkthrough
Julia SDK
Full SDK reference
Code Examples
Working examples
References
Implementation:- RxInfer.jl: https://rxinfer.com/
- Source code:
/src/models/rxlda/in NimbusSDK.jl
- Fisher, R. A. (1936). “The use of multiple measurements in taxonomic problems”
- Bishop, C. M. (2006). “Pattern Recognition and Machine Learning” (Chapter 4)
- Pooled Gaussian Classifier (PGC) with shared covariance structure
- Blankertz et al. (2008). “Optimizing spatial filters for robust EEG single-trial analysis”
- Lotte et al. (2018). “A review of classification algorithms for EEG-based BCI”