Spikes to Rates

One neuron says almost nothing in 200 ms. Forty-eight of them point at a reach within about eight degrees. Move the reach, resample the spikes, and watch a noisy crowd agree.

Learn how a train of spikes becomes a firing rate, why spikes are random, why a motor-cortex cell prefers one direction, and how adding the cells together recovers the reach.

Drag the reach direction, then press Resample the spikes a few times. Then cut the number of cells to 4 and press it again. Then we build, piece by piece, every part of this ring: the spikes, the noise, the tuning and the sum.

You need a rough idea of what a probability is and a little Python for the code. We build the rest from zero, including what a spike is.

Built from primary sources: Georgopoulos, Schwartz and Kettner, Science 1986, its 1982 companion in the Journal of Neuroscience and Fetz, Science 1969. Lesson 2 of the series Brain-Computer Interfaces, from the spike to the foundation model.

Decode the reach

40°
48

Illustrative model, not recorded data: every cell fires at 20 spikes per second plus 15 times the cosine of the angle between the reach and its preferred direction, and the counts are random (Poisson) over 200 ms. The cosine shape is Georgopoulos et al. 1982 and 1986.

Chapter 0

Why a Decoder Needs Rates

See why a wire full of identical spikes cannot be read directly, and what a decoder needs instead

A person with paralysis sits in front of a screen. A small array of electrodes sits in her motor cortex, the strip of brain that plans hand movements. She imagines pushing a cursor to the right. On the lab computer, a trace jumps. It shows sharp, narrow pulses, each one a fraction of a millisecond wide, scattered along a line.

Each pulse is a spike: a brief electrical event that one nerve cell fires and sends down its wire. Think of a spike as one knock on a door. A knock carries no volume and no meaning. Every knock sounds the same.

That is the problem. The program that must move the cursor receives a stream of identical knocks from perhaps a hundred cells. It cannot read a knock. It needs a number, such as “this cell fired 7 times in the last 200 ms”, and then a rule that turns many such numbers into a direction.

Here is the whole job as a function. The input is, for each of N cells, the list of spike times in the last 200 ms. The output is one angle. Between them sits a row of N whole numbers. Say N is 4 and the lists hold 7, 2, 0 and 5 spikes (illustrative). The counts are [7, 2, 0, 5]. Divide by 0.2 s and the rates are [35, 10, 0, 25] Hz. This lesson builds the rule that turns such a row into an angle, and then measures how wrong that angle can be. A decoder that skips the count and reads raw spike times meets lists of different lengths, which no table can hold. Chapter 1 shows the fix.

What a spike carries

If every spike is the same, the message must lie somewhere else. There are only two places. One is when each spike happens. The other is how many happen in a given time. The second is the firing rate: spikes per second, written in hertz (Hz). A cell that fires 35 times in one second has a rate of 35 Hz.

Try it. The device below records one imaginary motor-cortex cell for one second while the hand reaches in three directions. Look at the ticks, then at the count.

Count the knocks

Each tick is one spike of one cell over one second. Choose a reach direction. Then press Record again to see that a new second never looks the same.

Illustrative cell: it prefers a reach to the right and fires at 20 Hz plus 15 Hz times the cosine of the angle away from that direction (35, 20 and 5 Hz). The spike times are random each time you press the button.

All the ticks look alike. Only their number changes: many for a reach to the right, few for a reach to the left. Yet two recordings of the same reach never match. The cell is not broken. Its spikes are random, and the rest of this lesson is about living with that.

The plan

We build the decoder from four ideas. First, count the spikes in a window, which gives a rate. Second, learn exactly how random the counts are. Third, find the rule that links a rate to a reach direction. Fourth, add many cells so the randomness averages away. Along the way we meet the two papers that started the field.

  1. A spike train is a list of times: bins, counts and the array a decoder reads.
  2. Why spikes are random: the Poisson count and its one striking property.
  3. Reading a rate from few spikes: the window trade-off.
  4. Fetz, 1969: a monkey learns to steer one cell’s rate.
  5. Cosine tuning: Georgopoulos and the preferred direction.
  6. One cell cannot name a direction: why a single rate is ambiguous.
  7. The population vector: the sum that points where the hand went.
  8. Where the vector fails: few cells, short windows, gaps.
  9. Field guide: numbers, code and where to go next.

The hero ring at the top is the whole lesson in one picture. The ticks are Chapters 1 to 3, the cosine is Chapter 5, the arrow is Chapter 7 and the error is Chapter 8. Earlier in the series, lesson 1 showed where a spike comes from. Here we start from the spike and never look inside the cell again.

A decoder receives spikes from a cell. Every spike has the same shape and size. Where can the cell’s message be?

Chapter 1

A Spike Train Is a List of Times

Turn one cell’s spikes into a row of counts that a computer can use

Imagine you stand outside a stadium and want to know how excited the crowd is. You cannot hear words. You hear only claps, one at a time. You write down the moment of each clap on a long strip of paper. Your strip is a spike train, the full record of one cell: nothing but a list of times.

