pysal

Next topic

pysal.inequality — Spatial Inequality Analysis

This Page

esda.smoothing — Smoothing of spatial rates

New in version 1.0.

Apply smoothing to rate computation

class pysal.esda.smoothing.Age_Adjusted_Smoother(e, b, w, s, alpha=0.050000000000000003)

Age-adjusted rate smoothing

Parameters:

e : array (n*h, 1)

event variable measured for each age group across n spatial units

b : array (n*h, 1)

population at risk variable measured for each age group across n spatial units

w : spatial weights instance

s : array (n*h, 1)

standard million population for each age group across n spatial units

Notes

Weights used to smooth age-specific events and populations are simple binary weights

Examples

>>> e = np.array([10, 8, 1, 4, 3, 5, 4, 3, 2, 1, 5, 3])
>>> b = np.array([100, 90, 15, 30, 25, 20, 30, 20, 80, 80, 90, 60])
>>> s = np.array([98, 88, 15, 29, 20, 23, 33, 25, 76, 80, 89, 66])
>>> points=[(10, 10), (20, 10), (40, 10), (15, 20), (30, 20), (30, 30)]
>>> kw=Kernel(points)
>>> if not kw.id_order_set: kw.id_order = range(0,len(points))
>>> ar = Age_Adjusted_Smoother(e, b, kw, s)
>>> ar.r
array([ 0.10519625,  0.08494318,  0.06440072,  0.06898604,  0.06952076,
        0.05020968])

Attributes

r array (n, 1) rate values from spatial rate smoothing
class pysal.esda.smoothing.Disk_Smoother(e, b, w)

Locally weighted averages or disk smoothing

Parameters:

e : array (n, 1)

event variable measured across n spatial units

b : array (n, 1)

population at risk variable measured across n spatial units

w : spatial weights matrix

Examples

>>> stl = pysal.open('../examples/stl_hom.csv', 'r')
>>> stl_e, stl_b = np.array(stl[:,10]), np.array(stl[:,13])
>>> stl_w = pysal.open('../examples/stl.gal', 'r').read()
>>> if not stl_w.id_order_set: stl_w.id_order = range(1,len(stl) + 1)
>>> sr = Disk_Smoother(stl_e,stl_b,stl_w)
>>> sr.r[:10]
array([  4.56502262e-05,   3.44027685e-05,   3.38280487e-05,
         4.78530468e-05,   3.12278573e-05,   2.22596997e-05,
         2.67074856e-05,   2.36924573e-05,   3.48801587e-05,
         3.09511832e-05])

Attributes

r array (n, 1) rate values from disk smoothing
class pysal.esda.smoothing.Empirical_Bayes(e, b)

Aspatial Empirical Bayes Smoothing

Parameters:

e : array (n, 1)

event variable measured across n spatial units

b : array (n, 1)

population at risk variable measured across n spatial units

Examples

>>> stl = pysal.open('../examples/stl_hom.csv', 'r')
>>> stl_e, stl_b = np.array(stl[:,10]), np.array(stl[:,13])
>>> eb = Empirical_Bayes(stl_e, stl_b)
>>> eb.r[:10]
array([  2.36718950e-05,   4.54539167e-05,   4.78114019e-05,
         2.76907146e-05,   6.58989323e-05,   3.66494122e-05,
         5.79952721e-05,   2.03064590e-05,   3.31152999e-05,
         3.02748380e-05])
>>>

Attributes

r array (n, 1) rate values from Empirical Bayes Smoothing
class pysal.esda.smoothing.Excess_Risk(e, b)

Excess Risk

Parameters:

e : array (n, 1)

event variable measured across n spatial units

b : array (n, 1)

population at risk variable measured across n spatial units

Examples

>>> stl = pysal.open('../examples/stl_hom.csv', 'r')
>>> stl_e, stl_b = np.array(stl[:,10]), np.array(stl[:,13])
>>> er = Excess_Risk(stl_e, stl_b)
>>> er.r[:10]
array([ 0.20665681,  0.43613787,  0.42078261,  0.22066928,  0.57981596,
        0.35301709,  0.56407549,  0.17020994,  0.3052372 ,  0.25821905])
>>>

Attributes

r array (n, 1) execess risk values
class pysal.esda.smoothing.Headbanging_Median_Rate(e, b, t, aw=None, iteration=1)

Headbaning Median Rate Smoothing

Parameters:

e : array (n, 1)

event variable measured across n spatial units

b : array (n, 1)

population at risk variable measured across n spatial units

t : Headbanging_Triples instance

aw : array (n, 1)

auxilliary weight variable measured across n spatial units

iteration : integer

the number of iterations

Examples

>>> from pysal import knnW
>>> sids = pysal.open('../examples/sids2.shp', 'r')
>>> sids_d = np.array([i.centroid for i in sids])
>>> sids_w = knnW(sids_d,k=5)
>>> if not sids_w.id_order_set: sids_w.id_order = sids_w.id_order
>>> s_ht = Headbanging_Triples(sids_d,sids_w,k=5)
>>> sids_db = pysal.open('../examples/sids2.dbf', 'r')
>>> s_e, s_b = np.array(sids_db[:,9]), np.array(sids_db[:,8])
>>> sids_hb_r = Headbanging_Median_Rate(s_e,s_b,s_ht)
>>> sids_hb_r.r[:5]
array([ 0.00075586,  0.        ,  0.0008285 ,  0.0018315 ,  0.00498891])
>>> sids_hb_r2 = Headbanging_Median_Rate(s_e,s_b,s_ht,iteration=5)
>>> sids_hb_r2.r[:5]
array([ 0.0008285 ,  0.00084331,  0.00086896,  0.0018315 ,  0.00498891])
>>> sids_hb_r3 = Headbanging_Median_Rate(s_e,s_b,s_ht,aw=s_b)
>>> sids_hb_r3.r[:5]
array([ 0.00091659,  0.        ,  0.00156838,  0.0018315 ,  0.00498891])

Attributes

r array (n, 1) rate values from headbaning median smoothing
class pysal.esda.smoothing.Headbanging_Triples(data, w, k=5, t=3, angle=135.0, edgecor=False)

Generate a pseudo spatial weights instance that contains headbaning triples

Parameters:

data : array (n, 2)

numpy array of x, y coordinates

w : spatial weights instance

k : integer number of nearest neighbors

t : integer

the number of triples

angle : integer between 0 and 180

the angle criterium for a set of triples

edgecorr : boolean

whether or not correction for edge points is made

Examples

>>> from pysal import knnW
>>> stl_db = pysal.open('../examples/stl_hom.csv','r')
>>> fromWKT = pysal.core.IOHandlers.wkt.WKTParser()
>>> stl_db.cast('WKT',fromWKT)
>>> d = np.array([i.centroid for i in stl_db[:,0]])
>>> w = knnW(d,k=5)
>>> if not w.id_order_set: w.id_order = w.id_order
>>> ht = Headbanging_Triples(d,w,k=5)
>>> for k, item in ht.triples.items()[:5]: print k, item
0 [(5, 6), (10, 6)]
1 [(4, 7), (4, 14), (9, 7)]
2 [(0, 8), (10, 3), (0, 6)]
3 [(4, 2), (2, 12), (8, 4)]
4 [(8, 1), (12, 1), (8, 9)]
>>> sids = pysal.open('../examples/sids2.shp','r')
>>> sids_d = np.array([i.centroid for i in sids])
>>> sids_w = knnW(sids_d,k=5)
>>> if not sids_w.id_order_set: sids_w.id_order = sids_w.id_order
>>> s_ht = Headbanging_Triples(sids_d,sids_w,k=5)
>>> for k, item in s_ht.triples.items()[:5]: print k, item
0 [(1, 18), (1, 21), (1, 33)]
1 [(2, 40), (2, 22), (22, 40)]
2 [(39, 22), (1, 9), (39, 17)]
3 [(16, 6), (19, 6), (20, 6)]
4 [(5, 15), (27, 15), (35, 15)]
>>> s_ht2 = Headbanging_Triples(sids_d,sids_w,k=5,edgecor=True)
>>> for k, item in s_ht2.triples.items()[:5]: print k, item
0 [(1, 18), (1, 21), (1, 33)]
1 [(2, 40), (2, 22), (22, 40)]
2 [(39, 22), (1, 9), (39, 17)]
3 [(16, 6), (19, 6), (20, 6)]
4 [(5, 15), (27, 15), (35, 15)]
>>> extrapolated = s_ht2.extra[72]
>>> extrapolated[0]
(89, 77)
>>> round(extrapolated[1],5), round(extrapolated[2],6)
(0.33753, 0.302707)

