def adaptive_kernelW(points, bandwidths=None, k=2, function='triangular', radius=None, diagonal=False): """ Kernel weights with adaptive bandwidths. Parameters ---------- points : array (n,k) n observations on k characteristics used to measure distances between the n objects bandwidths : float or array-like (optional) the bandwidth :math:`h_i` for the kernel. if no bandwidth is specified k is used to determine the adaptive bandwidth k : int the number of nearest neighbors to use for determining bandwidth. For fixed bandwidth, :math:`h_i=max(dknn) \\forall i` where :math:`dknn` is a vector of k-nearest neighbor distances (the distance to the kth nearest neighbor for each observation). For adaptive bandwidths, :math:`h_i=dknn_i` function : {'triangular','uniform','quadratic','quartic','gaussian'} kernel function defined as follows with .. math:: z_{i,j} = d_{i,j}/h_i triangular .. math:: K(z) = (1 - |z|) \ if |z| \le 1 uniform .. math:: K(z) = |z| \ if |z| \le 1 quadratic .. math:: K(z) = (3/4)(1-z^2) \ if |z| \le 1 quartic .. math:: K(z) = (15/16)(1-z^2)^2 \ if |z| \le 1 gaussian .. math:: K(z) = (2\pi)^{(-1/2)} exp(-z^2 / 2) radius : float If supplied arc_distances will be calculated based on the given radius. p will be ignored. diagonal : boolean If true, set diagonal weights = 1.0, if false (default) diagonal weights are set to value according to kernel function Returns ------- w : W instance of spatial weights Examples -------- User specified bandwidths >>> points=[(10, 10), (20, 10), (40, 10), (15, 20), (30, 20), (30, 30)] >>> bw=[25.0,15.0,25.0,16.0,14.5,25.0] >>> kwa=adaptive_kernelW(points,bandwidths=bw) >>> kwa.weights[0] [1.0, 0.6, 0.552786404500042, 0.10557280900008403] >>> kwa.neighbors[0] [0, 1, 3, 4] >>> kwa.bandwidth array([[ 25. ], [ 15. ], [ 25. ], [ 16. ], [ 14.5], [ 25. ]]) Endogenous adaptive bandwidths >>> kwea=adaptive_kernelW(points) >>> kwea.weights[0] [1.0, 0.10557289844279438, 9.99999900663795e-08] >>> kwea.neighbors[0] [0, 1, 3] >>> kwea.bandwidth array([[ 11.18034101], [ 11.18034101], [ 20.000002 ], [ 11.18034101], [ 14.14213704], [ 18.02775818]]) Endogenous adaptive bandwidths with Gaussian kernel >>> kweag=adaptive_kernelW(points,function='gaussian') >>> kweag.weights[0] [0.3989422804014327, 0.2674190291577696, 0.2419707487162134] >>> kweag.bandwidth array([[ 11.18034101], [ 11.18034101], [ 20.000002 ], [ 11.18034101], [ 14.14213704], [ 18.02775818]]) with diagonal >>> kweag = pysal.adaptive_kernelW(points, function='gaussian') >>> kweagd = pysal.adaptive_kernelW(points, function='gaussian', diagonal=True) >>> kweag.neighbors[0] [0, 1, 3] >>> kweagd.neighbors[0] [0, 1, 3] >>> kweag.weights[0] [0.3989422804014327, 0.2674190291577696, 0.2419707487162134] >>> kweagd.weights[0] [1.0, 0.2674190291577696, 0.2419707487162134] """ if radius is not None: points = pysal.cg.KDTree(points, distance_metric='Arc', radius=radius) return Kernel(points, bandwidth=bandwidths, fixed=False, k=k, function=function, diagonal=diagonal)
def adaptive_kernelW_from_shapefile(shapefile, bandwidths=None, k=2, function='triangular', idVariable=None, radius=None, diagonal = False): """ Kernel weights with adaptive bandwidths. Parameters ---------- shapefile : string shapefile name with shp suffix bandwidths : float or array-like (optional) the bandwidth :math:`h_i` for the kernel. if no bandwidth is specified k is used to determine the adaptive bandwidth k : int the number of nearest neighbors to use for determining bandwidth. For fixed bandwidth, :math:`h_i=max(dknn) \\forall i` where :math:`dknn` is a vector of k-nearest neighbor distances (the distance to the kth nearest neighbor for each observation). For adaptive bandwidths, :math:`h_i=dknn_i` function : {'triangular','uniform','quadratic','quartic','gaussian'} kernel function defined as follows with .. math:: z_{i,j} = d_{i,j}/h_i triangular .. math:: K(z) = (1 - |z|) \ if |z| \le 1 uniform .. math:: K(z) = |z| \ if |z| \le 1 quadratic .. math:: K(z) = (3/4)(1-z^2) \ if |z| \le 1 quartic .. math:: K(z) = (15/16)(1-z^2)^2 \ if |z| \le 1 gaussian .. math:: K(z) = (2\pi)^{(-1/2)} exp(-z^2 / 2) idVariable : string name of a column in the shapefile's DBF to use for ids radius : float If supplied arc_distances will be calculated based on the given radius. p will be ignored. diagonal : boolean If true, set diagonal weights = 1.0, if false (default) diagonal weights are set to value according to kernel function Returns ------- w : W instance of spatial weights Examples -------- >>> kwa = pysal.adaptive_kernelW_from_shapefile(pysal.examples.get_path("columbus.shp"), function='gaussian') >>> kwad = pysal.adaptive_kernelW_from_shapefile(pysal.examples.get_path("columbus.shp"), function='gaussian', diagonal=True) >>> kwa.neighbors[0] [0, 2, 1] >>> kwad.neighbors[0] [0, 2, 1] >>> kwa.weights[0] [0.3989422804014327, 0.24966013701844503, 0.2419707487162134] >>> kwad.weights[0] [1.0, 0.24966013701844503, 0.2419707487162134] >>> Notes ----- Supports polygon or point shapefiles. For polygon shapefiles, distance is based on polygon centroids. Distances are defined using coordinates in shapefile which are assumed to be projected and not geographical coordinates. """ points = get_points_array_from_shapefile(shapefile) if radius is not None: points = pysal.cg.KDTree(points, distance_metric='Arc', radius=radius) if idVariable: ids = get_ids(shapefile, idVariable) return Kernel(points, bandwidth=bandwidths, fixed=False, k=k, function=function, ids=ids, diagonal=diagonal) return adaptive_kernelW(points, bandwidths=bandwidths, k=k, function=function, diagonal=diagonal)
def kernelW_from_shapefile(shapefile, k=2, function='triangular', idVariable=None, fixed=True, radius=None, diagonal=False): """ Kernel based weights. Parameters ---------- shapefile : string shapefile name with shp suffix k : int the number of nearest neighbors to use for determining bandwidth. Bandwidth taken as :math:`h_i=max(dknn) \\forall i` where :math:`dknn` is a vector of k-nearest neighbor distances (the distance to the kth nearest neighbor for each observation). function : {'triangular','uniform','quadratic','epanechnikov', 'quartic','bisquare','gaussian'} .. math:: z_{i,j} = d_{i,j}/h_i triangular .. math:: K(z) = (1 - |z|) \ if |z| \le 1 uniform .. math:: K(z) = |z| \ if |z| \le 1 quadratic .. math:: K(z) = (3/4)(1-z^2) \ if |z| \le 1 epanechnikov .. math:: K(z) = (1-z^2) \ if |z| \le 1 quartic .. math:: K(z) = (15/16)(1-z^2)^2 \ if |z| \le 1 bisquare .. math:: K(z) = (1-z^2)^2 \ if |z| \le 1 gaussian .. math:: K(z) = (2\pi)^{(-1/2)} exp(-z^2 / 2) idVariable : string name of a column in the shapefile's DBF to use for ids fixed : binary If true then :math:`h_i=h \\forall i`. If false then bandwidth is adaptive across observations. radius : float If supplied arc_distances will be calculated based on the given radius. p will be ignored. diagonal : boolean If true, set diagonal weights = 1.0, if false (default) diagonal weights are set to value according to kernel function Returns ------- w : W instance of spatial weights Examples -------- >>> kw = pysal.kernelW_from_shapefile(pysal.examples.get_path("columbus.shp"),idVariable='POLYID', function = 'gaussian') >>> kwd = pysal.kernelW_from_shapefile(pysal.examples.get_path("columbus.shp"),idVariable='POLYID', function = 'gaussian', diagonal = True) >>> set(kw.neighbors[1]) == set([4, 2, 3, 1]) True >>> set(kwd.neighbors[1]) == set([4, 2, 3, 1]) True >>> >>> set(kw.weights[1]) == set( [0.2436835517263174, 0.29090631630909874, 0.29671172124745776, 0.3989422804014327]) True >>> set(kwd.weights[1]) == set( [0.2436835517263174, 0.29090631630909874, 0.29671172124745776, 1.0]) True Notes ----- Supports polygon or point shapefiles. For polygon shapefiles, distance is based on polygon centroids. Distances are defined using coordinates in shapefile which are assumed to be projected and not geographical coordinates. """ points = get_points_array_from_shapefile(shapefile) if radius is not None: points = pysal.cg.KDTree(points, distance_metric='Arc', radius=radius) if idVariable: ids = get_ids(shapefile, idVariable) return Kernel(points, function=function, k=k, ids=ids, fixed=fixed, diagonal = diagonal) return kernelW(points, k=k, function=function, fixed=fixed, diagonal=diagonal)
def kernelW(points, k=2, function='triangular', fixed=True, radius=None, diagonal=False): """ Kernel based weights. Parameters ---------- points : array (n,k) n observations on k characteristics used to measure distances between the n objects k : int the number of nearest neighbors to use for determining bandwidth. Bandwidth taken as :math:`h_i=max(dknn) \\forall i` where :math:`dknn` is a vector of k-nearest neighbor distances (the distance to the kth nearest neighbor for each observation). function : {'triangular','uniform','quadratic','epanechnikov','quartic','bisquare','gaussian'} .. math:: z_{i,j} = d_{i,j}/h_i triangular .. math:: K(z) = (1 - |z|) \ if |z| \le 1 uniform .. math:: K(z) = |z| \ if |z| \le 1 quadratic .. math:: K(z) = (3/4)(1-z^2) \ if |z| \le 1 epanechnikov .. math:: K(z) = (1-z^2) \ if |z| \le 1 quartic .. math:: K(z) = (15/16)(1-z^2)^2 \ if |z| \le 1 bisquare .. math:: K(z) = (1-z^2)^2 \ if |z| \le 1 gaussian .. math:: K(z) = (2\pi)^{(-1/2)} exp(-z^2 / 2) fixed : boolean If true then :math:`h_i=h \\forall i`. If false then bandwidth is adaptive across observations. radius : float If supplied arc_distances will be calculated based on the given radius. p will be ignored. diagonal : boolean If true, set diagonal weights = 1.0, if false (default) diagonal weights are set to value according to kernel function Returns ------- w : W instance of spatial weights Examples -------- >>> points=[(10, 10), (20, 10), (40, 10), (15, 20), (30, 20), (30, 30)] >>> kw=kernelW(points) >>> kw.weights[0] [1.0, 0.500000049999995, 0.4409830615267465] >>> kw.neighbors[0] [0, 1, 3] >>> kw.bandwidth array([[ 20.000002], [ 20.000002], [ 20.000002], [ 20.000002], [ 20.000002], [ 20.000002]]) use different k >>> kw=kernelW(points,k=3) >>> kw.neighbors[0] [0, 1, 3, 4] >>> kw.bandwidth array([[ 22.36068201], [ 22.36068201], [ 22.36068201], [ 22.36068201], [ 22.36068201], [ 22.36068201]]) Diagonals to 1.0 >>> kq = kernelW(points,function='gaussian') >>> kq.weights {0: [0.3989422804014327, 0.35206533556593145, 0.3412334260702758], 1: [0.35206533556593145, 0.3989422804014327, 0.2419707487162134, 0.3412334260702758, 0.31069657591175387], 2: [0.2419707487162134, 0.3989422804014327, 0.31069657591175387], 3: [0.3412334260702758, 0.3412334260702758, 0.3989422804014327, 0.3011374490937829, 0.26575287272131043], 4: [0.31069657591175387, 0.31069657591175387, 0.3011374490937829, 0.3989422804014327, 0.35206533556593145], 5: [0.26575287272131043, 0.35206533556593145, 0.3989422804014327]} >>> kqd = kernelW(points, function='gaussian', diagonal=True) >>> kqd.weights {0: [1.0, 0.35206533556593145, 0.3412334260702758], 1: [0.35206533556593145, 1.0, 0.2419707487162134, 0.3412334260702758, 0.31069657591175387], 2: [0.2419707487162134, 1.0, 0.31069657591175387], 3: [0.3412334260702758, 0.3412334260702758, 1.0, 0.3011374490937829, 0.26575287272131043], 4: [0.31069657591175387, 0.31069657591175387, 0.3011374490937829, 1.0, 0.35206533556593145], 5: [0.26575287272131043, 0.35206533556593145, 1.0]} """ if radius is not None: points = pysal.cg.KDTree(points, distance_metric='Arc', radius=radius) return Kernel(points, function=function, k=k, fixed=fixed, diagonal=diagonal)
def adaptive_kernelW_from_shapefile(shapefile, bandwidths=None, k=2, function='triangular', idVariable=None, radius=None): """ Kernel weights with adaptive bandwidths Parameters ---------- shapefile : string shapefile name with shp suffix bandwidths : float or array-like (optional) the bandwidth :math:`h_i` for the kernel. if no bandwidth is specified k is used to determine the adaptive bandwidth k : int the number of nearest neighbors to use for determining bandwidth. For fixed bandwidth, :math:`h_i=max(dknn) \\forall i` where :math:`dknn` is a vector of k-nearest neighbor distances (the distance to the kth nearest neighbor for each observation). For adaptive bandwidths, :math:`h_i=dknn_i` function : string {'triangular','uniform','quadratic','quartic','gaussian'} kernel function defined as follows with .. math:: z_{i,j} = d_{i,j}/h_i triangular .. math:: K(z) = (1 - |z|) \ if |z| \le 1 uniform .. math:: K(z) = |z| \ if |z| \le 1 quadratic .. math:: K(z) = (3/4)(1-z^2) \ if |z| \le 1 quartic .. math:: K(z) = (15/16)(1-z^2)^2 \ if |z| \le 1 gaussian .. math:: K(z) = (2\pi)^{(-1/2)} exp(-z^2 / 2) idVariable : string name of a column in the shapefile's DBF to use for ids radius : If supplied arc_distances will be calculated based on the given radius. p will be ignored. Returns ------- w : W instance of spatial weights Examples -------- >>> kwa = adaptive_kernelW_from_shapefile(pysal.examples.get_path("columbus.shp")) >>> kwa.weights[0] [1.0, 0.03178906767736345, 9.99999900663795e-08] >>> kwa.bandwidth[:3] array([[ 0.59871832], [ 0.59871832], [ 0.56095647]]) Notes ----- Supports polygon or point shapefiles. For polygon shapefiles, distance is based on polygon centroids. Distances are defined using coordinates in shapefile which are assumed to be projected and not geographical coordinates. """ points = get_points_array_from_shapefile(shapefile) if radius is not None: points = pysal.cg.KDTree(points, distance_metric='Arc', radius=radius) if idVariable: ids = get_ids(shapefile, idVariable) return Kernel(points, bandwidth=bandwidths, fixed=False, k=k, function=function, ids=ids) return adaptive_kernelW(points, bandwidths=bandwidths, k=k, function=function)
def kernelW_from_shapefile(shapefile, k=2, function='triangular', idVariable=None, fixed=True, radius=None): """ Kernel based weights Parameters ---------- shapefile : string shapefile name with shp suffix k : int the number of nearest neighbors to use for determining bandwidth. Bandwidth taken as :math:`h_i=max(dknn) \\forall i` where :math:`dknn` is a vector of k-nearest neighbor distances (the distance to the kth nearest neighbor for each observation). function : string {'triangular','uniform','quadratic','epanechnikov', 'quartic','bisquare','gaussian'} .. math:: z_{i,j} = d_{i,j}/h_i triangular .. math:: K(z) = (1 - |z|) \ if |z| \le 1 uniform .. math:: K(z) = |z| \ if |z| \le 1 quadratic .. math:: K(z) = (3/4)(1-z^2) \ if |z| \le 1 epanechnikov .. math:: K(z) = (1-z^2) \ if |z| \le 1 quartic .. math:: K(z) = (15/16)(1-z^2)^2 \ if |z| \le 1 bisquare .. math:: K(z) = (1-z^2)^2 \ if |z| \le 1 gaussian .. math:: K(z) = (2\pi)^{(-1/2)} exp(-z^2 / 2) idVariable : string name of a column in the shapefile's DBF to use for ids fixed : binary If true then :math:`h_i=h \\forall i`. If false then bandwidth is adaptive across observations. radius : If supplied arc_distances will be calculated based on the given radius. p will be ignored. Returns ------- w : W instance of spatial weights Examples -------- >>> kw = kernelW_from_shapefile(pysal.examples.get_path("columbus.shp"),idVariable='POLYID') >>> kw.weights[1] [0.2052478782400463, 0.007078773148450623, 1.0, 0.23051223027663237] >>> kw.bandwidth[:3] array([[ 0.75333961], [ 0.75333961], [ 0.75333961]]) Notes ----- Supports polygon or point shapefiles. For polygon shapefiles, distance is based on polygon centroids. Distances are defined using coordinates in shapefile which are assumed to be projected and not geographical coordinates. """ points = get_points_array_from_shapefile(shapefile) if radius is not None: points = pysal.cg.KDTree(points, distance_metric='Arc', radius=radius) if idVariable: ids = get_ids(shapefile, idVariable) return Kernel(points, function=function, k=k, ids=ids, fixed=fixed) return kernelW(points, k=k, function=function, fixed=fixed)
def kernelW(points, k=2, function='triangular', fixed=True, radius=None): """ Kernel based weights Parameters ---------- points : array (n,k) n observations on k characteristics used to measure distances between the n objects k : int the number of nearest neighbors to use for determining bandwidth. Bandwidth taken as :math:`h_i=max(dknn) \\forall i` where :math:`dknn` is a vector of k-nearest neighbor distances (the distance to the kth nearest neighbor for each observation). function : string {'triangular','uniform','quadratic','epanechnikov', 'quartic','bisquare','gaussian'} .. math:: z_{i,j} = d_{i,j}/h_i triangular .. math:: K(z) = (1 - |z|) \ if |z| \le 1 uniform .. math:: K(z) = |z| \ if |z| \le 1 quadratic .. math:: K(z) = (3/4)(1-z^2) \ if |z| \le 1 epanechnikov .. math:: K(z) = (1-z^2) \ if |z| \le 1 quartic .. math:: K(z) = (15/16)(1-z^2)^2 \ if |z| \le 1 bisquare .. math:: K(z) = (1-z^2)^2 \ if |z| \le 1 gaussian .. math:: K(z) = (2\pi)^{(-1/2)} exp(-z^2 / 2) fixed : binary If true then :math:`h_i=h \\forall i`. If false then bandwidth is adaptive across observations. radius : If supplied arc_distances will be calculated based on the given radius. p will be ignored. Returns ------- w : W instance of spatial weights Examples -------- >>> points=[(10, 10), (20, 10), (40, 10), (15, 20), (30, 20), (30, 30)] >>> kw=kernelW(points) >>> kw.weights[0] [1.0, 0.500000049999995, 0.4409830615267465] >>> kw.neighbors[0] [0, 1, 3] >>> kw.bandwidth array([[ 20.000002], [ 20.000002], [ 20.000002], [ 20.000002], [ 20.000002], [ 20.000002]]) use different k >>> kw=kernelW(points,k=3) >>> kw.neighbors[0] [0, 1, 3, 4] >>> kw.bandwidth array([[ 22.36068201], [ 22.36068201], [ 22.36068201], [ 22.36068201], [ 22.36068201], [ 22.36068201]]) """ if radius is not None: points = pysal.cg.KDTree(points, distance_metric='Arc', radius=radius) return Kernel(points, function=function, k=k, fixed=fixed)