A real recording is that strip, times a hundred cells. A typical amplifier samples the voltage 30,000 times per second. Software then finds the spikes (that step is the subject of lesson 8) and keeps only their times. After that, the raw voltage is gone. What remains for the decoder is a set of lists.

Lists of different lengths do not fit a model

Cell 1 fired at 0.012 s, 0.047 s, 0.051 s and 0.188 s. Cell 2 fired twice. Cell 3 fired nine times. These lists have different lengths, so you cannot stack them into a table. A decoder, like a regression or a neural network, wants a fixed-size input of numbers.

The fix is simple. Cut time into equal slices called bins, each of width Δ (the Greek letter delta, here a few tens of milliseconds). Count the spikes of each cell inside each bin. Now every cell has the same number of entries.

nt,i
The spike count: how many spikes cell i fired in bin number t. It is a whole number, 0 or more.
i
The cell number, from 1 to N.
tΔ
The start time of bin t, in seconds. Bin t covers the half-open slice from tΔ up to, but not including, (t+1)Δ.
rt,i
The rate estimate: the count divided by the bin width, in spikes per second (Hz).

Work it with Cell 1 and Δ = 50 ms. The bin from 0 to 0.05 s holds 0.012 and 0.047: count 2. The bin from 0.05 to 0.10 holds 0.051: count 1. The bin from 0.10 to 0.15 holds nothing: count 0. The bin from 0.15 to 0.20 holds 0.188: count 1. The row is [2, 1, 0, 1]. Divide by 0.05 s and the rates are 40, 20, 0 and 20 Hz.

Do this for N cells and T bins. The result is one array of shape (T, N): rows are time, columns are cells. Every decoder in this series reads that array. A 1 s recording of 96 cells in 20 ms bins is (50, 96), which is 4,800 whole numbers.

The bin width is a choice, and it has a failure mode

Try the device. Eight imaginary cells fire over one second. The top shows each spike. The bottom shows the count in each bin, brighter for more spikes.

Cut time into bins

Top: one row per cell, one tick per spike. Bottom: the counts per bin. Drag the bin width from 5 ms up to 250 ms and watch the table change shape.

50 ms

Illustrative cells: eight constant-rate random cells at 4, 8, 12, 16, 20, 24, 30 and 36 Hz, fixed so the picture is the same on every visit. The counts are computed live from the ticks above.

At 5 ms the table is almost all zeros. A cell at 20 Hz fires once per 50 ms on average, so a 5 ms bin is empty about nine times in ten. A table that is nearly all zeros hides the rate. It also makes a bigger array, and a slower decoder. This is the first failure mode: bins too narrow.

The symptom is a count table of zeros and the odd 1. The measurement is the share of empty entries, which the read-out gives you. The fix is a wider bin. At 250 ms the opposite happens. Only four bins are left, and any change in the rate inside a bin is lost. Motor decoders often use 20 to 100 ms bins as a compromise, and Chapter 3 shows why the right width depends on the rate.

Notice one more thing about the bottom grid. Even for a cell at a steady 20 Hz, neighbouring counts differ: 0, 2, 1, 1, 3. Nothing in the cell changed. That scatter is the subject of the next chapter.

You record 64 cells for 2 seconds and bin the spikes in 20 ms bins. What is the shape of the count array, as (bins, cells)?

Chapter 2

Why Spikes Are Random

Derive the Poisson count and see why its spread equals its mean

Reach to the right ten times. Record one cell for 100 ms at the same moment of each reach. You might count 2, 4, 1, 2, 3, 0, 2, 3, 2, 5. The hand did the same thing every time. The cell’s job did not change. Yet the counts jump around.

We need a model of that scatter. Without one, we cannot say how much to trust a count of 3. The model that fits cortex surprisingly well is the Poisson process: spikes arrive at random moments, at an average speed, with no memory of each other. Think of rain on a roof. The drops come at some average rate. Each drop lands without knowing where the last one landed.

Deriving the count from one assumption

Start with a cell whose average rate is λ spikes per second, and a bin of width Δ seconds. Its expected count is μ = λΔ. A 20 Hz cell in a 100 ms bin should give μ = 2 spikes on average.

Now cut the bin into m very thin slices. Make them so thin that a slice holds either one spike or none. Each slice has chance μ/m of a spike. Slices are independent: knowing slice 3 had a spike tells you nothing about slice 4. The count is then the number of successes in m independent tries, a binomial count. As m grows without limit, the binomial turns into a simple formula.

P(n)
The chance of counting exactly n spikes in the bin. This is the Poisson distribution.
μn
The expected count, multiplied by itself n times. It makes counts near μ more likely than counts far from it.
n!
n factorial: n × (n−1) × … × 1, with 0! = 1. It accounts for the many orders in which n spikes can fall.
μ = λΔ
The expected count: rate (Hz) times bin width (s). A single number sets the whole shape.

Work it for λ = 20 Hz and Δ = 0.1 s, so μ = 2. Then P(0) = e−2 = 0.135. P(1) = 2 × 0.135 = 0.271. P(2) = 4/2 × 0.135 = 0.271. P(3) = 8/6 × 0.135 = 0.180. P(4) = 16/24 × 0.135 = 0.090. So a silent bin comes up 13.5% of the time, even though the cell is “on”. A count of 5 or more has a chance of about 5%.