Attributes

triples dictionary key is observation record id, value is a list of lists of triple ids
extra dictionary key is observation record id, value is a list of the following: tuple of original triple observations distance between original triple observations distance between an original triple observation and its extrapolated point
class pysal.esda.smoothing.Kernel_Smoother(e, b, w)

Kernal smoothing

Parameters:

e : array (n, 1)

event variable measured across n spatial units

b : array (n, 1)

population at risk variable measured across n spatial units

w : Kernel weights instance

Examples

>>> e = np.array([10, 1, 3, 4, 2, 5])
>>> b = np.array([100, 15, 20, 20, 80, 90])
>>> points=[(10, 10), (20, 10), (40, 10), (15, 20), (30, 20), (30, 30)]
>>> kw=Kernel(points)
>>> if not kw.id_order_set: kw.id_order = range(0,len(points))
>>> kr = Kernel_Smoother(e, b, kw)
>>> kr.r
array([ 0.10543301,  0.0858573 ,  0.08256196,  0.09884584,  0.04756872,
        0.04845298])

Attributes

r array (n, 1) rate values from spatial rate smoothing
class pysal.esda.smoothing.Spatial_Empirical_Bayes(e, b, w)

Spatial Empirical Bayes Smoothing

Parameters:

e : array (n, 1)

event variable measured across n spatial units

b : array (n, 1)

population at risk variable measured across n spatial units

w : spatial weights instance

Examples

>>> stl = pysal.open('../examples/stl_hom.csv', 'r')
>>> stl_e, stl_b = np.array(stl[:,10]), np.array(stl[:,13])
>>> stl_w = pysal.open('../examples/stl.gal', 'r').read()
>>> if not stl_w.id_order_set: stl_w.id_order = range(1,len(stl) + 1)
>>> s_eb = Spatial_Empirical_Bayes(stl_e, stl_b, stl_w)
>>> s_eb.r[:10]
array([  4.01485749e-05,   3.62437513e-05,   4.93034844e-05,
         5.09387329e-05,   3.72735210e-05,   3.69333797e-05,
         5.40245456e-05,   2.99806055e-05,   3.73034109e-05,
         3.47270722e-05])

Attributes

r array (n, 1) rate values from Empirical Bayes Smoothing
class pysal.esda.smoothing.Spatial_Filtering(bbox, data, e, b, x_grid, y_grid, r=None, pop=None)

Spatial Filtering

Parameters:

bbox : a list of two lists where each list is a pair of coordinates

a bounding box for the entire n spatial units

data : array (n, 2)

x, y coordinates

e : array (n, 1)

event variable measured across n spatial units

b : array (n, 1)

population at risk variable measured across n spatial units

x_grid : integer

the number of cells on x axis

y_grid : integer

the number of cells on y axis

r : float

fixed radius of a moving window

pop : integer

population threshold to create adaptive moving windows

Notes

No tool is provided to find an optimal value for r or pop.

Examples

>>> stl = pysal.open('../examples/stl_hom.csv', 'r')
>>> fromWKT = pysal.core.IOHandlers.wkt.WKTParser()
>>> stl.cast('WKT',fromWKT)
>>> d = np.array([i.centroid for i in stl[:,0]])
>>> bbox = [[-92.700676, 36.881809], [-87.916573, 40.3295669]]
>>> stl_e, stl_b = np.array(stl[:,10]), np.array(stl[:,13])
>>> sf_0 = Spatial_Filtering(bbox,d,stl_e,stl_b,10,10,r=2)
>>> sf_0.r[:10]
array([  4.23561763e-05,   4.45290850e-05,   4.56456221e-05,
         4.49133384e-05,   4.39671835e-05,   4.44903042e-05,
         4.19845497e-05,   4.11936548e-05,   3.93463504e-05,
         4.04376345e-05])
