+10 XP

Running Multiple Simulations

A single neuron trace is one sample. To understand population behavior, simulate N neurons with different random seeds and look at the statistics across the population.

Nested loops: an outer loop runs N simulations. The inner loop is the time loop. After each simulation, save the result to an outer list.

python
import numpy as np
dt=1e-3; tau=20e-3; el=-60e-3; vr=-70e-3; vth=-50e-3
r=100e6; i_mean=25e-11; i_std=0.5e-11; t_max=150e-3
N = 10
spike_counts = []
for neuron in range(N):
Outer loop: each iteration = one neuron
    np.random.seed(neuron)  # different seed per neuron
Different seed per neuron = different random input
    v = el
    n_spikes = 0
n_spikes resets to 0 for each new neuron
    for step in range(int(t_max/dt)):
        t = step * dt
        i_t = i_mean + i_std * np.random.normal() * np.sqrt(dt)
        v = v + (dt/tau) * (el - v + r * i_t)
        if v >= vth:
            n_spikes += 1
+= 1 is shorthand for n_spikes = n_spikes + 1
            v = vr
    spike_counts.append(n_spikes)
Append after inner loop finishes — one count per neuron
print(f"Spike counts: {spike_counts}")
print(f"Mean: {np.mean(spike_counts):.1f}")
print(f"Std:  {np.std(spike_counts):.1f}")

Each neuron fires a slightly different number of times due to different random inputs. np.mean() and np.std() summarize the population.