The striking property: spread equals mean

The variance of a count is the average squared distance of the count from its mean. It measures how much counts scatter. For the Poisson distribution it comes out at exactly μ, the same number as the mean. The ratio of variance to mean is the Fano factor, named after the physicist Ugo Fano. For a Poisson cell it equals 1, at every rate.

That makes noise easy to price. The typical wobble of a count is its standard deviation, √μ. At μ = 2 the wobble is 1.4 spikes, or 71% of the mean. At μ = 20 it is 4.5 spikes, or 22%. More spikes means a smaller relative error: the relative wobble is 1/√μ.

See it happen. Choose a rate and a window. The device runs 500 imaginary trials, plots how often each count came up, and overlays the formula.

Count 500 trials

Bars are how often each count came up in 500 trials. Dots are the Poisson formula. Move the rate and the window, then press Run 500 trials to draw new counts.

20 Hz
100 ms

Simulated, not recorded: counts are drawn from an exact Poisson process in your browser. A new run changes the bars a little; the dots do not move.

Run it a few times at 20 Hz and 100 ms. The measured variance sits close to the measured mean every time, a ratio near 1. Now slide the window to 500 ms. The bars slide right and flatten into a bell, but the ratio stays near 1. The spread grew in step with the mean.

When the model is wrong

Real cells break Poisson in two ways. After a spike, a cell cannot fire again for about a millisecond or two, called the refractory period (see lesson 1 for the cell biology). Poisson allows two spikes 0.1 ms apart. Also, in some cortical areas the variance of counts is at or above the mean, with a Fano factor over 1. Tolhurst, Movshon and Dean (1983) reported this for cat and monkey visual cortex; I know that paper only second hand. Treat Poisson as a good first model, not a law. Chapter 8 shows how far the decoder survives when the noise is worse than Poisson.

The idea also has a long career. A model that says “count ~ Poisson(rate), and rate depends on the movement” is called a Poisson generalized linear model. It is the standard tool for relating spikes to behaviour. We use its simplest form in Chapter 5.

A Poisson cell fires at 50 Hz. You count its spikes in a 20 ms window. What are the expected count and the typical wobble (standard deviation)?

Chapter 3

Reading a Rate From Few Spikes

Choose a window and trade a sharp estimate against a steady one

You are a weather forecaster with one rain gauge, and the gauge is a bucket that you check at fixed times. Count the drops in the last second and you have a decent estimate of how hard it rains. Count the drops in the last millisecond and you have almost nothing. Rate estimation is the same problem: you must choose how far back to look.

A cell’s firing rate is never observed. Only spikes are. The rate is a hidden quantity, the average speed behind the random arrivals, and every number we compute from spikes is an estimate of it. This chapter asks how good that estimate can be.

The simplest estimate: count and divide

Slide a window of width w along the spike train. At each moment, count the spikes inside the window and divide by w. This is the boxcar estimate, written r̂. It is just Chapter 1’s bin, slid along time one step at a time instead of cut into fixed slices.

r̂
The rate estimate in Hz. The hat means “estimated from data”, not the true rate.
n
The number of spikes inside the window.
w
The window width in seconds.
Var(r̂)
How much the estimate scatters from one recording to the next. It follows from Chapter 2: Var(n) = λw, and dividing a count by w divides its variance by w².
√(λ / w)
The typical error of the estimate, in Hz. Quadruple the window and the error halves.

Work it for a 20 Hz cell. A 100 ms window gives an error of √(20 / 0.1) = 14.1 Hz, which is 71% of the rate. A 1 s window gives √(20 / 1) = 4.5 Hz, which is 22%. The cost of the long window is time: for one second you learn nothing about what the cell did one second ago.

The other error: blur

A long window averages over a rate that may be changing. If a reach starts and the cell ramps from 10 Hz to 50 Hz in 100 ms, a 500 ms window mixes the quiet period with the burst. The estimate rises late, peaks too low and falls late. This error is a bias: it stays the same every time you repeat the recording. The scatter from Chapter 2 is the variance. A short window has low bias and high variance. A long window has the reverse.

Use the device. A cell has a true rate that sits at 10 Hz and bursts to 50 Hz around 0.5 s. Spikes are drawn from that rate. The blue curve is the truth. The purple curve is what you would estimate. Move the window, then add trials: averaging the same moment across many repeats is what experimenters call a peri-stimulus time histogram (PSTH). A BCI cannot repeat a reach 50 times before moving a cursor, so watch the single-trial case closely.

Estimate the hidden rate

Blue: the true rate. Purple: the estimate from the spikes shown as ticks below. Drag the window wide and narrow, then raise the trial count to 20.

100 ms
1

Illustrative: the true rate is 10 Hz plus a Gaussian bump of 40 Hz, centred at 0.5 s with a 80 ms spread. Error is the root-mean-square gap between the two curves over the one-second window. Spikes are exact Poisson draws.

