def mahalanobis_covariance(x, y, diff_degree, amp, val, vec, symm=None):
    """
    Converts x and y to a matrix of covariances. x and y are assumed to have
    columns (long,lat,t). Parameters are:
    - t_gam_fun: A function returning a matrix of variogram values.
      Inputs will be the 't' columns of x and y, as well as kwds.
    - amp: The MS amplitude of realizations.
    - scale: Scales distance.
    - inc, ecc: Anisotropy parameters.
    - n_threads: Maximum number of threads available to function.
    - symm: Flag indicating whether matrix will be symmetric (optional).
    - kwds: Passed to t_gam_fun.
    
    Output value should never drop below -1. This happens when:
    -1 > -sf*c+k
    
    """
    # Allocate
    nx = x.shape[0]
    ny = y.shape[0]
    ndim = x.shape[1]

    C = np.asmatrix(np.empty((nx, ny), order="F"))

    # Figure out symmetry and threading
    if symm is None:
        symm = x is y

    n_threads = min(pm.get_threadpool_size(), nx * ny / 2000)

    if n_threads > 1:
        if not symm:
            bounds = np.linspace(0, ny, n_threads + 1)
        else:
            bounds = np.array(np.sqrt(np.linspace(0, ny * ny, n_threads + 1)), dtype=int)

    # Target function for threads
    def targ(C, x, y, symm, diff_degree, amp, val, vec, cmin, cmax):
        mahal(C, x, y, symm, diff_degree, gamma(diff_degree), amp, val, vec, cmin, cmax)

    # Dispatch threads
    if n_threads <= 1:
        mahal(C, x, y, symm, diff_degree, gamma(diff_degree), amp, val, vec, cmin=0, cmax=C.shape[1])
    else:
        thread_args = [(C, x, y, symm, diff_degree, amp, val, vec, bounds[i], bounds[i + 1]) for i in xrange(n_threads)]
        pm.map_noreturn(targ, thread_args)

    if symm:
        pm.gp.symmetrize(C)

        # eigs = np.linalg.eigh(C)
        # val,vec = eigs
        # if np.any(val<-1.e-3):
        #     raise RuntimeError, 'Negative eigenvalues: %s'%val[np.where(val<0)]

    return C