>>> sf = Spatial_Filtering(bbox,d,stl_e,stl_b,10,10,pop=600000)
>>> sf.r.shape
(100,)
>>> sf.r[:10]
array([  3.73728738e-05,   4.04456300e-05,   4.04456300e-05,
         3.81035327e-05,   4.54831940e-05,   4.54831940e-05,
         3.75658628e-05,   3.75658628e-05,   3.75658628e-05,
         3.75658628e-05])

Attributes

grid array (x_grid*y_grid, 2) x, y coordinates for grid points
r array (x_grid*y_grid, 1) rate values for grid points
class pysal.esda.smoothing.Spatial_Median_Rate(e, b, w, aw=None, iteration=1)

Spatial Median Rate Smoothing

Parameters:

e : array (n, 1)

event variable measured across n spatial units

b : array (n, 1)

population at risk variable measured across n spatial units

w : spatial weights instance

aw : array (n, 1)

auxiliary weight variable measured across n spatial units

iteration : integer

the number of interations

Examples

>>> stl = pysal.open('../examples/stl_hom.csv', 'r')
>>> stl_e, stl_b = np.array(stl[:,10]), np.array(stl[:,13])
>>> stl_w = pysal.open('../examples/stl.gal', 'r').read()
>>> if not stl_w.id_order_set: stl_w.id_order = range(1,len(stl) + 1)
>>> smr0 = Spatial_Median_Rate(stl_e,stl_b,stl_w)
>>> smr0.r[:10]
array([  3.96047383e-05,   3.55386859e-05,   3.28308921e-05,
         4.30731238e-05,   3.12453969e-05,   1.97300409e-05,
         3.10159267e-05,   2.19279204e-05,   2.93763432e-05,
         2.93763432e-05])
>>> smr1 = Spatial_Median_Rate(stl_e,stl_b,stl_w,iteration=5)
>>> smr1.r[:10]
array([  3.11293620e-05,   2.95956330e-05,   3.11293620e-05,
         3.10159267e-05,   2.98436066e-05,   2.76406686e-05,
         3.10159267e-05,   2.94788171e-05,   2.99460806e-05,
         2.96981070e-05])
>>> smr2 = Spatial_Median_Rate(stl_e,stl_b,stl_w,aw=stl_b)
>>> smr2.r[:10]
array([  5.77412020e-05,   4.46449551e-05,   5.77412020e-05,
         5.77412020e-05,   4.46449551e-05,   3.61363528e-05,
         3.61363528e-05,   4.46449551e-05,   5.77412020e-05,
         4.03987355e-05])
>>> smr3 = Spatial_Median_Rate(stl_e,stl_b,stl_w,aw=stl_b,iteration=5)
>>> smr3.r[:10]
array([  3.61363528e-05,   4.46449551e-05,   3.61363528e-05,
         3.61363528e-05,   4.46449551e-05,   3.61363528e-05,
         3.61363528e-05,   4.46449551e-05,   3.61363528e-05,
         4.46449551e-05])
>>>

Attributes

r array (n, 1) rate values from spatial median rate smoothing
w spatial weights instance  
aw array (n, 1) auxiliary weight variable measured across n spatial units
class pysal.esda.smoothing.Spatial_Rate(e, b, w)

Spatial Rate Smoothing

Parameters:

e : array (n, 1)

event variable measured across n spatial units

b : array (n, 1)

population at risk variable measured across n spatial units

w : spatial weights instance

Examples

>>> stl = pysal.open('../examples/stl_hom.csv', 'r')
>>> stl_e, stl_b = np.array(stl[:,10]), np.array(stl[:,13])
>>> stl_w = pysal.open('../examples/stl.gal', 'r').read()
>>> if not stl_w.id_order_set: stl_w.id_order = range(1,len(stl) + 1)
>>> sr = Spatial_Rate(stl_e,stl_b,stl_w)
>>> sr.r[:10]
array([  4.59326407e-05,   3.62437513e-05,   4.98677081e-05,
         5.09387329e-05,   3.72735210e-05,   4.01073093e-05,
         3.79372794e-05,   3.27019246e-05,   4.26204928e-05,
         3.47270722e-05])

Attributes

r array (n, 1) rate values from spatial rate smoothing
pysal.esda.smoothing.crude_age_standardization(e, b, n)

A utility function to compute rate through crude age standardization

Parameters:

e : array(n*h, 1)