At 10 ms the purple curve is a forest of spikes, off by about 40 Hz. At 400 ms it is a low smooth hill that reads about 30 Hz at the peak. For one trial the error is lowest near 200 ms, about 10 Hz (I measured this by simulation). Raise the trials to 20 and the best window shrinks to about 120 ms with an error near 3 Hz. The sweet spot moves with the number of spikes you have and the speed of the burst.

The failure mode to watch for in a real decoder is the symptom of a cursor that jitters (window too short) or lags behind the hand (window too long). The measurement is the error against a known rate, or in a real system the error of the decoded cursor against the true target. The fix is to pick the window on held-out data, not by eye. Better still, smooth the estimate with a model that knows how the rate moves, which is what the Kalman filter of lesson 16 does.

One more point matters for a BCI. Averaging trials is not available online, but averaging cells is. Ten cells with similar rates give ten times the spikes in the same window. That is the real reason a population decodes better than a single cell, and Chapter 7 uses it.

Your decoded cursor shakes on a still target, but it reaches the target on time. A rate estimate for each cell uses a 20 ms window. What change is most likely to help?

Chapter 4

Fetz: A Rate You Can Steer

Watch a rewarded cell learn to fire more, the first proof that a brain can drive a BCI

Picture a monkey in a lab in the late 1960s. A hair-thin electrode sits next to one nerve cell in its motor cortex. A tone or a light follows how fast the cell fires. The monkey wants a food pellet. The experimenter makes one rule: when the cell fires fast enough, a pellet drops.

Nobody tells the monkey the rule. Nobody can. It cannot choose to fire a particular cell the way it chooses to lift a hand. Yet, as Eberhard Fetz reported in Science in 1969, the cell’s rate climbs. In the abstract he writes that newly isolated cells rose by 50 to 500 percent above their rate before reinforcement, with auditory or visual feedback of the firing usually given alongside the food reward.

Why this paper matters to a BCI

Fetz did not decode anything. He showed something earlier and more basic: operant conditioning, which means rewarding a behaviour until it happens more often, works even when the behaviour is the firing rate of one cell. The brain can learn to produce a rate on demand, if it is told how it is doing.

That is the whole principle of a closed-loop BCI. The decoder reads rates (Chapter 3), the screen shows the result, and the user adjusts. The user and the decoder learn together. A decoder need not be perfect, because the brain adapts to its mistakes. Later work, such as Taylor, Tillery and Schwartz in Science 2002, closed this loop with a population vector like the one in Chapter 7, and the monkeys improved with practice.

Here is the loop as data flow. Every 100 ms, the system converts the cell’s spike times to a count (input: a list of times; output: one integer). It compares the count with a threshold (output: reward or no reward). It shows a tone or a light that follows the count (output: feedback). The monkey’s brain changes how it recruits the cell. Nothing else is needed.

Learning from a noisy signal

Here is a toy of the idea. It is not Fetz’s data. A cell starts at 10 Hz. On each one-second trial, the animal tries something slightly different, which nudges the rate up or down at random. A trial is rewarded when the spike count reaches the threshold. After a reward, the animal repeats more of whatever it just did. Over many trials the average drifts toward higher rates.

Reward a cell until it speeds up

Each dot is the spike count of one 1 s trial. Green dots earned a reward. The purple line is a 10-trial average. Pick a threshold, then press Train 120 trials.

Toy model, not Fetz’s data: rate = 10 Hz × e(drive + attempt), the attempt is random, a rewarded trial adds 0.8 times its attempt to the drive, and the rate is capped at 60 Hz. The seeds are fixed, so each threshold gives the same run every time.

With the Medium threshold, 70 of 120 trials earn a reward and the average count climbs from about 10 to about 21. With Easy, it climbs to about 57 spikes, near the 60 Hz cap and more than five times the start. That is the same order as the 50 to 500 percent Fetz reported, but this is a toy, not his data. With Hard (40 spikes), not one trial in 120 earns a reward, so nothing is learned and the average stays near 10. This is the failure mode of every shaping schedule: a reward the learner cannot reach teaches nothing. Experimenters fix this by starting with an easy threshold and raising it as the rate rises.

The reason is plain arithmetic from Chapter 2. At 10 Hz the one-second count is Poisson with mean 10. The chance of 14 or more is 13.6%, so Easy pays about one trial in seven from the start. The chance of 18 or more is 1.4%, about one trial in seventy. The chance of 40 or more is 7 in 10 trillion. Medium still works: each lucky reward lifts the drive, which makes the next reward likelier, and 70 of the 120 trials end up paid. Hard never gets the first lucky trial, so the loop never starts.

BCI teams meet the same rule today. If the cursor cannot reach a target in the first minutes, the user gets no reward signal and gives up. Calibration tasks start with big, close targets for that reason.

In the toy, the Hard threshold (40 spikes) never trains the cell. What is the cause?

Chapter 5

Cosine Tuning

Fit the curve that links a reach direction to a cell’s firing rate

A monkey sits with its hand on a centre button. A light shows on one of eight targets arranged like the points of a compass, 45 degrees apart. The monkey moves its hand from the centre to the lit target. This is the center-out reaching task. An electrode listens to one cell in the motor cortex the whole time.

