Mercurial > dive4elements > river
comparison flys-artifacts/src/main/java/org/dive4elements/river/artifacts/model/sq/Fitting.java @ 5831:bd047b71ab37
Repaired internal references
author | Sascha L. Teichmann <teichmann@intevation.de> |
---|---|
date | Thu, 25 Apr 2013 12:06:39 +0200 |
parents | flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/sq/Fitting.java@aaf810d4ec82 |
children |
comparison
equal
deleted
inserted
replaced
5830:160f53ee0870 | 5831:bd047b71ab37 |
---|---|
1 package org.dive4elements.river.artifacts.model.sq; | |
2 | |
3 import org.dive4elements.river.artifacts.math.fitting.Function; | |
4 | |
5 import java.util.ArrayList; | |
6 import java.util.List; | |
7 | |
8 import org.apache.commons.math.MathException; | |
9 | |
10 import org.apache.commons.math.optimization.fitting.CurveFitter; | |
11 | |
12 import org.apache.commons.math.optimization.general.LevenbergMarquardtOptimizer; | |
13 | |
14 import org.apache.log4j.Logger; | |
15 | |
16 public class Fitting | |
17 implements Outlier.Callback | |
18 { | |
19 private static Logger log = Logger.getLogger(Fitting.class); | |
20 | |
21 public interface Callback { | |
22 | |
23 void afterIteration( | |
24 double [] parameters, | |
25 SQ [] measurements, | |
26 SQ [] outliers, | |
27 double standardDeviation, | |
28 double chiSqr); | |
29 } // interfacte | |
30 | |
31 protected Function function; | |
32 | |
33 protected double [] coeffs; | |
34 | |
35 protected org.dive4elements.river.artifacts.math.Function instance; | |
36 | |
37 protected double stdDevFactor; | |
38 protected double chiSqr; | |
39 | |
40 protected Callback callback; | |
41 | |
42 public Fitting() { | |
43 } | |
44 | |
45 public Fitting(Function function, double stdDevFactor) { | |
46 this(); | |
47 this.function = function; | |
48 this.stdDevFactor = stdDevFactor; | |
49 } | |
50 | |
51 public Function getFunction() { | |
52 return function; | |
53 } | |
54 | |
55 public void setFunction(Function function) { | |
56 this.function = function; | |
57 } | |
58 | |
59 public double getStdDevFactor() { | |
60 return stdDevFactor; | |
61 } | |
62 | |
63 public void setStdDevFactor(double stdDevFactor) { | |
64 this.stdDevFactor = stdDevFactor; | |
65 } | |
66 | |
67 @Override | |
68 public void initialize(List<SQ> sqs) throws MathException { | |
69 | |
70 LevenbergMarquardtOptimizer lmo = | |
71 new LevenbergMarquardtOptimizer(); | |
72 | |
73 CurveFitter cf = new CurveFitter(lmo); | |
74 for (SQ sq: sqs) { | |
75 cf.addObservedPoint(sq.getQ(), sq.getS()); | |
76 } | |
77 | |
78 coeffs = cf.fit( | |
79 function, function.getInitialGuess()); | |
80 | |
81 instance = function.instantiate(coeffs); | |
82 | |
83 chiSqr = lmo.getChiSquare(); | |
84 } | |
85 | |
86 @Override | |
87 public double eval(SQ sq) { | |
88 double s = instance.value(sq.q); | |
89 return sq.s - s; | |
90 } | |
91 | |
92 @Override | |
93 public void iterationFinished( | |
94 double standardDeviation, | |
95 SQ outlier, | |
96 List<SQ> remainings | |
97 ) { | |
98 if (log.isDebugEnabled()) { | |
99 log.debug("iterationFinished ----"); | |
100 log.debug(" num remainings: " + remainings.size()); | |
101 log.debug(" has outlier: " + outlier != null); | |
102 log.debug(" standardDeviation: " + standardDeviation); | |
103 log.debug(" Chi^2: " + chiSqr); | |
104 log.debug("---- iterationFinished"); | |
105 } | |
106 callback.afterIteration( | |
107 coeffs, | |
108 remainings.toArray(new SQ[remainings.size()]), | |
109 outlier != null ? new SQ [] { outlier} : new SQ [] {}, | |
110 standardDeviation, | |
111 chiSqr); | |
112 } | |
113 | |
114 protected static final List<SQ> onlyValid(List<SQ> sqs) { | |
115 | |
116 List<SQ> good = new ArrayList<SQ>(sqs.size()); | |
117 | |
118 for (SQ sq: sqs) { | |
119 if (sq.isValid()) { | |
120 good.add(sq); | |
121 } | |
122 } | |
123 | |
124 return good; | |
125 } | |
126 | |
127 public boolean fit(List<SQ> sqs, String method, Callback callback) { | |
128 | |
129 sqs = onlyValid(sqs); | |
130 | |
131 if (sqs.size() < 2) { | |
132 log.warn("Too less points for fitting."); | |
133 return false; | |
134 } | |
135 | |
136 this.callback = callback; | |
137 | |
138 try { | |
139 Outlier.detectOutliers(this, sqs, stdDevFactor, method); | |
140 } | |
141 catch (MathException me) { | |
142 log.warn(me); | |
143 return false; | |
144 } | |
145 | |
146 return true; | |
147 } | |
148 } | |
149 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |