Skip to content

Commit aefa7f3

Browse files
igerberclaude
andcommitted
Validate combined-weight IF ratio, zero aweight scores for zero-weight obs
- Reject combined_weights=True designs where w_r>0 but w_full=0 (the w_r/w_full ratio is undefined; silently zeroing gives wrong SEs) - Zero out aweight TSL scores for zero-weight observations to prevent arbitrary residuals from entering the meat computation Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent cb0880a commit aefa7f3

1 file changed

Lines changed: 15 additions & 0 deletions

File tree

diff_diff/survey.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1187,6 +1187,9 @@ def compute_survey_vcov(
11871187
# Compute weighted scores per observation: w_i * X_i * u_i
11881188
if resolved.weight_type == "aweight":
11891189
scores = X * residuals[:, np.newaxis]
1190+
# Zero-weight observations should not contribute to aweight meat
1191+
if np.any(weights == 0):
1192+
scores[weights == 0] = 0.0
11901193
else:
11911194
scores = X * (weights * residuals)[:, np.newaxis]
11921195

@@ -1442,6 +1445,18 @@ def compute_replicate_if_variance(
14421445
full_weights = resolved.weights
14431446
theta_full = float(np.sum(psi))
14441447

1448+
# Validate: combined_weights=True requires w_full > 0 wherever w_r > 0
1449+
if resolved.combined_weights:
1450+
for r in range(R):
1451+
bad = (rep_weights[:, r] > 0) & (full_weights <= 0)
1452+
if np.any(bad):
1453+
raise ValueError(
1454+
f"Replicate column {r} has positive weight where full-sample "
1455+
f"weight is zero. With combined_weights=True, every "
1456+
f"replicate-positive observation must have a positive "
1457+
f"full-sample weight."
1458+
)
1459+
14451460
# Compute replicate estimates via weight-ratio rescaling
14461461
theta_reps = np.full(R, np.nan)
14471462
for r in range(R):

0 commit comments

Comments
 (0)