Georgopoulos and his colleagues ran this experiment in the early 1980s. Their 1982 paper in the Journal of Neuroscience reports 606 cells related to arm movement, of which 323 were active in the task and 241 (74.6% of those) had a firing rate that varied in an orderly way with movement direction. A cell fired most for one direction and less and less as the direction moved away. They named that favourite direction the cell’s preferred direction.

The shape of one cell’s preference

Plot the rate against the movement angle and you get a smooth bump, high at the preferred direction and low on the opposite side. The paper calls it a bell-shaped tuning curve. For about three quarters of the tuned cells, the curve was well described by a sinusoid, which is a cosine. A cosine is the curve you get by tracking the horizontal position of a point going around a circle.

r(θ)
The cell’s expected firing rate, in Hz, when the hand moves in direction θ.
b0
The baseline: the rate averaged over all directions. A cell does not go silent for the wrong direction.
m
The modulation depth: how far the rate swings above and below the baseline.
θ
The movement direction, in degrees. 0 is right, 90 is up.
φ
The preferred direction. The cosine equals 1 when θ = φ, so that is where the rate peaks, at b0 + m.

This curve looks hard to fit because φ sits inside the cosine. A trig identity removes the problem: cos(θ − φ) = cosθ cosφ + sinθ sinφ. Set c = m cosφ and s = m sinφ. Then r(θ) = b0 + c cosθ + s sinθ. That is linear in the three unknowns b0, c and s. Ordinary least squares, the straight-line fitting you may know, solves it. Afterwards, m = √(c² + s²) and φ = atan2(s, c).

For eight evenly spaced directions the least-squares answer has a short form: b0 is the mean of the eight rates, c = (2/8) Σ r cosθ, and s = (2/8) Σ r sinθ. Work it with measured rates at 0, 45, …, 315 degrees of 34, 31, 28, 15, 8, 6, 12 and 25 Hz. The mean is 159/8 = 19.9 Hz. The cosine sum is 34 − 8 + 0.707 × (31 − 15 − 6 + 25) = 50.75, so c = 12.7. The sine sum is 28 − 12 + 0.707 × (31 + 15 − 6 − 25) = 26.6, so s = 6.65. Then m = 14.3 Hz and φ = atan2(6.65, 12.7) = 27.7 degrees. This cell prefers a reach up and to the right.

Try it. Set a cell’s true preferred direction, then run the eight-direction experiment. Each dot is the average rate over several reaches in one direction. The purple curve is the fit.

Fit one cell’s tuning curve

Warm curve: the true tuning. Blue dots: the measured average rate in each of eight directions. Purple curve: the least-squares cosine. Drag the true preferred direction, then change the reaches per direction.

120°
4

Illustrative cell: baseline 20 Hz and modulation 15 Hz, counted over 500 ms of each reach with Poisson noise. The cosine form is Georgopoulos et al. 1982, which found it for about 75% of the direction-tuned cells.

With 4 reaches per direction the fitted angle is usually within 10 degrees of the truth. With 1 reach per direction it can be 20 or more off, and the dots wander. With 30 it nearly sits on the curve. The error shrinks as the square root of the number of reaches, the Poisson rule from Chapter 2 once more.

Two caveats from the paper itself. Not every cell is cosine: about a quarter of the tuned cells were not described well by it, and 82 of the 323 cells studied showed no orderly direction dependence at all. And tuning is broad. The curve spans the full circle, so one cell is active, to some degree, for most reach directions. Chapter 6 shows what that costs.

A cell has baseline 20 Hz, modulation 15 Hz and preferred direction 90 degrees (up). What is its expected rate for a reach to the right (0 degrees)?

Chapter 6

One Cell Cannot Name a Direction

Invert a single tuning curve and see why the answer is a wide smear

A friend tells you only this: “It was warm today.” Can you name the month? Not really. Many months are warm, and you cannot tell spring from autumn. One cell’s rate is that friend. It narrows the field, but it leaves many answers open.

Chapter 5 gave us the forward rule: direction in, expected rate out. A decoder needs the backward rule: spike count in, direction out. We get it by asking, for each possible direction, how likely the count we saw would be.

Inverting a tuning curve

Take a cell with baseline 20 Hz, modulation 15 Hz and preferred direction 0 degrees. Watch it for 200 ms and count 7 spikes. For each candidate direction θ, the expected count is μ(θ) = (20 + 15 cosθ) × 0.2 = 4 + 3 cosθ. Chapter 2’s formula says how likely a count of 7 would be.

At θ = 0 degrees, μ = 7, and P(7) = 0.149. At 60 degrees, μ = 5.5, and P(7) = 0.123. At 90 degrees, μ = 4, and P(7) = 0.060. At 180 degrees, μ = 1, and P(7) = 0.0001. So a count of 7 favours the right, but 60 degrees on either side is nearly as good. This function of θ is the likelihood: how well each possible direction explains the data. It is not yet a probability of the direction, but with no reason to prefer any direction, we scale it to add up to 1 and read it that way.

For several cells that fire independently, multiply their likelihoods, or add their logarithms. For Poisson cells with evenly spread preferred directions, the log likelihood reduces to a plain sum: log L(θ) = Σi ni log μi(θ) + a constant that does not depend on θ. The direction with the highest value is the maximum likelihood estimate. In plain words: score each direction by how well it predicts every cell’s count, and keep the best one.

