commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From celes...@apache.org
Subject svn commit: r1334315 - in /commons/proper/math/trunk/src: main/java/org/apache/commons/math3/optimization/general/ main/java/org/apache/commons/math3/special/ test/java/org/apache/commons/math3/optimization/general/
Date Sat, 05 May 2012 05:10:22 GMT
Author: celestin
Date: Sat May  5 05:10:22 2012
New Revision: 1334315

URL: http://svn.apache.org/viewvc?rev=1334315&view=rev
Log:
In o.a.c.m3.optimization.general.AbstractLeastSquaresOptimizer
  - deprecated guessParametersErrors()
  - created getSigma() which should be used instead (but is not strictly equivalent).

Modified:
    commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optimization/general/AbstractLeastSquaresOptimizer.java
    commons/proper/math/trunk/src/main/java/org/apache/commons/math3/special/Gamma.java
    commons/proper/math/trunk/src/test/java/org/apache/commons/math3/optimization/general/AbstractLeastSquaresOptimizerTest.java

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optimization/general/AbstractLeastSquaresOptimizer.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optimization/general/AbstractLeastSquaresOptimizer.java?rev=1334315&r1=1334314&r2=1334315&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optimization/general/AbstractLeastSquaresOptimizer.java
(original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optimization/general/AbstractLeastSquaresOptimizer.java
Sat May  5 05:10:22 2012
@@ -248,6 +248,9 @@ public abstract class AbstractLeastSquar
      * @throws NumberIsTooSmallException if the number of degrees of freedom is not
      * positive, i.e. the number of measurements is less or equal to the number of
      * parameters.
+     * @deprecated as of version 3.1, {@link #getSigma()} should be used
+     * instead. It should be emphasized that {@link #guessParametersErrors()} and
+     * {@link #getSigma()} are <em>not</em> strictly equivalent.
      */
     public double[] guessParametersErrors() {
         if (rows <= cols) {
@@ -263,6 +266,28 @@ public abstract class AbstractLeastSquar
         return errors;
     }
 
+    /**
+     * <p>
+     * Returns an estimate of the standard deviation of the parameters. The
+     * returned values are the square root of the diagonal coefficients of the
+     * covariance matrix, {@code sd(a[i]) ~= sqrt(C[i][i])}, where {@code a[i]}
+     * is the optimized value of the {@code i}-th parameter, and {@code C} is
+     * the covariance matrix.
+     * </p>
+     *
+     * @return an estimate of the standard deviation of the optimized parameters
+     * @throws org.apache.commons.math3.linear.SingularMatrixException
+     * if the covariance matrix cannot be computed.
+     */
+    public double[] getSigma() {
+        double[] sig = new double[cols];
+        double[][] cov = getCovariances();
+        for (int i = 0; i < sig.length; ++i) {
+            sig[i] = FastMath.sqrt(cov[i][i]);
+        }
+        return sig;
+    }
+
     /** {@inheritDoc} */
     @Override
     public PointVectorValuePair optimize(int maxEval,

Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/special/Gamma.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/special/Gamma.java?rev=1334315&r1=1334314&r2=1334315&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/special/Gamma.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/special/Gamma.java Sat
May  5 05:10:22 2012
@@ -325,4 +325,12 @@ public class Gamma {
 
         return trigamma(x + 1) + 1 / (x * x);
     }
+
+    public static double lanczos(final double x){
+        double sum = 0.0;
+        for (int i = LANCZOS.length - 1; i > 0; --i) {
+            sum = sum + (LANCZOS[i] / (x + i));
+        }
+        return sum + LANCZOS[0];
+    }
 }

Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math3/optimization/general/AbstractLeastSquaresOptimizerTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math3/optimization/general/AbstractLeastSquaresOptimizerTest.java?rev=1334315&r1=1334314&r2=1334315&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math3/optimization/general/AbstractLeastSquaresOptimizerTest.java
(original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math3/optimization/general/AbstractLeastSquaresOptimizerTest.java
Sat May  5 05:10:22 2012
@@ -75,7 +75,7 @@ public class AbstractLeastSquaresOptimiz
     }
 
     @Test
-    public void testGuessParametersErrors() throws IOException {
+    public void testGetSigma() throws IOException {
         final StatisticalReferenceDataset dataset;
         dataset = StatisticalReferenceDatasetFactory.createKirby2();
         final AbstractLeastSquaresOptimizer optimizer;
@@ -85,12 +85,14 @@ public class AbstractLeastSquaresOptimiz
         final double[] w = new double[y.length];
         Arrays.fill(w, 1.0);
 
+        final int dof = y.length-a.length;
         optimizer.optimize(1, dataset.getLeastSquaresProblem(), y, w, a);
-        final double[] actual = optimizer.guessParametersErrors();
+        final double[] sig = optimizer.getSigma();
         final double[] expected = dataset.getParametersStandardDeviations();
-        for (int i = 0; i < actual.length; i++) {
+        for (int i = 0; i < sig.length; i++) {
+            final double actual = FastMath.sqrt(optimizer.getChiSquare()/dof)*sig[i];
             Assert.assertEquals(dataset.getName() + ", parameter #" + i,
-                                actual[i], expected[i], 1E-8 * expected[i]);
+                                actual, expected[i], 1E-8 * expected[i]);
         }
     }
 }



Mime
View raw message