# I Built My First Leaky Integrate-and-Fire Neuron Model
URL: https://www.montek.dev/post/i-built-my-first-leaky-integrate-and-fire-neuron-model
Slug: i-built-my-first-leaky-integrate-and-fire-neuron-model
Date: 2026-05-26
Categories: BCI, Research
> I was reading An Introductory Course in Computational Neuroscience by Paul Miller, and I wanted to do more than just read the equations. I wanted to build the model myself and see if a few simple lines of code could produce something that looks like neural spiking.
This post is my build log for the Leaky Integrate-and-Fire neuron, usually called the LIF model. What I created was my own implementation of it, starting from the book, then extending it with firing-rate curves, refractory periods, and a few biological interpretations.

The surprising part is that the model is extremely simple. A neuron is treated like a tiny electrical circuit: the membrane stores charge like a capacitor, current pushes the membrane voltage upward, and leak channels pull it back toward a resting value. When the voltage crosses a threshold, I record a spike and reset the voltage.

That is enough to create a first computational model of a spiking neuron.

[Embedded block: the-core-idea-integrate-leak-then-fire]

The LIF model tracks one main variable: the membrane potential, which I write as `V` . This is the voltage difference between the inside and outside of the neuron.

The equation I used is:

$$ C_m \frac{dV}{dt} = I_{app} + G_L(E_L - V) $$

where:

Symbol

Meaning

$C_m$

membrane capacitance, how much charge the membrane can store

$G_L$

leak conductance, how easily charge leaks out

$E_L$

leak reversal potential, the voltage the cell relaxes toward

$I_{app}$

injected current

$V$

membrane potential

The equation has two parts.

First, the input current $I_{app}$ pushes the voltage up. Second, the leak term $G_L(E_L - V)$ pulls the voltage back toward $E_L$. This is why the model is called leaky integrate-and-fire. It integrates incoming current, but the integration is imperfect because charge leaks away.

In code, I wrote that continuous equation as one explicit Euler update:

```
dV = (I(i) + G_L*(E_L - V(i-1))) / Cm;
V(i) = V(i-1) + dt*dV;
```

Then I add the firing rule:

$$ \text{if } V \ge V_{th}, \text{ record a spike and reset } V \to V_{reset} $$

The spike/reset part is the discrete event rule layered on top of the voltage update:

```
if V(i) >= Vth
    spikes(i) = true;
    V(i-1) = Vpeak;
    V(i) = Vreset;
end
```

In my simulation, I used values similar to the tutorial code:

Parameter

Value used

Leak potential, $E_L$

-70 mV

Threshold, $V_{th}$

-50 mV

Reset, $V_{reset}$

-65 mV

Potassium reversal, $E_K$

-80 mV

Membrane capacitance, $C_m$

100 pF

Membrane resistance, $R_m$

100 MΩ

Leak conductance, $G_L = 1/R_m$

10 nS

[Embedded block: my-first-simulation]

I simulated the neuron under three injected currents: 180 pA, 220 pA, and 500 pA.

The sweep itself just loops over input currents and stores the two summaries I cared about:

```
for k = 1:numel(currents)
    I = currents(k) * ones(size(t));
    [V, spikes] = simulate_lif(I, params);

    meanV(k) = mean(V);
    rate(k) = sum(spikes) / tmax;
end
```

At 180 pA, the voltage rises but never crosses the threshold. The neuron stays silent.

At 220 pA, the voltage crosses threshold, spikes, resets, and repeats. This is the first point where the model behaves like a spiking neuron.

At 500 pA, the voltage reaches threshold much faster after every reset, so the neuron fires at a higher rate.

One important detail: when I plot the voltage trace, the tall spike line is not generated by the LIF differential equation itself. In the basic LIF model, the spike is added as a plotting convention. The actual rule is just: cross threshold, record spike time, reset voltage. This is a simplification compared with real neurons, where sodium and potassium channels create the full action potential shape.

[Embedded block: threshold-current-when-does-the-neuron-start-firing]

The steady-state voltage is the value the neuron would approach if I ignored the spike-and-reset rule:

$$ V_{\infty} = E_L + \frac{I_{app}}{G_L} $$

