Dirguis / ipfn

Iterative Proportional Fitting for Python with N dimensions
MIT License
91 stars 17 forks source link

README algorithm examples #25

Open edyhsgr opened 1 year ago

edyhsgr commented 1 year ago

I think the 3D algorithm examples aren't set to fit on all dimensions. I checked the numpy version and found it wasn't fitting to 'xipk'. Proposed code with commented (##) revisions below. I also compared to results from https://github.com/AppliedDemogToolbox/IPF_R (Excel example and application of the R code).

from ipfn import ipfn import numpy as np import pandas as pd

m = np.zeros((2,4,3)) m[0,0,0] = 1 m[0,0,1] = 2 m[0,0,2] = 1 m[0,1,0] = 3 m[0,1,1] = 5 m[0,1,2] = 5 m[0,2,0] = 6 m[0,2,1] = 2 m[0,2,2] = 2 m[0,3,0] = 1 m[0,3,1] = 7 m[0,3,2] = 2 m[1,0,0] = 5 m[1,0,1] = 4 m[1,0,2] = 2 m[1,1,0] = 5 m[1,1,1] = 5 m[1,1,2] = 5 m[1,2,0] = 3 m[1,2,1] = 8 m[1,2,2] = 7 m[1,3,0] = 2 m[1,3,1] = 7 m[1,3,2] = 6

xipp = np.array([52, 48]) xpjp = np.array([20, 30, 35, 15]) xppk = np.array([35, 40, 25]) xijp = np.array([[9, 17, 19, 7], [11, 13, 16, 8]]) xpjk = np.array([[7, 9, 4], [8, 12, 10], [15, 12, 8], [5, 7, 3]]) xipk = np.array([[22, 20, 10], [13, 20, 15]]) ##Added line

m[0,:,:].sum() == xipp[0] m[1,:,:].sum() == xipp[1]

aggregates = [xipp, xpjp, xppk, xijp, xpjk, xipk] dimensions = [[0], [1], [2], [0, 1], [1, 2], [0, 2]] ##Revised line

IPF = ipfn.ipfn(m, aggregates, dimensions) m = IPF.iteration() print(xijp[0,0]) print(m[0, 0, :].sum())

print(m)

Dirguis commented 8 months ago

Hi, Yes, you are right, xipk is not provided. I did it on purpose, to show that the algorithm still works if the information is incomplete. It will fit what it can, based on the marginals provided, which is fine. Let me know if this is still an issue. Best