commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From er...@apache.org
Subject svn commit: r1433367 - in /commons/proper/math/trunk/src: changes/changes.xml main/java/org/apache/commons/math3/distribution/MultivariateNormalDistribution.java test/java/org/apache/commons/math3/distribution/MultivariateNormalDistributionTest.java
Date Tue, 15 Jan 2013 12:15:50 GMT
Author: erans
Date: Tue Jan 15 12:15:50 2013
New Revision: 1433367

URL: http://svn.apache.org/viewvc?rev=1433367&view=rev
Log:
MATH-929
Fixed truncated value. Thanks to Piotr Wydrych.
Added unit test: comparing density values with univariate
normal distribution.

Modified:
    commons/proper/math/trunk/src/changes/changes.xml
    commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/MultivariateNormalDistribution.java
    commons/proper/math/trunk/src/test/java/org/apache/commons/math3/distribution/MultivariateNormalDistributionTest.java

Modified: commons/proper/math/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/changes/changes.xml?rev=1433367&r1=1433366&r2=1433367&view=diff
==============================================================================
--- commons/proper/math/trunk/src/changes/changes.xml (original)
+++ commons/proper/math/trunk/src/changes/changes.xml Tue Jan 15 12:15:50 2013
@@ -55,6 +55,9 @@ This is a minor release: It combines bug
   Changes to existing features were made in a backwards-compatible
   way such as to allow drop-in replacement of the v3.1[.1] JAR file.
 ">
+      <action dev="erans" type="fix" issue="MATH-929" due-to="Piotr Wydrych">
+        Fixed truncated value in "MultivariateNormalDistribution".
+      </action>
       <action dev="erans" type="fix" issue="MATH-927" due-to="Dennis Hendriks">
         Made "BitStreamGenerator" implement the "Serializable" interface.
       </action>

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/MultivariateNormalDistribution.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/MultivariateNormalDistribution.java?rev=1433367&r1=1433366&r2=1433367&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/MultivariateNormalDistribution.java
(original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/MultivariateNormalDistribution.java
Tue Jan 15 12:15:50 2013
@@ -180,7 +180,7 @@ public class MultivariateNormalDistribut
             throw new DimensionMismatchException(vals.length, dim);
         }
 
-        return FastMath.pow(2 * FastMath.PI, -dim / 2) *
+        return FastMath.pow(2 * FastMath.PI, -0.5 * dim) *
             FastMath.pow(covarianceMatrixDeterminant, -0.5) *
             getExponentTerm(vals);
     }

Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math3/distribution/MultivariateNormalDistributionTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math3/distribution/MultivariateNormalDistributionTest.java?rev=1433367&r1=1433366&r2=1433367&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math3/distribution/MultivariateNormalDistributionTest.java
(original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math3/distribution/MultivariateNormalDistributionTest.java
Tue Jan 15 12:15:50 2013
@@ -20,6 +20,7 @@ package org.apache.commons.math3.distrib
 import org.apache.commons.math3.stat.correlation.Covariance;
 import org.apache.commons.math3.linear.RealMatrix;
 
+import java.util.Random;
 import org.junit.After;
 import org.junit.Assert;
 import org.junit.Before;
@@ -130,4 +131,24 @@ public class MultivariateNormalDistribut
             Assert.assertEquals(correctDensities[i], densities[i], 1e-16);
         }
     }
+
+    /**
+     * Test the accuracy of the distribution when calculating densities.
+     */
+    @Test
+    public void testUnivariateDistribution() {
+        final double[] mu = { -1.5 };
+        final double[][] sigma = { { 1 } };
+ 
+        final MultivariateNormalDistribution multi = new MultivariateNormalDistribution(mu,
sigma);
+
+        final NormalDistribution uni = new NormalDistribution(mu[0], sigma[0][0]);
+        final Random rng = new Random();
+        final int numCases = 100;
+        final double tol = Math.ulp(1d);
+        for (int i = 0; i < numCases; i++) {
+            final double v = rng.nextDouble() * 10 - 5;
+            Assert.assertEquals(uni.density(v), multi.density(new double[] { v }), tol);
+        }
+    }
 }



Mime
View raw message