changeset 3010:05a3fe8800b3

FixA: Store Chi^2 of fitting into result, too. flys-artifacts/trunk@4575 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Mon, 04 Jun 2012 10:13:20 +0000
parents 4dda67a893da
children ab81ffd1343e
files flys-artifacts/ChangeLog flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/Parameters.java flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/fixings/FixCalculation.java
diffstat 3 files changed, 34 insertions(+), 11 deletions(-) [+]
line wrap: on
line diff
--- a/flys-artifacts/ChangeLog	Thu May 31 16:32:03 2012 +0000
+++ b/flys-artifacts/ChangeLog	Mon Jun 04 10:13:20 2012 +0000
@@ -1,3 +1,11 @@
+2012-06-04	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
+
+	* src/main/java/de/intevation/flys/artifacts/model/fixings/FixCalculation.java:
+	  Store Chi^2 of fitting into result, too.
+
+	* src/main/java/de/intevation/flys/artifacts/model/Parameters.java:
+	  Added indexed set method.
+
 2012-05-31	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
 
 	* src/main/java/de/intevation/flys/artifacts/math/fitting/Function.java:
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/Parameters.java	Thu May 31 16:32:03 2012 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/Parameters.java	Mon Jun 04 10:13:20 2012 +0000
@@ -49,8 +49,8 @@
         return N;
     }
 
-    public double get(int i, int index) {
-        return columns[index].getQuick(i);
+    public double get(int row, int index) {
+        return columns[index].getQuick(row);
     }
 
     public double get(int i, String columnName) {
@@ -60,6 +60,10 @@
             : Double.NaN;
     }
 
+    public void set(int row, int index, double value) {
+        columns[index].setQuick(row, value);
+    }
+
     public void set(int i, String columnName, double value) {
         int idx = columnIndex(columnName);
         if (idx >= 0) {
--- a/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/fixings/FixCalculation.java	Thu May 31 16:32:03 2012 +0000
+++ b/flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/fixings/FixCalculation.java	Mon Jun 04 10:13:20 2012 +0000
@@ -25,6 +25,7 @@
 
 import de.intevation.flys.utils.DateAverager;
 import de.intevation.flys.utils.DoubleUtil;
+import de.intevation.flys.utils.Pair;
 
 import java.util.ArrayList;
 import java.util.Date;
@@ -197,6 +198,9 @@
             log.debug("number of kms: " + kms.length);
         }
 
+        int kmIndex     = results.columnIndex("km");
+        int chiSqrIndex = results.columnIndex("chi_sqr");
+
         int numFailed = 0;
 
         for (int i = 0; i < kms.length; ++i) {
@@ -209,16 +213,20 @@
                 // TODO: mark as interpolated.
             }
 
+            Pair<double [], Double> fitResult = fit(func, km, ws, qs);
+
             // TODO: Do preprocessing here!
-            double [] parameters = fit(func, km, ws, qs);
-            if (parameters == null) { // Problems are reported already.
+            if (fitResult == null) { // Problems are reported already.
                 ++numFailed;
                 continue;
             }
 
             int row = results.newRow();
 
-            results.set(row, "km", km);
+            results.set(row, kmIndex, km);
+            results.set(row, chiSqrIndex, fitResult.getB());
+
+            double [] parameters = fitResult.getA();
             for (int j = 0; j < parameters.length; ++j) {
                 if (Double.isNaN(parameters[j])) {
                     invalid = true;
@@ -227,7 +235,6 @@
                     results.set(row, parameterNames[j], parameters[j]);
                 }
             }
-            // TODO: Calculate statistics, too!
         }
 
         if (debug) {
@@ -574,14 +581,14 @@
     }
 
     protected static String [] createColumnNames(String [] parameters) {
-        String [] result = new String[parameters.length + 1];
+        String [] result = new String[parameters.length + 2];
         result[0] = "km";
-        // TODO: Add statistic columns, too.
-        System.arraycopy(parameters, 0, result, 1, parameters.length);
+        result[0] = "chi_sqr";
+        System.arraycopy(parameters, 0, result, 2, parameters.length);
         return result;
     }
 
-    protected double [] fit(
+    protected Pair<double [], Double> fit(
         Function  function,
         double    km,
         double [] ws, 
@@ -597,7 +604,11 @@
         }
 
         try {
-            return cf.fit(function, function.getInitialGuess());
+            double [] parameters =
+                cf.fit(function, function.getInitialGuess());
+            double chiSqr = lmo.getChiSquare();
+
+            return new Pair<double [], Double>(parameters, chiSqr);
         }
         catch (MathException me) {
             log.warn(me, me);

http://dive4elements.wald.intevation.org