dag.dag_to_spec()

Translate a causal DAG into a pathmc DSL specification string.

Usage

dag.dag_to_spec(
    dag,
    *,
    target=None,
)

Every node with at least one parent becomes a regression equation node ~ parent1 + parent2 + ...; root (exogenous) nodes appear only on the right-hand side. The result is a plain pathmc spec that can be passed straight to pathmc.model(), optionally after editing in transforms, labels, or families.

Parameters

dag: str | networkx.DiGraph

The graph, as DOT ("digraph { A -> B; }"), an "A->B" edge list, or a networkx.DiGraph.

target: str | None = None
If given, the node is validated to exist in the DAG. It does not change the emitted spec (pathmc derives estimands at query time), but guards against typos when wiring discovery output into a model.

Returns

str
A pathmc DSL string, one equation per line, ordered topologically.

Raises

ValueError
If the graph is not a DAG, has no edges, contains a node name that is not a valid pathmc identifier, or target is not a node.

Examples

::

from pathmc import dag_to_spec, model

spec = dag_to_spec("digraph { X -> M; M -> Y; X -> Y; }")
print(spec)
# M ~ X
# Y ~ M + X
m = model(spec, data=df, families={"Y": "bernoulli"})