Skip to content

Continuous-time threshold model (CNTM)

Detailed information about the CNTM can be found here.

CNTMParameters(network, r, r_tilde, threshold_01, threshold_10) dataclass

Container for the parameters of the Threshold Model.

At the rate r, each node evaluates to change their opinion from its current opinion m=0,1 to the other opinion n=1-m. It changes the opinion if the percentage of neighbors of opinion n exceeds the threshold_mn.

Additionally, each node changes its state randomly at rate r_tilde (noise).

CNTM(params)

Continuous-time Noisy Threshold Model.

Parameters:

Source code in sponet/cntm/model.py
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
def __init__(self, params: CNTMParameters):
    """
    Continuous-time Noisy Threshold Model.

    Parameters
    ----------
    params : CNTMParameters
    """
    self.params = params

    # self.neighbor_list[i] = array of neighbors of node i
    self.neighbor_list = List(calculate_neighbor_list(params.network))  # type: ignore

    self.noise_prob = self.params.r_tilde / (self.params.r + self.params.r_tilde)
    self.next_event_rate = 1 / (
        self.params.num_agents * (self.params.r + self.params.r_tilde)
    )

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 ) –

    shape=(num_agents,)

  • 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/cntm/model.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
 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
def simulate(
    self,
    t_max: float,
    x_init: ArrayLike | None = None,
    t_eval: ArrayLike | None = None,
    rng: Generator = default_rng(),
) -> tuple[NDArray, NDArray]:
    """
    Simulate the model from t=0 to t=t_max.

    Parameters
    ----------
    t_max : float
    x_init : ArrayLike
        shape=(num_agents,)
    t_eval : ArrayLike, optional
        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, optional
        random number generator

    Returns
    -------
    tuple[NDArray, NDArray]
        t_traj (shape=(?,)), x_traj (shape=(?,num_agents))
    """

    opinion_dtype = np.min_scalar_type(1)
    if x_init is None:
        x = rng.choice(
            np.arange(self.params.num_opinions, dtype=opinion_dtype),
            size=self.params.num_agents,
        )
    else:
        x = np.array(x_init, dtype=opinion_dtype)

    if t_eval is not None:
        t_eval = t_eval_to_ndarray(t_eval, t_max)

    if t_eval is None:
        t_traj, x_traj = _simulate_all(
            x,
            self.next_event_rate,
            self.noise_prob,
            t_max,
            self.params.threshold_01,
            self.params.threshold_10,
            self.neighbor_list,
            rng,
        )
    else:
        t_traj, x_traj = _simulate_teval(
            x,
            t_eval,
            self.next_event_rate,
            self.noise_prob,
            self.params.threshold_01,
            self.params.threshold_10,
            self.neighbor_list,
            rng,
        )

    t_traj = np.array(t_traj)
    x_traj = np.array(x_traj)

    if t_eval is not None and t_traj.shape[0] != t_eval.shape[0]:
        # there might be less samples than len(t_eval)
        # -> fill with duplicates
        t_ind = argmatch(t_eval, t_traj)
        t_traj = t_eval
        x_traj = x_traj[t_ind]

    return t_traj, x_traj