Evaluation¶
evaluate_graph ¶
Compute structure-recovery metrics for a fitted graph.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
predicted
|
TimeSeriesGraph
|
The :class: |
required |
truth
|
TruthLike
|
The ground-truth structure. Can be either a
:class: |
required |
Returns:
| Type | Description |
|---|---|
StructureRecoveryMetrics
|
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
Example
from cdans import CDANs from cdans.utils import generate_synthetic_cdans from cdans.evaluation import evaluate_graph dataset = generate_synthetic_cdans(n_vars=4, n_samples=400, tau_max=2) result = CDANs(tau_max=2).fit(dataset.data) metrics = evaluate_graph(result.graph, dataset) print(metrics.summary()) # doctest: +SKIP
shd ¶
Total Structural Hamming Distance (lagged + contemporaneous).
Convenience wrapper around the corresponding fields of
:func:evaluate_graph.
GraphMetrics
dataclass
¶
Edge-level confusion-matrix counts plus derived rates.
Attributes:
| Name | Type | Description |
|---|---|---|
tp |
int
|
True positives — edges in both prediction and truth. |
fp |
int
|
False positives — edges in prediction but not in truth. |
fn |
int
|
False negatives — edges in truth but not in prediction. |
n_truth |
int
|
Total number of true edges ( |
n_pred |
int
|
Total number of predicted edges ( |
recall
property
¶
TP / (TP + FN). Same as :attr:tpr. Returns 0.0 when no
true edges exist (i.e. truth is empty).
StructureRecoveryMetrics
dataclass
¶
StructureRecoveryMetrics(
lagged: GraphMetrics,
contemp_skeleton: GraphMetrics,
contemp_directed: GraphMetrics,
changing_modules: GraphMetrics,
shd_lagged: int,
shd_contemp: int,
)
All metrics for a single graph-vs-truth comparison.
Use :func:evaluate_graph to produce one of these.
Attributes:
| Name | Type | Description |
|---|---|---|
lagged |
GraphMetrics
|
:class: |
contemp_skeleton |
GraphMetrics
|
:class: |
contemp_directed |
GraphMetrics
|
:class: |
changing_modules |
GraphMetrics
|
:class: |
shd_lagged |
int
|
Structural Hamming Distance for lagged edges. Equal to the symmetric difference of the two edge sets. |
shd_contemp |
int
|
PDAG-aware SHD for contemporaneous edges. For each unordered
pair |
shd_total |
int
|
|
Notes
Two convenience aggregates are also available as computed properties:
- :attr:
total— pools TP/FP/FN across :attr:lagged, :attr:contemp_directed, and :attr:changing_modulesinto a single :class:GraphMetrics. Use this when you want one overall TPR/FDR/F1 number per fit (the typical "full TPR/FDR" reported in benchmarks). - :attr:
total_skeleton— same astotalbut uses :attr:contemp_skeletoninstead of :attr:contemp_directed. More lenient: a contemp edge with the wrong direction still counts.
total
property
¶
Aggregate metrics across all categories at the directed-edge level.
Pools TP/FP/FN counts from :attr:lagged,
:attr:contemp_directed, and :attr:changing_modules into a
single :class:GraphMetrics. This yields the overall TPR, FDR,
precision, recall, and F1 typically reported in causal-discovery
papers as a single per-method number.
Use :attr:total_skeleton for the more lenient adjacency-only
version that doesn't penalize direction errors.
total_skeleton
property
¶
Aggregate metrics at the contemp-skeleton level (more lenient).
Same as :attr:total but uses :attr:contemp_skeleton instead
of :attr:contemp_directed, so a contemp edge with the wrong
direction (or left undirected) still counts as a true positive
as long as the adjacency was found.
Useful for separating "did we find the right structure" from "did we orient it correctly".