Skip to content

Commit d6cf291

Browse files
neuralsorcerermeta-codesync[bot]
authored andcommitted
Strengthen rake non-uniform design-weight regression test (#437)
Summary: - Related to #431 Pull Request resolved: #437 Differential Revision: D103634273 Pulled By: talgalili fbshipit-source-id: 4ccea2a78e390be5634ededb2bb1d8f732d75a5e
1 parent 49484a9 commit d6cf291

1 file changed

Lines changed: 28 additions & 0 deletions

File tree

tests/test_rake.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,7 @@ def test_rake_nonuniform_design_weights_marginal_recovery(self) -> None:
447447
target_weights,
448448
variables=["a", "b"],
449449
transformations=None,
450+
store_fit_metadata=True,
450451
)
451452
w = result["weight"]
452453

@@ -504,6 +505,7 @@ def test_rake_nonuniform_design_weights_per_cell_totals(self) -> None:
504505
target_weights,
505506
variables=["a", "b"],
506507
transformations=None,
508+
store_fit_metadata=True,
507509
)
508510
w = result["weight"]
509511

@@ -523,6 +525,32 @@ def test_rake_nonuniform_design_weights_per_cell_totals(self) -> None:
523525
observed_ratio = w.iloc[idx_heavy] / w.iloc[idx_light]
524526
self.assertAlmostEqual(observed_ratio, expected_ratio, places=4)
525527

528+
# Per-cell totals must match m_fit after the same global rescaling
529+
# induced by trim_weights (m_fit is on the pre-trim sample-sum scale).
530+
weighted_by_cell = (
531+
sample_df.assign(final_weight=w)
532+
.groupby(["a", "b"], observed=False)["final_weight"]
533+
.sum()
534+
.sort_index()
535+
)
536+
model = _assert_type(result["model"])
537+
categories = _assert_type(model["categories"])
538+
m_fit = _assert_type(model["m_fit"])
539+
expected_by_cell = pd.Series(
540+
m_fit.flatten(),
541+
index=pd.MultiIndex.from_product(categories, names=["a", "b"]),
542+
dtype=float,
543+
).sort_index()
544+
expected_by_cell *= w.sum() / expected_by_cell.sum()
545+
pd.testing.assert_series_equal(
546+
weighted_by_cell,
547+
expected_by_cell,
548+
check_names=False,
549+
check_dtype=False,
550+
rtol=1e-6,
551+
atol=1e-6,
552+
)
553+
526554
def test_rake_weights_scale_to_pop(self) -> None:
527555
"""
528556
Test that rake weights properly scale to match target population size.

0 commit comments

Comments
 (0)