Mercurial > dive4elements > river
comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/sq/Fitting.java @ 3566:8d0f06b76e09
S/Q relation: Fixed flys/issue748
flys-artifacts/trunk@5164 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Wed, 01 Aug 2012 10:19:45 +0000 |
parents | 1df6984628c3 |
children | b8b1280606c2 |
comparison
equal
deleted
inserted
replaced
3565:b136113dad53 | 3566:8d0f06b76e09 |
---|---|
33 | 33 |
34 protected double [] coeffs; | 34 protected double [] coeffs; |
35 | 35 |
36 protected de.intevation.flys.artifacts.math.Function instance; | 36 protected de.intevation.flys.artifacts.math.Function instance; |
37 | 37 |
38 protected List<SQ> remainings; | |
39 protected List<SQ> outliers; | |
40 | |
41 protected double standardDeviation; | |
42 protected double stdDevFactor; | 38 protected double stdDevFactor; |
43 protected double chiSqr; | 39 protected double chiSqr; |
44 | 40 |
45 protected Callback callback; | 41 protected Callback callback; |
46 | 42 |
47 public Fitting() { | 43 public Fitting() { |
48 remainings = new ArrayList<SQ>(); | |
49 outliers = new ArrayList<SQ>(); | |
50 } | 44 } |
51 | 45 |
52 public Fitting(Function function, double stdDevFactor) { | 46 public Fitting(Function function, double stdDevFactor) { |
53 this(); | 47 this(); |
54 this.function = function; | 48 this.function = function; |
70 public void setStdDevFactor(double stdDevFactor) { | 64 public void setStdDevFactor(double stdDevFactor) { |
71 this.stdDevFactor = stdDevFactor; | 65 this.stdDevFactor = stdDevFactor; |
72 } | 66 } |
73 | 67 |
74 @Override | 68 @Override |
75 public void initialize(Iterator<SQ> good) throws MathException { | 69 public void initialize(List<SQ> sqs) throws MathException { |
76 | 70 |
77 LevenbergMarquardtOptimizer lmo = | 71 LevenbergMarquardtOptimizer lmo = |
78 new LevenbergMarquardtOptimizer(); | 72 new LevenbergMarquardtOptimizer(); |
79 | 73 |
80 CurveFitter cf = new CurveFitter(lmo); | 74 CurveFitter cf = new CurveFitter(lmo); |
81 while (good.hasNext()) { | 75 for (SQ sq: sqs) { |
82 SQ sq = good.next(); | |
83 cf.addObservedPoint(sq.getQ(), sq.getS()); | 76 cf.addObservedPoint(sq.getQ(), sq.getS()); |
84 } | 77 } |
85 | 78 |
86 coeffs = cf.fit( | 79 coeffs = cf.fit( |
87 function, function.getInitialGuess()); | 80 function, function.getInitialGuess()); |
88 | 81 |
89 instance = function.instantiate(coeffs); | 82 instance = function.instantiate(coeffs); |
90 | 83 |
91 chiSqr = lmo.getChiSquare(); | 84 chiSqr = lmo.getChiSquare(); |
92 | |
93 } | 85 } |
94 | 86 |
95 @Override | 87 @Override |
96 public double eval(SQ sq) { | 88 public double eval(SQ sq) { |
97 double s = instance.value(sq.q); | 89 double s = instance.value(sq.q); |
98 return sq.s - s; | 90 return sq.s - s; |
99 } | 91 } |
100 | 92 |
101 @Override | 93 @Override |
102 public void outlier(SQ sq) { | 94 public void iterationFinished( |
103 outliers.add(sq); | 95 double standardDeviation, |
104 } | 96 SQ outlier, |
105 | 97 List<SQ> remainings |
106 @Override | 98 ) { |
107 public void remaining(SQ sq) { | |
108 remainings.add(sq); | |
109 } | |
110 | |
111 @Override | |
112 public void standardDeviation(double standardDeviation) { | |
113 this.standardDeviation = standardDeviation; | |
114 } | |
115 | |
116 @Override | |
117 public void iterationFinished() { | |
118 if (log.isDebugEnabled()) { | 99 if (log.isDebugEnabled()) { |
119 log.debug("iterationFinished ----"); | 100 log.debug("iterationFinished ----"); |
120 log.debug(" num remainings: " + remainings.size()); | 101 log.debug(" num remainings: " + remainings.size()); |
121 log.debug(" num outliers: " + outliers.size()); | 102 log.debug(" has outlier: " + outlier != null); |
122 log.debug(" standardDeviation: " + standardDeviation); | 103 log.debug(" standardDeviation: " + standardDeviation); |
123 log.debug(" Chi^2: " + chiSqr); | 104 log.debug(" Chi^2: " + chiSqr); |
124 log.debug("---- iterationFinished"); | 105 log.debug("---- iterationFinished"); |
125 } | 106 } |
126 callback.afterIteration( | 107 callback.afterIteration( |
127 coeffs, | 108 coeffs, |
128 remainings.toArray(new SQ[remainings.size()]), | 109 remainings.toArray(new SQ[remainings.size()]), |
129 outliers.toArray(new SQ[outliers.size()]), | 110 outlier != null ? new SQ [] { outlier} : new SQ [] {}, |
130 standardDeviation, | 111 standardDeviation, |
131 chiSqr); | 112 chiSqr); |
132 remainings.clear(); | |
133 outliers.clear(); | |
134 } | 113 } |
135 | 114 |
136 protected static final List<SQ> onlyValid(List<SQ> sqs) { | 115 protected static final List<SQ> onlyValid(List<SQ> sqs) { |
137 | 116 |
138 List<SQ> good = new ArrayList<SQ>(sqs.size()); | 117 List<SQ> good = new ArrayList<SQ>(sqs.size()); |