CausalDynamics#

Here, we showcase some key features of CausalDynamics using small example graphs.

import torch
import matplotlib.pyplot as plt

from causaldynamics.scm import create_scm_graph
from causaldynamics.plot import plot_trajectories, plot_scm, plot_3d_trajectories
from causaldynamics.creator import create_scm, simulate_system

Basic coupled model#

num_nodes = 3
node_dim = 3
num_timesteps = 1000

confounders=False,
system_name='Lorenz'

A = torch.tensor([[0,0,0],
                 [1,0,0],
                 [1,0,0]])

A, W, b, root_nodes, _ = create_scm(num_nodes,
                                    node_dim=node_dim,
                                    confounders=confounders,
                                    adjacency_matrix=A)

data = simulate_system(A, W, b, 
                      num_timesteps=num_timesteps, 
                      num_nodes=num_nodes,
                      system_name=system_name) 


plot_scm(G=create_scm_graph(A), root_nodes=root_nodes)
plot_3d_trajectories(data, root_nodes, line_alpha=1.)
plot_trajectories(data, root_nodes, sharey=False)
plt.show()
INFO - Creating SCM with 3 nodes and 3 dimensions each...
INFO - Simulating Lorenz system for 1000 timesteps...
../_images/b4abba75c2602f31ad13dc0a4b11b280252372c06ecc832f1d112a167f417214.png ../_images/731ace43a8deae574f8d5cd91853ebe88827068e24009f1c9c4ba09f302ca729.png ../_images/0e936147a75cd7e45d1a08aa5d111293d4dddadb19912dfdff5c02ecf0f6da11.png

Confounders#

A = torch.tensor([[0,0,0],
                 [1,0,0],
                 [1,1,0]])

A, W, b, root_nodes, _ = create_scm(num_nodes,
                                    node_dim=node_dim,
                                    confounders=confounders,
                                    adjacency_matrix=A)

data = simulate_system(A, W, b, 
                      num_timesteps=num_timesteps, 
                      num_nodes=num_nodes,
                      system_name=system_name) 

plot_scm(G=create_scm_graph(A), root_nodes=root_nodes)
plot_3d_trajectories(data, root_nodes, line_alpha=1.)
plot_trajectories(data, root_nodes, sharey=False)
plt.show()
INFO - Creating SCM with 3 nodes and 3 dimensions each...
INFO - Simulating Lorenz system for 1000 timesteps...
../_images/4b302fc4d3a86c824d413e9afef9d8a55ae33444e1acd2517b4e0d3f24b60f04.png ../_images/1dca00aab591f1c5643b6a4d7d0b2e1ef84d049dcd911386ba1622961f0adcfa.png ../_images/ca6da70adafd75cfcbc7b2fd8b5bec30ebc6a436dd4de7849e3729a46c0cbc94.png

Time-lag edges#

time_lag = 10
time_lag_edge_probability = 0.1

A, W, b, root_nodes, magnitudes = create_scm(
    num_nodes,
    node_dim=node_dim,
    confounders=False,
    graph="scale-free",
    time_lag=time_lag,
    time_lag_edge_probability=time_lag_edge_probability
)

data = simulate_system(A, W, b, 
                      num_timesteps=num_timesteps, 
                      num_nodes=num_nodes,
                      system_name=system_name,
                      time_lag=time_lag,) 

plot_scm(G=create_scm_graph(A), root_nodes=root_nodes)
plot_3d_trajectories(data, root_nodes, line_alpha=1.)
plot_trajectories(data, root_nodes, sharey=False)
plt.show()
INFO - Creating SCM with 3 nodes and 3 dimensions each...
INFO - Simulating Lorenz system for 1000 timesteps...
../_images/0aa20bb63a591794964795a781a30b2c47106059ceab9da4893466c24b60ef11.png ../_images/3790903b592364b9ea4a1c8a204c26f092701c6b600e10cce230732d9bd6507e.png ../_images/4e6e82c70ac67b5993b826eeb07d51ab89efe9f5ced0ce1e80ebaafca89e76ff.png

