"""Shared normalized panel DR score; nuisance-rate assumptions are required for IF inference.""" from statistics import NormalDist import numpy as np def validate_table(x, d, y): x, d, y = (np.asarray(v, dtype=float) for v in (x, d, y)) if ( d.ndim != 1 or y.shape != d.shape or x.ndim != 2 or len(x) != len(d) or len(d) < 4 or not all(np.isfinite(v).all() for v in (x, d, y)) or not np.isin(d, [0, 1]).all() or len(np.unique(d)) != 2 ): raise ValueError( "Expected finite numeric X, binary D with both arms, and finite change" ) return x, d.astype(int), y def drdid_panel(y, d, propensity, outcome, clip=0.05, confidence=0.95): _, d, y = validate_table(np.empty((len(d), 0)), d, y) propensity, outcome = map( lambda v: np.asarray(v, dtype=float), (propensity, outcome) ) if ( propensity.shape != y.shape or outcome.shape != y.shape or not np.isfinite(propensity).all() or not np.isfinite(outcome).all() or ((propensity < 0) | (propensity > 1)).any() or not 0 < clip < 0.5 or not 0 < confidence < 1 ): raise ValueError("Invalid nuisance predictions, clipping, or confidence") e = np.clip(propensity, clip, 1 - clip) weights = (1 - d) * e / (1 - e) residual = y - outcome treated_mean = np.average(residual, weights=d) control_mean = np.average(residual, weights=weights) att = float(treated_mean - control_mean) influence = d / d.mean() * (residual - treated_mean) - weights / weights.mean() * ( residual - control_mean ) se = float(influence.std(ddof=1) / np.sqrt(len(d))) margin = NormalDist().inv_cdf((1 + confidence) / 2) * se return dict( att=att, se=se, lower=att - margin, upper=att + margin, clipped_fraction=float(np.mean(e != propensity)), control_ess=float(weights.sum() ** 2 / (weights @ weights)), )