Created
February 20, 2012 23:25
-
-
Save darribas/1872243 to your computer and use it in GitHub Desktop.
Segregation Theil index
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import multiprocessing as mp | |
def theil_seg(g): | |
''' | |
Compute segregation Theil index for one observation | |
... | |
g : array | |
Array of shape (g, ) with counts for each group in the tract | |
''' | |
c_ig = g / g.sum() | |
lc_ig = np.emath.logn(g.shape[0], 1./c_ig) | |
return (c_ig * lc_ig).sum() | |
def theil_seg_several(ng): | |
''' | |
Compute segregation Theil index for several observations | |
... | |
ng : array | |
Array of shape (n, g) where n is the number of observations | |
(e.g. tracts) for the Theil to be computed on and g is the | |
number of groups per tract | |
''' | |
pool = mp.Pool(mp.cpu_count()) | |
return pool.map(theil_seg, ng) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment