def testqr(a, eps): q,r = qr_decomposition(a,mode='e') q,r = qr_decomposition(a,mode='r') q,r = qr_decomposition(a,mode='full') res = num.matrixmultiply(q,r) b = num.ravel( num.abs(res - a) ) if num.maximum.reduce(b) > eps: raise SelftestFailure else: print "OK"
def testqr(a, eps): q, r = qr_decomposition(a, mode='e') q, r = qr_decomposition(a, mode='r') q, r = qr_decomposition(a, mode='full') res = num.matrixmultiply(q, r) b = num.ravel(num.abs(res - a)) if num.maximum.reduce(b) > eps: raise SelftestFailure else: print "OK"
def multivariate_normal(mean, cov, shape=[]): """multivariate_normal(mean, cov) or multivariate_normal(mean, cov, [m, n, ...]) Returns an array containing multivariate normally distributed random numbers with specified mean and covariance. |mean| must be a one-dimensional array. |cov| must be a square two-dimensional array with the same number of rows and columns as |mean| has elements. The first form returns a single 1-D array containing a multivariate normal. The second form returns an array of shape (m, n, ..., cov.getshape()[0]). In this case, output[i,j,...,:] is a 1-D array containing a multivariate normal. """ # Check preconditions on arguments mean = num.array(mean) cov = num.array(cov) if len(mean.getshape()) != 1: raise ArgumentError, "mean must be 1 dimensional." if (len(cov.getshape()) != 2) or (cov.getshape()[0] != cov.getshape()[1]): raise ArgumentError, "cov must be 2 dimensional and square." if mean.getshape()[0] != cov.getshape()[0]: raise ArgumentError, "mean and cov must have same length." # Compute shape of output if isinstance(shape, _types.IntType): shape = [shape] final_shape = list(shape[:]) final_shape.append(mean.getshape()[0]) # Create a matrix of independent standard normally distributed random # numbers. The matrix has rows with the same length as mean and as # many rows are necessary to form a matrix of shape final_shape. x = ranlib.standard_normal(num.multiply.reduce(final_shape)) x.setshape(num.multiply.reduce(final_shape[0:len(final_shape) - 1]), mean.getshape()[0]) # Transform matrix of standard normals into matrix where each row # contains multivariate normals with the desired covariance. # Compute A such that matrixmultiply(transpose(A),A) == cov. # Then the matrix products of the rows of x and A has the desired # covariance. Note that sqrt(s)*v where (u,s,v) is the singular value # decomposition of cov is such an A. (u, s, v) = linalg.singular_value_decomposition(cov) x = num.matrixmultiply(x * num.sqrt(s), v) # The rows of x now have the correct covariance but mean 0. Add # mean to each row. Then each row will have mean mean. num.add(mean, x, x) x.setshape(final_shape) return x
def multivariate_normal(mean, cov, shape=[]): """multivariate_normal(mean, cov) or multivariate_normal(mean, cov, [m, n, ...]) Returns an array containing multivariate normally distributed random numbers with specified mean and covariance. |mean| must be a one-dimensional array. |cov| must be a square two-dimensional array with the same number of rows and columns as |mean| has elements. The first form returns a single 1-D array containing a multivariate normal. The second form returns an array of shape (m, n, ..., cov.getshape()[0]). In this case, output[i,j,...,:] is a 1-D array containing a multivariate normal. """ # Check preconditions on arguments mean = num.array(mean) cov = num.array(cov) if len(mean.getshape()) != 1: raise ArgumentError, "mean must be 1 dimensional." if (len(cov.getshape()) != 2) or (cov.getshape()[0] != cov.getshape()[1]): raise ArgumentError, "cov must be 2 dimensional and square." if mean.getshape()[0] != cov.getshape()[0]: raise ArgumentError, "mean and cov must have same length." # Compute shape of output if isinstance(shape, _types.IntType): shape = [shape] final_shape = list(shape[:]) final_shape.append(mean.getshape()[0]) # Create a matrix of independent standard normally distributed random # numbers. The matrix has rows with the same length as mean and as # many rows are necessary to form a matrix of shape final_shape. x = ranlib.standard_normal(num.multiply.reduce(final_shape)) x.setshape(num.multiply.reduce(final_shape[0:len(final_shape)-1]), mean.getshape()[0]) # Transform matrix of standard normals into matrix where each row # contains multivariate normals with the desired covariance. # Compute A such that matrixmultiply(transpose(A),A) == cov. # Then the matrix products of the rows of x and A has the desired # covariance. Note that sqrt(s)*v where (u,s,v) is the singular value # decomposition of cov is such an A. (u,s,v) = linalg.singular_value_decomposition(cov) x = num.matrixmultiply(x*num.sqrt(s),v) # The rows of x now have the correct covariance but mean 0. Add # mean to each row. Then each row will have mean mean. num.add(mean,x,x) x.setshape(final_shape) return x