Skip to content

Commit

Permalink
Merge pull request #28 from Eh2406/patch-1
Browse files Browse the repository at this point in the history
only calculate loc for marginals in ipf once.
  • Loading branch information
jiffyclub committed Feb 8, 2016
2 parents 4659c99 + a68d6d3 commit 02a719b
Showing 1 changed file with 7 additions and 11 deletions.
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

0 comments on commit 02a719b

Please sign in to comment.