예제 #1
0
    def dfun(self, state_variables, coupling, local_coupling=0.00):
        r"""
        .. math::
            T \dot{\nu_\mu} &= -F_\mu(\nu_e,\nu_i) + \nu_\mu ,\all\mu\in\{e,i\}\\
            dot{W}_k &= W_k/tau_w-b*E_k  \\

        """
        E = state_variables[0, :]
        I = state_variables[1, :]
        W = state_variables[2, :]
        derivative = numpy.empty_like(state_variables)

        # long-range coupling
        c_0 = coupling[0, :]

        # short-range (local) coupling
        lc_E = local_coupling * E
        lc_I = local_coupling * I

        # Excitatory firing rate derivation
        derivative[0] = (self.TF_excitatory(
            E + c_0 + lc_E + self.external_input_ex_ex,
            I + lc_I + self.external_input_ex_in, W) - E) / self.T
        # Inhibitory firing rate derivation
        derivative[1] = (
            self.TF_inhibitory(E + lc_E + self.external_input_in_ex, I + lc_I +
                               self.external_input_in_in, W) - I) / self.T
        # Adaptation
        derivative[2] = -W / self.tau_w + self.b * E

        return derivative
예제 #2
0
파일: Zerlaut.py 프로젝트: xojeng/YAdEX_TVB
    def dfun(self, state_variables, coupling, local_coupling=0.00):
        r"""
        .. math::
            T \dot{\nu_\mu} &= -F_\mu(\nu_e,\nu_i) + \nu_\mu ,\all\mu\in\{e,i\}\\
            dot{W}_k &= W_k/tau_w-b*E_k  \\

        """
        E = state_variables[0, :]
        I = state_variables[1, :]
        W_e = state_variables[2, :]
        W_i = state_variables[3, :]
        noise = state_variables[4, :]
        derivative = numpy.empty_like(state_variables)

        # long-range coupling
        c_0 = coupling[0, :]

        # short-range (local) coupling
        lc_E = local_coupling * E
        lc_I = local_coupling * I

        # external firing rate
        Fe_ext = c_0 + lc_E + self.weight_noise * noise
        index_bad_input = numpy.where(Fe_ext * self.K_ext_e < 0)
        Fe_ext[index_bad_input] = 0.0
        Fi_ext = lc_I

        # Excitatory firing rate derivation
        derivative[0] = (self.TF_excitatory(
            E, I, Fe_ext + self.external_input_ex_ex,
            Fi_ext + self.external_input_ex_in, W_e) - E) / self.T
        # Inhibitory firing rate derivation
        derivative[1] = (self.TF_inhibitory(
            E, I, Fe_ext + self.external_input_in_ex,
            Fi_ext + self.external_input_in_in, W_i) - I) / self.T
        # Adaptation excitatory
        mu_V, sigma_V, T_V = self.get_fluct_regime_vars(
            E, I, Fe_ext + self.external_input_ex_ex,
            Fi_ext + self.external_input_ex_in, W_e, self.Q_e, self.tau_e,
            self.E_e, self.Q_i, self.tau_i, self.E_i, self.g_L, self.C_m,
            self.E_L_e, self.N_tot, self.p_connect_e, self.p_connect_i, self.g,
            self.K_ext_e, self.K_ext_i)
        derivative[2] = -W_e / self.tau_w_e + self.b_e * E + self.a_e * (
            mu_V - self.E_L_e) / self.tau_w_e
        # Adaptation inhibitory
        mu_V, sigma_V, T_V = self.get_fluct_regime_vars(
            E, I, Fe_ext + self.external_input_in_ex,
            Fi_ext + self.external_input_in_in, W_i, self.Q_e, self.tau_e,
            self.E_e, self.Q_i, self.tau_i, self.E_i, self.g_L, self.C_m,
            self.E_L_i, self.N_tot, self.p_connect_e, self.p_connect_i, self.g,
            self.K_ext_e, self.K_ext_i)
        derivative[3] = -W_i / self.tau_w_i + self.b_i * I + self.a_i * (
            mu_V - self.E_L_i) / self.tau_w_i
        derivative[4] = -noise / self.tau_OU

        return derivative
