KDE
Kernel density estimation (KDE) of neural firing-rate maps and Poisson log-likelihood.
The M-step of the SIMPL EM loop estimates each neuron's receptive field using KDE: spikes are smoothed with a Gaussian kernel over the spatial grid, and normalised by occupancy, yielding a firing-rate map in units of spikes per time bin.
For 1-D angular environments (is_1D_angular=True), the specialised
kde_angular function convolves with a wrapped Gaussian kernel via FFT so
that the estimate is seamless across the \([-\pi, \pi)\) boundary.
The Poisson log-likelihood functions (poisson_log_likelihood_maps and
poisson_log_likelihood) evaluate how well the estimated
receptive fields explain the observed spike counts and are used during the
E-step to construct likelihood maps over position space.
decode_observations(xF, spikes, mean_rate, mask, batch_size=None, return_log_maps=False)
Compute Poisson likelihood maps, fit Gaussian observations, and flag silent bins.
This combines poisson_log_likelihood_maps and fit_gaussian in a single
batched pipeline so that the full (T, N_bins) likelihood tensor is never
materialised at once, keeping peak memory low for long sessions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
xF
|
(Array, shape(N_bins, D))
|
Spatial bin centres. |
required |
spikes
|
(Array, shape(T, N_neurons))
|
Spike counts. |
required |
mean_rate
|
(Array, shape(N_neurons, N_bins))
|
Receptive fields (expected spike counts per bin per time step). |
required |
mask
|
(Array, shape(T, N_neurons))
|
Boolean mask (True = use neuron at this time step). |
required |
batch_size
|
int or None
|
Number of time bins per batch. If None (default), chosen adaptively to target ~64 MB peak memory for the likelihood tensor. |
None
|
return_log_maps
|
bool
|
If True, also return the full |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
mu_l, mode_l, sigma_l : jax.Array
|
Gaussian observation parameters fitted from the likelihood. |
|
no_spikes |
(Array, shape(T))
|
Boolean, True where total (masked) spike count is zero. |
logPYXF_maps |
(Array, shape(T, N_bins))
|
Only returned when |
Source code in src/simpl/kde.py
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 | |
gaussian_kernel(x1, x2, bandwidth)
Evaluates the Gaussian kernel between two points \(x_1\) and \(x_2\):
where \(\Sigma = \sigma^2 I\) is the isotropic covariance with bandwidth \(\sigma\).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x1
|
Array
|
The first position |
required |
x2
|
Array
|
The second position |
required |
bandwidth
|
float
|
The bandwidth of the kernel |
required |
Returns:
| Name | Type | Description |
|---|---|---|
kernel |
float
|
The probability density at x |
Source code in src/simpl/kde.py
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 | |
get_ll_and_bps_splits(Y, FX, mask)
Compute train/val log-likelihoods and bits-per-spike from spikes and predicted rates.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
Y
|
(Array, shape(T, N_neurons))
|
Observed spike counts. |
required |
FX
|
(Array, shape(T, N_neurons))
|
Predicted firing rates (expected spikes per time bin). |
required |
mask
|
(Array, shape(T, N_neurons))
|
Boolean training mask. True = train, False = validation. |
required |
Returns:
| Type | Description |
|---|---|
dict
|
Keys: |
Source code in src/simpl/kde.py
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 | |
kde(bins, trajectory, spikes, kernel=gaussian_kernel, kernel_bandwidth=0.01, mask=None, batch_size=None, return_position_density=False)
Performs KDE to estimate the expected number of spikes each neuron will fire
at each position in bins given past trajectory and spikes data. This
estimate is an expected-spike-count-per-timebin, in order to get firing rate
in Hz, divide this by dt.
Kernel Density Estimation goes as follows (the denominator corrects for non-uniform position density):
In practice this is computed in log-space as \(\mu(x) = \exp[\log(K_s) - \log(K_x)]\).
Optionally, a boolean mask same shape as spikes can be passed to ignore certain spikes. This restricts the KDE calculation to only the spikes where mask is True.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bins
|
(Array, shape(N_bins, D))
|
The position bins at which to estimate the firing rate |
required |
trajectory
|
(Array, shape(T, D))
|
The position of the agent at each time step |
required |
spikes
|
(Array, shape(T, N_neurons))
|
The spike counts of the neuron at each time step (integer array, can be > 1) |
required |
kernel
|
function
|
The kernel function to use for density estimation. See |
gaussian_kernel
|
kernel_bandwidth
|
float
|
The bandwidth of the kernel |
0.01
|
mask
|
(Array, shape(T, N_neurons))
|
A boolean mask to apply to the spikes. If None, no mask is applied. Default is None. |
None
|
batch_size
|
int or None
|
The time axis is split into batches of this size to avoid memory errors, each batch is then processed in series. If None (default), chosen adaptively to target ~64 MB peak for the kernel matrix. |
None
|
return_position_density
|
bool
|
If True, this function also returns the position density (the denominator of the KDE) at each bin. |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
kernel_density_estimate |
(Array, shape(N_neurons, N_bins))
|
|
position_density |
(Array, shape(N_bins)(optional))
|
Normalised position density (sums to 1 over bins), independent of neuron masks. |
Source code in src/simpl/kde.py
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 | |
kde_angular(bins, trajectory, spikes, kernel=None, kernel_bandwidth=0.3, mask=None, return_position_density=False, eps=1e-06)
Circular KDE for angular data. Estimates expected spike count per timebin
at each angular bin (divide by dt for Hz). See kde() for the linear
equivalent.
where \(K_s\) and \(K_x\) are the kernel-smoothed spike count and occupancy histograms, respectively.
Unlike kde(), which evaluates all pairwise kernel values, this function
first histograms the data then smooths via FFT-based circular convolution
with a von Mises kernel:
This is \(O(N_{\text{bins}} \log N_{\text{bins}})\) rather than \(O(N_{\text{bins}} \cdot T)\).
Both bins and trajectory must be in radians in \([-\pi, \pi)\). Bins
must be uniformly spaced. kernel_bandwidth is in radians and is converted
internally to von Mises concentration \(\kappa = 1 / \sigma^2\)
(accurate for \(\kappa > 2\), i.e. small bandwidth \(\sigma < {\sim}0.7\) rad).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bins
|
(Array, shape(N_bins) or (N_bins, 1))
|
Angle bin centres in radians, uniformly spaced in [-pi, pi). |
required |
trajectory
|
(Array, shape(T) or (T, 1))
|
Angular position of the agent at each time step, in radians in [-pi, pi). |
required |
spikes
|
(Array, shape(T, N_neurons))
|
Spike counts at each time step (integer array, can be > 1). |
required |
kernel
|
None
|
Unused, kept for API consistency with |
None
|
kernel_bandwidth
|
float
|
Std dev of smoothing kernel in radians. Larger = smoother. Converted to von Mises kappa = 1 / kernel_bandwidth^2. |
0.3
|
mask
|
(Array, shape(T, N_neurons))
|
Boolean mask for spikes. Default is None (no masking). |
None
|
return_position_density
|
bool
|
If True, also returns normalised position density. Default is False. |
False
|
eps
|
float
|
Small constant to avoid division by zero. Default is 1e-6. |
1e-06
|
Returns:
| Name | Type | Description |
|---|---|---|
kernel_density_estimate |
(Array, shape(N_neurons, N_bins))
|
|
position_density |
(Array, shape(N_bins)(optional))
|
Normalised position density (sums to 1), independent of neuron masks. |
Source code in src/simpl/kde.py
408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 | |
poisson_log_likelihood(spikes, rates)
Per-element Poisson log-likelihood of spike counts given predicted rates.
The Poisson probability of observing \(X\) spikes given mean rate \(\mu\) is:
so the log-likelihood is:
where \(\log(X!)\) is computed via Stirling's approximation (manually correcting for when \(X = 0\)):
Accepts arrays of any shape; spikes and rates must share the same
shape. Returns an array of that same shape.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spikes
|
Array
|
Observed spike counts. |
required |
rates
|
Array
|
Predicted firing rates (expected spikes per time bin). Same shape as |
required |
Returns:
| Name | Type | Description |
|---|---|---|
log_likelihood |
Array
|
Per-element Poisson log-likelihood. Same shape as inputs. |
Source code in src/simpl/kde.py
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 | |
poisson_log_likelihood_maps(spikes, mean_rate, mask=None)
Version of poisson_log_likelihood optimised to broadcast over the
spatial binning dimension and sum over neurons.
Used in the E-step to build likelihood maps: for each time step, evaluates
the Poisson log-likelihood at every spatial bin simultaneously via a matrix
multiply (T, N) @ (N, B) → (T, B).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spikes
|
(Array, shape(T, N_neurons))
|
Observed spike counts. |
required |
mean_rate
|
(Array, shape(N_neurons, N_bins))
|
Receptive fields: expected spike count per time bin at each spatial bin. |
required |
mask
|
(Array, shape(T, N_neurons))
|
Boolean spike mask. If None, all neurons are used. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
log_likelihood |
(Array, shape(T, N_bins))
|
Log-likelihood summed over neurons at each spatial bin. |
Source code in src/simpl/kde.py
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 | |