Esempio n. 1
0
def test_boundscheck(x):
    """
    >>> test_boundscheck([1, 2, 3])
    3
    >>> try: test_boundscheck([1, 2])
    ... except IndexError: pass
    """
    with cython.boundscheck(True):
        return x[2]
Esempio n. 2
0
def test_boundscheck(x):
    """
    >>> test_boundscheck([1, 2, 3])
    3
    >>> try: test_boundscheck([1, 2])
    ... except IndexError: pass
    """
    with cython.boundscheck(True):
        return x[2]
Esempio n. 3
0
    if mu.shape[1] > 1:
        mu = mu.T

    cdef int D = int_max(xs.shape[0],xs.shape[1])
    return sum( -O*(0.5*np.log(2*np.pi*sigma)) - ((xs-mu)**2)/(2*(O)*sigma)) 
@cython.boundscheck(False)
@cython.wraparound(False)
@cython.initializedcheck(False)
@cython.cdivision(True) 
cdef lfrogupdate(np.ndarray[np.float64_t, ndim=2] E, np.ndarray[np.float64_t, ndim=2] X, np.ndarray[np.float64_t, ndim=1] Y, np.ndarray[np.float64_t, ndim=2] f, int D, double alpha, np.ndarray[np.float64_t, ndim=2] wnew):
    """Update momentum given current data in leapfrog iterations
    """
    return ( np.dot(X.T,( Y[:,None] - (1./(1+np.exp(-f))) )) - E*(1/alpha)@wnew)

cython.boundscheck(False)
@cython.wraparound(False)
@cython.initializedcheck(False)
def BLR_sghmc_cython(int C, int Bhat, int D, np.ndarray[np.float64_t, ndim=2] Mass, np.ndarray[np.float64_t, ndim=2] w, double m, np.ndarray[np.float64_t, ndim=2] X, np.ndarray[np.float64_t, ndim=1] Y, np.ndarray[np.float64_t, ndim=2] f, double alpha, double StepSize, int BurnIn, int niters, double CurrentLJL):
    """Bayesian Linear Regression using HMC algorithm implemented using Cython
    C is a user specified constant
    Bhat is an approximate set to 0 here (it should converge to 0)
    D is shape of data
    Mass is the mass matrix of kinetic energy
    w is a vector of coefficients to estimate
   m is number of iterations for Monte-Carlo
    X is the explanatory data matrix
    Y is the explained vector
    f fit given initial coefficients (0s)
    alpha is variance of prior
    StepSize dt for dynamics