Skip to contents

Treats each construct (in file order) as a state variable and each proposition between two declared constructs whose relation is increases, decreases, causes or mediates as a linear coupling term. It then propagates dX/dt = A X - damping * X exactly, with the matrix exponential of (A - damping * I) * dt, or with fixed-step (Euler) updates. The result is deterministic. Construct ids must be unique, and duplicates are refused rather than assigned to an arbitrary state. A construct without an id is a state with no couplings, and two of them do not count as duplicates.

Usage

tf_simulate(
  theory,
  steps = 10,
  dt = 0.1,
  k = 1,
  damping = 0.5,
  init = 1,
  method = "exact"
)

Arguments

theory

A theory object (named list), such as one from tf_read().

steps

Number of steps, a whole number of at least 0 (default 10).

dt

Time between rows, a finite number greater than 0 (default 0.1).

k

Coupling gain applied to each signed edge, a finite number (default 1.0).

damping

Per-state linear decay, a finite number (default 0.5).

init

Initial value of every state, a single finite number (default 1.0), or a named numeric vector or named list giving the initial value of the constructs it names, the others starting from 0.

method

"exact" (the default) or "euler".

Value

A named list list(states, dt, steps, k, damping, init, initial_state, method, ignored, assumed_positive, opposed, trajectory), where states are the construct ids in file order and trajectory is a list of steps + 1 numeric vectors (row 0 is the initial state), every value rounded to 6 decimals. The settings and the method are returned as given (a mapping init as a named list), because the trajectory cannot be reproduced without them, and initial_state gives the value each state starts from, in the order of states. ignored lists the ids of the propositions that couple nothing (moderates, associates, a relation outside the schema's set or an endpoint that is not a declared construct), assumed_positive the ids of the causes and mediates propositions coupled at +k, and opposed the c(from, to) pairs that carry couplings of both signs, which offset each other. An invalid setting is refused with an error.

Details

A theory states no parameter values, so the trajectory is that of one linear model consistent with the signs the propositions state, with one gain k for every coupling and unsigned relations taken as positive. It is not fitted to data and does not show what the propositions imply. Every state decays at rate damping in the absence of couplings, and whether a feedback loop grows or settles is set by k against damping. Time is in units of 1/k, and the window that steps and dt choose decides what is seen: the last construct of a chain that decays in the long run can show only a rise in a short window. A linear system has one equilibrium, or a continuum of them when its matrix is singular, and never two separate ones, so it cannot show bistability.

Couplings

Each such proposition adds k times the sign of its relation to the coupling from from to to. The signs are those of the relation table (API_SPEC.md section 28): +1 for increases and -1 for decreases. causes and mediates state no sign, and they are coupled at +k by assumption. assumed_positive lists their ids, in file order, since that choice alone can decide whether a feedback loop grows or settles. moderates and associates couple nothing, and ignored lists them.

Initial state

init is either one finite number, the value every state starts from, or a mapping from construct ids to finite numbers, given as a named numeric vector or a named list, in which case each state it does not name starts from 0. A mapping with one entry gives the response to an impulse in that construct. A name that is not a declared construct id is refused.

Methods

With method = "exact", the default, each step multiplies the state by the matrix exponential, computed by scaling and squaring a degree-18 Taylor polynomial (Moler and Van Loan, 2003, method 3), so the trajectory is the solution of the linear system at times 0, dt, 2 * dt, ... whatever dt is.

With method = "euler", the explicit step is stable only when \(|1 + dt \lambda| < 1\) for every eigenvalue \(\lambda\) of A - damping * I. A theory that decays at rate damping therefore grows without bound, alternating in sign, once dt * damping exceeds 2, and the steps turn a sustained oscillation into growth. Raising damping can cause a divergence as well as fail to stop one. The function warns when the Euler trajectory departs from the exact one, at any step, by more than 5 per cent of the larger of |exact| and the largest absolute starting value (1 when every starting value is 0), so that the warning does not depend on the unit of init.

When a state grows so large that a million times it is not a finite number, the function stops with a message naming the step and the state. The initial state is tested as step 0, so a starting value beyond about 1.8e302 in absolute value stops there.

The model is deliberately simple: one gain k for every coupling, causes and mediates taken as positive, moderates and associates coupling nothing and functional_form not read.

References

Moler, C., & Van Loan, C. (2003). Nineteen dubious ways to compute the exponential of a matrix, twenty-five years later. SIAM Review, 45(1), 3-49. doi:10.1137/S00361445024180

Examples

theory <- tf_theory("demo-1", "A demonstration theory") |>
  tf_add_construct("c_arousal", "Arousal", "Bodily activation.") |>
  tf_add_construct("c_threat", "Perceived threat", "Appraised danger.") |>
  tf_add_proposition("p1", "c_arousal", "c_threat", "increases")
sim <- tf_simulate(theory, steps = 5)
sim$states
#> [[1]]
#> [1] "c_arousal"
#> 
#> [[2]]
#> [1] "c_threat"
#> 
sim$trajectory[[1]] # the common initial state
#> [1] 1 1
sim$trajectory[[length(sim$trajectory)]] # at time 5 * dt
#> [1] 0.778801 1.168201
# The response to an impulse in arousal, threat starting from 0
impulse <- tf_simulate(theory, steps = 5, init = c(c_arousal = 1))
impulse$initial_state
#> [1] 1 0