Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

only calculate loc for marginals in ipf once. #28

Merged
merged 2 commits into from
Feb 8, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 7 additions & 11 deletions synthpop/ipf/ipf.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,20 +52,16 @@ def calc_diff(x, y):

iterations = 0

list_of_loc = [
((flat_joint_dist[idx[0]] == idx[1]).values, marginals[idx])
for idx in marginals.index
]

while calc_diff(constraints, prev_constraints) > tolerance:
prev_constraints[:] = constraints

for idx in marginals.index:
# get the locations of the things we're updating
cat = idx[0] # top level category (col name in flat_joint_dist)
subcat = idx[1] # subcategory (col values in flat_joint_dist)
loc = (flat_joint_dist[cat] == subcat).values

# figure out the proportions for this update
proportions = constraints[loc] / constraints[loc].sum()

# distribute new total for these classes
constraints[loc] = proportions * marginals[idx]
for loc, target in list_of_loc:
constraints[loc] *= target / constraints[loc].sum()

iterations += 1

Expand Down