The neuron starts firing only if this steady-state value is above threshold. So the threshold current is:

$$ I_{th} = G_L(V_{th} - E_L) $$

Using my parameters:

$$ I_{th} = 10,\text{nS} \times (20,\text{mV}) = 200,\text{pA} $$

That matches the simulation: 180 pA is below threshold, 220 pA is above threshold.

[Embedded block: the-firing-rate-curve-input-current-becomes-spike-rate]

After getting spikes, I wanted to know how the firing rate changes as I increase injected current. This is called the f-I curve, firing rate versus input current.

The key observation is simple: more current means the voltage reaches threshold faster, so the neuron fires more frequently.

For the LIF model, the firing rate can be calculated analytically. The interspike interval is:

$$ T_{ISI} = \tau_m \ln\left(\frac{V_{\infty} - V_{reset}}{V_{\infty} - V_{th}}\right) $$

and the firing rate is:

$$ f(I) = \frac{1}{T_{ISI}} $$

where:

$$ \tau_m = \frac{C_m}{G_L} $$

The curve is sharp near threshold because the ideal LIF model is deterministic. Below threshold, no spikes. Just above threshold, spikes suddenly appear. If I add noise to the voltage update, the threshold becomes softer because random fluctuations can occasionally push the membrane above threshold even when the average input is slightly too small.

[Embedded block: what-is-the-nernst-potential]

While building the refractory version of the model, I had to understand reversal potentials, especially the potassium reversal potential $E_K$.

The Nernst potential is the voltage at which one ion species has no net driving force across the membrane. For an ion like potassium, it depends on the ratio of outside concentration to inside concentration:

$$ E_{ion} = \frac{RT}{zF}\ln\left(\frac{c_{out}}{c_{in}}\right) $$

Here, $c_{out}$ and $c_{in}$ are the outside and inside ion concentrations. In plain English: if potassium concentration changes outside the cell, the voltage that balances potassium movement also changes.

This matters because the resting membrane potential is strongly influenced by potassium leak channels. If extracellular potassium is reduced, the outside-to-inside potassium concentration ratio becomes smaller, so $E_K$ becomes more negative. Since resting voltage is pulled toward potassium's reversal potential, the neuron becomes more hyperpolarized, meaning its resting membrane potential moves farther from threshold.

In the tutorial code, I used $E_K = -80$ mV for the refractory conductance model. When a potassium-like refractory conductance opens after a spike, it pulls the voltage downward toward $E_K$, which helps prevent immediate re-spiking.

[Embedded block: adding-a-refractory-period]

Real neurons cannot fire again immediately after a spike. This short recovery window is called the refractory period.

I implemented three versions of a refractory LIF neuron.

I kept the refractory variants behind one method switch so the comparison stayed clean:

```
switch method
    case "clamp"
        inRefractory = t(i) < lastSpike + tRef;

    case "threshold"
        Vth(i) = Vth(i-1) + (Vth0 - Vth(i-1))*dt/tauVth;

    case "conductance"
        Gref(i) = Gref(i-1) * (1 - dt/tauGref);
        Iref = Gref(i) * (EK - V(i-1));
end
```

Method

What I changed

Biological idea

Line style in my plots

Forced voltage clamp

Hold voltage at reset after a spike

Simple absolute refractory period

blue solid line

Raised threshold

Increase threshold after a spike, then let it decay

Harder to spike immediately after firing

orange dotted line

Refractory conductance + raised threshold

Add a temporary potassium-like conductance and raise threshold

More biophysical than a hard clamp

yellow dashed line

The next six figures are for the refractory extensions, not the first basic LIF simulation. I arranged them in this order: first the summary behavior, then the voltage traces that explain why the curves look different.

[Embedded block: mean-voltage-as-input-current-increases]

## Embedded: Image

