Skip to content

Keep each value whole in group-to-group map_to_entity(how="divide") - #770

Draft
MaxGhenis wants to merge 3 commits into
mainfrom
fix/map-to-entity-group-divide
Draft

MaxGhenis wants to merge 3 commits into
mainfrom
fix/map-to-entity-group-divide

Conversation

@MaxGhenis

@MaxGhenis MaxGhenis commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #768

Group-to-group map_to_entity(how="divide") now splits each source value equally among the source's members and gives each target the shares of the members it holds. Each (source, target) pair row carries value * pair_count / source_count, and the branch sums those rows by target. A source's shares add up to its value whichever targets its members sit in, so the $100 two-person tax unit in #768 maps to $100 in its household instead of $50. A member with no target link keeps its share out of every target, and a source with no members adds nothing, as in a group-to-person divide that feeds a person-to-group sum.

tests/test_entity_mapping_group_properties.py adds Hypothesis properties over random households of tax units and SPM units, crossing and nested, in both person-link layouts. The random tables list their rows in shuffled order and may hold a group with no members. Each household draws a weight its people and groups share, and every group carries two value columns.

  • divide keeps each column's total over the sources with members, for every ordered pair of tax unit, SPM unit and household, and returns the target table's groups in the table's order;
  • the result carries the target weights, so weighted totals match too;
  • each target receives the shares of the members it holds in every column, which the test counts person by person;
  • group-to-group divide matches a group-to-person divide that feeds a person-to-group sum;
  • passing values= returns a weighted MicroSeries that matches naming the column;
  • when sources nest in targets, sum keeps the total and agrees with divide;
  • sum from a household or SPM unit gives each group inside it the containing group's value, as it does today.

Three fixed cases cover the $100 unit, a $300 unit that splits 200/100 across two SPM units, and a member with no household link. On main, seven of the ten tests fail. The values= test, the sum pin and the missing-link case pass on both. The tests call policyengine.core.map_to_entity on plain tables and run without a country model under pytest --noconftest. The map_to_entity docstrings and .claude/policyengine-guide.md now describe the group-to-group split.

sum keeps its current behavior. #769 asks how Aggregate should total a group value at a finer entity.

🤖 Generated with Claude Code

MaxGhenis and others added 3 commits November 26, 2026 11:20
The group-to-group divide branch divided each (source, target) pair row
by the source's member count, so a source whose n members touched k
targets kept k/n of its value. It now weights each pair row by the
members that pair holds, value * pair_count / source_count, so a
source's shares add up to its value.

Hypothesis properties cover crossing and nested tax units, SPM units
and households in both person-link layouts.

Fixes #768

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Round-1 review and a mutation sweep found test gaps. The random cases now
shuffle every table, add memberless groups, draw household weights and
map two value columns. New properties check weighted totals, each
target's share against a person-by-person count, and the values= return
type. The coarse-to-fine sum pin covers household and SPM unit sources,
and a fixed case pins a member with no target link. Totals compare with
an absolute tolerance so exact zeros pass.

The map_to_entity docstrings, the divide comment and the guide now say
how group-to-group divide splits a value and what happens to unlinked
members and memberless groups.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…olumns

Round-2 review found three regressions against main in the divide
branch. value * pair_count overflowed int64 and turned large floats into
inf; merging the counts into the source rows collided with source
columns of the same name; and a NaN pair count for an unlinked member
made the counts float, so Decimal amounts raised TypeError.

The branch now drops rows without a target first, looks the integer
counts up on a frame of keys alone, and divides before it multiplies.
All three groupbys pass observed=True, so categorical links on pandas
2.x no longer build every category pair.

New tests pin each counterexample, check that reweighting people
changes no share, and check categorical links against plain ones. The
changelog now says a group passes on its whole value when all its
members link to target groups.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Group-to-group map_to_entity(how="divide") keeps only part of each value

1 participant