예제 #3
0
    def _numpy_dfun(self, state_variables, coupling, local_coupling=0.0, ev=numexpr.evaluate):
        r"""
        The two state variables :math:`V` and :math:`W` are typically considered
        to represent a function of the neuron's membrane potential, such as the
        firing rate or dendritic currents, and a recovery variable, respectively.
        If there is a time scale hierarchy, then typically :math:`V` is faster
        than :math:`W` corresponding to a value of :math:`\tau` greater than 1.

        The equations of the generic 2D population model read

        .. math::
                \dot{V} &= d \, \tau (-f V^3 + e V^2 + g V + \alpha W + \gamma I), \\
                \dot{W} &= \dfrac{d}{\tau}\,\,(c V^2 + b V - \beta W + a),

        where external currents :math:`I` provide the entry point for local,
        long-range connectivity and stimulation.

        """
        V = state_variables[0, :]
        V = numpy.where(self.V_m == 0, V, self.V_m)
        W = state_variables[1, :]

        #[State_variables, nodes]
        c_0 = coupling[0, :]

        tau = self.tau
        I = self.I
        a = self.a
        b = self.b
        c = self.c
        d = self.d
        e = self.e
        f = self.f
        g = self.g
        beta = self.beta
        alpha = self.alpha
        gamma = self.gamma

        lc_0 = local_coupling * V

        # Pre-allocate the result array then instruct numexpr to use it as output.
        # This avoids an expensive array concatenation
        derivative = numpy.empty_like(state_variables)

        ev('d * tau * (alpha * W - f * V**3 + e * V**2 + g * V + gamma * I + gamma *c_0 + lc_0)', out=derivative[0])
        ev('d * (a + b * V + c * V**2 - beta * W) / tau', out=derivative[1])

        return derivative
예제 #4
0
    def dfun(self, state_variables, coupling, local_coupling=0.00):
        r"""

        .. math::
            T \dot{\nu_\mu} &= -F_\mu(\nu_e,\nu_i) + \nu_\mu ,\all\mu\in\{e,i\}

        """
        #this variable can be internal variable for optimization
        TF = Transfer_Function(self, self.P_e, self.P_i)

        E = state_variables[0, :]
        I = state_variables[1, :]
        derivative = numpy.empty_like(state_variables)

        # long-range coupling
        c_0 = coupling[0, :]

        # short-range (local) coupling
        lc_E = local_coupling * E
        lc_I = local_coupling * I

        #equation is inspired from github of Zerlaut :
        # https://bitbucket.org/yzerlaut/mean_field_for_multi_input_integration
        # ./mean_field/master_equation.py
        # Excitatory firing rate derivation
        derivative[0] = (
            TF.excitatory(E + c_0 + lc_E + self.external_input,
                          I + lc_I + self.external_input) -
            E) / self.T  #TODO : need to check the accuracy of the equation
        # Inhibitory firing rate derivation
        derivative[1] = (
            TF.inhibitory(E + self.external_input,
                          I + lc_I + self.external_input) -
            I) / self.T  #TODO : need to check the accuracy of the equation

        return derivative
