{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# Correlospinmatrix detector example\n\nThis scripts simulates two connected binary neurons, similar\nas in [1]_. It measures and plots the auto- and cross covariance functions\nof the individual neurons and between them, repsectively.\n\n## References\n\n.. [1] Ginzburg and Sompolinsky (1994). Theory of correlations in stochastic neural netoworks. 50(4) p. 3175. Fig. 1.\n\n\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import matplotlib.pyplot as plt\nimport nest\nimport numpy as np\n\nm_x = 0.5\ntau_m = 10.\nh = 0.1\nT = 1000000.\ntau_max = 100.\n\ncsd = nest.Create(\"correlospinmatrix_detector\")\ncsd.set(N_channels=2, tau_max=tau_max, Tstart=tau_max, delta_tau=h)\n\nnest.SetDefaults('ginzburg_neuron', {'theta': 0.0, 'tau_m': tau_m,\n                                     'c_1': 0.0, 'c_2': 2. * m_x, 'c_3': 1.0})\nn1 = nest.Create(\"ginzburg_neuron\")\n\nnest.SetDefaults(\"mcculloch_pitts_neuron\", {'theta': 0.5, 'tau_m': tau_m})\nn2 = nest.Create(\"mcculloch_pitts_neuron\")\n\nnest.Connect(n1, n2, syn_spec={\"weight\": 1.0})\n\nnest.Connect(n1, csd, syn_spec={\"receptor_type\": 0})\nnest.Connect(n2, csd, syn_spec={\"receptor_type\": 1})\n\nnest.Simulate(T)\n\nc = csd.get(\"count_covariance\")\n\nm = np.zeros(2, dtype=float)\nfor i in range(2):\n    m[i] = c[i][i][int(tau_max / h)] * (h / T)\n\nprint('mean activities =', m)\n\ncmat = np.zeros((2, 2, int(2 * tau_max / h) + 1), dtype=float)\nfor i in range(2):\n    for j in range(2):\n        cmat[i, j] = c[i][j] * (h / T) - m[i] * m[j]\n\nts = np.arange(-tau_max, tau_max + h, h)\n\nplt.title(\"auto- and cross covariance functions\")\n\nplt.plot(ts, cmat[0, 1], 'r', label=r\"$c_{12}$\")\nplt.plot(ts, cmat[1, 0], 'b', label=r\"$c_{21}$\")\nplt.plot(ts, cmat[0, 0], 'g', label=r\"$c_{11}$\")\nplt.plot(ts, cmat[1, 1], 'y', label=r\"$c_{22}$\")\nplt.xlabel(r\"time $t \\; \\mathrm{ms}$\")\nplt.ylabel(r\"$c$\")\nplt.legend()\n\nplt.show()"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.6.12"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}