1# -*- coding: utf-8 -*-
2#
3# correlospinmatrix_detector_two_neuron.py
4#
5# This file is part of NEST.
6#
7# Copyright (C) 2004 The NEST Initiative
8#
9# NEST is free software: you can redistribute it and/or modify
10# it under the terms of the GNU General Public License as published by
11# the Free Software Foundation, either version 2 of the License, or
12# (at your option) any later version.
13#
14# NEST is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17# GNU General Public License for more details.
18#
19# You should have received a copy of the GNU General Public License
20# along with NEST.  If not, see <http://www.gnu.org/licenses/>.
21
22"""
23Correlospinmatrix detector example
24----------------------------------
25
26This scripts simulates two connected binary neurons, similar
27as in [1]_. It measures and plots the auto- and cross covariance functions
28of the individual neurons and between them, repsectively.
29
30References
31~~~~~~~~~~
32
33.. [1] Ginzburg and Sompolinsky (1994). Theory of correlations in stochastic neural networks. 50(4) p. 3175. Fig. 1.
34
35"""
36
37import matplotlib.pyplot as plt
38import nest
39import numpy as np
40
41m_x = 0.5
42tau_m = 10.
43h = 0.1
44T = 1000000.
45tau_max = 100.
46
47csd = nest.Create("correlospinmatrix_detector")
48csd.set(N_channels=2, tau_max=tau_max, Tstart=tau_max, delta_tau=h)
49
50n1 = nest.Create("ginzburg_neuron")
51n1.set(theta=0.0, tau_m=tau_m, c_1=0.0, c_2=2. * m_x, c_3=1.0)
52
53n2 = nest.Create("mcculloch_pitts_neuron")
54n2.set(theta=0.5, tau_m=tau_m)
55
56nest.Connect(n1, n2, syn_spec={"weight": 1.0})
57
58nest.Connect(n1, csd, syn_spec={"receptor_type": 0})
59nest.Connect(n2, csd, syn_spec={"receptor_type": 1})
60
61nest.Simulate(T)
62
63count_covariance = csd.count_covariance
64
65mean_activities = np.zeros(2, dtype=float)
66for i in range(2):
67    mean_activities[i] = count_covariance[i][i][int(tau_max / h)] * (h / T)
68
69print('mean activities =', mean_activities)
70
71covariance_matrix = np.zeros((2, 2, int(2 * tau_max / h) + 1), dtype=float)
72for i in range(2):
73    for j in range(2):
74        covariance_matrix[i, j] = count_covariance[i][j] * (h / T) - mean_activities[i] * mean_activities[j]
75
76ts = np.arange(-tau_max, tau_max + h, h)
77
78plt.title("auto- and cross covariance functions")
79
80plt.plot(ts, covariance_matrix[0, 1], 'r', label=r"$c_{12}$")
81plt.plot(ts, covariance_matrix[1, 0], 'b', label=r"$c_{21}$")
82plt.plot(ts, covariance_matrix[0, 0], 'g', label=r"$c_{11}$")
83plt.plot(ts, covariance_matrix[1, 1], 'y', label=r"$c_{22}$")
84plt.xlabel(r"time $t \; \mathrm{ms}$")
85plt.ylabel(r"$c$")
86plt.legend()
87
88plt.show()
89