covType = "abs" rank = 5 mvnDistribution = distribution1D.BasicMultivariateNormal( covCpp, muCpp, str(covType), 5) dimVector = mvnDistribution.getTransformationMatrixDimensions() uCpp_vector = mvnDistribution.getTransformationMatrix() uCpp = [uCpp_vector[i] for i in range(dimVector[0] * dimVector[1])] uCpp = np.asarray(uCpp) uCpp = np.reshape(uCpp, (dimVector[0], dimVector[1])) # using numpy to compute the svd covNp = np.asarray(cov).reshape(-1, int(sqrt(len(cov)))) uNp, sNp, vNp = LA.svd(covNp, full_matrices=False) # compute the transformation matrix = U*sqrt(S) uReCompute = np.dot(uNp, np.sqrt(np.diag(sNp))) results = {"pass": 0, "fail": 0} utils.checkArrayAllClose("MVN transformation matrix", np.absolute(uCpp), np.absolute(uReCompute), results) utils.checkAnswer("MVN row dimensions of transformation matrix", dimVector[0], 5, results) utils.checkAnswer("MVN col dimensions of transformation matrix", dimVector[1], 5, results) print(results) sys.exit(results["fail"])
mvnDistribution = distribution1D.BasicMultivariateNormal( covCpp, muCpp, str(covType), rank) # using numpy to compute the svd COVn = np.asarray(cov).reshape(-1, int(sqrt(len(cov)))) Un, Sn, Vn = LA.svd(COVn, full_matrices=False) uNp = Un[:, index] sNp = Sn[index] coordinateInOriginalSpace = np.dot( uNp, np.dot(np.diag(np.sqrt(sNp)), coordinateInTransformedSpace)) #compute the gold solution: mu = np.asarray(mu) coordinateInOriginalSpace += mu #call crow to compute the coordinate Xcoordinate = mvnDistribution.coordinateInverseTransformed( coordinateInTransformedSpaceCpp, indexCpp) Xcoordinate = [Xcoordinate[i] for i in range(5)] Xcoordinate = np.asarray(Xcoordinate) results = {"pass": 0, "fail": 0} utils.checkArrayAllClose("MVN return coordinate in original space", Xcoordinate, coordinateInOriginalSpace, results) print(results) sys.exit(results["fail"])
vCpp = np.reshape(vCpp, (dimVectorRight[0], dimVectorRight[1])) # using numpy to compute the svd covNp = np.asarray(cov).reshape(-1, int(sqrt(len(cov)))) uNp, sNp, vNp = LA.svd(covNp, full_matrices=False) # reconstruct the matrix using A = U*S*V.T using the results from crow covReCompute = np.dot(uCpp, np.dot(np.diag(sCpp), vCpp.T)) # reconstruct the matrix using A = U*S*V.T using the results from numpy covNpReCompute = np.dot(uNp[:, 0:rank], np.dot(np.diag(sNp[0:rank]), vNp[0:rank, :])) results = {"pass": 0, "fail": 0} utils.checkArrayAllClose("MVN singular values", sCpp, sNp[0:rank], results) utils.checkArrayAllClose("MVN left singular vectors", np.absolute(uCpp), np.absolute(uNp[:, 0:rank]), results) utils.checkArrayAllClose("MVN right singular vectors", np.absolute(vCpp), np.absolute(vNp[0:rank, :].T), results) utils.checkArrayAllClose("MVN singular value decomposition", covNpReCompute, covReCompute, results) utils.checkAnswer("MVN dimensions of singular values", dim, rank, results) utils.checkAnswer("MVN row dimensions of left singular vectors", dimVectorLeft[0], size, results) utils.checkAnswer("MVN col dimensions of left singular vectors", dimVectorLeft[1], rank, results) utils.checkAnswer("MVN row dimensions of right singular vectors", dimVectorRight[0], size, results) utils.checkAnswer("MVN col dimensions of right singular vectors",
uCpp = [uCpp_vector[i] for i in range(dimVectorLeft[0] * dimVectorLeft[1])] uCpp = np.asarray(uCpp) uCpp = np.reshape(uCpp, (dimVectorLeft[0], dimVectorLeft[1])) # using numpy to compute the eigenvalues cov = np.asarray(cov) # compute the symmetric matrix covNp = cov.reshape(5, 5) covNp = (covNp + covNp.T) / 2.0 sNp, uNp = LA.eig(covNp) #reset the singular values for i in range(sNp.size): if sNp[i] < 0: sNp[i] = 0.0 #reorder the singular values and left singular vectors indexNp = np.argsort(sNp) indexCpp = np.argsort(sCpp) sNp = sNp[indexNp] sCpp = sCpp[indexCpp] uCpp = uCpp[:, indexCpp] uNp = uNp[:, indexNp] results = {"pass": 0, "fail": 0} utils.checkArrayAllClose("Singular values", sCpp, sNp, results) utils.checkArrayAllClose("Left singular vectors", np.absolute(uCpp), np.absolute(uNp), results) print(results) sys.exit(results["fail"])