예제 #5
0
    def dfun(self, state_variables, coupling, local_coupling=0.00):
        r"""
        .. math::
            \forall \mu,\lambda,\eta \in \{e,i\}^3\, ,
            \left\{
            \begin{split}
            T \, \frac{\partial \nu_\mu}{\partial t} = & (\mathcal{F}_\mu - \nu_\mu )
            + \frac{1}{2} \, c_{\lambda \eta} \,
            \frac{\partial^2 \mathcal{F}_\mu}{\partial \nu_\lambda \partial \nu_\eta} \\
            T \, \frac{\partial c_{\lambda \eta} }{\partial t}  =  & A_{\lambda \eta} +
            (\mathcal{F}_\lambda - \nu_\lambda ) \, (\mathcal{F}_\eta - \nu_\eta ) + \\
            & c_{\lambda \mu} \frac{\partial \mathcal{F}_\mu}{\partial \nu_\lambda} +
            c_{\mu \eta} \frac{\partial \mathcal{F}_\mu}{\partial \nu_\eta}
            - 2  c_{\lambda \eta}
            \end{split}
            \right.
            dot{W}_k &= W_k/tau_w-b*E_k  \\

            with:
            A_{\lambda \eta} =
            \left\{
            \begin{split}
            \frac{\mathcal{F}_\lambda \, (1/T - \mathcal{F}_\lambda)}{N_\lambda}
            \qquad & \textrm{if  } \lambda=\eta \\
            0 \qquad & \textrm{otherwise}
            \end{split}
            \right.

        """
        N_e = self.N_tot * (1 - self.g)
        N_i = self.N_tot * self.g

        E = state_variables[0, :]
        I = state_variables[1, :]
        C_ee = state_variables[2, :]
        C_ei = state_variables[3, :]
        C_ii = state_variables[4, :]
        W = state_variables[5, :]
        derivative = numpy.empty_like(state_variables)

        # long-range coupling
        c_0 = coupling[0, :]

        # short-range (local) coupling
        lc_E = local_coupling * E
        lc_I = local_coupling * I

        E_input_excitatory = E + c_0 + lc_E + self.external_input_ex_ex
        E_input_inhibitory = E + lc_E + self.external_input_in_ex
        I_input_excitatory = I + lc_I + self.external_input_ex_in
        I_input_inhibitory = I + lc_I + self.external_input_in_in

        # Transfer function of excitatory and inhibitory neurons
        _TF_e = self.TF_excitatory(E_input_excitatory, I_input_excitatory, W)
        _TF_i = self.TF_inhibitory(E_input_inhibitory, I_input_inhibitory, W)

        # Derivatives taken numerically : use a central difference formula with spacing `dx`
        def _diff_fe(TF, fe, fi, W, df=1e-7):
            return (TF(fe + df, fi, W) - TF(fe - df, fi, W)) / (2 * df * 1e3)

        def _diff_fi(TF, fe, fi, W, df=1e-7):
            return (TF(fe, fi + df, W) - TF(fe, fi - df, W)) / (2 * df * 1e3)

        def _diff2_fe_fe_e(fe, fi, W, df=1e-7):
            TF = self.TF_excitatory
            return (TF(fe + df, fi, W) - 2 * _TF_e + TF(fe - df, fi, W)) / (
                (df * 1e3)**2)

        def _diff2_fe_fe_i(fe, fi, W, df=1e-7):
            TF = self.TF_inhibitory
            return (TF(fe + df, fi, W) - 2 * _TF_i + TF(fe - df, fi, W)) / (
                (df * 1e3)**2)

        def _diff2_fi_fe(TF, fe, fi, W, df=1e-7):
            return (_diff_fi(TF, fe + df, fi, W) -
                    _diff_fi(TF, fe - df, fi, W)) / (2 * df * 1e3)

        def _diff2_fe_fi(TF, fe, fi, W, df=1e-7):
            return (_diff_fe(TF, fe, fi + df, W) -
                    _diff_fe(TF, fe, fi - df, W)) / (2 * df * 1e3)

        def _diff2_fi_fi_e(fe, fi, W, df=1e-7):
            TF = self.TF_excitatory
            return (TF(fe, fi + df, W) - 2 * _TF_e + TF(fe, fi - df, W)) / (
                (df * 1e3)**2)

        def _diff2_fi_fi_i(fe, fi, W, df=1e-7):
            TF = self.TF_inhibitory
            return (TF(fe, fi + df, W) - 2 * _TF_i + TF(fe, fi - df, W)) / (
                (df * 1e3)**2)

        #Precompute some result
        _diff_fe_TF_e = _diff_fe(self.TF_excitatory, E_input_excitatory,
                                 I_input_excitatory, W)
        _diff_fe_TF_i = _diff_fe(self.TF_inhibitory, E_input_inhibitory,
                                 I_input_inhibitory, W)
        _diff_fi_TF_e = _diff_fi(self.TF_excitatory, E_input_excitatory,
                                 I_input_excitatory, W)
        _diff_fi_TF_i = _diff_fi(self.TF_inhibitory, E_input_inhibitory,
                                 I_input_inhibitory, W)

        # equation is inspired from github of Zerlaut :
        # https://github.com/yzerlaut/notebook_papers/blob/master/modeling_mesoscopic_dynamics/mean_field/master_equation.py
        # Excitatory firing rate derivation
        derivative[0] = (
            _TF_e - E + .5 * C_ee *
            _diff2_fe_fe_e(E_input_excitatory, I_input_excitatory, W) +
            .5 * C_ei * _diff2_fe_fi(self.TF_excitatory, E_input_excitatory,
                                     I_input_excitatory, W) +
            .5 * C_ei * _diff2_fi_fe(self.TF_excitatory, E_input_excitatory,
                                     I_input_excitatory, W) + .5 * C_ii *
            _diff2_fi_fi_e(E_input_excitatory, I_input_excitatory, W)) / self.T
        # Inhibitory firing rate derivation
        derivative[1] = (
            _TF_i - I + .5 * C_ee *
            _diff2_fe_fe_i(E_input_inhibitory, I_input_inhibitory, W) +
            .5 * C_ei * _diff2_fe_fi(self.TF_inhibitory, E_input_inhibitory,
                                     I_input_inhibitory, W) +
            .5 * C_ei * _diff2_fi_fe(self.TF_inhibitory, E_input_inhibitory,
                                     I_input_inhibitory, W) + .5 * C_ii *
            _diff2_fi_fi_i(E_input_inhibitory, I_input_inhibitory, W)) / self.T
        # Covariance excitatory-excitatory derivation
        derivative[2] = (_TF_e * (1. / self.T - _TF_e) / N_e +
                         (_TF_e - E)**2 + 2. * C_ee * _diff_fe_TF_e +
                         2. * C_ei * _diff_fi_TF_i - 2. * C_ee) / self.T
        # Covariance excitatory-inhibitory or inhibitory-excitatory derivation
        derivative[3] = (
            (_TF_e - E) *
            (_TF_i - I) + C_ee * _diff_fe_TF_e + C_ei * _diff_fe_TF_i +
            C_ei * _diff_fi_TF_e + C_ii * _diff_fi_TF_i - 2. * C_ei) / self.T
        # Covariance inhibitory-inhibitory derivation
        derivative[4] = (_TF_i * (1. / self.T - _TF_i) / N_i +
                         (_TF_i - I)**2 + 2. * C_ii * _diff_fi_TF_i +
                         2. * C_ei * _diff_fe_TF_e - 2. * C_ii) / self.T
        # Adaptation
        derivative[5] = -W / self.tau_w + self.b * E

        return derivative
