annotate artifacts/src/main/java/org/dive4elements/river/artifacts/model/sq/Fitting.java @ 6761:9479cb7c8cd5

flys/issue748: Force linear curve fitting. This is a real hack! Set the system property "minfo.sq.fitting.nonlinear" to re-enable the old behavior.
author Sascha L. Teichmann <teichmann@intevation.de>
date Tue, 06 Aug 2013 17:00:49 +0200
parents af13ceeba52a
children 48f6780c372d
rev   line source
5863
4897a58c8746 River artifacts: Added new copyright headers.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5838
diff changeset
1 /* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde
4897a58c8746 River artifacts: Added new copyright headers.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5838
diff changeset
2 * Software engineering by Intevation GmbH
4897a58c8746 River artifacts: Added new copyright headers.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5838
diff changeset
3 *
5994
af13ceeba52a Removed trailing whitespace.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5863
diff changeset
4 * This file is Free Software under the GNU AGPL (>=v3)
5863
4897a58c8746 River artifacts: Added new copyright headers.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5838
diff changeset
5 * and comes with ABSOLUTELY NO WARRANTY! Check out the
5994
af13ceeba52a Removed trailing whitespace.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5863
diff changeset
6 * documentation coming with Dive4Elements River for details.
5863
4897a58c8746 River artifacts: Added new copyright headers.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5838
diff changeset
7 */
4897a58c8746 River artifacts: Added new copyright headers.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5838
diff changeset
8
5831
bd047b71ab37 Repaired internal references
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5396
diff changeset
9 package org.dive4elements.river.artifacts.model.sq;
3188
1e46ced2bb57 SQ: Added fitting shell for SQ curves.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
10
5831
bd047b71ab37 Repaired internal references
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5396
diff changeset
11 import org.dive4elements.river.artifacts.math.fitting.Function;
6761
9479cb7c8cd5 flys/issue748: Force linear curve fitting. This is a real hack! Set the system property "minfo.sq.fitting.nonlinear" to re-enable the old behavior.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5994
diff changeset
12 import org.dive4elements.river.artifacts.math.fitting.Linear;
3188
1e46ced2bb57 SQ: Added fitting shell for SQ curves.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
13
1e46ced2bb57 SQ: Added fitting shell for SQ curves.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
14 import java.util.ArrayList;
1e46ced2bb57 SQ: Added fitting shell for SQ curves.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
15 import java.util.List;
1e46ced2bb57 SQ: Added fitting shell for SQ curves.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
16
1e46ced2bb57 SQ: Added fitting shell for SQ curves.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
17 import org.apache.commons.math.MathException;
1e46ced2bb57 SQ: Added fitting shell for SQ curves.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
18
1e46ced2bb57 SQ: Added fitting shell for SQ curves.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
19 import org.apache.commons.math.optimization.fitting.CurveFitter;
1e46ced2bb57 SQ: Added fitting shell for SQ curves.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
20
6761
9479cb7c8cd5 flys/issue748: Force linear curve fitting. This is a real hack! Set the system property "minfo.sq.fitting.nonlinear" to re-enable the old behavior.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5994
diff changeset
21 import org.apache.commons.math.optimization.general.AbstractLeastSquaresOptimizer;
9479cb7c8cd5 flys/issue748: Force linear curve fitting. This is a real hack! Set the system property "minfo.sq.fitting.nonlinear" to re-enable the old behavior.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5994
diff changeset
22 import org.apache.commons.math.optimization.general.GaussNewtonOptimizer;
3188
1e46ced2bb57 SQ: Added fitting shell for SQ curves.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
23 import org.apache.commons.math.optimization.general.LevenbergMarquardtOptimizer;
1e46ced2bb57 SQ: Added fitting shell for SQ curves.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
24
1e46ced2bb57 SQ: Added fitting shell for SQ curves.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
25 import org.apache.log4j.Logger;
1e46ced2bb57 SQ: Added fitting shell for SQ curves.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
26
1e46ced2bb57 SQ: Added fitting shell for SQ curves.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
27 public class Fitting
3552
1df6984628c3 S/Q: Extented the result data model of the S/Q calculation to
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3190
diff changeset
28 implements Outlier.Callback
3188
1e46ced2bb57 SQ: Added fitting shell for SQ curves.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
29 {
6761
9479cb7c8cd5 flys/issue748: Force linear curve fitting. This is a real hack! Set the system property "minfo.sq.fitting.nonlinear" to re-enable the old behavior.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5994
diff changeset
30 // XXX: Hack to force linear fitting!
9479cb7c8cd5 flys/issue748: Force linear curve fitting. This is a real hack! Set the system property "minfo.sq.fitting.nonlinear" to re-enable the old behavior.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5994
diff changeset
31 private static final boolean USE_NON_LINEAR_FITTING =
9479cb7c8cd5 flys/issue748: Force linear curve fitting. This is a real hack! Set the system property "minfo.sq.fitting.nonlinear" to re-enable the old behavior.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5994
diff changeset
32 Boolean.getBoolean("minfo.sq.fitting.nonlinear");
9479cb7c8cd5 flys/issue748: Force linear curve fitting. This is a real hack! Set the system property "minfo.sq.fitting.nonlinear" to re-enable the old behavior.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5994
diff changeset
33
3188
1e46ced2bb57 SQ: Added fitting shell for SQ curves.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
34 private static Logger log = Logger.getLogger(Fitting.class);
1e46ced2bb57 SQ: Added fitting shell for SQ curves.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
35
3552
1df6984628c3 S/Q: Extented the result data model of the S/Q calculation to
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3190
diff changeset
36 public interface Callback {
1df6984628c3 S/Q: Extented the result data model of the S/Q calculation to
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3190
diff changeset
37
1df6984628c3 S/Q: Extented the result data model of the S/Q calculation to
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3190
diff changeset
38 void afterIteration(
1df6984628c3 S/Q: Extented the result data model of the S/Q calculation to
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3190
diff changeset
39 double [] parameters,
1df6984628c3 S/Q: Extented the result data model of the S/Q calculation to
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3190
diff changeset
40 SQ [] measurements,
1df6984628c3 S/Q: Extented the result data model of the S/Q calculation to
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3190
diff changeset
41 SQ [] outliers,
1df6984628c3 S/Q: Extented the result data model of the S/Q calculation to
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3190
diff changeset
42 double standardDeviation,
1df6984628c3 S/Q: Extented the result data model of the S/Q calculation to
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3190
diff changeset
43 double chiSqr);
1df6984628c3 S/Q: Extented the result data model of the S/Q calculation to
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3190
diff changeset
44 } // interfacte
1df6984628c3 S/Q: Extented the result data model of the S/Q calculation to
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3190
diff changeset
45
3188
1e46ced2bb57 SQ: Added fitting shell for SQ curves.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
46 protected Function function;
1e46ced2bb57 SQ: Added fitting shell for SQ curves.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
47
3552
1df6984628c3 S/Q: Extented the result data model of the S/Q calculation to
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3190
diff changeset
48 protected double [] coeffs;
3188
1e46ced2bb57 SQ: Added fitting shell for SQ curves.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
49
5831
bd047b71ab37 Repaired internal references
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5396
diff changeset
50 protected org.dive4elements.river.artifacts.math.Function instance;
3552
1df6984628c3 S/Q: Extented the result data model of the S/Q calculation to
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3190
diff changeset
51
1df6984628c3 S/Q: Extented the result data model of the S/Q calculation to
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3190
diff changeset
52 protected double stdDevFactor;
3188
1e46ced2bb57 SQ: Added fitting shell for SQ curves.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
53 protected double chiSqr;
1e46ced2bb57 SQ: Added fitting shell for SQ curves.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
54
3552
1df6984628c3 S/Q: Extented the result data model of the S/Q calculation to
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3190
diff changeset
55 protected Callback callback;
3188
1e46ced2bb57 SQ: Added fitting shell for SQ curves.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
56
1e46ced2bb57 SQ: Added fitting shell for SQ curves.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
57 public Fitting() {
1e46ced2bb57 SQ: Added fitting shell for SQ curves.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
58 }
1e46ced2bb57 SQ: Added fitting shell for SQ curves.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
59
1e46ced2bb57 SQ: Added fitting shell for SQ curves.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
60 public Fitting(Function function, double stdDevFactor) {
3552
1df6984628c3 S/Q: Extented the result data model of the S/Q calculation to
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3190
diff changeset
61 this();
3188
1e46ced2bb57 SQ: Added fitting shell for SQ curves.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
62 this.function = function;
1e46ced2bb57 SQ: Added fitting shell for SQ curves.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
63 this.stdDevFactor = stdDevFactor;
1e46ced2bb57 SQ: Added fitting shell for SQ curves.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
64 }
1e46ced2bb57 SQ: Added fitting shell for SQ curves.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
65
1e46ced2bb57 SQ: Added fitting shell for SQ curves.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
66 public Function getFunction() {
1e46ced2bb57 SQ: Added fitting shell for SQ curves.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
67 return function;
1e46ced2bb57 SQ: Added fitting shell for SQ curves.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
68 }
1e46ced2bb57 SQ: Added fitting shell for SQ curves.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
69
1e46ced2bb57 SQ: Added fitting shell for SQ curves.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
70 public void setFunction(Function function) {
1e46ced2bb57 SQ: Added fitting shell for SQ curves.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
71 this.function = function;
1e46ced2bb57 SQ: Added fitting shell for SQ curves.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
72 }
1e46ced2bb57 SQ: Added fitting shell for SQ curves.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
73
1e46ced2bb57 SQ: Added fitting shell for SQ curves.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
74 public double getStdDevFactor() {
1e46ced2bb57 SQ: Added fitting shell for SQ curves.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
75 return stdDevFactor;
1e46ced2bb57 SQ: Added fitting shell for SQ curves.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
76 }
1e46ced2bb57 SQ: Added fitting shell for SQ curves.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
77
1e46ced2bb57 SQ: Added fitting shell for SQ curves.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
78 public void setStdDevFactor(double stdDevFactor) {
1e46ced2bb57 SQ: Added fitting shell for SQ curves.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
79 this.stdDevFactor = stdDevFactor;
1e46ced2bb57 SQ: Added fitting shell for SQ curves.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
80 }
1e46ced2bb57 SQ: Added fitting shell for SQ curves.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
81
3552
1df6984628c3 S/Q: Extented the result data model of the S/Q calculation to
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3190
diff changeset
82 @Override
3566
8d0f06b76e09 S/Q relation: Fixed flys/issue748
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3552
diff changeset
83 public void initialize(List<SQ> sqs) throws MathException {
3552
1df6984628c3 S/Q: Extented the result data model of the S/Q calculation to
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3190
diff changeset
84
6761
9479cb7c8cd5 flys/issue748: Force linear curve fitting. This is a real hack! Set the system property "minfo.sq.fitting.nonlinear" to re-enable the old behavior.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5994
diff changeset
85 AbstractLeastSquaresOptimizer optimizer = getOptimizer();
3552
1df6984628c3 S/Q: Extented the result data model of the S/Q calculation to
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3190
diff changeset
86
6761
9479cb7c8cd5 flys/issue748: Force linear curve fitting. This is a real hack! Set the system property "minfo.sq.fitting.nonlinear" to re-enable the old behavior.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5994
diff changeset
87 CurveFitter cf = new CurveFitter(optimizer);
9479cb7c8cd5 flys/issue748: Force linear curve fitting. This is a real hack! Set the system property "minfo.sq.fitting.nonlinear" to re-enable the old behavior.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5994
diff changeset
88 double [] values = new double[2];
3566
8d0f06b76e09 S/Q relation: Fixed flys/issue748
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3552
diff changeset
89 for (SQ sq: sqs) {
6761
9479cb7c8cd5 flys/issue748: Force linear curve fitting. This is a real hack! Set the system property "minfo.sq.fitting.nonlinear" to re-enable the old behavior.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5994
diff changeset
90 values[0] = sq.getQ();
9479cb7c8cd5 flys/issue748: Force linear curve fitting. This is a real hack! Set the system property "minfo.sq.fitting.nonlinear" to re-enable the old behavior.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5994
diff changeset
91 values[1] = sq.getS();
9479cb7c8cd5 flys/issue748: Force linear curve fitting. This is a real hack! Set the system property "minfo.sq.fitting.nonlinear" to re-enable the old behavior.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5994
diff changeset
92 transformInputValues(values);
9479cb7c8cd5 flys/issue748: Force linear curve fitting. This is a real hack! Set the system property "minfo.sq.fitting.nonlinear" to re-enable the old behavior.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5994
diff changeset
93 cf.addObservedPoint(values[0], values[1]);
3552
1df6984628c3 S/Q: Extented the result data model of the S/Q calculation to
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3190
diff changeset
94 }
1df6984628c3 S/Q: Extented the result data model of the S/Q calculation to
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3190
diff changeset
95
1df6984628c3 S/Q: Extented the result data model of the S/Q calculation to
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3190
diff changeset
96 coeffs = cf.fit(
1df6984628c3 S/Q: Extented the result data model of the S/Q calculation to
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3190
diff changeset
97 function, function.getInitialGuess());
1df6984628c3 S/Q: Extented the result data model of the S/Q calculation to
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3190
diff changeset
98
6761
9479cb7c8cd5 flys/issue748: Force linear curve fitting. This is a real hack! Set the system property "minfo.sq.fitting.nonlinear" to re-enable the old behavior.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5994
diff changeset
99 transformCoeffsBack(coeffs);
9479cb7c8cd5 flys/issue748: Force linear curve fitting. This is a real hack! Set the system property "minfo.sq.fitting.nonlinear" to re-enable the old behavior.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5994
diff changeset
100
3552
1df6984628c3 S/Q: Extented the result data model of the S/Q calculation to
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3190
diff changeset
101 instance = function.instantiate(coeffs);
1df6984628c3 S/Q: Extented the result data model of the S/Q calculation to
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3190
diff changeset
102
6761
9479cb7c8cd5 flys/issue748: Force linear curve fitting. This is a real hack! Set the system property "minfo.sq.fitting.nonlinear" to re-enable the old behavior.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5994
diff changeset
103 chiSqr = optimizer.getChiSquare();
9479cb7c8cd5 flys/issue748: Force linear curve fitting. This is a real hack! Set the system property "minfo.sq.fitting.nonlinear" to re-enable the old behavior.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5994
diff changeset
104 }
9479cb7c8cd5 flys/issue748: Force linear curve fitting. This is a real hack! Set the system property "minfo.sq.fitting.nonlinear" to re-enable the old behavior.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5994
diff changeset
105
9479cb7c8cd5 flys/issue748: Force linear curve fitting. This is a real hack! Set the system property "minfo.sq.fitting.nonlinear" to re-enable the old behavior.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5994
diff changeset
106 protected Function getFunction(Function function) {
9479cb7c8cd5 flys/issue748: Force linear curve fitting. This is a real hack! Set the system property "minfo.sq.fitting.nonlinear" to re-enable the old behavior.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5994
diff changeset
107 return USE_NON_LINEAR_FITTING
9479cb7c8cd5 flys/issue748: Force linear curve fitting. This is a real hack! Set the system property "minfo.sq.fitting.nonlinear" to re-enable the old behavior.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5994
diff changeset
108 ? function
9479cb7c8cd5 flys/issue748: Force linear curve fitting. This is a real hack! Set the system property "minfo.sq.fitting.nonlinear" to re-enable the old behavior.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5994
diff changeset
109 : Linear.INSTANCE;
9479cb7c8cd5 flys/issue748: Force linear curve fitting. This is a real hack! Set the system property "minfo.sq.fitting.nonlinear" to re-enable the old behavior.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5994
diff changeset
110 }
9479cb7c8cd5 flys/issue748: Force linear curve fitting. This is a real hack! Set the system property "minfo.sq.fitting.nonlinear" to re-enable the old behavior.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5994
diff changeset
111
9479cb7c8cd5 flys/issue748: Force linear curve fitting. This is a real hack! Set the system property "minfo.sq.fitting.nonlinear" to re-enable the old behavior.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5994
diff changeset
112 protected void transformInputValues(double [] values) {
9479cb7c8cd5 flys/issue748: Force linear curve fitting. This is a real hack! Set the system property "minfo.sq.fitting.nonlinear" to re-enable the old behavior.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5994
diff changeset
113 if (!USE_NON_LINEAR_FITTING) {
9479cb7c8cd5 flys/issue748: Force linear curve fitting. This is a real hack! Set the system property "minfo.sq.fitting.nonlinear" to re-enable the old behavior.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5994
diff changeset
114 for (int i = 0; i < values.length; ++i) {
9479cb7c8cd5 flys/issue748: Force linear curve fitting. This is a real hack! Set the system property "minfo.sq.fitting.nonlinear" to re-enable the old behavior.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5994
diff changeset
115 values[i] = Math.log(values[i]);
9479cb7c8cd5 flys/issue748: Force linear curve fitting. This is a real hack! Set the system property "minfo.sq.fitting.nonlinear" to re-enable the old behavior.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5994
diff changeset
116 }
9479cb7c8cd5 flys/issue748: Force linear curve fitting. This is a real hack! Set the system property "minfo.sq.fitting.nonlinear" to re-enable the old behavior.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5994
diff changeset
117 }
9479cb7c8cd5 flys/issue748: Force linear curve fitting. This is a real hack! Set the system property "minfo.sq.fitting.nonlinear" to re-enable the old behavior.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5994
diff changeset
118 }
9479cb7c8cd5 flys/issue748: Force linear curve fitting. This is a real hack! Set the system property "minfo.sq.fitting.nonlinear" to re-enable the old behavior.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5994
diff changeset
119
9479cb7c8cd5 flys/issue748: Force linear curve fitting. This is a real hack! Set the system property "minfo.sq.fitting.nonlinear" to re-enable the old behavior.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5994
diff changeset
120 protected AbstractLeastSquaresOptimizer getOptimizer() {
9479cb7c8cd5 flys/issue748: Force linear curve fitting. This is a real hack! Set the system property "minfo.sq.fitting.nonlinear" to re-enable the old behavior.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5994
diff changeset
121 return USE_NON_LINEAR_FITTING
9479cb7c8cd5 flys/issue748: Force linear curve fitting. This is a real hack! Set the system property "minfo.sq.fitting.nonlinear" to re-enable the old behavior.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5994
diff changeset
122 ? new LevenbergMarquardtOptimizer()
9479cb7c8cd5 flys/issue748: Force linear curve fitting. This is a real hack! Set the system property "minfo.sq.fitting.nonlinear" to re-enable the old behavior.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5994
diff changeset
123 : new GaussNewtonOptimizer(false);
9479cb7c8cd5 flys/issue748: Force linear curve fitting. This is a real hack! Set the system property "minfo.sq.fitting.nonlinear" to re-enable the old behavior.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5994
diff changeset
124 }
9479cb7c8cd5 flys/issue748: Force linear curve fitting. This is a real hack! Set the system property "minfo.sq.fitting.nonlinear" to re-enable the old behavior.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5994
diff changeset
125
9479cb7c8cd5 flys/issue748: Force linear curve fitting. This is a real hack! Set the system property "minfo.sq.fitting.nonlinear" to re-enable the old behavior.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5994
diff changeset
126 protected void transformCoeffsBack(double [] coeffs) {
9479cb7c8cd5 flys/issue748: Force linear curve fitting. This is a real hack! Set the system property "minfo.sq.fitting.nonlinear" to re-enable the old behavior.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5994
diff changeset
127 if (!USE_NON_LINEAR_FITTING && coeffs.length > 0) {
9479cb7c8cd5 flys/issue748: Force linear curve fitting. This is a real hack! Set the system property "minfo.sq.fitting.nonlinear" to re-enable the old behavior.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5994
diff changeset
128 coeffs[0] = Math.exp(coeffs[0]);
9479cb7c8cd5 flys/issue748: Force linear curve fitting. This is a real hack! Set the system property "minfo.sq.fitting.nonlinear" to re-enable the old behavior.
Sascha L. Teichmann <teichmann@intevation.de>
parents: 5994
diff changeset
129 }
3188
1e46ced2bb57 SQ: Added fitting shell for SQ curves.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
130 }
1e46ced2bb57 SQ: Added fitting shell for SQ curves.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
131
3552
1df6984628c3 S/Q: Extented the result data model of the S/Q calculation to
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3190
diff changeset
132 @Override
1df6984628c3 S/Q: Extented the result data model of the S/Q calculation to
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3190
diff changeset
133 public double eval(SQ sq) {
1df6984628c3 S/Q: Extented the result data model of the S/Q calculation to
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3190
diff changeset
134 double s = instance.value(sq.q);
1df6984628c3 S/Q: Extented the result data model of the S/Q calculation to
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3190
diff changeset
135 return sq.s - s;
1df6984628c3 S/Q: Extented the result data model of the S/Q calculation to
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3190
diff changeset
136 }
1df6984628c3 S/Q: Extented the result data model of the S/Q calculation to
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3190
diff changeset
137
1df6984628c3 S/Q: Extented the result data model of the S/Q calculation to
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3190
diff changeset
138 @Override
3566
8d0f06b76e09 S/Q relation: Fixed flys/issue748
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3552
diff changeset
139 public void iterationFinished(
8d0f06b76e09 S/Q relation: Fixed flys/issue748
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3552
diff changeset
140 double standardDeviation,
8d0f06b76e09 S/Q relation: Fixed flys/issue748
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3552
diff changeset
141 SQ outlier,
8d0f06b76e09 S/Q relation: Fixed flys/issue748
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3552
diff changeset
142 List<SQ> remainings
8d0f06b76e09 S/Q relation: Fixed flys/issue748
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3552
diff changeset
143 ) {
3552
1df6984628c3 S/Q: Extented the result data model of the S/Q calculation to
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3190
diff changeset
144 if (log.isDebugEnabled()) {
1df6984628c3 S/Q: Extented the result data model of the S/Q calculation to
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3190
diff changeset
145 log.debug("iterationFinished ----");
1df6984628c3 S/Q: Extented the result data model of the S/Q calculation to
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3190
diff changeset
146 log.debug(" num remainings: " + remainings.size());
3566
8d0f06b76e09 S/Q relation: Fixed flys/issue748
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3552
diff changeset
147 log.debug(" has outlier: " + outlier != null);
3552
1df6984628c3 S/Q: Extented the result data model of the S/Q calculation to
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3190
diff changeset
148 log.debug(" standardDeviation: " + standardDeviation);
1df6984628c3 S/Q: Extented the result data model of the S/Q calculation to
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3190
diff changeset
149 log.debug(" Chi^2: " + chiSqr);
1df6984628c3 S/Q: Extented the result data model of the S/Q calculation to
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3190
diff changeset
150 log.debug("---- iterationFinished");
1df6984628c3 S/Q: Extented the result data model of the S/Q calculation to
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3190
diff changeset
151 }
1df6984628c3 S/Q: Extented the result data model of the S/Q calculation to
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3190
diff changeset
152 callback.afterIteration(
1df6984628c3 S/Q: Extented the result data model of the S/Q calculation to
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3190
diff changeset
153 coeffs,
1df6984628c3 S/Q: Extented the result data model of the S/Q calculation to
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3190
diff changeset
154 remainings.toArray(new SQ[remainings.size()]),
3566
8d0f06b76e09 S/Q relation: Fixed flys/issue748
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3552
diff changeset
155 outlier != null ? new SQ [] { outlier} : new SQ [] {},
3552
1df6984628c3 S/Q: Extented the result data model of the S/Q calculation to
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3190
diff changeset
156 standardDeviation,
1df6984628c3 S/Q: Extented the result data model of the S/Q calculation to
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3190
diff changeset
157 chiSqr);
3188
1e46ced2bb57 SQ: Added fitting shell for SQ curves.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
158 }
1e46ced2bb57 SQ: Added fitting shell for SQ curves.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
159
1e46ced2bb57 SQ: Added fitting shell for SQ curves.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
160 protected static final List<SQ> onlyValid(List<SQ> sqs) {
1e46ced2bb57 SQ: Added fitting shell for SQ curves.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
161
1e46ced2bb57 SQ: Added fitting shell for SQ curves.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
162 List<SQ> good = new ArrayList<SQ>(sqs.size());
1e46ced2bb57 SQ: Added fitting shell for SQ curves.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
163
1e46ced2bb57 SQ: Added fitting shell for SQ curves.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
164 for (SQ sq: sqs) {
1e46ced2bb57 SQ: Added fitting shell for SQ curves.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
165 if (sq.isValid()) {
1e46ced2bb57 SQ: Added fitting shell for SQ curves.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
166 good.add(sq);
1e46ced2bb57 SQ: Added fitting shell for SQ curves.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
167 }
1e46ced2bb57 SQ: Added fitting shell for SQ curves.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
168 }
1e46ced2bb57 SQ: Added fitting shell for SQ curves.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
169
1e46ced2bb57 SQ: Added fitting shell for SQ curves.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
170 return good;
1e46ced2bb57 SQ: Added fitting shell for SQ curves.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
171 }
1e46ced2bb57 SQ: Added fitting shell for SQ curves.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
172
5396
aaf810d4ec82 Use new state 'outlier mehtod selection' in SQ-relation.
Raimund Renkert <rrenkert@intevation.de>
parents: 3572
diff changeset
173 public boolean fit(List<SQ> sqs, String method, Callback callback) {
3188
1e46ced2bb57 SQ: Added fitting shell for SQ curves.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
174
1e46ced2bb57 SQ: Added fitting shell for SQ curves.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
175 sqs = onlyValid(sqs);
1e46ced2bb57 SQ: Added fitting shell for SQ curves.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
176
1e46ced2bb57 SQ: Added fitting shell for SQ curves.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
177 if (sqs.size() < 2) {
1e46ced2bb57 SQ: Added fitting shell for SQ curves.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
178 log.warn("Too less points for fitting.");
1e46ced2bb57 SQ: Added fitting shell for SQ curves.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
179 return false;
1e46ced2bb57 SQ: Added fitting shell for SQ curves.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
180 }
1e46ced2bb57 SQ: Added fitting shell for SQ curves.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
181
3552
1df6984628c3 S/Q: Extented the result data model of the S/Q calculation to
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents: 3190
diff changeset
182 this.callback = callback;
3188
1e46ced2bb57 SQ: Added fitting shell for SQ curves.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
183
1e46ced2bb57 SQ: Added fitting shell for SQ curves.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
184 try {
5396
aaf810d4ec82 Use new state 'outlier mehtod selection' in SQ-relation.
Raimund Renkert <rrenkert@intevation.de>
parents: 3572
diff changeset
185 Outlier.detectOutliers(this, sqs, stdDevFactor, method);
3188
1e46ced2bb57 SQ: Added fitting shell for SQ curves.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
186 }
1e46ced2bb57 SQ: Added fitting shell for SQ curves.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
187 catch (MathException me) {
1e46ced2bb57 SQ: Added fitting shell for SQ curves.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
188 log.warn(me);
1e46ced2bb57 SQ: Added fitting shell for SQ curves.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
189 return false;
1e46ced2bb57 SQ: Added fitting shell for SQ curves.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
190 }
1e46ced2bb57 SQ: Added fitting shell for SQ curves.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
191
1e46ced2bb57 SQ: Added fitting shell for SQ curves.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
192 return true;
1e46ced2bb57 SQ: Added fitting shell for SQ curves.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
193 }
1e46ced2bb57 SQ: Added fitting shell for SQ curves.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
194 }
1e46ced2bb57 SQ: Added fitting shell for SQ curves.
Sascha L. Teichmann <sascha.teichmann@intevation.de>
parents:
diff changeset
195 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org