covType = "abs" rank = 5 mvnDistribution = distribution1D.BasicMultivariateNormal( covCpp, muCpp, str(covType), rank) results = {"pass": 0, "fail": 0} x0 = [-2.0, -2.0, -2.0, -2.0, -2.0] dx = [0.5, 0.5, 0.5, 0.5, 0.5] center = distribution1D.vectord_cxx(len(x0)) dx_center = distribution1D.vectord_cxx(len(dx)) for i in range(len(mu)): center[i] = x0[i] dx_center[i] = dx[i] utils.checkAnswer("multivariate norm cellProbabilityWeight (center,dx_center)", mvnDistribution.cellProbabilityWeight(center, dx_center), 1.6708270299032051e-08, results) x0 = [-1.0, -1.0, -1.0, -1.0, -1.0] dx = [0.5, 0.5, 0.5, 0.5, 0.5] center = distribution1D.vectord_cxx(len(x0)) dx_center = distribution1D.vectord_cxx(len(dx)) for i in range(len(mu)): center[i] = x0[i] dx_center[i] = dx[i] utils.checkAnswer("multivariate norm cellProbabilityWeight (center,dx_center)", mvnDistribution.cellProbabilityWeight(center, dx_center), 2.5913402492078289e-05, results) x0 = [0.0, 0.0, 0.0, 0.0, 0.0] dx = [0.5, 0.5, 0.5, 0.5, 0.5]
# 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", dimVectorRight[1], rank, results) print(results) sys.exit(results["fail"])
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"])
# using numpy to compute the svd COVn = np.asarray(cov).reshape(-1,sqrt(len(cov))) Un,Sn,Vn = LA.svd(COVn,full_matrices=False) uNp = Un[:,index] sNp = Sn[index] vNp = Vn[index,:] covNp = np.dot(uNp,np.dot(np.diag(sNp),vNp)) # reconstruct the matrix using A = U*S*V.T covReCompute = np.dot(uCpp,np.dot(np.diag(sCpp),vCpp.T)) results = {"pass":0,"fail":0} utils.checkArrayAllClose("MVN singular values",sCpp,sNp,results) utils.checkArrayAllClose("MVN left singular vectors",np.absolute(uCpp),np.absolute(uNp),results) utils.checkArrayAllClose("MVN right singular vectors", np.absolute(vCpp),np.absolute(vNp.T),results) utils.checkArrayAllClose("MVN singular value decomposition",covNp,covReCompute,results) utils.checkAnswer("MVN dimensions of singular values",dim,len(index),results) utils.checkAnswer("MVN row dimensions of left singular vectors",dimVectorLeft[0],5,results) utils.checkAnswer("MVN col dimensions of left singular vectors",dimVectorLeft[1],len(index),results) utils.checkAnswer("MVN row dimensions of right singular vectors",dimVectorRight[0],5,results) utils.checkAnswer("MVN col dimensions of right singular vectors",dimVectorRight[1],len(index),results) print(results) sys.exit(results["fail"])
for i in range(len(mu)): muCpp[i] = mu[i] covCpp = distribution1D.vectord_cxx(len(cov)) for i in range(len(cov)): covCpp[i] = cov[i] # call the functions from the crow to compute the svd covType = "abs" rank = 4 mvnDistribution = distribution1D.BasicMultivariateNormal( covCpp, muCpp, str(covType), rank) results = {"pass": 0, "fail": 0} utils.checkAnswer("multivariate norm inverse marginal for pca (0.1)", mvnDistribution.inverseMarginalForPCA(0.1), -1.2815515655446004, results) utils.checkAnswer("multivariate norm inverse marginal for pca (0.25)", mvnDistribution.inverseMarginalForPCA(0.25), -0.67448975019608171, results) utils.checkAnswer("multivariate norm inverse marginal for pca (0.5)", mvnDistribution.inverseMarginalForPCA(0.5), 0.0, results) utils.checkAnswer("multivariate norm inverse marginal for pca (0.75)", mvnDistribution.inverseMarginalForPCA(0.75), 0.67448975019608171, results) utils.checkAnswer("multivariate norm inverse marginal for pca (0.9)", mvnDistribution.inverseMarginalForPCA(0.9), 1.2815515655446004, results) print(results)
# limitations under the License. #For future compatibility with Python 3 from __future__ import division, print_function, unicode_literals, absolute_import import warnings warnings.simplefilter('default',DeprecationWarning) import sys import utils distribution1D = utils.find_distribution1D() normal_distribution = distribution1D.BasicNormalDistribution(1.0,2.0,-sys.float_info.max, sys.float_info.max) results = {"pass":0,"fail":0} utils.checkAnswer("normal cdf(0.0)",normal_distribution.cdf(0.0),0.308537538726,results) utils.checkAnswer("normal cdf(1.0)",normal_distribution.cdf(1.0),0.5,results) utils.checkAnswer("normal cdf(2.0)",normal_distribution.cdf(2.0),0.691462461274,results) utils.checkAnswer("normal mean",normal_distribution.untrMean(),1.0,results) utils.checkAnswer("normal stddev",normal_distribution.untrStdDev(),2.0,results) utils.checkAnswer("normal ppf(0.1)",normal_distribution.inverseCdf(0.1),-1.56310313109,results) utils.checkAnswer("normal ppf(0.5)",normal_distribution.inverseCdf(0.5),1.0,results) utils.checkAnswer("normal ppf(0.9)",normal_distribution.inverseCdf(0.9),3.56310313109,results) utils.checkAnswer("normal mean()",normal_distribution.untrMean(),1.0,results) utils.checkAnswer("normal median()",normal_distribution.untrMedian(),1.0,results) utils.checkAnswer("normal mode()",normal_distribution.untrMode(),1.0,results) print(results)
#For future compatibility with Python 3 from __future__ import division, print_function, unicode_literals, absolute_import import warnings warnings.simplefilter('default', DeprecationWarning) import sys import utils distribution1D = utils.find_distribution1D() geometric_distribution = distribution1D.BasicGeometricDistribution(0.25) results = {"pass": 0, "fail": 0} utils.checkAnswer("geometric cdf(0)", geometric_distribution.cdf(0), 0.25, results) utils.checkAnswer("geometric cdf(1)", geometric_distribution.cdf(1), 0.4375, results) utils.checkAnswer("geometric mean", geometric_distribution.untrMean(), 3.0, results) utils.checkAnswer("geometric stddev", geometric_distribution.untrStdDev(), 3.46410161514, results) utils.checkAnswer("geometric ppf(0.1)", geometric_distribution.inverseCdf(0.1), 0.0, results) utils.checkAnswer("geometric ppf(0.3)", geometric_distribution.inverseCdf(0.3), 0.239823326142, results) utils.checkAnswer("geometric ppf(0.8)", geometric_distribution.inverseCdf(0.8), 4.59450194, results) utils.checkAnswer("geometric ppf(0.9)", geometric_distribution.inverseCdf(0.9), 7.00392277965, results)
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 = 1./sqrt(S) * U.T temp = np.dot(LA.inv(np.sqrt(np.diag(sNp))), uNp.T) uReCompute = temp[:, index] results = {"pass": 0, "fail": 0} utils.checkArrayAllClose("MVN inverse transformation matrix", np.absolute(uCpp), np.absolute(uReCompute), results) utils.checkAnswer("MVN row dimensions of inverse transformation matrix", dimVector[0], 5, results) utils.checkAnswer("MVN col dimensions of inverse transformation matrix", dimVector[1], len(index), results) dimVector = mvnDistribution.getInverseTransformationMatrixDimensions() uCpp_vector = mvnDistribution.getInverseTransformationMatrix() uCpp = [uCpp_vector[i] for i in range(dimVector[0] * dimVector[1])] uCpp = np.asarray(uCpp) uCpp = np.reshape(uCpp, (dimVector[0], dimVector[1])) # compute the transformation matrix = 1./sqrt(S) * U.T temp = np.dot(LA.inv(np.sqrt(np.diag(sNp))), uNp.T) uReCompute = temp utils.checkArrayAllClose("MVN inverse transformation matrix", np.absolute(uCpp), np.absolute(uReCompute), results)
for i in range(len(mu)): muCpp[i] = mu[i] covCpp = distribution1D.vectord_cxx(len(cov)) for i in range(len(cov)): covCpp[i] = cov[i] # call the functions from the crow to compute the svd covType = "abs" rank = 5 mvnDistribution = distribution1D.BasicMultivariateNormal( covCpp, muCpp, str(covType), rank) results = {"pass": 0, "fail": 0} # the marginal cdf is defined as standard norm distribution with mean 0 and variance 1 utils.checkAnswer("multivariate norm marginal for pca (-4.0)", mvnDistribution.marginalCdfForPCA(-4.0), 3.1671241833119863e-05, results) utils.checkAnswer("multivariate norm marginal for pca (-3.5)", mvnDistribution.marginalCdfForPCA(-3.5), 0.00023262907903552502, results) utils.checkAnswer("multivariate norm marginal for pca (-2.2)", mvnDistribution.marginalCdfForPCA(-2.2), 0.013903447513498595, results) utils.checkAnswer("multivariate norm marginal for pca (-1.1)", mvnDistribution.marginalCdfForPCA(-1.1), 0.13566606094638267, results) utils.checkAnswer("multivariate norm marginal for pca (-0.7)", mvnDistribution.marginalCdfForPCA(-0.7), 0.24196365222307303, results) utils.checkAnswer("multivariate norm marginal for pca (4.0)", mvnDistribution.marginalCdfForPCA(4.0), 0.99996832875816688,
#For future compatibility with Python 3 from __future__ import division, print_function, unicode_literals, absolute_import import warnings warnings.simplefilter('default', DeprecationWarning) import sys import utils distribution1D = utils.find_distribution1D() laplace_distribution = distribution1D.BasicLaplaceDistribution( 0.0, 2.0, -sys.float_info.max, sys.float_info.max) results = {"pass": 0, "fail": 0} utils.checkAnswer("normal cdf(0.0)", laplace_distribution.cdf(0.0), 0.5, results) utils.checkAnswer("normal cdf(1.0)", laplace_distribution.cdf(1.0), 0.696734670144, results) utils.checkAnswer("normal cdf(2.0)", laplace_distribution.cdf(2.0), 0.816060279414, results) utils.checkAnswer("normal mean", laplace_distribution.untrMean(), 0.0, results) utils.checkAnswer("normal stddev", laplace_distribution.untrStdDev(), 2.82842712475, results) utils.checkAnswer("normal ppf(0.1)", laplace_distribution.inverseCdf(0.1), -3.21887582487, results) utils.checkAnswer("normal ppf(0.5)", laplace_distribution.inverseCdf(0.5), 0.0, results) utils.checkAnswer("normal ppf(0.9)", laplace_distribution.inverseCdf(0.9), 3.21887582487, results) utils.checkAnswer("normal mean()", laplace_distribution.untrMean(), 0.0,