Continuous-time noisy voter model (CNVM)
Detailed information about the CNVM can be found here.
CNVMParameters(num_opinions=None, num_agents=None, network=None, network_generator=None, alpha=1.0, r=None, r_tilde=None, r_imit=None, r_noise=None, prob_imit=1, prob_noise=1)
Container for the parameters of the Continuous-time Noisy Voter Model (CNVM).
A node i transitions from its current opinion m to a different opinion n at rate r[m,n] * d(i,n) / (d(i)^alpha) + r_tilde[m,n], where d(i,n) is the count of opinion n in the neighborhood of agent i, and d(i) the degree of node i.
Either a network has to specified, or a NetworkGenerator,
or num_agents, in which case a complete network is used.
If multiple are given, NetworkGenerator overrules network, and network overrules num_agents.
A network can either be given as a nx.Graph or as an adjacency list,
where the i-th element is the list of neighbors of node i.
(The most efficient way to give a network is via an adjacency list of type list[NDArray].)
The rate parameters r and r_tilde can be given as arrays of shape (num_opinions, num_opinions), or as floats, in which case all rates are set to this value.
(Internally, the CNVM uses an equivalent set of rate parameters: r_imit, r_noise, prob_imit and prob_noise. A node i transitions from its current opinion m to a different opinion n at rate r_imit * d(i,n) / (d(i)^alpha) * prob_imit[m, n] + r_noise * (1/num_opinions) * prob_noise[m, n]. These parameters can be provided instead of the usual r and r_tilde.)
Source code in sponet/cnvm/parameters.py
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | |
get_network()
If self.network exists, returns the network. Else, if a NetworkGenerator was given, returns a network generated by it. Else, returns the complete graph.
Returns:
-
Graph–
Source code in sponet/cnvm/parameters.py
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 | |
set_network(network)
Set new network.
Only works if no NetworkGenerator was given.
Parameters:
-
network(Graph | Iterable | None) –Graph, or adjacency list, or None (=complete graph).
Source code in sponet/cnvm/parameters.py
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 | |
update_network_by_generator()
Update self.network via the NetworkGenerator.
Only works if a NetworkGenerator was given.
Source code in sponet/cnvm/parameters.py
108 109 110 111 112 113 114 115 116 | |
change_rates(r=None, r_tilde=None)
Change one or both rate parameters.
If only one argument is given, the other rate parameter stays the same.
Parameters:
-
r(float | ndarray, default:None) – -
r_tilde(float | ndarray, default:None) –
Source code in sponet/cnvm/parameters.py
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 | |
CNVM(params)
Continuous-time Noisy Voter Model.
Parameters:
-
params(CNVMParameters) –
Source code in sponet/cnvm/model.py
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | |
update_network()
Update network from NetworkGenerator in params.
Source code in sponet/cnvm/model.py
44 45 46 47 48 49 50 | |
update_rates(r=None, r_tilde=None)
Update one or both rate parameters.
If only one argument is given, the other rate parameter stays the same.
Parameters:
-
r(ArrayLike, default:None) – -
r_tilde(ArrayLike, default:None) –
Source code in sponet/cnvm/model.py
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 | |
simulate(t_max, x_init=None, t_eval=None, rng=default_rng())
Simulate the model from t=0 to t=t_max.
Parameters:
-
t_max(float) – -
x_init(ArrayLike, default:None) –Initial state, shape=(num_agents,). If no x_init is given, a random one is generated.
-
t_eval(ArrayLike, default:None) –Array of time points where the solution should be saved, or number "n" in which case the solution is stored equidistantly at "n" time points. If None, store every snapshot.
-
rng(Generator, default:default_rng()) –random number generator
Returns:
-
tuple[NDArray, NDArray]–t_traj (shape=(?,)), x_traj (shape=(?,num_agents))
Source code in sponet/cnvm/model.py
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 | |
CNVM Approximations
calc_rre_traj(params, initial_states, t_max, t_eval=None)
Solve the RRE given by parameters, starting from c_0, up to time t_max.
Solves the ODE using scipy's "solve_ivp".
Parameters:
-
params(CNVMParameters) – -
initial_states(ArrayLike) –Either shape = (num_opinions,) or (num_states, num_opinions)
-
t_max(float) –End time.
-
t_eval(ArrayLike, default:None) –Array of time points where the solution should be saved, or number "n" in which case the solution is stored equidistantly at "n" time points.
Returns:
-
tuple[NDArray, NDArray]–(t, c), t.shape=(num_timesteps,), c.shape = (num_states, num_timesteps, num_opinions), or c.shape = (num_timesteps, num_opinions) if a single initial state was given.
Source code in sponet/cnvm/approximations/reaction_rate_equation.py
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 | |
calc_modified_rre_traj(params, initial_states, t_max, alpha=1.0, t_eval=None)
Solve the RRE with modified parameters, starting from c_0, up to time t_max.
The parameters are modified by multiplying the imitation rates r with the factor alpha.
For instance, if alpha < 1, this effectively slows the dynamics.
Parameters:
-
params(CNVMParameters) – -
initial_states(ArrayLike) –Either shape = (num_opinions,) or (num_states, num_opinions)
-
t_max(float) –End time.
-
alpha(float, default:1.0) –Factor for modification of imitation rates.
-
t_eval(ArrayLike, default:None) –Array of time points where the solution should be saved, or number "n" in which case the solution is stored equidistantly at "n" time points.
Returns:
-
tuple[NDArray, NDArray]–(t, c), t.shape=(num_time_steps,), c.shape = (num_states, num_time_steps, num_opinions), or c.shape = (num_time_steps, num_opinions) if a single initial state was given.
Source code in sponet/cnvm/approximations/reaction_rate_equation.py
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 | |
calc_pair_approximation_traj(params, c_0, s_0, d, t_max, t_eval=None)
For a CNVM with 2 opinions, calculate the pair approximation.
The opinions are referred to as "0" and "1". The pair approximation is a system of two ODEs. The first ODE describes the evolution of the share of nodes with state 1, which is called "c". The second ODE describes the evolution of the variable "s", which can be interpreted has 0.5 times the share of active edges. So in this package, s can be computed as s = 0.5 * sponet.collective_variables.Interfaces.
Parameters:
-
params(CNVMParameters) – -
c_0(float) –Initial state of c.
-
s_0(float) –Initial state of s.
-
t_max(float) –End time.
-
t_eval(ArrayLike, default:None) –Array of time points where the solution should be saved, or number "n" in which case the solution is stored equidistantly at "n" time points.
Returns:
-
tuple[ndarray, ndarray]–- timepoints, shape=(?,).
- c, shape=(?, 2).
Source code in sponet/cnvm/approximations/pair_approximation.py
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 | |
sample_stochastic_approximation(params, initial_states, t_max, num_samples, t_eval, rng=None, seed=None)
Simulate the opinion shares directly.
Assumes well-mixedness in the sense that the propensity of reaction m -> n is given by c[m] * (r[m, n] * c[n] + r_tilde[m, n]).
This is only true for complete networks. For other networks the quality of this approximation may vary.
Parameters:
-
params(CNVMParameters) – -
initial_states(NDArray) –Either shape = (num_opinions,) or (num_states, num_opinions)
-
t_max(float) – -
num_samples(int) – -
t_eval(ArrayLike) –Array of time points where the solution should be saved, or number "n" in which case the solution is stored equidistantly at "n" time points.
-
rng(Generator, default:None) –Random number generator.
-
seed(int, default:None) –Seed for random numbers. If both
rngandseedare given,rngtakes precedence.
Returns:
-
tuple[NDArray, NDArray]–(t, c), t.shape=(num_timesteps), c.shape = (num_states, num_samples, num_timesteps, num_opinions), or c.shape = (num_samples, num_timesteps, num_opinions) if a single initial state was given.
Source code in sponet/cnvm/approximations/stochastic_approximation.py
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 | |
sample_cle(params, initial_states, t_max, num_samples, delta_t=None, t_eval=None, boundary_process='clipping', rng=None, seed=None)
Sample Chemical Langevin Equation (CLE) approximation for the CNVM.
The Euler-Maruyama method is used to integrate the SDE.
Either delta_t or t_eval or both have to be provided.
Parameters:
-
params(CNVMParameters) – -
initial_states(ArrayLike) –Either shape = (num_opinions,) or (num_states, num_opinions)
-
t_max(float) – -
num_samples(int) – -
delta_t(float, default:None) –Step size.
-
t_eval(ArrayLike, default:None) –Array of time points where the solution should be saved, or number "n" in which case the solution is stored equidistantly at "n" time points.
-
boundary_process(str, default:'clipping') –Kind of process used to deal with the approximation leaving the simplex boundary. Possible values: "clipping", "jump", "normal-reflection" Defaults to "clipping".
-
rng(Generator, default:None) –Random number generator.
-
seed(int, default:None) –Seed for random numbers. If both
rngandseedare given,rngtakes precedence.
Returns:
-
tuple[NDArray, NDArray]–(t, c), t.shape=(num_timesteps), c.shape = (num_states, num_samples, num_timesteps, num_opinions), or c.shape = (num_samples, num_timesteps, num_opinions) if a single initial state was given. (If saving_offset > 1, the number of time steps will be smaller.)
Source code in sponet/cnvm/approximations/chemical_langevin_equation/chemical_langevin_equation.py
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 | |