示例#2
0
文件: cov_utils.py 项目: huard/pymc
    def __call__(self,x,y,amp=1.,scale=1.,symm=None,*args,**kwargs):

        if amp<0. or scale<0.:
            raise ValueError, 'The amp and scale parameters must be positive.'

        if symm is None:
            symm = (x is y)

        # Figure out how to divide job up between threads.
        nx = x.shape[0]
        ny = y.shape[0]
        n_threads = min(get_threadpool_size(), nx*ny / 10000)

        if n_threads > 1:
            if not symm:
                bounds = np.linspace(0,ny,n_threads+1)
            else:
                bounds = np.array(np.sqrt(np.linspace(0,ny*ny,n_threads+1)),dtype=int)

        # Split off the distance arguments
        distance_arg_dict = {}
        if hasattr(self.distance_fun, 'extra_parameters'):
            for key in self.extra_distance_params.iterkeys():
                if key in kwargs.keys():
                    distance_arg_dict[key] = kwargs.pop(key)

        # Allocate the matrix
        C = np.asmatrix(np.empty((nx,ny),dtype=float,order='F'))

        def targ(C,x,y, cmin, cmax,symm, d_kwargs=distance_arg_dict, c_args=args, c_kwargs=kwargs):
            # Compute distance for this bit
            self.distance_fun(C, x, y, cmin=cmin, cmax=cmax, symm=symm, **d_kwargs)
            imul(C, 1./scale, cmin=cmin, cmax=cmax, symm=symm)
            # Compute covariance for this bit
            self.cov_fun(C, cmin=cmin, cmax=cmax,symm=symm, *c_args, **c_kwargs)
            imul(C, amp*amp, cmin=cmin, cmax=cmax, symm=symm)
            # Possibly symmetrize this bit
            # FIXME: Intermittent errors apparently originating in symmetrize!
            # if symm:
            #     symmetrize(C, cmin=cmin, cmax=cmax)

        if n_threads <= 1:
            targ(C,x,y,0,-1,symm)
        else:
            thread_args = [(C,x,y,bounds[i],bounds[i+1],symm) for i in xrange(n_threads)]
            map_noreturn(targ, thread_args)

        if symm:
            symmetrize(C)


        return C
    def __call__(self,x,y,amp=1.,scale=1.,symm=None,*args,**kwargs):

        if amp<0. or scale<0.:
            raise ValueError('The amp and scale parameters must be positive.')

        if symm is None:
            symm = (x is y)

        # Figure out how to divide job up between threads.
        nx = x.shape[0]
        ny = y.shape[0]
        n_threads = min(get_threadpool_size(), nx*ny // 10000)

        if n_threads > 1:
            if not symm:
                bounds = np.linspace(0,ny,n_threads+1)
            else:
                bounds = np.array(np.sqrt(np.linspace(0,ny*ny,n_threads+1)),dtype=int)

        # Split off the distance arguments
        distance_arg_dict = {}
        if hasattr(self.distance_fun, 'extra_parameters'):
            for key in self.extra_distance_params:
                if key in kwargs.keys():
                    distance_arg_dict[key] = kwargs.pop(key)

        # Allocate the matrix
        C = np.asmatrix(np.empty((nx,ny),dtype=float,order='F'))

        def targ(C,x,y, cmin, cmax,symm, d_kwargs=distance_arg_dict, c_args=args, c_kwargs=kwargs):
            # Compute distance for this bit
            self.distance_fun(C, x, y, cmin=cmin, cmax=cmax, symm=symm, **d_kwargs)
            imul(C, 1./scale, cmin=cmin, cmax=cmax, symm=symm)
            # Compute covariance for this bit
            if self.with_x:
                self.cov_fun(C,x,y,cmin=cmin, cmax=cmax,symm=symm,*c_args,**c_kwargs)
            else:
                self.cov_fun(C, cmin=cmin, cmax=cmax,symm=symm, *c_args, **c_kwargs)
            imul(C, amp*amp, cmin=cmin, cmax=cmax, symm=symm)

        if n_threads <= 1:
            targ(C,x,y,0,-1,symm)
        else:
            thread_args = [(C,x,y,bounds[i],bounds[i+1],symm) for i in xrange(n_threads)]
            map_noreturn(targ, thread_args)

        if symm:
            symmetrize(C)


        return C
示例#4
0
def brownian(x, y, amp=1.0, scale=1.0, origin=None, h=0.5, symm=None):
    """
    brownian(x,y,amp=1., scale=1.,h=.5,origin=None)

    Fractional n-dimensional brownian motion. h=.5 corresponds to standard
    Brownian motion.

    A covariance function. Remember, broadcasting for covariance functions works
    differently than for numpy universal functions. C(x,y) returns a matrix, and
    C(x) returns a vector.

    :Parameters:

        - `amp`: The pointwise standard deviation of f.

        - `scale`: The factor by which to scale the distance between points.
                 Large value implies long-range correlation.

        - `h': The fractional parameter.


        - `x and y` are arrays of points in Euclidean coordinates
          formatted as follows:

          [[x_{0,0} ... x_{0,ndim}],
           [x_{1,0} ... x_{1,ndim}],
           ...
           [x_{N,0} ... x_{N,ndim}]]

        - `symm` indicates whether x and y are references to
          the same array.

        - `cmin' and `cmax' indicate which columns to compute.
          These are used for multithreaded evaluation.

    :Reference: http://en.wikipedia.org/wiki/Fractional_brownian_motion
    """
    # Thanks to Anne Archibald for handythread.py, the model for the
    # multithreaded call.

    if h < 0 or h > 1:
        raise ValueError("Parameter h must be between 0 and 1.")

    if amp < 0.0 or scale < 0.0:
        raise ValueError("The amp and scale parameters must be positive.")

    if symm is None:
        symm = x is y

    # Figure out how to divide job up between threads.
    nx = x.shape[0]
    ny = y.shape[0]
    n_threads = min(get_threadpool_size(), nx * ny / 10000)

    if n_threads > 1:
        if not symm:
            bounds = np.linspace(0, ny, n_threads + 1)
        else:
            bounds = np.array(np.sqrt(np.linspace(0, ny * ny, n_threads + 1)), dtype=int)

    # Allocate the matrix
    C = np.asmatrix(np.empty((nx, ny), dtype=float, order="F"))
    if origin is not None:
        x = x - origin
        y = y - origin
    x = x / float(scale)
    y = y / float(scale)

    if n_threads <= 1:
        brownian_targ(C, x, y, h, amp, 0, -1, symm)
    else:
        thread_args = [(C, x, y, h, amp, bounds[i], bounds[i + 1], symm) for i in xrange(n_threads)]
        map_noreturn(brownian_targ, thread_args)

    return C
示例#5
0
def brownian(x,y,amp=1.,scale=1.,origin=None,h=.5,symm=None):
    """
    brownian(x,y,amp=1., scale=1.,h=.5,origin=None)

    Fractional n-dimensional brownian motion. h=.5 corresponds to standard
    Brownian motion.

    A covariance function. Remember, broadcasting for covariance functions works
    differently than for numpy universal functions. C(x,y) returns a matrix, and
    C(x) returns a vector.

    :Parameters:

        - `amp`: The pointwise standard deviation of f.

        - `scale`: The factor by which to scale the distance between points.
                 Large value implies long-range correlation.

        - `h': The fractional parameter.


        - `x and y` are arrays of points in Euclidean coordinates
          formatted as follows:

          [[x_{0,0} ... x_{0,ndim}],
           [x_{1,0} ... x_{1,ndim}],
           ...
           [x_{N,0} ... x_{N,ndim}]]

        - `symm` indicates whether x and y are references to
          the same array.

        - `cmin' and `cmax' indicate which columns to compute.
          These are used for multithreaded evaluation.

    :Reference: http://en.wikipedia.org/wiki/Fractional_brownian_motion
    """
    # Thanks to Anne Archibald for handythread.py, the model for the
    # multithreaded call.

    if h<0 or h>1:
        raise ValueError, 'Parameter h must be between 0 and 1.'

    if amp<0. or scale<0.:
        raise ValueError, 'The amp and scale parameters must be positive.'

    if symm is None:
        symm = (x is y)

    # Figure out how to divide job up between threads.
    nx = x.shape[0]
    ny = y.shape[0]
    n_threads = min(get_threadpool_size(), nx*ny / 10000)

    if n_threads > 1:
        if not symm:
            bounds = np.linspace(0,ny,n_threads+1)
        else:
            bounds = np.array(np.sqrt(np.linspace(0,ny*ny,n_threads+1)),dtype=int)

    # Allocate the matrix
    C = np.asmatrix(np.empty((nx,ny),dtype=float,order='F'))
    if origin is not None:
        x = x-origin
        y = y-origin
    x = x / float(scale)
    y = y / float(scale)

    if n_threads <= 1:
        brownian_targ(C,x,y,h,amp,0,-1,symm)
    else:
        thread_args=[(C,x,y,h,amp,bounds[i],bounds[i+1],symm) for i in xrange(n_threads)]
        map_noreturn(brownian_targ, thread_args)

    return C
示例#6
0
def nonstationary_spatiotemporal(x,y,amp,scale,diff_degree,t_gam_fun=default_t_gam_fun,h=default_h,symm=None,geometry='aniso_geo_rad',**kwds):
    """
    Spatiotemporal covariance function. Converts x and y
    to a matrix of covariances. x and y are assumed to have
    columns (long,lat,t). Parameters are:
    - t_gam_fun: A function returning a matrix of variogram values.
      Inputs will be the 't' columns of x and y, as well as kwds.
    - amp: The MS amplitude of realizations.
    - scale: Scales distance.
    - diff_degree: A function that returns local degree of differentiability at x.
    - h: A function that returns local relative amplitude at x.
    - inc, ecc: Anisotropy parameters. Needed if geometry=='aniso_geo_rad'.
    - n_threads: Maximum number of threads available to function.
    - symm: Flag indicating whether matrix will be symmetric (optional).
    - geometry: Must be 'aniso_geo_rad' or 'euclidean'.
    - kwds: Passed to t_gam_fun.
    
    References:
    
    Stein, 2005. "Space-Time Covariance Functions". Journal of the American Statistical 
        Association 100(469).
    
    Pintore and Holmes, 2010, "Spatially adaptive non-stationary covariance functions
        via spatially adaptive spectra". Journal of the American Statistical Association.
        Forthcoming.
    
    """
    # Allocate 
    nx = x.shape[0]
    ny = y.shape[0]
        
    if kwds.has_key('n_threads'):
        kwds.pop('n_threads')
    
    if geometry=='aniso_geo_rad':
        inc = kwds.pop('inc')
        ecc = kwds.pop('ecc')
    else:
        inc = None
        ecc = None
    
    if geometry not in ['aniso_geo_rad','euclidean']:
        raise ValueError, 'Geometry %s unknown, must be aniso_geo_rad or euclidean.'%geometry
    
    D = np.asmatrix(np.empty((nx,ny),order='F'))
    GT = np.asmatrix(np.empty((nx,ny),order='F'))
    
    # Figure out symmetry and threading
    if symm is None:
        symm = (x is y)

    n_threads = min(get_threadpool_size(), nx*ny / 10000)    
    if n_threads > 1:
        if not symm:
            bounds = np.linspace(0,ny,n_threads+1)
        else:
            bounds = np.array(np.sqrt(np.linspace(0,ny*ny,n_threads+1)),dtype=int)

    # Target function for threads
    def targ(D,GT,x,y,cmin,cmax,symm,inc=inc,ecc=ecc,amp=amp,scale=scale,diff_degree=diff_degree,h=h,geometry=geometry,kwds=kwds):
        # Spatial distance
        if geometry=='aniso_geo_rad':
            aniso_geo_rad(D, x[:,:-1], y[:,:-1], inc, ecc,cmin=cmin,cmax=cmax,symm=symm)    
        else:
            euclidean(D, x[:,:-1], y[:,:-1], cmin=cmin,cmax=cmax,symm=symm)    
        imul(D,1./scale,cmin=cmin,cmax=cmax,symm=symm)            
        # Temporal variogram
        ddx, ddy = diff_degree(x), diff_degree(y)
        origin_val = t_gam_fun(GT, x[:,-1], y[:,-1], ddx, ddy, cmin=cmin,cmax=cmax,symm=False,**kwds)
        if np.any(GT<0):
            raise pm.ZeroProbability, 'GT < 0.'
        # GT = np.add.outer(ddx*.5,ddy*.5)
        # Local properties
        hx, hy = h(x), h(y)
        # Covariance
        nsst(D,GT,origin_val,hx,hy,cmin=cmin,cmax=cmax,symm=symm)                        
        imul(D,amp*amp,cmin=cmin,cmax=cmax,symm=symm)            
    
    # Serial version
    if n_threads <= 1:
        targ(D,GT,x,y,0,-1,symm)
    
    # Parallel version
    else:   
        thread_args = [(D,GT,x,y,bounds[i],bounds[i+1],symm) for i in xrange(n_threads)]
        map_noreturn(targ, thread_args)

    if symm:
        symmetrize(D)
    
    return D
示例#7
0
def my_st(x,y,amp,scale,inc,ecc,symm=None,**kwds):
    """
    Spatiotemporal covariance function. Converts x and y
    to a matrix of covariances. x and y are assumed to have
    columns (long,lat,t). Parameters are:
    - t_gam_fun: A function returning a matrix of variogram values.
      Inputs will be the 't' columns of x and y, as well as kwds.
    - amp: The MS amplitude of realizations.
    - scale: Scales distance.
    - inc, ecc: Anisotropy parameters.
    - n_threads: Maximum number of threads available to function.
    - symm: Flag indicating whether matrix will be symmetric (optional).
    - kwds: Passed to t_gam_fun.
    
    Output value should never drop below -1. This happens when:
    -1 > -sf*c+k
    
    """
    # Allocate 
    nx = x.shape[0]
    ny = y.shape[0]
    
    k=kwds['tlc']/kwds['sd']
    c=1./kwds['sd']-k
    sf=kwds['sf']
    tlc=kwds['tlc']
    sd=kwds['sd']
    
    if kwds.has_key('n_threads'):
        kwds.pop('n_threads')
    
    # If parameter values are illegal, just return zeros.
    # This case will be caught by the Potential.
    if -sd >= 1./(-sf*(1-tlc)+tlc):
        return np.zeros((nx,ny))
    
    D = np.asmatrix(np.empty((nx,ny),order='F'))
    GT = np.asmatrix(np.empty((nx,ny),order='F'))
    
    # Figure out symmetry and threading
    if symm is None:
        symm = (x is y)

    n_threads = min(get_threadpool_size(), nx*ny / 10000)    
    if n_threads > 1:
        if not symm:
            bounds = np.linspace(0,ny,n_threads+1)
        else:
            bounds = np.array(np.sqrt(np.linspace(0,ny*ny,n_threads+1)),dtype=int)

    # Target function for threads
    def targ(D,GT,x,y,cmin,cmax,symm,inc=inc,ecc=ecc,amp=amp,scale=scale,kwds=kwds):
        # Spatial distance
        aniso_geo_rad(D, x[:,:-1], y[:,:-1], inc, ecc,cmin=cmin,cmax=cmax,symm=symm)    
        imul(D,1./scale,cmin=cmin,cmax=cmax,symm=symm)            
        # Temporal variogram
        origin_val = t_gam_fun(GT, x[:,-1], y[:,-1],cmin=cmin,cmax=cmax,symm=symm,**kwds)
        # Covariance
        stein_spatiotemporal(D,GT,origin_val,cmin=cmin,cmax=cmax,symm=symm)                        
        imul(D,amp*amp,cmin=cmin,cmax=cmax,symm=symm)            
        # if symm:
        #     symmetrize(D, cmin=cmin, cmax=cmax)
    
    # Serial version
    if n_threads <= 1:
        targ(D,GT,x,y,0,-1,symm)
    
    # Parallel version
    else:   
        thread_args = [(D,GT,x,y,bounds[i],bounds[i+1],symm) for i in xrange(n_threads)]
        map_noreturn(targ, thread_args)

    if symm:
        symmetrize(D)
    
    return D