Bayesian decoding and the Kalman filter
A decoder as an estimator that blends what it expected with what it measured, weighted by how much it trusts each. The population vector, the Kalman filter, and why intracortical cursor control works.
On this page16 min
A classifierClassifier An algorithm that assigns each trial a label, such as left hand or right hand, after being trained on labelled examples. Related: Linear discriminant analysis (LDA)Linear discriminant analysis (LDA) A simple, robust classifier that draws a straight boundary between classes; the workhorse of EEG decoding for two decades. , Support vector machine (SVM)Support vector machine (SVM) A classifier that finds the boundary with the widest margin between classes; strong on small datasets. , Cross-validationCross-validation Testing a model on data it never saw during training, repeatedly, so the reported accuracy means something. . Glossary entry picks one of a few labels. A cursor decoderDecoder The algorithm that maps neural signals to intended actions, such as a classifier or a Kalman filter. Related: ClassifierClassifier An algorithm that assigns each trial a label, such as left hand or right hand, after being trained on labelled examples. , Kalman filterKalman filter An estimator that blends a prediction from a motion model with a noisy measurement, weighting each by how much it trusts it; the decoder behind most intracortical cursor control. , Brain-computer interface (BCI)Brain-computer interface (BCI) A system that turns measured brain activity into control of something outside the body, from a cursor to a wheelchair to a speech synthesizer. . Glossary entry has to produce a continuous position, sixty times a second, from neural activity that is noisy and only loosely related to movement. The trick that makes this work is to stop asking “what does the neural data say right now” and start asking “given what I believed a moment ago and what I just measured, what should I believe now.” That is Bayesian estimationBayesian estimation Updating a belief about a hidden quantity by combining what you expected with what you measured, in proportion to their reliabilities. Related: Kalman filterKalman filter An estimator that blends a prediction from a motion model with a noisy measurement, weighting each by how much it trusts it; the decoder behind most intracortical cursor control.. Glossary entry, the Kalman filterKalman filter An estimator that blends a prediction from a motion model with a noisy measurement, weighting each by how much it trusts it; the decoder behind most intracortical cursor control. Related: DecoderDecoder The algorithm that maps neural signals to intended actions, such as a classifier or a Kalman filter., Bayesian estimationBayesian estimation Updating a belief about a hidden quantity by combining what you expected with what you measured, in proportion to their reliabilities.. Glossary entry is its simplest form, and it has been the decoderDecoder The algorithm that maps neural signals to intended actions, such as a classifier or a Kalman filter. Related: ClassifierClassifier An algorithm that assigns each trial a label, such as left hand or right hand, after being trained on labelled examples. , Kalman filterKalman filter An estimator that blends a prediction from a motion model with a noisy measurement, weighting each by how much it trusts it; the decoder behind most intracortical cursor control. , Brain-computer interface (BCI)Brain-computer interface (BCI) A system that turns measured brain activity into control of something outside the body, from a cursor to a wheelchair to a speech synthesizer. . Glossary entry behind most intracortical cursor control since the early 2000s.
- Raise the observation noise. The naive path wanders off; the Kalman path smooths but lags.
- Lower the process noise Q. The filterFilter An operation that passes some frequencies and attenuates others; every filter also delays or distorts the timing of what it passes. Related: High-pass filterHigh-pass filter Removes slow drifts and DC offsets, passing everything above a cutoff; set it too high and you erase slow brain activity. , Low-pass filterLow-pass filter Removes fast content above a cutoff; the anti-alias filter is one, and so is the smoothing you apply to a noisy trace. , Notch filterNotch filter A very narrow filter that removes one frequency, usually 60 Hz mains hum; it also rings and distorts nearby content, so fix the hum at the source first. , Phase distortionPhase distortion Different frequencies being delayed by different amounts, which smears and shifts waveforms such as an ERP peak. . Glossary entry trusts its own prediction more: smoother, slower to turn.
- Raise Q. It trusts observations more: faster, noisier. There is a sweet spot, and it depends on the noise.
First, the population vector
Motor cortex neurons fire more for movements in a preferred direction, following roughly a cosine of the angle between the movement and that direction. Georgopoulos in the 1980s showed that if you take each neuron’s preferred direction, weight it by how fast the neuron is firing now, and add the vectors up, the sum points where the arm is about to move. That population vectorPopulation vector A way to read out intended movement direction by adding up the preferred directions of many motor cortex neurons, weighted by how fast each is firing. Related: DecoderDecoder The algorithm that maps neural signals to intended actions, such as a classifier or a Kalman filter., Rate codeRate code The idea that a neuron carries information in how many spikes per second it fires.. Glossary entry was the first real neural decoderDecoder The algorithm that maps neural signals to intended actions, such as a classifier or a Kalman filter. Related: ClassifierClassifier An algorithm that assigns each trial a label, such as left hand or right hand, after being trained on labelled examples. , Kalman filterKalman filter An estimator that blends a prediction from a motion model with a noisy measurement, weighting each by how much it trusts it; the decoder behind most intracortical cursor control. , Brain-computer interface (BCI)Brain-computer interface (BCI) A system that turns measured brain activity into control of something outside the body, from a cursor to a wheelchair to a speech synthesizer. . Glossary entry, and it is a snapshot method: each moment’s estimate uses only that moment’s firing.
Each neuron’s rate follows a cosine of the movement direction:
Here is the firing rate of neuron , its baseline, its modulation depth, the movement direction, and the neuron’s preferred direction. The population vectorPopulation vector A way to read out intended movement direction by adding up the preferred directions of many motor cortex neurons, weighted by how fast each is firing. Related: DecoderDecoder The algorithm that maps neural signals to intended actions, such as a classifier or a Kalman filter. , Rate codeRate code The idea that a neuron carries information in how many spikes per second it fires. . Glossary entry weights each preferred direction by its rate:
Here is the unit vector for neuron ’s preferred direction and estimates the movement vector.
Why snapshots are not enough
SpikeSpike Shorthand for an action potential as seen by an electrode, a brief blip about a millisecond wide. Related: Action potentialAction potential The brief, all-or-nothing voltage spike a neuron fires, about a millisecond long and about 100 millivolts tall across its membrane. , Spike sortingSpike sorting Deciding which spikes on an electrode came from which neuron, by clustering their waveform shapes. , Extracellular recordingExtracellular recording Recording with an electrode near neurons rather than inside one; you see small, fast blips from nearby cells rather than the full membrane swing. . Glossary entry counts in a 50 ms bin are noisy: a neuron firing at 20 per second gives one spikeSpike Shorthand for an action potential as seen by an electrode, a brief blip about a millisecond wide. Related: Action potentialAction potential The brief, all-or-nothing voltage spike a neuron fires, about a millisecond long and about 100 millivolts tall across its membrane. , Spike sortingSpike sorting Deciding which spikes on an electrode came from which neuron, by clustering their waveform shapes. , Extracellular recordingExtracellular recording Recording with an electrode near neurons rather than inside one; you see small, fast blips from nearby cells rather than the full membrane swing. . Glossary entry per bin on average, so the count is zero or one or two by chance. A snapshot decoderDecoder The algorithm that maps neural signals to intended actions, such as a classifier or a Kalman filter. Related: ClassifierClassifier An algorithm that assigns each trial a label, such as left hand or right hand, after being trained on labelled examples. , Kalman filterKalman filter An estimator that blends a prediction from a motion model with a noisy measurement, weighting each by how much it trusts it; the decoder behind most intracortical cursor control. , Brain-computer interface (BCI)Brain-computer interface (BCI) A system that turns measured brain activity into control of something outside the body, from a cursor to a wheelchair to a speech synthesizer. . Glossary entry inherits all of that noise, and a cursor driven by it jitters. You could average over longer bins, but then the cursor lags. What you want is to use the past without being stuck in it.
The Kalman filter in two lines
Keep a belief about the state (position and velocity) and an uncertainty about it. Each time step:
In probability terms, each step multiplies the prior belief by the likelihood of the new measurement:
Here is the cursor state, is the neural observation, is what was believed before, and is the updated belief.
Predict. Move the belief forward with a model of how cursors move: position advances by velocity, velocity mostly persists. The uncertainty grows, by an amount Q that says how unpredictable movement is.
Update. Take the new neural observation, which the model says should relate linearly to velocity with noise R. Compare it to what the prediction said the observation should be. Move the belief toward the observation by a fraction called the Kalman gainGain How many times an amplifier multiplies its input; 1000× turns 50 microvolts into 50 millivolts, enough for an ADC to see. Related: Instrumentation amplifierInstrumentation amplifier A precision differential amplifier with very high input impedance and high CMRR, built from three op-amps and sold as a single chip such as the INA128 or AD8232. . Glossary entry, which is large when the prediction is uncertain and the observation is reliable, and small when the reverse. The uncertainty shrinks.
That is the whole algorithm. The gainGain How many times an amplifier multiplies its input; 1000× turns 50 microvolts into 50 millivolts, enough for an ADC to see. Related: Instrumentation amplifierInstrumentation amplifier A precision differential amplifier with very high input impedance and high CMRR, built from three op-amps and sold as a single chip such as the INA128 or AD8232. . Glossary entry is computed from Q and R, so you never hand-tune a smoothing constant; you state how noisy the world and the sensors are and the optimal blend follows. In Figure 1 the sliders are Q and R.
A participant's neurons become noisier over the course of a session. If R is fixed, what happens to the cursor?
Jittery. The filterFilter An operation that passes some frequencies and attenuates others; every filter also delays or distorts the timing of what it passes. Related: High-pass filterHigh-pass filter Removes slow drifts and DC offsets, passing everything above a cutoff; set it too high and you erase slow brain activity. , Low-pass filterLow-pass filter Removes fast content above a cutoff; the anti-alias filter is one, and so is the smoothing you apply to a noisy trace. , Notch filterNotch filter A very narrow filter that removes one frequency, usually 60 Hz mains hum; it also rings and distorts nearby content, so fix the hum at the source first. , Phase distortionPhase distortion Different frequencies being delayed by different amounts, which smears and shifts waveforms such as an ERP peak. . Glossary entry’s trust in observations is set by R. If the real noise rises but R does not, the filterFilter An operation that passes some frequencies and attenuates others; every filter also delays or distorts the timing of what it passes. Related: High-pass filterHigh-pass filter Removes slow drifts and DC offsets, passing everything above a cutoff; set it too high and you erase slow brain activity. , Low-pass filterLow-pass filter Removes fast content above a cutoff; the anti-alias filter is one, and so is the smoothing you apply to a noisy trace. , Notch filterNotch filter A very narrow filter that removes one frequency, usually 60 Hz mains hum; it also rings and distorts nearby content, so fix the hum at the source first. , Phase distortionPhase distortion Different frequencies being delayed by different amounts, which smears and shifts waveforms such as an ERP peak. . Glossary entry keeps weighting noisy observations as if they were clean. Real systems re-estimate R periodically, or adapt it online, which is one form of the “closed-loop decoderDecoder The algorithm that maps neural signals to intended actions, such as a classifier or a Kalman filter. Related: ClassifierClassifier An algorithm that assigns each trial a label, such as left hand or right hand, after being trained on labelled examples. , Kalman filterKalman filter An estimator that blends a prediction from a motion model with a noisy measurement, weighting each by how much it trusts it; the decoder behind most intracortical cursor control. , Brain-computer interface (BCI)Brain-computer interface (BCI) A system that turns measured brain activity into control of something outside the body, from a cursor to a wheelchair to a speech synthesizer. . Glossary entry adaptation” that made intracortical BCIsBrain-computer interface (BCI) A system that turns measured brain activity into control of something outside the body, from a cursor to a wheelchair to a speech synthesizer. Related: DecoderDecoder The algorithm that maps neural signals to intended actions, such as a classifier or a Kalman filter. , Closed loopClosed loop A system in which the output feeds back to influence the input, such as a user adjusting their brain activity while watching the cursor it controls. . Glossary entry practical.
Fitting it
From a calibrationCalibration The session at the start of BCI use in which the decoder is trained on this person, today, because brains and electrodes drift. Related: Transfer learningTransfer learning Using data from other people or other days to shorten calibration for a new user. . Glossary entry block where the participant watches or attempts movements with known velocity: fit the observation model (a linear regression from velocity to each neuron’s firing rate) and estimate R from its residuals; estimate the movement model and Q from the kinematics. Twenty minutes of data suffices. Then run it. The BrainGate group’s refinement, ReFIT, refits the model using the participant’s intended direction (toward the target) rather than the cursor’s actual path, and roughly doubles performance; it is the single most important practical trick in the field.
Why it works for intracortical and less for EEG
Intracortical arrays give a hundred channels of spikesSpike Shorthand for an action potential as seen by an electrode, a brief blip about a millisecond wide. Related: Action potentialAction potential The brief, all-or-nothing voltage spike a neuron fires, about a millisecond long and about 100 millivolts tall across its membrane. , Spike sortingSpike sorting Deciding which spikes on an electrode came from which neuron, by clustering their waveform shapes. , Extracellular recordingExtracellular recording Recording with an electrode near neurons rather than inside one; you see small, fast blips from nearby cells rather than the full membrane swing. . Glossary entry, each a reasonably direct report of a few neurons’ intent about movement direction. The linear model between velocity and firing holds well enough. EEGElectroencephalography (EEG) Recording the brain's electrical activity from electrodes on the scalp, a few tens of microvolts of blurred, summed cortical activity. Related: Electrocorticography (ECoG)Electrocorticography (ECoG) Recording from electrodes placed on the surface of the brain under the skull, usually during epilepsy surgery evaluation; cleaner and sharper than EEG. , Magnetoencephalography (MEG)Magnetoencephalography (MEG) Recording the tiny magnetic fields produced by cortical currents with superconducting sensors in a shielded room; less blurred by the skull than EEG, and far more expensive. , Volume conductionVolume conduction The spreading of currents from a source through the conductive tissues of the head, which is why one source shows up on many electrodes and why scalp EEG is blurry. , Microvolt (µV)Microvolt (µV) One millionth of a volt. Scalp EEG is tens of microvolts; a AA battery is 1.5 million of them. . Glossary entry gives a few tens of channels of blurred oscillatory power whose relation to continuous movement is weak, so continuous EEGElectroencephalography (EEG) Recording the brain's electrical activity from electrodes on the scalp, a few tens of microvolts of blurred, summed cortical activity. Related: Electrocorticography (ECoG)Electrocorticography (ECoG) Recording from electrodes placed on the surface of the brain under the skull, usually during epilepsy surgery evaluation; cleaner and sharper than EEG. , Magnetoencephalography (MEG)Magnetoencephalography (MEG) Recording the tiny magnetic fields produced by cortical currents with superconducting sensors in a shielded room; less blurred by the skull than EEG, and far more expensive. , Volume conductionVolume conduction The spreading of currents from a source through the conductive tissues of the head, which is why one source shows up on many electrodes and why scalp EEG is blurry. , Microvolt (µV)Microvolt (µV) One millionth of a volt. Scalp EEG is tens of microvolts; a AA battery is 1.5 million of them. . Glossary entry decoding gives correlations of 0.3 to 0.5 rather than 0.8 to 0.9. The Kalman machinery is the same; the signal is not.
Deep dive The equations 5 min
State x (position, velocity), transition A, process noise Q. Observation z (firing rates), observation matrix H, observation noise R.
Predict moves the belief through the movement model and grows the uncertainty:
The gainGain How many times an amplifier multiplies its input; 1000× turns 50 microvolts into 50 millivolts, enough for an ADC to see. Related: Instrumentation amplifierInstrumentation amplifier A precision differential amplifier with very high input impedance and high CMRR, built from three op-amps and sold as a single chip such as the INA128 or AD8232. . Glossary entry weights the prediction against the observation:
Update blends the observation in and shrinks the uncertainty:
P is the belief’s covariance. Everything in Figure 1 is these five lines in JavaScript. For a hundred neurons, H is 100 × 4 and R is 100 × 100, and the matrix inverse is the whole computational cost, which a laptop does thousands of times a second.
Deep dive Beyond linear: what the current decoders do 3 min
Firing rates are not Gaussian and not linear in velocity. Extensions: a Poisson observation model (the point-process filterFilter An operation that passes some frequencies and attenuates others; every filter also delays or distorts the timing of what it passes. Related: High-pass filterHigh-pass filter Removes slow drifts and DC offsets, passing everything above a cutoff; set it too high and you erase slow brain activity. , Low-pass filterLow-pass filter Removes fast content above a cutoff; the anti-alias filter is one, and so is the smoothing you apply to a noisy trace. , Notch filterNotch filter A very narrow filter that removes one frequency, usually 60 Hz mains hum; it also rings and distorts nearby content, so fix the hum at the source first. , Phase distortionPhase distortion Different frequencies being delayed by different amounts, which smears and shifts waveforms such as an ERP peak. . Glossary entry), an unscented or particle filterFilter An operation that passes some frequencies and attenuates others; every filter also delays or distorts the timing of what it passes. Related: High-pass filterHigh-pass filter Removes slow drifts and DC offsets, passing everything above a cutoff; set it too high and you erase slow brain activity. , Low-pass filterLow-pass filter Removes fast content above a cutoff; the anti-alias filter is one, and so is the smoothing you apply to a noisy trace. , Notch filterNotch filter A very narrow filter that removes one frequency, usually 60 Hz mains hum; it also rings and distorts nearby content, so fix the hum at the source first. , Phase distortionPhase distortion Different frequencies being delayed by different amounts, which smears and shifts waveforms such as an ERP peak. . Glossary entry for nonlinearities, and, since about 2020, recurrent neural networks trained end to end from neural data to kinematics, which win on offline benchmarks like the Neural Latents Benchmark by learning the neural population’s own low-dimensional dynamics. In closed loopClosed loop A system in which the output feeds back to influence the input, such as a user adjusting their brain activity while watching the cursor it controls. Related: Brain-computer interface (BCI)Brain-computer interface (BCI) A system that turns measured brain activity into control of something outside the body, from a cursor to a wheelchair to a speech synthesizer. , NeurofeedbackNeurofeedback Showing someone a measure of their own brain activity in real time so they can try to change it; its clinical claims range from plausible to unsupported. . Glossary entry, with a person adapting to the decoderDecoder The algorithm that maps neural signals to intended actions, such as a classifier or a Kalman filter. Related: ClassifierClassifier An algorithm that assigns each trial a label, such as left hand or right hand, after being trained on labelled examples. , Kalman filterKalman filter An estimator that blends a prediction from a motion model with a noisy measurement, weighting each by how much it trusts it; the decoder behind most intracortical cursor control. , Brain-computer interface (BCI)Brain-computer interface (BCI) A system that turns measured brain activity into control of something outside the body, from a cursor to a wheelchair to a speech synthesizer. . Glossary entry, simple Kalman variants remain surprisingly competitive, and the interaction between a learning user and a learning decoderDecoder The algorithm that maps neural signals to intended actions, such as a classifier or a Kalman filter. Related: ClassifierClassifier An algorithm that assigns each trial a label, such as left hand or right hand, after being trained on labelled examples. , Kalman filterKalman filter An estimator that blends a prediction from a motion model with a noisy measurement, weighting each by how much it trusts it; the decoder behind most intracortical cursor control. , Brain-computer interface (BCI)Brain-computer interface (BCI) A system that turns measured brain activity into control of something outside the body, from a cursor to a wheelchair to a speech synthesizer. . Glossary entry is a research field of its own.
Explain what this page was about to your roommate in three sentences. No jargon they would not know.
We track a moving cursor by predicting where it should be next and then blending that prediction with fresh brain measurements in proportion to how much we trust each side. We set that trust with two numbers for how jumpy movement is and how noisy the sensors are, fitting both from about 20 minutes of calibration moves so we never hand tune a smoothing knob. We get strong tracking near 80 to 90 percent agreement with implanted spikes but only 30 to 50 percent with scalp signals, and refitting to the intended target direction roughly doubles practical control.
Yours does not have to match. Check that the three ideas are there.