我有一个实质上的分配问题。
我所拥有的:我观察了一些小的地理区域,比如人口普查区。我有四个不同年龄组的人数。每个区域都属于一个分区域。
现在,我知道小区域的分布并不完全正确,因为我知道正确的分布--在更高的聚集级别,子区域级别,以及更精细的域级数据,加起来后,显示不同的组总计。
我想要的是:,我想调整我的区域水平,四组之间的分布,所以它与已知正确的四组之间的汇总级分布是一致的,但是保留了域级分布的信号。根据更多的粗数据来调整它,但不要把它抛出窗外。
因此,我想要做的是,改变范围内的人口数量,以满足以下标准,其中前两项是最重要的(我意识到在满足所有这些方面都需要权衡):
下面是模拟数据和占位符代码:
首先,小面积数据:
n=1000
np.random.seed(123)
df_small_area_scale = pd.DataFrame(data={
'grp1':np.random.randint(10,250,n),
'grp2':np.random.randint(10,250,n),
'grp3':np.random.randint(10,250,n),
'grp4':np.random.randint(10,250,n),
'subregion': np.random.choice(['A', 'B', 'C', 'D', 'E'],n),
'tract_id':range(1000)}).set_index(['subregion','tract_id'])
df_small_area_scale.head()
grp1 grp2 grp3 grp4
subregion tract_id
B 0 119 85 11 19
D 1 136 100 46 239
A 2 76 26 198 109
B 3 230 180 84 222
A 4 108 101 222 244并且,通过subregion聚合这些信息,我们得到如下结果:
df_small_area_scale.groupby(level=0).sum()
grp1 grp2 grp3 grp4
subregion
A 27241 27050 27471 26215
B 26507 24696 23315 24857
C 27474 28871 28882 28743
D 26671 26163 25077 27612
E 22739 23077 23797 24473(让我们获取每个组中每个分区的目标份额)
summary_area_scale_shares = summary_area_scale.stack().groupby(level=0).apply(lambda x: x/float(x.sum()))
summary_area_scale_shares.head()
subregion
A grp1 0.244444
grp2 0.266667
grp3 0.244444
grp4 0.244444
B grp1 0.255319
dtype: float64第二,小区域数据在分区域一级的总和。
这些是分区域的“已知”分布。这就是我想要调整的域级数据,这样当区域被聚集时,它们大致地匹配每个组中的这些区域总数。具体来说,grp4 in subregion A总计为26,215,但根据目标,应该是22,000,所以在分区域A区的人应该重新分类从grp4到其他一些群体。
summary_area_scale = pd.DataFrame(data={'grp1':[22000,24000,21000,25000,28000],
'grp2':[24000,22000,26000,20000,28000],
'grp3':[22000,24000,21000,25000,28000],
'grp4':[22000,24000,21000,25000,28000],
'subregion':list('ABCDE')}).set_index('subregion')
summary_area_scale
grp1 grp2 grp3 grp4
subregion
A 22000 24000 22000 22000
B 24000 22000 24000 24000
C 21000 26000 21000 21000
D 25000 20000 25000 25000
E 28000 28000 28000 28000一种想法是在每个分区域内取样,然后按需要从一个垃圾桶转移到另一个垃圾桶的总人数按一定比例迁移人员,尽管我不确定是否有一种符合上述标准的聪明办法。
给我带来问题的主要原因是找出了一种方法,将人群跨群体重新分配,以与次区域总数相匹配,同时保持创纪录的总人数,而不是完全丢弃我想要保持的空间分布,我希望以此作为一个信号(但调整到现在已知的不同的总体分布)。
从总体上讲,对于如何使一个细节分布更适合于聚合分布,除了采样域和将x个人从grp4 -> grp3、grp2 -> grp1以及现有分布与目标分布之间的任何区别之外,有什么想法吗?
占位符码
这个函数很大程度上是查找每个组中区域共享的表,将该分布推到每个域,因此它只会设置数据绑定。
def some_redistribution_algorithm(df):
"""
how many persons need to be moved across groups in each subregion?
minimal solution is to just take those shifts and apply uniformly
tracts keep the same counts, but the *distribution* across bins will change slightly
Quality criteria for algorithm:
- switch population at tract level such that
- tract-level population counts maintained
- Pre- and post-adjustment spatial distribution be largely unchanged
- change is not disproportional / dramatically impacting some tracts over others
(i.e. a tract with 10 grp4 population losing 8 would lose 80%, while a tract with 100 q4 hhs would lose 8%)
"""
adjustments = summary_area_scale.xs(df.name)
size = (adjustments).apply(lambda x: abs(x)).loc['grp4'].astype(np.int64)/df.shape[0]
print "Processing %s (%s tracts), beg. pop: %s, avg pop to move (here q4) %s" %(df.name,df.shape[0],
df.sum().loc['grp4'].astype(np.int64),size)
print 'Average pop per tract:'
print df.sum()/df.shape[0]
## tract-level distribution, if all tracts had the same distribution within each subregion (placeholder)
return df_small_area_scale.xs(df.name).mul(summary_area_scale_shares.unstack().xs(df.name),axis=1)
#samplerows= np.random.choice(a=df.index, size=size)
#df.loc[samplerows,:] = df.loc[samplerows,:]#, p=df.totalshare.tolist()),:]
df_small_area_scale.groupby(level=0).apply(some_redistribution_algorithm)发布于 2015-10-28 00:54:49
如果我正确理解你的问题,我认为迭代比例拟合可能是你想要的。如果可以的话,我会说出我最近遇到的类似问题。这就是我想要解决的问题:
我知道大城市的年龄分布,我也知道每个地区的人口总数,但由于人口普查的工作方式,我想我知道每个地区的年龄分布,但我不确定。
我知道我想要满足区域内的人口总数(行边缘),我知道我想要满足都市一级的年龄分布(列边缘),我想要“种子”指规数和每一区域的分布,这是我对答案的最佳猜测。当然,它不起作用(我的意思是数字不会加起来),所以为了满足边际值,你立即偏离了这个猜测。这就是迭代比例拟合的目的。
也许不是防弹的,但我使用的代码(在Python /numpy中)是这样的:
# this should be fairly self explanitory if you know ipf
# seed_matrix is your best bet at the totals, col_marginals are
# observed column marginals and row_marginals is the same for rows
def simple_ipf(seed_matrix, col_marginals, row_marginals, tolerance=1, cnt=0):
assert np.absolute(row_marginals.sum() - col_marginals.sum()) < 5.0
# first normalize on columns
ratios = col_marginals / seed_matrix.sum(axis=0)
seed_matrix *= ratios
closeness = np.absolute(row_marginals - seed_matrix.sum(axis=1)).sum()
assert np.absolute(col_marginals - seed_matrix.sum(axis=0)).sum() < .01
# print "row closeness", closeness
if closeness < tolerance:
return seed_matrix
# first normalize on rows
ratios = row_marginals / seed_matrix.sum(axis=1)
ratios[row_marginals == 0] = 0
seed_matrix = seed_matrix * ratios.reshape((ratios.size, 1))
assert np.absolute(row_marginals - seed_matrix.sum(axis=1)).sum() < .01
closeness = np.absolute(col_marginals - seed_matrix.sum(axis=0)).sum()
# print "col closeness", closeness
if closeness < tolerance:
return seed_matrix
if cnt >= 50:
return seed_matrix
return simple_ipf(seed_matrix, col_marginals, row_marginals,
tolerance, cnt+1) https://stackoverflow.com/questions/33357409
复制相似问题