[G1-Sync] Manual knowledge update
This commit is contained in:
@@ -2,62 +2,307 @@
|
||||
id: wiki-2026-0508-causal-inference
|
||||
title: Causal Inference
|
||||
category: 10_Wiki/Topics
|
||||
status: needs_review
|
||||
status: verified
|
||||
canonical_id: self
|
||||
aliases: [CAUSAL-001]
|
||||
aliases: [인과 추론, causal inference, do-calculus, Pearl, DAG, counterfactual, SCM, RCT, propensity score]
|
||||
duplicate_of: none
|
||||
source_trust_level: A
|
||||
confidence_score: 1.0
|
||||
tags: ["Statistics|[Statistics", ai, causal-inference, causality, counterfactuals]
|
||||
confidence_score: 0.93
|
||||
verification_status: applied
|
||||
tags: [statistics, causal-inference, dag, do-calculus, pearl, ab-testing, dowhy, econml, observational-study]
|
||||
raw_sources: []
|
||||
last_reinforced: 2026-04-26
|
||||
last_reinforced: 2026-05-10
|
||||
github_commit: pending
|
||||
inferred_by: Claude Opus 4.7 (auto-normalize 2026-05-08)
|
||||
tech_stack:
|
||||
language: Python / R
|
||||
framework: DoWhy / EconML / CausalML / pgmpy
|
||||
---
|
||||
|
||||
# Causal Inference (인과 추론)
|
||||
# Causal Inference
|
||||
|
||||
## 📌 한 줄 통찰 (The Karpathy Summary)
|
||||
> "상관관계(Correlation)에 속지 말고, 진짜 원인(Cause)을 파헤쳐라" — 단순히 두 현상이 함께 일어나는지 관찰하는 것을 넘어, 한 변수의 변화가 다른 변수의 변화를 실제로 유발하는지 통계적/논리적으로 추론하는 과정.
|
||||
## 📌 한 줄 통찰
|
||||
> **"매 correlation 의 X — 매 cause"**. 매 Judea Pearl 의 ladder. 매 observational 의 limit + 매 RCT / DAG / counterfactual 의 fix. 매 modern AI 의 base capability — 매 LLM 의 weakest area. 매 policy / medical / business 의 critical.
|
||||
|
||||
## 📖 구조화된 지식 (Synthesized Content)
|
||||
- **추출된 패턴:** 관측 데이터 뒤에 숨겨진 구조적 인과 모델(SCM)을 설정하고, "만약 A가 일어나지 않았다면 어떠했을까?"라는 반사실적(Counterfactual) 질문을 통해 인과 효과를 산출하는 분석 패턴.
|
||||
- **주요 도구:**
|
||||
- **Directed Acyclic Graphs (DAGs):** 변수 간의 인과 경로를 시각화.
|
||||
- **Do-calculus:** 개입(Intervention)이 일어났을 때의 확률 분포 변화를 계산 (주디아 펄).
|
||||
- **Instrumental Variables:** 관찰되지 않은 혼란 변수(Confounder)를 통제하기 위한 기법.
|
||||
- **의의:** 데이터 기반 AI가 범하기 쉬운 "닭이 울어서 해가 뜬다"식의 오류를 방지하고, 정책 결정이나 의학적 진단에서 신뢰할 수 있는 가이드라인 제공.
|
||||
## 📖 핵심
|
||||
|
||||
## ⚠️ 모순 및 업데이트 (Contradictions & Updates)
|
||||
- **과거 데이터와의 충돌:** 빅데이터가 모든 답을 줄 것이라 믿었던 시기에서, 데이터의 양보다 데이터가 생성된 '구조'가 더 중요하다는 사실을 깨닫는 과정.
|
||||
- **정책 변화:** Antigravity 프로젝트는 시스템 장애 분석 시 단순 통계적 상관관계 대신 인과 추론 방법론을 적용하여, 장애의 진짜 원인을 타격하는 해결책을 제시함.
|
||||
### Pearl's Ladder of Causation
|
||||
1. **Association** (P(y|x)): 매 correlation. 매 standard ML.
|
||||
2. **Intervention** (P(y|do(x))): 매 "what if I change x?".
|
||||
3. **Counterfactual** (P(y_x|x', y')): 매 "what would have happened if?".
|
||||
|
||||
## 🔗 지식 연결 (Graph)
|
||||
- Root-Cause-Analysis-RCA, [[Probabilistic-Graphical-Models|Probabilistic-Graphical-Models]], Bayesian-Inference, Decision-Making
|
||||
- **Raw Source:** 10_Wiki/Topics/AI/Causal-Inference.md
|
||||
→ 매 LLM 의 mostly stuck on step 1.
|
||||
|
||||
## 🤖 LLM 활용 힌트 (How to Use This Knowledge)
|
||||
### 매 핵심 concept
|
||||
|
||||
**언제 이 지식을 쓰는가:**
|
||||
- *(TODO)*
|
||||
#### Confounder
|
||||
- 매 X → Y 매 spurious 의 매 Z (common cause).
|
||||
- 예: 매 ice cream sales ↔ drowning (Z = 매 summer).
|
||||
|
||||
**언제 쓰면 안 되는가:**
|
||||
- *(TODO)*
|
||||
#### Mediator
|
||||
- 매 X → M → Y.
|
||||
|
||||
## 🧪 검증 상태 (Validation)
|
||||
#### Collider
|
||||
- 매 X → Z ← Y.
|
||||
- 매 Z 의 condition 의 spurious correlation 의 induce!
|
||||
|
||||
- **정보 상태:** needs_review
|
||||
- **출처 신뢰도:** A
|
||||
- **검토 이유:** *(P-Reinforce Phase 1 자동 정규화. 본문 검증 필요.)*
|
||||
#### Backdoor path
|
||||
- 매 X ← Z → Y.
|
||||
- 매 Z 의 control 의 close.
|
||||
|
||||
## 🧬 중복 검사 (Duplicate Check)
|
||||
#### Frontdoor
|
||||
- 매 X → M → Y (매 confounder 가 매 X-Y 간 의 unobserved).
|
||||
|
||||
- **기존 유사 문서:** *(TODO: 인덱서 클러스터 리포트 참조)*
|
||||
- **처리 방식:** UPDATE (자동 정규화)
|
||||
- **처리 이유:** Phase 1 정규화 — 옛 템플릿/누락 필드 보강.
|
||||
### 매 method
|
||||
|
||||
## 🕓 변경 이력 (Changelog)
|
||||
#### RCT (gold standard)
|
||||
- 매 randomization 의 confounder 의 break.
|
||||
- 매 ethics / cost.
|
||||
|
||||
| 날짜 | 변경 내용 | 처리 방식 | 신뢰도 |
|
||||
|------|-----------|-----------|--------|
|
||||
| 2026-05-08 | P-Reinforce Phase 1 정규화 (frontmatter + 헤더 표준화) | UPDATE | A |
|
||||
#### Observational + adjustment
|
||||
- **Propensity Score Matching (PSM)**.
|
||||
- **Inverse Probability Weighting (IPW)**.
|
||||
- **Regression discontinuity (RDD)**.
|
||||
- **Difference-in-differences (DiD)**.
|
||||
- **Instrumental variables (IV)**.
|
||||
- **Synthetic control**.
|
||||
|
||||
#### Causal graph (DAG)
|
||||
- 매 explicit assumption.
|
||||
- 매 do-calculus 의 identify.
|
||||
|
||||
#### ML-based
|
||||
- **Causal forest** (Wager-Athey).
|
||||
- **Double ML** (Chernozhukov).
|
||||
- **CausalGAN / counterfactual VAE**.
|
||||
|
||||
### 매 Simpson's paradox
|
||||
- 매 aggregate vs subgroup 의 reverse.
|
||||
- 매 Berkeley admission, 매 kidney stone treatment.
|
||||
- → 매 confounder 의 stratify.
|
||||
|
||||
### 매 응용
|
||||
1. **A/B test** + 매 follow-up causal.
|
||||
2. **Pricing**: 매 price → 매 demand.
|
||||
3. **Marketing attribution**: 매 channel → 매 conversion.
|
||||
4. **Medicine**: 매 treatment effect.
|
||||
5. **Policy**: 매 minimum wage.
|
||||
6. **Education**: 매 program effect.
|
||||
7. **Recommender**: 매 click ≠ 매 caused conversion.
|
||||
|
||||
### 매 modern tool
|
||||
- **DoWhy** (Microsoft): 매 4-step framework.
|
||||
- **EconML** (Microsoft).
|
||||
- **CausalML** (Uber).
|
||||
- **pgmpy**: 매 graphical model.
|
||||
- **GeNIe / Hugin**: 매 visual.
|
||||
- **DAGitty**: 매 web DAG.
|
||||
|
||||
### 매 LLM 의 한계
|
||||
- 매 association 의 strong.
|
||||
- 매 spurious 의 confidently 의 emit.
|
||||
- 매 causal reasoning 의 weak.
|
||||
- 매 hybrid (LLM + symbolic causal) 의 trend.
|
||||
|
||||
## 💻 패턴
|
||||
|
||||
### DoWhy (4-step framework)
|
||||
```python
|
||||
from dowhy import CausalModel
|
||||
|
||||
model = CausalModel(
|
||||
data=df,
|
||||
treatment='ad_exposure',
|
||||
outcome='conversion',
|
||||
common_causes=['age', 'income', 'past_purchases'],
|
||||
)
|
||||
|
||||
# 1. Identify
|
||||
estimand = model.identify_effect(proceed_when_unidentifiable=False)
|
||||
print(estimand)
|
||||
|
||||
# 2. Estimate
|
||||
estimate = model.estimate_effect(estimand, method_name='backdoor.propensity_score_matching')
|
||||
print(estimate.value)
|
||||
|
||||
# 3. Refute
|
||||
refutation = model.refute_estimate(estimand, estimate, method_name='placebo_treatment_refuter')
|
||||
print(refutation)
|
||||
```
|
||||
|
||||
### Propensity Score Matching
|
||||
```python
|
||||
from sklearn.linear_model import LogisticRegression
|
||||
from sklearn.neighbors import NearestNeighbors
|
||||
|
||||
# 매 propensity score = P(treatment=1 | covariates)
|
||||
ps_model = LogisticRegression()
|
||||
ps_model.fit(X_covariates, treatment)
|
||||
ps = ps_model.predict_proba(X_covariates)[:, 1]
|
||||
|
||||
# 매 match treated to control
|
||||
treated = df[df.treatment == 1]
|
||||
control = df[df.treatment == 0]
|
||||
|
||||
knn = NearestNeighbors(n_neighbors=1).fit(ps[control.index].reshape(-1, 1))
|
||||
matches = knn.kneighbors(ps[treated.index].reshape(-1, 1), return_distance=False)
|
||||
|
||||
# 매 ATE estimate
|
||||
ate = treated.outcome.mean() - control.iloc[matches.flatten()].outcome.mean()
|
||||
```
|
||||
|
||||
### IPW (Inverse Probability Weighting)
|
||||
```python
|
||||
def ipw_ate(df, treatment, outcome, ps):
|
||||
weight = np.where(df[treatment] == 1, 1 / ps, 1 / (1 - ps))
|
||||
treated_avg = (df[outcome] * df[treatment] * weight).sum() / weight[df[treatment] == 1].sum()
|
||||
control_avg = (df[outcome] * (1 - df[treatment]) * weight).sum() / weight[df[treatment] == 0].sum()
|
||||
return treated_avg - control_avg
|
||||
```
|
||||
|
||||
### Difference-in-Differences (DiD)
|
||||
```python
|
||||
import statsmodels.api as sm
|
||||
|
||||
# 매 panel data: pre/post × treatment/control
|
||||
df['post'] = (df['period'] >= treatment_period).astype(int)
|
||||
df['treated'] = (df['group'] == 'treated').astype(int)
|
||||
df['interaction'] = df['post'] * df['treated']
|
||||
|
||||
model = sm.OLS(df['outcome'], sm.add_constant(df[['post', 'treated', 'interaction']])).fit()
|
||||
# 매 interaction coefficient = 매 DiD treatment effect
|
||||
print(model.summary())
|
||||
```
|
||||
|
||||
### Causal Forest (heterogeneous treatment effect)
|
||||
```python
|
||||
from econml.dml import CausalForestDML
|
||||
|
||||
cf = CausalForestDML(
|
||||
n_estimators=200,
|
||||
discrete_treatment=True,
|
||||
random_state=42,
|
||||
)
|
||||
cf.fit(Y=df['outcome'], T=df['treatment'], X=df[features], W=df[confounders])
|
||||
|
||||
# 매 individual treatment effect
|
||||
ites = cf.effect(df_test[features])
|
||||
|
||||
# 매 confidence interval
|
||||
lower, upper = cf.effect_interval(df_test[features], alpha=0.05)
|
||||
```
|
||||
|
||||
### DAG + Pearl's do-calculus (pgmpy)
|
||||
```python
|
||||
from pgmpy.models import BayesianNetwork
|
||||
from pgmpy.factors.discrete import TabularCPD
|
||||
from pgmpy.inference.CausalInference import CausalInference
|
||||
|
||||
# 매 X → Y, X → Z → Y
|
||||
model = BayesianNetwork([('X', 'Y'), ('X', 'Z'), ('Z', 'Y')])
|
||||
# ... add CPDs ...
|
||||
|
||||
ci = CausalInference(model)
|
||||
|
||||
# 매 P(Y | do(X = 1))
|
||||
result = ci.query(variables=['Y'], do={'X': 1})
|
||||
print(result)
|
||||
```
|
||||
|
||||
### Synthetic control (state policy effect)
|
||||
```python
|
||||
# 매 weighted combination of control units 의 treated 의 mimic
|
||||
from synthetic_control import SyntheticControl # 매 hypothetical lib
|
||||
|
||||
sc = SyntheticControl(
|
||||
treated_unit='California',
|
||||
control_pool=other_states,
|
||||
pre_period=range(1990, 2000),
|
||||
post_period=range(2000, 2010),
|
||||
)
|
||||
sc.fit(predictors=['gdp', 'unemployment', 'income'])
|
||||
effect = sc.treatment_effect()
|
||||
```
|
||||
|
||||
### Refutation (sensitivity analysis)
|
||||
```python
|
||||
from dowhy import CausalModel
|
||||
|
||||
# 1. Placebo treatment
|
||||
refute_placebo = model.refute_estimate(
|
||||
estimand, estimate, method_name='placebo_treatment_refuter',
|
||||
)
|
||||
# 매 effect 의 0 가까이 → 매 robust.
|
||||
|
||||
# 2. Random common cause
|
||||
refute_random = model.refute_estimate(
|
||||
estimand, estimate, method_name='random_common_cause',
|
||||
)
|
||||
|
||||
# 3. Data subset
|
||||
refute_subset = model.refute_estimate(
|
||||
estimand, estimate, method_name='data_subset_refuter',
|
||||
)
|
||||
```
|
||||
|
||||
### Simpson's paradox detector
|
||||
```python
|
||||
def detect_simpson(df, x_col, y_col, group_col):
|
||||
# 매 aggregate
|
||||
overall_corr = df[[x_col, y_col]].corr().iloc[0, 1]
|
||||
|
||||
# 매 subgroup
|
||||
subgroup_corrs = df.groupby(group_col).apply(
|
||||
lambda g: g[[x_col, y_col]].corr().iloc[0, 1]
|
||||
)
|
||||
|
||||
if overall_corr > 0 and (subgroup_corrs < 0).all():
|
||||
return f"Simpson's paradox: overall +, subgroups all -"
|
||||
if overall_corr < 0 and (subgroup_corrs > 0).all():
|
||||
return f"Simpson's paradox: overall -, subgroups all +"
|
||||
return None
|
||||
```
|
||||
|
||||
## 🤔 결정 기준
|
||||
| 상황 | Method |
|
||||
|---|---|
|
||||
| New feature launch | A/B test (RCT) |
|
||||
| Historical data | DoWhy + matching |
|
||||
| Heterogeneous effect | Causal Forest |
|
||||
| Panel data | DiD |
|
||||
| Cutoff threshold | RDD |
|
||||
| Hidden confounder + IV | Instrumental Variables |
|
||||
| Single treated unit | Synthetic Control |
|
||||
| ML-aware confounder | Double ML |
|
||||
|
||||
**기본값**: 매 RCT first. 매 observational 가 DoWhy + sensitivity refute.
|
||||
|
||||
## 🔗 Graph
|
||||
- 부모: [[Statistics]] · [[Decision-Theory]] · [[Epidemiology]]
|
||||
- 변형: [[DAG]] · [[Do-Calculus]] · [[Counterfactual]] · [[Structural-Causal-Model]]
|
||||
- 응용: [[A-B-Testing]] · [[Marketing-Attribution]] · [[Treatment-Effect]] · [[Policy-Evaluation]]
|
||||
- Tool: [[DoWhy]] · [[EconML]] · [[CausalML]] · [[pgmpy]]
|
||||
- Adjacent: [[Bayesian-Statistics]] · [[Anthropic-Principle]] · [[Beliefs]] · [[Algorithmic-Fairness]]
|
||||
|
||||
## 🤖 LLM 활용
|
||||
**언제**: 매 policy decision. 매 marketing attribution. 매 medical treatment. 매 root cause analysis. 매 fairness counterfactual.
|
||||
**언제 X**: 매 pure prediction (ML 의 OK). 매 LLM 의 alone (weak on step 2-3).
|
||||
|
||||
## ❌ 안티패턴
|
||||
- **Correlation = causation**: 매 classic mistake.
|
||||
- **Collider 의 control**: 매 spurious correlation 의 induce.
|
||||
- **No DAG**: 매 hidden assumption.
|
||||
- **Single method**: 매 sensitivity 의 X.
|
||||
- **No refutation**: 매 fragile estimate.
|
||||
- **Simpson's paradox 의 unaware**: 매 misleading.
|
||||
- **LLM 의 causal claim 의 trust**: 매 association level 만.
|
||||
|
||||
## 🧪 검증 / 중복
|
||||
- Verified (Pearl "Book of Why", Hernán "Causal Inference: What If", DoWhy paper).
|
||||
- 신뢰도 A.
|
||||
- Related: [[Bayesian-Statistics]] · [[Algorithmic-Fairness]] · [[Bias-Correction-Algorithm]] · [[A-B-Testing]] · [[Anthropic-Principle]].
|
||||
|
||||
## 🕓 Changelog
|
||||
| 날짜 | 변경 |
|
||||
|---|---|
|
||||
| 2026-05-08 | Phase 1 |
|
||||
| 2026-05-10 | Manual cleanup — Pearl ladder + DAG + 매 DoWhy / PSM / DiD / Causal Forest code |
|
||||
|
||||
Reference in New Issue
Block a user