예제 #6
0
파일: Zerlaut.py 프로젝트: xojeng/YAdEX_TVB
    def dfun(self, state_variables, coupling, local_coupling=0.00):
        r"""
        .. math::
            \forall \mu,\lambda,\eta \in \{e,i\}^3\, ,
            \left\{
            \begin{split}
            T \, \frac{\partial \nu_\mu}{\partial t} = & (\mathcal{F}_\mu - \nu_\mu )
            + \frac{1}{2} \, c_{\lambda \eta} \,
            \frac{\partial^2 \mathcal{F}_\mu}{\partial \nu_\lambda \partial \nu_\eta} \\
            T \, \frac{\partial c_{\lambda \eta} }{\partial t}  =  & A_{\lambda \eta} +
            (\mathcal{F}_\lambda - \nu_\lambda ) \, (\mathcal{F}_\eta - \nu_\eta ) + \\
            & c_{\lambda \mu} \frac{\partial \mathcal{F}_\mu}{\partial \nu_\lambda} +
            c_{\mu \eta} \frac{\partial \mathcal{F}_\mu}{\partial \nu_\eta}
            - 2  c_{\lambda \eta}
            \end{split}
            \right.
            dot{W}_k &= W_k/tau_w-b*E_k  \\

            with:
            A_{\lambda \eta} =
            \left\{
            \begin{split}
            \frac{\mathcal{F}_\lambda \, (1/T - \mathcal{F}_\lambda)}{N_\lambda}
            \qquad & \textrm{if  } \lambda=\eta \\
            0 \qquad & \textrm{otherwise}
            \end{split}
            \right.

        """
        #number of neurons
        N_e = self.N_tot * (1 - self.g)
        N_i = self.N_tot * self.g
        #state variable
        E = state_variables[0, :]
        I = state_variables[1, :]
        C_ee = state_variables[2, :]
        C_ei = state_variables[3, :]
        C_ii = state_variables[4, :]
        W_e = state_variables[5, :]
        W_i = state_variables[6, :]
        noise = state_variables[7, :]
        derivative = numpy.empty_like(state_variables)

        # long-range coupling
        c_0 = coupling[0, :]

        # short-range (local) coupling
        lc_E = local_coupling * E
        lc_I = local_coupling * I

        # external firing rate for the different population
        E_input_excitatory = c_0 + lc_E + self.external_input_ex_ex + self.weight_noise * noise
        index_bad_input = numpy.where(E_input_excitatory < 0)
        E_input_excitatory[index_bad_input] = 0.0
        E_input_inhibitory = c_0 + lc_E + self.external_input_in_ex + self.weight_noise * noise
        index_bad_input = numpy.where(E_input_inhibitory < 0)
        E_input_inhibitory[index_bad_input] = 0.0
        I_input_excitatory = lc_I + self.external_input_ex_in
        I_input_inhibitory = lc_I + self.external_input_in_in

        # Transfer function of excitatory and inhibitory neurons
        _TF_e = self.TF_excitatory(E, I, E_input_excitatory,
                                   I_input_excitatory, W_e)
        _TF_i = self.TF_inhibitory(E, I, E_input_inhibitory,
                                   I_input_inhibitory, W_i)

        # Derivatives taken numerically : use a central difference formula with spacing `dx`
        df = 1e-7

        def _diff_fe(TF, fe, fi, fe_ext, fi_ext, W, df=df):
            return (TF(fe + df, fi, fe_ext, fi_ext, W) -
                    TF(fe - df, fi, fe_ext, fi_ext, W)) / (2 * df * 1e3)

        def _diff_fi(TF, fe, fi, fe_ext, fi_ext, W, df=df):
            return (TF(fe, fi + df, fe_ext, fi_ext, W) -
                    TF(fe, fi - df, fe_ext, fi_ext, W)) / (2 * df * 1e3)

        def _diff2_fe_fe_e(fe, fi, fe_ext, fi_ext, W, df=df):
            TF = self.TF_excitatory
            return (TF(fe + df, fi, fe_ext, fi_ext, W) - 2 * _TF_e +
                    TF(fe - df, fi, fe_ext, fi_ext, W)) / ((df * 1e3)**2)

        def _diff2_fe_fe_i(fe, fi, fe_ext, fi_ext, W, df=df):
            TF = self.TF_inhibitory
            return (TF(fe + df, fi, fe_ext, fi_ext, W) - 2 * _TF_i +
                    TF(fe - df, fi, fe_ext, fi_ext, W)) / ((df * 1e3)**2)

        def _diff2_fi_fe(TF, fe, fi, fe_ext, fi_ext, W, df=df):
            return (_diff_fi(TF, fe + df, fi, fe_ext, fi_ext, W) - _diff_fi(
                TF, fe - df, fi, fe_ext, fi_ext, W)) / (2 * df * 1e3)

        def _diff2_fe_fi(TF, fe, fi, fe_ext, fi_ext, W, df=df):
            return (_diff_fe(TF, fe, fi + df, fe_ext, fi_ext, W) - _diff_fe(
                TF, fe, fi - df, fe_ext, fi_ext, W)) / (2 * df * 1e3)

        def _diff2_fi_fi_e(fe, fi, fe_ext, fi_ext, W, df=df):
            TF = self.TF_excitatory
            return (TF(fe, fi + df, fe_ext, fi_ext, W) - 2 * _TF_e +
                    TF(fe, fi - df, fe_ext, fi_ext, W)) / ((df * 1e3)**2)

        def _diff2_fi_fi_i(fe, fi, fe_ext, fi_ext, W, df=df):
            TF = self.TF_inhibitory
            return (TF(fe, fi + df, fe_ext, fi_ext, W) - 2 * _TF_i +
                    TF(fe, fi - df, fe_ext, fi_ext, W)) / ((df * 1e3)**2)

        #Precompute some result
        _diff_fe_TF_e = _diff_fe(self.TF_excitatory, E, I, E_input_excitatory,
                                 I_input_excitatory, W_e)
        _diff_fe_TF_i = _diff_fe(self.TF_inhibitory, E, I, E_input_inhibitory,
                                 I_input_inhibitory, W_i)
        _diff_fi_TF_e = _diff_fi(self.TF_excitatory, E, I, E_input_excitatory,
                                 I_input_excitatory, W_e)
        _diff_fi_TF_i = _diff_fi(self.TF_inhibitory, E, I, E_input_inhibitory,
                                 I_input_inhibitory, W_i)

        # equation is inspired from github of Zerlaut :
        # https://github.com/yzerlaut/notebook_papers/blob/master/modeling_mesoscopic_dynamics/mean_field/master_equation.py
        # Excitatory firing rate derivation
        derivative[0] = (
            _TF_e - E + .5 * C_ee * _diff2_fe_fe_e(
                E, I, E_input_excitatory, I_input_excitatory, W_e) + .5 *
            C_ei * _diff2_fe_fi(self.TF_excitatory, E, I, E_input_excitatory,
                                I_input_excitatory, W_e) + .5 * C_ei *
            _diff2_fi_fe(self.TF_excitatory, E, I, E_input_excitatory,
                         I_input_excitatory, W_e) +
            .5 * C_ii * _diff2_fi_fi_e(E, I, E_input_excitatory,
                                       I_input_excitatory, W_e)) / self.T
        # Inhibitory firing rate derivation
        derivative[1] = (
            _TF_i - I + .5 * C_ee * _diff2_fe_fe_i(
                E, I, E_input_inhibitory, I_input_inhibitory, W_i) + .5 *
            C_ei * _diff2_fe_fi(self.TF_inhibitory, E, I, E_input_inhibitory,
                                I_input_inhibitory, W_i) + .5 * C_ei *
            _diff2_fi_fe(self.TF_inhibitory, E, I, E_input_inhibitory,
                         I_input_inhibitory, W_i) +
            .5 * C_ii * _diff2_fi_fi_i(E, I, E_input_inhibitory,
                                       I_input_inhibitory, W_i)) / self.T
        # Covariance excitatory-excitatory derivation
        derivative[2] = (_TF_e * (1. / self.T - _TF_e) / N_e +
                         (_TF_e - E)**2 + 2. * C_ee * _diff_fe_TF_e +
                         2. * C_ei * _diff_fi_TF_i - 2. * C_ee) / self.T
        # Covariance excitatory-inhibitory or inhibitory-excitatory derivation
        derivative[3] = (
            (_TF_e - E) *
            (_TF_i - I) + C_ee * _diff_fe_TF_e + C_ei * _diff_fe_TF_i +
            C_ei * _diff_fi_TF_e + C_ii * _diff_fi_TF_i - 2. * C_ei) / self.T
        # Covariance inhibitory-inhibitory derivation
        derivative[4] = (_TF_i * (1. / self.T - _TF_i) / N_i +
                         (_TF_i - I)**2 + 2. * C_ii * _diff_fi_TF_i +
                         2. * C_ei * _diff_fe_TF_e - 2. * C_ii) / self.T

        # Adaptation excitatory
        mu_V, sigma_V, T_V = self.get_fluct_regime_vars(
            E, I, E_input_excitatory, I_input_excitatory, W_e, self.Q_e,
            self.tau_e, self.E_e, self.Q_i, self.tau_i, self.E_i, self.g_L,
            self.C_m, self.E_L_e, self.N_tot, self.p_connect_e,
            self.p_connect_i, self.g, self.K_ext_e, self.K_ext_i)
        derivative[5] = -W_e / self.tau_w_e + self.b_e * E + self.a_e * (
            mu_V - self.E_L_e) / self.tau_w_e

        # Adaptation inhibitory
        mu_V, sigma_V, T_V = self.get_fluct_regime_vars(
            E, I, E_input_inhibitory, I_input_inhibitory, W_i, self.Q_e,
            self.tau_e, self.E_e, self.Q_i, self.tau_i, self.E_i, self.g_L,
            self.C_m, self.E_L_i, self.N_tot, self.p_connect_e,
            self.p_connect_i, self.g, self.K_ext_e, self.K_ext_i)
        derivative[6] = -W_i / self.tau_w_i + self.b_i * I + self.a_i * (
            mu_V - self.E_L_i) / self.tau_w_i

        derivative[7] = -noise / self.tau_OU
        return derivative
    def dfun(self, state_variables, coupling, local_coupling=0.00):
        r"""

        .. math::
           #TODO need to write the equation

        """
        #this variable can be internal varible for optimization
        TF = Transfer_Function(self, self.P_e, self.P_i)
        Ne = self.N_tot * (1 - self.g)
        Ni = self.N_tot * self.g

        E = state_variables[0, :]
        I = state_variables[1, :]
        C_ee = state_variables[2, :]
        C_ei = state_variables[3, :]
        C_ii = state_variables[4, :]
        derivative = numpy.empty_like(state_variables)

        # long-range coupling
        c_0 = coupling[0, :]

        # short-range (local) coupling
        lc_E = local_coupling * E
        lc_I = local_coupling * I

        #equation is inspired from github of Zerlaut :
        # function from github of Zerlaut :
        # https://github.com/yzerlaut/notebook_papers/blob/master/modeling_mesoscopic_dynamics/mean_field/master_equation.py

        # Excitatory firing rate derivation
        # #TODO : need to check the accuracy of all equations
        derivative[0] = 1./self.T*(\
                .5*C_ee*self._diff2_fe_fe(TF.excitatory, E+c_0+lc_E+ self.external_input, I+lc_I+ self.external_input)+\
                .5*C_ei*self._diff2_fe_fi(TF.excitatory, E+c_0+lc_E+ self.external_input, I+lc_I+ self.external_input)+\
                .5*C_ei*self._diff2_fi_fe(TF.excitatory, E+c_0+lc_E+ self.external_input, I+lc_I+ self.external_input)+\
                .5*C_ii*self._diff2_fi_fi(TF.excitatory, E+c_0+lc_E+ self.external_input, I+lc_I+ self.external_input)+\
                TF.excitatory(E+c_0+lc_E+ self.external_input, I+lc_I+ self.external_input)-E)
        #Inhibitory firing rate derivation
        derivative[1] = 1./self.T*(\
                .5*C_ee*self._diff2_fe_fe(TF.inhibitory, E+lc_E+ self.external_input, I+lc_I+ self.external_input)+\
                .5*C_ei*self._diff2_fe_fi(TF.inhibitory, E+lc_E+ self.external_input, I+lc_I+ self.external_input)+\
                .5*C_ei*self._diff2_fi_fe(TF.inhibitory, E+lc_E+ self.external_input, I+lc_I+ self.external_input)+\
                .5*C_ii*self._diff2_fi_fi(TF.inhibitory, E+lc_E+ self.external_input, I+lc_I+ self.external_input)+\
                TF.inhibitory(E+lc_E+ self.external_input, I+lc_I+ self.external_input)-I)
        #Covariance excitatory-excitatory derivation
        derivative[2] = 1./self.T*(\
                1./Ne*TF.excitatory(E+c_0+lc_E+ self.external_input, I+lc_I+ self.external_input)*\
                (1./self.T-TF.excitatory(E+c_0+lc_E+ self.external_input, I+lc_I+ self.external_input))+\
                (TF.excitatory(E+c_0+lc_E+ self.external_input, I+lc_I+ self.external_input)-E)**2+\
                2.*C_ee*self._diff_fe(TF.excitatory, E+c_0+lc_E+ self.external_input, I+lc_I+ self.external_input)+\
                2.*C_ei*self._diff_fi(TF.inhibitory, E+lc_E+ self.external_input, I+lc_I+ self.external_input)+\
                -2.*C_ee)

        #Covariance excitatory-inhibitory or inhibitory-excitatory derivation
        derivative[3] = 1./self.T*\
                ((TF.excitatory(E+c_0+lc_E+ self.external_input, I+lc_I+ self.external_input)-E)*\
                (TF.inhibitory(E+lc_E+ self.external_input, I+lc_I+ self.external_input)-I)+\
                C_ee*self._diff_fe(TF.excitatory, E+c_0+lc_E+ self.external_input, I+lc_I+ self.external_input)+\
                C_ei*self._diff_fe(TF.inhibitory, E+lc_E+ self.external_input, I+lc_I+ self.external_input)+\
                C_ei*self._diff_fi(TF.excitatory, E+c_0+lc_E+ self.external_input, I+lc_I+ self.external_input)+\
                C_ii*self._diff_fi(TF.inhibitory, E+lc_E+ self.external_input, I+lc_I+ self.external_input)+\
                -2.*C_ei)

        #Covariance inhibitory-inhibitory derivation
        derivative[4] = 1./self.T*(\
                1./Ni*TF.inhibitory(E+lc_E+ self.external_input, I+lc_I+ self.external_input)*\
                (1./self.T-TF.inhibitory(E+lc_E+ self.external_input, I+lc_I+ self.external_input))+\
                (TF.inhibitory(E+lc_E+ self.external_input, I+lc_I+ self.external_input)-I)**2+\
                2.*C_ii*self._diff_fi(TF.inhibitory, E+lc_E+ self.external_input, I+lc_I+ self.external_input)+\
                2.*C_ei*self._diff_fe(TF.excitatory, E+c_0+lc_E+ self.external_input, I+lc_I+ self.external_input)+\
                -2.*C_ii)

        return derivative