event variable measured for each age group across n spatial units

b : array(n*h, 1)

population at risk variable measured for each age group across n spatial units

n : integer

the number of spatial units

Returns:

: array(n, 1) :

age standardized rate

Notes

e and b are arranged in the same order

Examples

>>> e = np.array([30, 25, 25, 15, 33, 21, 30, 20])
>>> b = np.array([100, 100, 110, 90, 100, 90, 110, 90])
>>> n = 2
>>> crude_age_standardization(e, b, n)
array([ 0.2375    ,  0.26666667])
pysal.esda.smoothing.direct_age_standardization(e, b, s, n, alpha=0.050000000000000003)

A utility function to compute rate through direct age standardization

Parameters:

e : array(n*h, 1)

event variable measured for each age group across n spatial units

b : array(n*h, 1)

population at risk variable measured for each age group across n spatial units

s : array(n*h, 1)

standard million population for each age group across n spatial units

n : integer

the number of spatial units

alpha : float

significance level for confidence interval

Returns:

: a list of n tuples; a tuple has a rate and its lower and upper limits :

age standardized rates and confidence intervals

Notes

e, b, and s are arranged in the same order

Examples

>>> e = np.array([30, 25, 25, 15, 33, 21, 30, 20])
>>> b = np.array([1000, 1000, 1100, 900, 1000, 900, 1100, 900])
>>> s = np.array([1000, 900, 1000, 900, 1000, 900, 1000, 900])
>>> n = 2
>>> [i[0] for i in direct_age_standardization(e, b, s, n)]
[0.023744019138755977, 0.026650717703349279]
pysal.esda.smoothing.flatten(l, unique=True)

flatten a list of lists

Parameters:

l : list of lists

unique : boolean

whether or not only unique items are wanted

Returns:

: list of single items :

Examples

>>> l = [[1,2],[3,4,],[5,6]]
>>> flatten(l)
[1, 2, 3, 4, 5, 6]
pysal.esda.smoothing.indirect_age_standardization(e, b, s_e, s_b, n, alpha=0.050000000000000003)

A utility function to compute rate through indirect age standardization

Parameters:

e : array(n*h, 1)

event variable measured for each age group across n spatial units

b : array(n*h, 1)

population at risk variable measured for each age group across n spatial units

s_e : array(n*h, 1)

event variable measured for each age group across n spatial units in a standard million

s_b : array(n*h, 1)

population variable measured for each age group across n spatial units in a standard million

n : integer

the number of spatial units

alpha : float

significance level for confidence interval

Returns:

: a list of n tuples; a tuple has a rate and its lower and upper limits :

age standardized rate

Notes

e, b, s_e, and s_b are arranged in the same order

Examples

>>> e = np.array([30, 25, 25, 15, 33, 21, 30, 20])
>>> b = np.array([100, 100, 110, 90, 100, 90, 110, 90])
>>> s_e = np.array([100, 45, 120, 100, 50, 30, 200, 80])
>>> s_b = np.array([1000, 900, 1000, 900, 1000, 900, 1000, 900])
>>> n = 2
>>> [i[0] for i in indirect_age_standardization(e, b, s_e, s_b, n)]
[0.23723821989528798, 0.2610803324099723]
pysal.esda.smoothing.sum_by_n(d, w, n)
A utility function to summarize a data array into n values
after weighting the array with another weight array w
Parameters:

d : array(t, 1)

numerical values

w : array(t, 1)

numerical values for weighting

n : integer

the number of groups t = c*n (c is a constant)

Returns:

: array(n, 1) :

an array with summarized values

Examples

>>> d = np.array([10, 9, 20, 30])
>>> w = np.array([0.5, 0.1, 0.3, 0.8])
>>> n = 2
>>> sum_by_n(d, w, n)
array([  5.9,  30. ])
pysal.esda.smoothing.weighted_median(d, w)

A utility function to find a median of d based on w

Parameters:

d : array (n, 1)

variable for which median will be found

w : array (n, 1)

variable on which d’s medain will be decided

Returns:

: numeric :

median of d

Notes

d and w are arranged in the same order

Examples

>>> d = np.array([5,4,3,1,2])
>>> w = np.array([10,22,9,2,5])
>>> weighted_median(d,w)
4