Ask what the spikes allow

The purple curve shows how well each direction explains the spikes of the last 200 ms. The teal line is the true reach. Start with 1 cell, press Record 200 ms a few times, then add cells.

150°
1

Illustrative cells as in the hero: 20 Hz baseline, 15 Hz modulation, preferred directions spread around the circle. The curve is the exact Poisson likelihood on a 1 degree grid, scaled to add up to 1; the width is the narrowest set of directions that holds 90% of it.

With one cell the curve is a wide hump, and often two humps. The shape is a lesson in itself. Any rate that lies between the cell’s highest and lowest rates fits two directions: one on each side of its preferred direction. A count of 4 in the example above, which equals the baseline, fits 90 degrees to the left and 90 degrees to the right. The cell cannot tell them apart.

The failure mode: ambiguity, then a fix

The symptom is a decoded direction that flips between two mirror-image answers from one window to the next. The measurement is the width of the likelihood, which the read-out reports as an arc in degrees. For one cell the arc is typically over 200 degrees: the median in my simulation of this device was 223 degrees. The fix is more cells with different preferred directions. A second cell tuned 90 degrees away has its peak where the first cell has its tie. Its count says which of the two mirror answers is right.

Work one case with illustrative counts. Cell A prefers 0 degrees and counts 4, its baseline, so it scores 90 degrees and 270 degrees equally. Cell B prefers 90 degrees, so its expected count is 4 + 3 sinθ. Say B counts 6. A reach at 90 degrees makes B expect 7, and P(6) = 0.149. A reach at 270 degrees makes B expect 1, and P(6) = 0.0005. The scores for the two mirror answers differ by 0.149 / 0.0005, about 290 to 1 for 90 degrees. Two cells turned a coin toss into a near certainty.

Add cells in the device. In the same simulation the median arc fell to 155 degrees with 2 cells, 78 with 4, 60 with 8 and 34 with 24. Each cell is noisy and broad, yet together they pin the angle. That is the central idea of population coding, and it is the point of the 1986 paper.

Maximum likelihood is the best you can do if you know every cell’s tuning exactly. It needs the whole curve for each cell, and a search over angles. The next chapter shows a much simpler rule that gets close, and was invented first.

One cell with preferred direction 0 degrees fires at exactly its baseline rate. Which reach directions fit that rate best?

Chapter 7

The Population Vector

Add one small arrow per cell and watch the sum point where the hand went

A jury of twelve people each hold a small compass needle that points one way. Nobody is reliable alone. One juror is lazy, one is distracted, one points almost backwards. Yet when you add up their pushes, the group leans in the right direction. That is the idea Georgopoulos, Schwartz and Kettner proposed in Science in 1986.

Their abstract makes two statements. Single motor-cortex cells are only broadly tuned to direction, which is Chapter 6’s problem. And yet the direction of movement is predicted by the action of a population of cells, found by summing weighted “cell vectors”. They recorded 282 cells, and 224 of them (79.4%) were directionally tuned and entered the sum. Their monkeys reached to eight targets in 3D space. We draw the same idea in a flat 2D plane, as their 1982 paper did. A figure of 475 cells belongs to a later paper from the same group, not to this one.

One vote per cell

Give each cell an arrow, a cell vector, that points in its preferred direction φi. Make its length the cell’s change from baseline: wi = ri − b0, where ri is the rate you measured. A cell firing above baseline pushes toward its preferred direction. A cell firing below baseline has a negative length, so it pushes the opposite way. Then add all the arrows. The sum is the population vector.

P
The population vector: a pair of numbers (Px, Py), the sum of all the cell votes.
Σi=1N
Add up the votes of all N recorded cells.
wi
The vote strength: measured rate minus baseline, in Hz. It can be negative.
(cosφi, sinφi)
A unit arrow (length 1) pointing along cell i’s preferred direction.

Why the sum points the right way

Use the expected rates, wi = m cos(θ − φi). The x part of the sum is m Σ cos(θ − φi) cosφi. A trig identity gives cos(θ − φ) cosφ = ½[cosθ + cos(θ − 2φ)]. If the preferred directions spread evenly around the circle, the second term sums to zero over the cells: it goes round the circle twice and cancels. What stays is N/2 times cosθ. The y part works the same way. So the expected sum is (N m / 2) (cosθ, sinθ): an arrow of length Nm/2 pointing exactly along the reach.

Check with four cells at 0, 90, 180 and 270 degrees and a reach at 60 degrees, using b0 = 20 and m = 15. The rates are 27.5, 33.0, 12.5 and 7.0 Hz. The votes are +7.5, +13.0, −7.5 and −13.0. The arrows are (7.5, 0), (0, 13.0), (7.5, 0) and (0, 13.0). The sum is (15, 26). Its angle is atan2(26, 15) = 60 degrees, and its length is 30, which equals Nm/2 = 4 × 15 / 2. No search, no fitting, no likelihood: just a sum.