Stochastic systems#

num_nodes = 3
node_dim = 3
num_timesteps = 1000
noise = 1.

confounders=False,
system_name='Lorenz'

A = torch.tensor([[0,0,0],
                 [1,0,0],
                 [1,0,0]])

A, W, b, root_nodes, _ = create_scm(num_nodes,
                                    node_dim=node_dim,
                                    confounders=confounders,
                                    adjacency_matrix=A)

data = simulate_system(A, W, b, 
                      num_timesteps=num_timesteps, 
                      num_nodes=num_nodes,
                      system_name=system_name,
                      make_trajectory_kwargs={'noise': noise}) 


plot_scm(G=create_scm_graph(A), root_nodes=root_nodes)
plot_3d_trajectories(data, root_nodes, line_alpha=1.)
plot_trajectories(data, root_nodes, sharey=False)
plt.show()
INFO - Creating SCM with 3 nodes and 3 dimensions each...
INFO - Simulating Lorenz system for 1000 timesteps...
../_images/8aadd74232b68237a79c47ed982be78d5f6a9bb779448f71fe396eb8e25d0a1a.png ../_images/2e09fb6dedb597441e1ced4d67bb8fb0782aef3e5730453fe7eef78ebc80d21b.png ../_images/8c4b3d29b50e37c7a1b53d4989e410a82f0ab2014eeb5efda6977a19313307d5.png

Periodic drivers#

num_nodes = 3
node_dim = 3
num_timesteps = 1000

confounders=False,
system_name='Lorenz'

A = torch.tensor([[0,0,0],
                 [1,0,0],
                 [1,0,0]])

A, W, b, root_nodes, _ = create_scm(num_nodes,
                                    node_dim=node_dim,
                                    confounders=confounders,
                                    adjacency_matrix=A)

data = simulate_system(A, W, b, 
                      num_timesteps=num_timesteps, 
                      num_nodes=num_nodes,
                      system_name=system_name,
                      init_ratios=[1,1,0]) 

plot_scm(G=create_scm_graph(A), root_nodes=root_nodes)
plot_3d_trajectories(data, root_nodes, line_alpha=1.)
plot_trajectories(data, root_nodes, sharey=False)
plt.show()
INFO - Creating SCM with 3 nodes and 3 dimensions each...
INFO - Simulating Lorenz system for 1000 timesteps...
../_images/cb7a024bba5658568cc9ef3b5471debd38aa3bb6e55ddaa9b4d51b386319b58b.png ../_images/00080c76cc6014eff17840ec340e2ac08202b1df0d7dc942d848f67e27685eff.png ../_images/da470f164f42dedd3e1fe113dba277dca366644d2d97192910579db8af574faa.png

Internal standardization#

num_nodes = 3
node_dim = 3
num_timesteps = 1000
standardize = True

confounders=False,
system_name='Lorenz'

A = torch.tensor([[0,0,0],
                 [1,0,0],
                 [1,0,0]])

A, W, b, root_nodes, _ = create_scm(num_nodes,
                                    node_dim=node_dim,
                                    confounders=confounders,
                                    adjacency_matrix=A)

data = simulate_system(A, W, b, 
                      num_timesteps=num_timesteps, 
                      standardize=standardize,
                      num_nodes=num_nodes,
                      system_name=system_name) 


plot_scm(G=create_scm_graph(A), root_nodes=root_nodes)
plot_3d_trajectories(data, root_nodes, line_alpha=0.5)
plot_trajectories(data, root_nodes, sharey=False)
plt.show()
INFO - Creating SCM with 3 nodes and 3 dimensions each...
INFO - Simulating Lorenz system for 1000 timesteps...
../_images/a3bfd0430788260230bf3d82ac37f7dd8badc566ea11ad91baf55780fd094b5c.png ../_images/f93c767ecc2df093014596a3e39705866f7cba9e0bf9f28f7400089ecda87979.png ../_images/2d4055b189bc8270910c987cd56f392ef59e5d6383acc1c19ffac80623e61feb.png