| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 5bec03c commit f065afe
4 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -552,8 +552,8 @@ def generate_survey_multiplier_weights_batch( | |||
| 552 | 552 | pooled_cols = np.array(_singleton_cols) | |
| 553 | 553 | weights[:, pooled_cols] = pooled_weights | |
| 554 | 554 | else: | |
| 555 | - # Single singleton — cannot pool, zero weight (matches R survey | ||
| 556 | - # package behavior where adjust with one singleton = remove). | ||
| 555 | + # Single singleton — cannot pool, zero weight (library-specific | ||
| 556 | + # fallback; bootstrap adjust with one singleton = remove). | ||
| 557 | 557 | import warnings | |
| 558 | 558 | ||
| 559 | 559 | warnings.warn( | |
@@ -684,8 +684,8 @@ def generate_rao_wu_weights( | |||
| 684 | 684 | p = int(obs_psu[idx]) | |
| 685 | 685 | rescaled[idx] = base_weights[idx] * psu_scale_map.get(p, 1.0) | |
| 686 | 686 | else: | |
| 687 | - # Single singleton — cannot pool, keep base weights (matches R | ||
| 688 | - # survey package: adjust with one singleton = remove). | ||
| 687 | + # Single singleton — cannot pool, keep base weights (library-specific | ||
| 688 | + # fallback; bootstrap adjust with one singleton = remove). | ||
| 689 | 689 | import warnings | |
| 690 | 690 | ||
| 691 | 691 | warnings.warn( | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1280,6 +1280,12 @@ def trim_weights( | |||
| 1280 | 1280 | raise ValueError(f"quantile must be in (0, 1), got {quantile}") | |
| 1281 | 1281 | upper = float(np.nanquantile(w, quantile)) | |
| 1282 | 1282 | ||
| 1283 | + if upper is not None and lower is not None and lower > upper: | ||
| 1284 | + raise ValueError( | ||
| 1285 | + f"lower ({lower}) must be <= upper ({upper}). " | ||
| 1286 | + f"When using quantile, the resolved upper cap may be below lower." | ||
| 1287 | + ) | ||
| 1288 | + | ||
| 1283 | 1289 | if upper is not None: | |
| 1284 | 1290 | w = np.minimum(w, upper) | |
| 1285 | 1291 | if lower is not None: | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -2222,8 +2222,9 @@ ContinuousDiD, EfficientDiD): | |||
| 2222 | 2222 | ContinuousDiD, EfficientDiD) and Rao-Wu bootstrap (SunAbraham, SyntheticDiD, TROP). | |
| 2223 | 2223 | FPC scaling is skipped for pooled singletons (conservative). When only one singleton | |
| 2224 | 2224 | stratum exists total, pooling is not possible — the singleton contributes zero bootstrap | |
| 2225 | - variance (same as `remove`), with a `UserWarning` emitted. This matches R's `survey` | ||
| 2226 | - package behavior. Reference: Rust & Rao (1996). | ||
| 2225 | + variance (same as `remove`), with a `UserWarning` emitted. This is a library-specific | ||
| 2226 | + documented fallback (R's analytical `adjust` uses grand-mean centering, but the bootstrap | ||
| 2227 | + analogue for a single singleton is not defined in the literature). Reference: Rust & Rao (1996). | ||
| 2227 | 2228 | - **Deviation from R:** For the no-FPC case (`m_h = n_h - 1`), this matches R | |
| 2228 | 2229 | `survey::as.svrepdesign(type="subbootstrap")`. The FPC-adjusted resample size | |
| 2229 | 2230 | `m_h = round((1-f_h)*(n_h-1))` follows Rao, Wu & Yue (1992) Section 3. | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1201,6 +1201,14 @@ def test_trim_quantile_with_nan(self): | |||
| 1201 | 1201 | assert np.isfinite(result["w"].iloc[0]) | |
| 1202 | 1202 | assert np.isfinite(result["w"].iloc[4]) | |
| 1203 | 1203 | ||
| 1204 | + def test_trim_lower_exceeds_upper_raises(self): | ||
| 1205 | + """lower > upper raises ValueError.""" | ||
| 1206 | + from diff_diff.prep import trim_weights | ||
| 1207 | + | ||
| 1208 | + data = pd.DataFrame({"w": [1.0, 5.0, 10.0]}) | ||
| 1209 | + with pytest.raises(ValueError, match="lower.*<=.*upper"): | ||
| 1210 | + trim_weights(data, "w", upper=5.0, lower=6.0) | ||
| 1211 | + | ||
| 1204 | 1212 | ||
| 1205 | 1213 | # =========================================================================== | |
| 1206 | 1214 | # 8e-iii: ImputationDiD Pretrends + Survey | |
| Back | FazBrowse Home | New Git URL |
0 commit comments