def mvnpdf(x, mu, sigma): # Compute the residuals #if x.shape[0] == 1: # residual = np.repeat(x, mu.shape[0], 0) #else: # residual = x.copy(order='c') #blas.daxpy(-1.0, mu, residual) residual = x-mu #if x.shape[0] == 1: # x = np.repeat(x, mu.shape[0], 0) #residual = x-mu chol_sigma = blas.dpotrf(sigma) # Compute the determinant diag_vec = np.array([np.diag(chol_sigma[i]) for i in range(chol_sigma.shape[0])]) det_sigma = diag_vec.prod(1)**2 # If same number of sigma and residuals, or only residual and many sigma if sigma.shape[0] == residual.shape[0] or residual.shape[0] == 1: inv_sigma_times_residual = blas.dpotrs(chol_sigma, residual) exp_term = blas.ddot(residual, inv_sigma_times_residual) # Otherwise, we have only one sigma - compute the inverse once else: # Compute the inverse of the square root inv_sqrt_sigma = blas.dtrtri(chol_sigma, 'l') exp_term = np.power(blas.dgemv(inv_sqrt_sigma,residual), 2).sum(axis=1) pdf = np.exp(-0.5*exp_term)/np.sqrt(det_sigma*(2*np.pi)**residual.shape[1]) return pdf
def test2(self): ''' Test col vector dot row vector. ''' random.seed(13) n = 432 maxTotalBlocks = 13 # instantiate x sparsityX = 0.2 protocolX = MatrixWrapper.RAW dfsDirX = None maxCoresX = 13 x = randomSparseMatrix(n, 1, sparsityX) xWrap = MatrixWrapper.wrapMatrix(x, protocolX, dfsDirX, maxCoresX) # instantiate y sparsityY = 0.5 protocolY = MatrixWrapper.RAW dfsDirY = None maxCoresY = 13 y = randomSparseMatrix(1, n, sparsityY) yWrap = MatrixWrapper.wrapMatrix(y, protocolY, dfsDirY, maxCoresY) # dot product val = ddot(disco, n, xWrap, yWrap, maxTotalBlocks) # validate self.validate(n, x, y, val)