Now use noisy counts. Step through the sum and watch the arrows chain head to tail. The purple arrow from the start to the end of the chain is the population vector.

Add the votes

Each warm arrow is one cell’s vote from 200 ms of noisy spikes, chained in order of preferred direction. The purple arrow is the sum. The teal line is the true reach. Press Add votes one by one.

60°
12

Illustrative model as in the hero: 20 Hz baseline, 15 Hz modulation, Poisson counts over 200 ms, the baseline known. Arrow lengths are rescaled to fit the screen, so compare directions, not lengths.

Every single vote is crooked. Some arrows even point away from the truth. But the arrows that are right point more strongly and more often, and the crooked ones cancel each other. The end of the chain lands near the line. This is averaging: Chapter 3’s promise that more cells means more spikes in the same window, put to work.

Notice what the decoder does not need. It needs each cell’s preferred direction and baseline, both found once, in a calibration like Chapter 5’s. It does not need the modulation depth, the noise model or a search over angles. That simplicity is why the population vector drove the first closed-loop cursor and robot-arm experiments, and why it is still a baseline that newer decoders are measured against.

The big idea. A crowd of broad, noisy and individually ambiguous cells can name a direction precisely, because their errors point in different directions and cancel, while their signal adds.
A cell fires below its baseline during a reach. How does the population vector treat that cell’s vote?

Chapter 8

Where the Vector Fails

Measure the error against cell count, window length and gaps in the preferred directions

A bridge engineer does not stop at “the bridge holds”. She asks how much load it carries, in which weather, and what happens when one cable snaps. We owe the population vector the same questions. When does it hold, and how does it fail?

There are three levers: the number of cells, the length of the window, and how the preferred directions are spread. Each one has a clear effect. We measure it by running the decoder many times and recording the root-mean-square error (RMS): square each angle error, average the squares, take the square root. It is a typical error in degrees that punishes big misses.

More cells: the error falls like one over the square root

Each cell adds one noisy vote. The signal parts of the votes add up in a straight line: they grow like N. The noise parts point in random directions and add like √N. The ratio of signal to noise therefore improves like √N, and the angle error shrinks like 1/√N. Quadruple the cells, halve the error.

My simulation of the hero model (200 ms window, evenly spread directions, 300 random reaches) gives an RMS error of 31 degrees with 4 cells, 14 with 16, 7.0 with 64 and 4.7 with 128. From 16 to 64 cells the error halves, as the rule predicts. The 1986 study had 224 cells.

Compare with real data. The 1986 paper built vectors from 12 cells picked at random from its 224. The average angle between the vector and the movement was 29.7 degrees in one animal and 30.4 degrees in the other. My toy gives 16 degrees for 12 cells. Real cells are noisier, spread less evenly and do not all follow a cosine, so real errors sit above the toy’s. With all 224 cells the paper reports a vector “very close” to the movement.

A longer window: the same square root

More time means more spikes per cell. At 64 cells the same simulation gives 13 degrees for a 50 ms window, 7 for 200 ms and 4 for 500 ms. This is Chapter 3’s trade again. A longer window gives a calmer decode, and a slower one. A BCI that waits 500 ms before moving the cursor feels sluggish to the user.

A gap in the preferred directions: a floor that more cells cannot lift

The proof in Chapter 7 assumed the preferred directions spread evenly, so that the second term, cos(θ − 2φ), cancelled. Suppose an array happens to sit on cells that all prefer directions in the right-hand 240 degrees of the circle, with none in the remaining 120. The term no longer cancels. The vector is pulled toward the crowded side. Reaches that fall in the empty wedge decode too close to its edges.

The symptom is a cursor that never quite reaches targets in one region of the screen. The measurement is the error plotted against reach direction: a bump in the empty wedge. The fix is not more cells of the same kind. In my simulation 64 cells with the gap give 10.6 degrees and 128 give 9.8, while evenly spread cells reach 4.7. The error hits a floor from the bias. The real fix is to sample cells with the missing directions, or to correct the bias by fitting a decoder (such as the regression of the next chapter’s Field Guide) rather than assuming even spread.

Measure the error

The purple line is the typical error of the population vector against the number of cells. The dashed grey line is the evenly spread case for comparison. Switch to the gap, then slide the window.

200 ms
16

Simulated with the hero model: 20 Hz baseline, 15 Hz modulation, Poisson counts, 120 random reach directions per point, fixed seeds. The numbers change a little with the seeds, not in shape.

Two more failures deserve a name. First, the baseline b0 of each cell must be right. A shift shared by all cells cancels when the preferred directions spread evenly, because the unit arrows sum to zero. A shift in one cell does not cancel. If a cell’s baseline is 5 Hz too low, it adds a 5 Hz push toward its preferred direction in every window, and the cursor leans that way. The fix is to track each baseline from recent data. Second, real noise may be worse than Poisson (Chapter 2). Then each vote is noisier, the error curve sits higher, and the 1/√N slope still holds as long as the cells fail independently. Cells that share noise do not average away.

A team has 16 cells and a typical error of 14 degrees. They want about 7 degrees, with the same window and well-spread cells. Roughly how many cells do they need?

Chapter 9