![figure 1](https://assets.montek.dev/images/blogs/i-built-my-first-leaky-integrate-and-fire-neuron-model/Figure_1.png)

Figure 1 shows the mean membrane potential as applied current increases. This is useful because two models can produce spikes but still have different voltage behavior between spikes. The forced voltage clamp can push the mean voltage downward at high input because the neuron spends more time artificially held at reset. The raised-threshold method behaves more naturally because the voltage is allowed to keep evolving after reset.

[Embedded block: firing-rate-curve]

## Embedded: Image

![figure 2](https://assets.montek.dev/images/blogs/i-built-my-first-leaky-integrate-and-fire-neuron-model/Figure_2.png)

Figure 2 shows the firing rate as a function of applied current. This is the main f-I curve for the refractory models. The raised-threshold model fires fastest because the membrane voltage is not clamped down after each spike. The forced-clamp model is slower because the neuron must wait through a fixed refractory window. The refractory conductance model is also slower because the temporary potassium-like conductance pulls the voltage downward after each spike.

[Embedded block: mean-voltage-versus-firing-rate]

## Embedded: Image

![figure 3](https://assets.montek.dev/images/blogs/i-built-my-first-leaky-integrate-and-fire-neuron-model/Figure_3.png)

Figure 3 compares mean membrane potential directly against firing rate. This makes the modeling tradeoff clearer. A simple refractory clamp is computationally easy, but it can distort the voltage statistics. A dynamic threshold or conductance-based refractory mechanism preserves more of the voltage dynamics.

[Embedded block: forced-voltage-clamp-trace]

## Embedded: Image

![figure 4](https://assets.montek.dev/images/blogs/i-built-my-first-leaky-integrate-and-fire-neuron-model/Figure_4.png)

Figure 4 shows the forced voltage clamp method. After each spike, the membrane potential is held at the reset value for the refractory period. This is the simplest implementation, but it is also the most artificial because the voltage is manually fixed instead of being shaped by an ionic current.

[Embedded block: raised-threshold-trace]

## Embedded: Image

![figure 5](https://assets.montek.dev/images/blogs/i-built-my-first-leaky-integrate-and-fire-neuron-model/Figure_5.png)

Figure 5 shows the raised-threshold method. The voltage resets after a spike, but the threshold temporarily increases and then decays back. This separates the idea of resetting voltage from the idea of preventing another spike.

[Embedded block: refractory-conductance-with-raised-threshold-trace]

## Embedded: Image

![figure 6](https://assets.montek.dev/images/blogs/i-built-my-first-leaky-integrate-and-fire-neuron-model/Figure_6.png)

Figure 6 shows the refractory conductance method combined with a raised threshold. Here, the voltage drops more biologically because the refractory conductance acts like a temporary potassium current. Instead of manually forcing the voltage to stay low, the model creates a current that naturally pulls the membrane potential downward toward the potassium reversal potential.

The main lesson is that different simplifications can give similar spike trains but different voltage statistics. If I only care about spike times, a hard reset might be enough. If I care about the mean voltage, refractory conductance or dynamic threshold is more realistic.

[Embedded block: spike-rate-adaptation]

The next extension is spike-rate adaptation. In many real neurons, the first few spikes after a current step are close together, but later spikes spread farther apart.

This can be modeled by adding a slow potassium-like conductance that increases after each spike and decays slowly between spikes. It is similar to the refractory conductance idea, but with two key differences:

Adaptation uses the same state-variable pattern, just with a slower decay and a smaller jump:

```
Gadapt(i) = Gadapt(i-1) * (1 - dt/tauAdapt);

if spikes(i)
    Gadapt(i) = Gadapt(i) + deltaGadapt;
end
```

Feature

Refractory conductance

Adaptation conductance

Strength

large

smaller

Timescale

fast, a few ms

slow, often hundreds of ms

Effect

prevents immediate next spike

gradually slows firing

Purpose

enforces recovery

encodes recent spiking history

So refractory period and spike-rate adaptation are similar computationally: both can use a conductance that jumps after spikes and decays over time. But biologically and dynamically, they are different. Refractory conductance prevents immediate firing. Adaptation conductance changes the firing pattern over a longer window.

[Embedded block: could-adaptation-make-a-neuron-fire-a-few-times-and-then-stop-forever]

In the simple adaptation model I implemented, the answer is no, not if the applied current remains truly above threshold.

And the reason, the adaptation conductance only increases when spikes happen. If the neuron stops spiking, the adaptation conductance decays back toward zero. Once it decays enough, the same applied current can push the voltage back to threshold again. So adaptation can slow firing or create a long pause, but it cannot permanently silence a suprathreshold LIF neuron by itself.

The only exception would be if the model included some nonzero adaptation current that persists without spikes, or if the applied current was actually below threshold and the early spikes were caused by a transient or noise.

[Embedded block: chapter-question-checks-i-worked-through]

After building the model, I also used it to sanity-check a few chapter-style questions. I treat this section as a short appendix rather than a new model.

[Embedded block: 1-what-happens-if-extracellular-potassium-is-reduced]

The resting membrane potential becomes more negative. Reducing extracellular potassium makes the potassium Nernst potential more negative, and since resting voltage is strongly influenced by potassium conductance, the neuron hyperpolarizes.

[Embedded block: 2-steady-state-voltage-and-firing-rate-calculation]

This calculation uses a separate chapter parameter set from the earlier simulation. Using the common interpretation of those parameters:

$$ E_L = -70,\text{mV},\quad G_L = 100,\text{nS},\quad C_m = 1,\text{nF},\quad V_{th} = -50,\text{mV},\quad V_{reset} = -80,\text{mV} $$

For 6 nA:

$$ V_{\infty} = E_L + \frac{I}{G_L} $$

$$ V_{\infty} = -70,\text{mV} + \frac{6,\text{nA}}{100,\text{nS}} = -10,\text{mV} $$

So the steady-state membrane potential would be -10 mV if the reset rule were ignored. In the actual LIF model, the neuron would spike before sitting at that voltage.

For 15 nA:

$$ V_{\infty} = -70,\text{mV} + \frac{15,\text{nA}}{100,\text{nS}} = 80,\text{mV} $$

The membrane time constant is:

$$ \tau_m = \frac{C_m}{G_L} = \frac{1,\text{nF}}{100,\text{nS}} = 10,\text{ms} $$

Then:

$$ T_{ISI} = 10,\text{ms}\ln\left(\frac{80 - (-80)}{80 - (-50)}\right) $$

$$ T_{ISI} \approx 2.08,\text{ms} $$

So the firing rate is:

$$ f \approx \frac{1}{0.00208} \approx 481,\text{Hz} $$

This is the no-refractory firing rate. If I added a refractory period, the rate would be lower.

[Embedded block: 3-two-similarities-and-two-differences-between-refractory-period-and-adaptation-conductance]

Similarities:

1. Both are usually triggered by spikes.
2. Both can be modeled with an extra state variable that decays over time.

Differences:

1. Refractory mechanisms are short and strong. Adaptation is slower and weaker.
2. Refractory mechanisms prevent immediate spiking. Adaptation mainly reduces the firing rate over time.

[Embedded block: 4-can-simple-adaptation-cause-a-few-spikes-and-then-permanent-silence]

Not in the basic spike-triggered adaptation model. If spikes stop, the adaptation conductance decays. If the input current is still above threshold, the neuron should eventually spike again.

[Embedded block: what-the-lif-model-gets-right-and-wrong]

The LIF model gets the basic input-output logic right. It captures how injected current can push membrane voltage toward threshold and how stronger current produces higher firing rates.

But it is not a full biological neuron.

It does not model voltage-gated sodium channels, voltage-gated potassium channels, calcium dynamics, dendrites, ion-channel modulation, or spatial structure. It also uses an artificial threshold and reset rule instead of generating a real action potential.

That is why models like ELIF and AELIF exist.

The Exponential LIF model adds an exponential term near threshold, which makes the voltage accelerate upward like a real spike onset. The Adaptive Exponential LIF model adds adaptation, allowing richer firing patterns. But if the goal is to understand ion-channel mechanisms directly, then eventually we need conductance-based models such as Hodgkin-Huxley.

[Embedded block: what-i-learned]

Building this model made the theory feel concrete.

The LIF neuron is not powerful because it is biologically complete. It is powerful because it gives a clean bridge between electrical intuition and computational neuroscience. It teaches how voltage integrates input, how leak creates a timescale, how threshold creates spikes, how firing rate emerges from current, and how small modeling choices can change the interpretation of neural behavior.
