CausalGraph<T>
Models & Types · AiDotNet.CausalDiscovery
Represents a causal Directed Acyclic Graph (DAG) discovered from observational data. This is both a standalone queryable model and an analysis result.
For Beginners
Think of this as a map of cause-and-effect relationships. Each variable is a node, and arrows between nodes show which variables directly cause changes in others. The weight of each arrow tells you how strong the causal effect is.
You can query this graph to answer questions like:
- “What causes variable X?” — use
GetParents - “What does variable X affect?” — use
GetChildren - “What is the Markov blanket of X?” — use
GetMarkovBlanket - “What order should I process variables?” — use
TopologicalSort
How It Works
A causal graph encodes the causal relationships between variables as a weighted adjacency matrix. An edge from variable i to variable j with weight w means “variable i directly causes variable j with strength w.” The graph is guaranteed to be a DAG (no cycles), meaning you cannot follow directed edges and return to the starting variable.
Constructors
| Constructor | Summary |
|---|---|
CausalGraph(Matrix<T> adjacencyMatrix, string[] featureNames) |
Initializes a new CausalGraph from a weighted adjacency matrix. |
Properties
| Property | Summary |
|---|---|
Matrix<T> AdjacencyMatrix { get; } |
Gets the weighted adjacency matrix where entry [i,j] represents the causal effect from variable i to variable j. |
double Density { get; } |
Gets the density of the graph (fraction of possible edges that exist). |
int EdgeCount { get; } |
Gets the total number of edges in the graph. |
int NumVariables { get; } |
Gets the number of variables in the graph. |
string[] FeatureNames { get; } |
Gets the feature/variable names. |
Methods
| Method | Summary |
|---|---|
Dictionary<int, double> GetNodeImportance() |
Computes a simple node importance score based on out-degree weighted by edge strength. |
InterventionalDistribution<T> ComputeInterventionalDistribution(int interventionVariable, T interventionValue, int targetVariable, Matrix<T> observationalData) |
Computes the interventional distribution using named variables. |
InterventionalDistribution<T> ComputeInterventionalDistribution(string interventionVariableName, T interventionValue, string targetVariableName, Matrix<T> observationalData) |
Computes the interventional distribution using named variables. |
List<ValueTuple<int, int, T>> GetEdges(double minAbsWeight) |
Gets all edges in the graph as (from, to, weight) tuples, optionally filtered by minimum weight. |
List<ValueTuple<string, string, T>> GetNamedEdges(double minAbsWeight) |
Gets all edges as named tuples for human-readable output. |
T GetEdgeWeight(int fromIndex, int toIndex) |
Gets the weight of the edge between named variables. |
T GetEdgeWeight(string fromName, string toName) |
Gets the weight of the edge between named variables. |
bool HasEdge(int fromIndex, int toIndex) |
Returns whether there is a directed edge from one variable to another. |
bool IsDAG() |
Returns whether the graph is a valid DAG (no directed cycles). |
int[] GetAncestors(int variableIndex) |
Gets all ancestor variables (direct and indirect causes) of the specified variable. |
int[] GetChildren(int variableIndex) |
Gets the child variables (direct effects) of a named variable. |
int[] GetDescendants(int variableIndex) |
Gets all descendant variables (direct and indirect effects) of the specified variable. |
int[] GetMarkovBlanket(int variableIndex) |
Gets the Markov blanket of a variable: its parents, children, and co-parents of its children. |
int[] GetParents(int variableIndex) |
Gets the parent variables (direct causes) of a named variable. |
int[] TopologicalSort() |
Returns a topological ordering of the variables (causes before effects). |
string[] GetChildren(string variableName) |
Gets the child variables (direct effects) of a named variable. |
string[] GetParents(string variableName) |
Gets the parent variables (direct causes) of a named variable. |