{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# 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 networks. 50(4) p. 3175. Fig. 1.\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\nn1 = nest.Create(\"ginzburg_neuron\")\nn1.set(theta=0.0, tau_m=tau_m, c_1=0.0, c_2=2. * m_x, c_3=1.0)\n\nn2 = nest.Create(\"mcculloch_pitts_neuron\")\nn2.set(theta=0.5, tau_m=tau_m)\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\ncount_covariance = csd.count_covariance\n\nmean_activities = np.zeros(2, dtype=float)\nfor i in range(2):\n    mean_activities[i] = count_covariance[i][i][int(tau_max / h)] * (h / T)\n\nprint('mean activities =', mean_activities)\n\ncovariance_matrix = np.zeros((2, 2, int(2 * tau_max / h) + 1), dtype=float)\nfor i in range(2):\n    for j in range(2):\n        covariance_matrix[i, j] = count_covariance[i][j] * (h / T) - mean_activities[i] * mean_activities[j]\n\nts = np.arange(-tau_max, tau_max + h, h)\n\nplt.title(\"auto- and cross covariance functions\")\n\nplt.plot(ts, covariance_matrix[0, 1], 'r', label=r\"$c_{12}$\")\nplt.plot(ts, covariance_matrix[1, 0], 'b', label=r\"$c_{21}$\")\nplt.plot(ts, covariance_matrix[0, 0], 'g', label=r\"$c_{11}$\")\nplt.plot(ts, covariance_matrix[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.8.6"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}