# Basic Example for generating samples from a GCM

A graphical causal model (GCM) describes the data generation process of the modeled variables. Therefore, after we fit a GCM, we can also generate completely new samples from it and, thus, can see it as data generator for synthetic data based on the underlying models. Generating new samples can generally be done by sorting the nodes in topological order, randomly sample from root-nodes and then propagate the data through the graph by evaluating the downstream causal mechanisms with randomly
sampled noise. The `dowhy.gcm`

package provides a simple helper function that does this automatically and, by this, offers a simple API to draw samples from a GCM.

Lets take a look at the following example:

```
[1]:
```

```
import numpy as np, pandas as pd
X = np.random.normal(loc=0, scale=1, size=1000)
Y = 2 * X + np.random.normal(loc=0, scale=1, size=1000)
Z = 3 * Y + np.random.normal(loc=0, scale=1, size=1000)
data = pd.DataFrame(data=dict(X=X, Y=Y, Z=Z))
data.head()
```

```
[1]:
```

X | Y | Z | |
---|---|---|---|

0 | 0.626849 | 0.437180 | 0.255219 |

1 | 2.216517 | 5.248521 | 14.616385 |

2 | 0.293370 | 3.004939 | 11.000477 |

3 | 1.689173 | 3.184794 | 9.998655 |

4 | 0.905032 | 3.120774 | 9.578387 |

Similar as in the introduction, we generate data for the simple linear DAG X→Y→Z. Lets define the GCM and fit it to the data:

```
[2]:
```

```
import networkx as nx
import dowhy.gcm as gcm
causal_model = gcm.StructuralCausalModel(nx.DiGraph([('X', 'Y'), ('Y', 'Z')]))
gcm.auto.assign_causal_mechanisms(causal_model, data) # Automatically assigns additive noise models to non-root nodes
gcm.fit(causal_model, data)
```

```
Fitting causal mechanism of node Z: 100%|██████████| 3/3 [00:00<00:00, 493.25it/s]
```

We now learned the generative models of the variables, based on the defined causal graph and the additive noise model assumption. To generate new samples from this model, we can now simply call:

```
[3]:
```

```
generated_data = gcm.draw_samples(causal_model, num_samples=1000)
generated_data.head()
```

```
[3]:
```

X | Y | Z | |
---|---|---|---|

0 | -1.795505 | -3.478594 | -12.254416 |

1 | -0.592618 | -1.110506 | -0.425806 |

2 | 0.783405 | 0.325487 | 1.822675 |

3 | -0.602467 | -0.814489 | -2.103829 |

4 | -0.158978 | -0.848972 | -2.102372 |

If our modeling assumptions are correct, the generated data should now resemble the observed data distribution, i.e. the generated samples correspond to the joint distribution we defined for our example data at the beginning. One way to make sure of this is to estimate the KL-divergence between observed and generated distribution. For this, we can make use of the evaluation module:

```
[4]:
```

```
print(gcm.evaluate_causal_model(causal_model, data, evaluate_causal_mechanisms=False, evaluate_invertibility_assumptions=False))
```

```
Test permutations of given graph: 100%|██████████| 6/6 [00:00<00:00, 18.13it/s]
```

```
Evaluated and the overall average KL divergence between generated and observed distribution and the graph structure. The results are as follows:
==== Evaluation of Generated Distribution ====
The overall average KL divergence between the generated and observed distribution is 0.006424910794033553
The estimated KL divergence indicates an overall very good representation of the data distribution.
==== Evaluation of the Causal Graph Structure ====
+-------------------------------------------------------------------------------------------------------+
| Falsificaton Summary |
+-------------------------------------------------------------------------------------------------------+
| The given DAG is not informative because 2 / 6 of the permutations lie in the Markov |
| equivalence class of the given DAG (p-value: 0.33). |
| The given DAG violates 0/1 LMCs and is better than 66.7% of the permuted DAGs (p-value: 0.33). |
| Based on the provided significance level (0.2) and because the DAG is not informative, |
| we do not reject the DAG. |
+-------------------------------------------------------------------------------------------------------+
==== NOTE ====
Always double check the made model assumptions with respect to the graph structure and choice of causal mechanisms.
All these evaluations give some insight into the goodness of the causal model, but should not be overinterpreted, since some causal relationships can be intrinsically hard to model. Furthermore, many algorithms are fairly robust against misspecifications or poor performances of causal mechanisms.
```

This confirms that the generated distribution is close to the observed one.

While the evaluation provides us insights toward the causal graph structure as well, we cannot confirm the graph structure, only reject it if we find inconsistencies between the dependencies of the observed structure and what the graph represents. In our case, we do not reject the DAG, but there are other equivalent DAGs that would not be rejected as well. To see this, consider the example above - X→Y→Z and X←Y←Z would generate the same observational distribution (since they encode the same conditionals), but only X→Y→Z would generate the correct interventional distribution (e.g., when intervening on Y).