# Stochastic Differential Equation (SDEs)

Continous-state dynamical systems lead determinitical Ordinary Differential Equations systems (ODEs). Stochastic versions of this model are Stochastic Differential Equations (SDEs).

## Fitzhugh Nagumo Model
    

\begin{equation}
\begin{split}
\frac{dv}{dt} =&  v-v^3-w+I_{ext}\\
\frac{dw}{dt} =& \frac{1}{\tau}(v-a-bw)
\end{split}
\end{equation}


```python
import numpy as np
import pandas as pd

def fitzhugh_nagumo(x, t, a, b, tau, I):
    """
        Fitzhugh-Nagumo model.
    """
    v, w = x
    
    dvdt = v-v**3-w+I
    dwdt = (v-a-b*w)/tau
    
    return np.array([dvdt, dwdt])
```


```python
from functools import partial
from scipy.integrate import odeint

def integration_SDE(model, noise_flow, y0, t) :
    ''' Euler integration.
        y(t) = f(Y(t),t)dt + g(Yt,t)dBt
        y(0) = y0 
    '''
    y = np.zeros((len(t),len(y0)))
    y[0] = y0
    
    for n, dt in enumerate(np.diff(t),1):
        y[n] = y[n-1] + model(y[n-1],dt) * dt + model(y[n-1],dt) * np.random.normal(0,np.sqrt(dt))
    
    return y

n_runs = 10
t_span = np.linspace(0, 1000, num=10000)
brownian_noise = lambda y,t: 0.01


initial_conditions = [(-0.5,-0.1), [0, -0.16016209760708508]]

I_ext = [0, 0.19, 0.22, 0.5]


import matplotlib.pyplot as plt
fig, ax = plt.subplots(len(I_ext), 1, figsize=(15, 10*len(I_ext)))

for idx, current_ext in enumerate(I_ext):
    # Evaluate fitzhugh_nagumo model with specified pameters a, b, tau, I in param
    param = {'a': -0.3, 'b':1.4, 'tau':20, 'I':current_ext}
    
    model = partial(fitzhugh_nagumo, **param)
    ic = initial_conditions[1]

    sde_solutions = np.zeros((10000,2,n_runs))
    for i in range(n_runs):
        sde_solutions[:,:,i] = integration_SDE(model,
                                      brownian_noise,
                                      y0=ic,
                                      t=t_span)

    ode_solution = odeint(model, y0=ic, t=t_span)

    v_sde, w_sde = (sde_solutions[:,0,:], sde_solutions[:,1,:])

    # Drop nans in case stochastic run results in ill solution
    v_sde = pd.DataFrame(v_sde).dropna(axis=1).to_numpy()
    v_ode, w_ode = (ode_solution[:,0], ode_solution[:,1])


    ax[idx].plot(t_span, v_ode, label='V - ODE', color='k')
    ax[idx].plot(t_span, np.median(v_sde, 1), label=r'Median V - SDE', color='r', linestyle='-.')
    ax[idx].plot(t_span, v_sde, color='r', linestyle='-.', alpha=0.2)
    ax[idx].set_xlabel('Time (ms)')
    ax[idx].set_ylabel('Membrane Potential (mV)')
    ax[idx].set_title(r'External Stimulus $I_e=${}'.format(param['I']))
    ax[idx].set_ylim([-2,2])
    ax[idx].legend()




```


```python

```

de35877c7c2cf6b71921d819a638ef4c56649037
ipynb
Jupyter Notebook
SDEs/.ipynb_checkpoints/example-checkpoint.ipynb
ChaosDonkey06/MODSIM_IBIO
e2ce70538aaba3b01f22cf037db21a80316a802f
Apache-2.0
SDEs/.ipynb_checkpoints/example-checkpoint.ipynb
ChaosDonkey06/MODSIM_IBIO
e2ce70538aaba3b01f22cf037db21a80316a802f
Apache-2.0
SDEs/.ipynb_checkpoints/example-checkpoint.ipynb
ChaosDonkey06/MODSIM_IBIO
e2ce70538aaba3b01f22cf037db21a80316a802f
Apache-2.0
Qwen/Qwen-72B
1. YES
2. YES