xaheli's picture
download
raw
2.73 kB
"""Non-LLM baselines, so the language-model result means something.
Every baseline sees exactly the same information the model is shown: the candidate list
and the marginal correlation with the target. The partial-correlation baseline also gets
the full correlation matrix, which is a strictly stronger input -- if the LM cannot beat
a partial-correlation baseline that it is *not* being shown, the claim "the LM is doing
correlation" is safe.
"""
from __future__ import annotations
from typing import Dict, List
import numpy as np
from dgp import CANDIDATE_NAMES, Instance
def marginal_corr(inst: Instance) -> Dict[str, float]:
return dict(inst.marginal_corr)
def partial_corr_all(inst: Instance) -> Dict[str, float]:
"""Partial correlation of each candidate with the target, given all other candidates."""
df = inst.df
cols = inst.columns
y = df[:, cols.index("demand_mw")]
xs = df[:, : len(CANDIDATE_NAMES)]
r = np.corrcoef(np.column_stack([xs, y]).T)
p = np.linalg.pinv(r)
out = {}
for i, name in enumerate(CANDIDATE_NAMES):
d = np.sqrt(p[i, i] * p[-1, -1])
out[name] = float(-p[i, -1] / d) if d > 1e-12 else 0.0
return out
def topk_from_scores(scores: Dict[str, float], k: int) -> List[str]:
return sorted(scores, key=lambda n: -abs(scores[n]))[:k]
def oracle(inst: Instance, k: int) -> List[str]:
return list(inst.true_parents)
def corr_baseline(inst: Instance, k: int) -> List[str]:
return topk_from_scores(marginal_corr(inst), k)
def partial_corr_baseline(inst: Instance, k: int) -> List[str]:
return topk_from_scores(partial_corr_all(inst), k)
def score_selection(pred: List[str], truth: List[str]) -> Dict[str, float]:
"""Set-level precision / recall / F1 / Jaccard against the true parent set."""
p, t = set(pred), set(truth)
tp = len(p & t)
prec = tp / len(p) if p else 0.0
rec = tp / len(t) if t else 0.0
f1 = 2 * prec * rec / (prec + rec) if (prec + rec) > 0 else 0.0
jac = tp / len(p | t) if (p | t) else 0.0
return {
"precision": prec,
"recall": rec,
"f1": f1,
"jaccard": jac,
"tp": float(tp),
"fp": float(len(p - t)),
"fn": float(len(t - p)),
}
def per_variable_scores(pred: List[str], truth: List[str], names: List[str]) -> Dict[str, Dict[str, float]]:
"""For every candidate, was it called a cause (and should it have been)?"""
p, t = set(pred), set(truth)
out = {}
for n in names:
called, actual = (n in p), (n in t)
out[n] = {
"called_cause": float(called),
"is_true_parent": float(actual),
"correct": float(called == actual),
}
return out

Xet Storage Details

Size:
2.73 kB
·
Xet hash:
9d33c833549f643d0f57c12921b95e938e318228fb5d3f9cd70b6a70103dd43c

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.