Field Guide

Keep the numbers, the terms, the code and the sources on one page

A cell’s spikes are identical knocks, so the message lives in their count per second, the firing rate. Counts scatter like a Poisson process, with variance equal to the mean, so a rate from few spikes is noisy and the window sets the price. Fetz (1969) showed that reward can raise one cell’s rate, which is why a closed loop works. Georgopoulos and colleagues showed that motor cells are cosine-tuned to reach direction, and that a sum of weighted cell vectors names the direction. Errors cancel as 1/√N, until bias from the layout of the cells sets a floor.

The numbers to keep

QuantityValueSource
Reach directions in the centre-out task8, at 45° stepsGeorgopoulos et al. 1982, abstract
Cells active in the task323Georgopoulos et al. 1982, abstract
Cells with orderly direction dependence241 (74.6%)Georgopoulos et al. 1982, abstract
Tuned cells well described by a cosineabout 75%Georgopoulos et al. 1982, abstract
Cells recorded, and directionally tuned, in the population vector study282 and 224Georgopoulos et al. 1986, full text
Average error of a vector from 12 random cells29.7° and 30.4°Georgopoulos et al. 1986, two animals
Rate rise of newly isolated cells after reward50% to 500%Fetz 1969, abstract
Poisson count: mean, variance, Fano factorμ, μ, 1Derived in Chapter 2
Typical error of a window rate estimate√(λ / w)Derived in Chapter 3
Expected population vector lengthN m / 2Derived in Chapter 7
Decode error, 16 and 64 cells, 200 ms (toy)14° and 7°This page’s simulation, illustrative

The terms in plain words

TermPlain meaning
Spike trainThe list of times at which one cell fired.
Bin and spike countA slice of time, and the number of spikes inside it.
Firing rateSpikes per second, in hertz; the count divided by the bin width.
Poisson processSpikes at random moments with a steady average rate and no memory.
Fano factorThe variance of the counts divided by their mean; 1 for Poisson.
Tuning curveA cell’s expected rate plotted against the movement direction.
Preferred directionThe direction where the cell fires most.
Population vectorThe sum of one weighted arrow per cell; its angle is the decoded direction.
LikelihoodHow well each possible direction explains the spikes you saw.

The core as code

Three small functions carry the whole lesson. Shapes are in the comments: T bins, N cells, K training reaches.

pythonimport numpy as np

def bin_spikes(times, t_end, dt):
    # times: list of N arrays of spike times (s). Returns counts of shape (T, N)
    edges = np.arange(0, t_end + dt / 2, dt)
    return np.stack([np.histogram(t, edges)[0] for t in times], axis=1)

def fit_tuning(counts, theta, dt):
    # counts (K, N) per reach, theta (K,) in radians. r = b0 + c cos(theta) + s sin(theta)
    X = np.column_stack([np.ones_like(theta), np.cos(theta), np.sin(theta)])   # (K, 3)
    coef, *_ = np.linalg.lstsq(X, counts / dt, rcond=None)                  # (3, N)
    b0, c, s = coef
    return b0, np.arctan2(s, c), np.hypot(c, s)   # baseline, preferred direction, depth: each (N,)

def population_vector(counts_t, b0, phi, dt):
    # counts_t (N,) from one window. Returns the decoded angle in radians
    w = counts_t / dt - b0                                   # vote strengths (N,)
    return np.arctan2((w * np.sin(phi)).sum(), (w * np.cos(phi)).sum())

I ran these on a simulated 48-cell population (10 reaches in each of 8 directions for calibration, 200 ms windows). The fitted preferred directions were on average 4.9 degrees from the truth, and a reach at 40 degrees decoded to 48.7 degrees from one window of noisy counts. Real data adds drift and non-Poisson noise.

Check yourself

Keep going

Sources and credit

This lesson has no course slides. It is built from primary sources. Georgopoulos AP, Schwartz AB, Kettner RE, Neuronal population coding of movement direction, Science 233(4771):1416–9, 1986 (abstract and full text). Georgopoulos AP, Kalaska JF, Caminiti R, Massey JT, On the relations between the direction of two-dimensional arm movements and cell discharge in primate motor cortex, J Neurosci 2(11):1527–37, 1982 (abstract). Fetz EE, Operant conditioning of cortical unit activity, Science 163(3870):955–8, 1969 (abstract). Tolhurst DJ, Movshon JA, Dean AF, Vision Research 23(8):775–85, 1983, on cat and monkey visual cortex, cited second hand for Fano factors above 1. Taylor DM, Tillery SI, Schwartz AB, Science 2002, cited by title only. All simulations are my own illustrations with the parameters stated on each device.

The takeaway. Spikes are identical knocks. Their count per second is the signal, Poisson noise is the price, and a crowd of broad cosine-tuned cells, added with the right weights, names the reach direction.
Which statement about the population vector is correct?

Now press Teach and explain, from memory, how a crowd of noisy cells names a direction. Then go back to the ring at the top: cut the cells to 4, press resample, and say which chapter explains what you see.

BCI 02: Spikes to Rates
Georgopoulos et al. 1986 · Fetz 1969 · Back to Gleams