List of usage examples for org.apache.commons.math3.distribution MultivariateNormalDistribution density
public double density(final double[] vals) throws DimensionMismatchException
From source file:com.datumbox.framework.core.statistics.distributions.ContinuousDistributions.java
/** * Calculates the PDF of Multinomial Normal Distribution for a particular x. * // ww w . jav a2 s. c om * @param mean * @param covariance * @param x The x record * @return The multinomialGaussianPdf of x */ public static double multinomialGaussianPdf(double[] mean, double[][] covariance, double[] x) { MultivariateNormalDistribution gaussian = new MultivariateNormalDistribution(mean, covariance); return gaussian.density(x); }
From source file:common.matrix.GaussianProbaTest.java
@Test public void MultinomialTest() { final Graph graph = new GraphBuilder().withPlugin(new MLXPlugin()).withScheduler(new NoopScheduler()) .build();/*from www. j a v a2 s . c o m*/ graph.connect(new Callback<Boolean>() { @Override public void on(Boolean result) { GaussianMixtureNode gaussianNodeLive = (GaussianMixtureNode) graph.newTypedNode(0, 0, GaussianMixtureNode.NAME); gaussianNodeLive.set(GaussianMixtureNode.FROM, "f1;f2"); int test = 100; int feat = 2; double[][] b = new double[test][feat]; double[] v = new double[feat]; Random random = new Random(); for (int i = 0; i < test; i++) { for (int j = 0; j < feat; j++) { v[j] = random.nextDouble() * (1 + 100 * j); b[i][j] = v[j]; } gaussianNodeLive.set("f1", b[i][0]); gaussianNodeLive.set("f2", b[i][1]); gaussianNodeLive.learn(new Callback<Boolean>() { @Override public void on(Boolean result) { } }); } double[] ravg = gaussianNodeLive.getAvg(); Matrix rcovData = gaussianNodeLive.getCovariance(ravg, null); double[][] temp = new double[rcovData.rows()][]; for (int i = 0; i < rcovData.rows(); i++) { temp[i] = new double[rcovData.columns()]; for (int j = 0; j < rcovData.columns(); j++) { temp[i][j] = rcovData.get(i, j); } } //Test probability calculation. MultivariateNormalDistribution apache = new MultivariateNormalDistribution(ravg, temp); double eps = 1e-8; double d = apache.density(v); //System.out.println("apache: " + d); double y = gaussianNodeLive.getProbability(v, null, false); //System.out.println("live: " + y); Assert.assertTrue(Math.abs(d - y) < eps); } }); }