1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.math.estimation;
19  
20  import org.apache.commons.math.estimation.EstimatedParameter;
21  import org.apache.commons.math.estimation.WeightedMeasurement;
22  
23  import junit.framework.*;
24  
25  @Deprecated
26  public class WeightedMeasurementTest
27    extends TestCase {
28  
29    public WeightedMeasurementTest(String name) {
30      super(name);
31      p1 = null;
32      p2 = null;
33    }
34  
35    public void testConstruction() {
36      WeightedMeasurement m = new MyMeasurement(3.0, theoretical() + 0.1, this);
37      checkValue(m.getWeight(), 3.0);
38      checkValue(m.getMeasuredValue(), theoretical() + 0.1);
39    }
40  
41    public void testIgnored() {
42      WeightedMeasurement m = new MyMeasurement(3.0, theoretical() + 0.1, this);
43      assertTrue(!m.isIgnored());
44      m.setIgnored(true);
45      assertTrue(m.isIgnored());
46      m.setIgnored(false);
47      assertTrue(!m.isIgnored());
48    }
49  
50    public void testTheory() {
51      WeightedMeasurement m = new MyMeasurement(3.0, theoretical() + 0.1, this);
52      checkValue(m.getTheoreticalValue(), theoretical());
53      checkValue(m.getResidual(), 0.1);
54  
55      double oldP1 = p1.getEstimate();
56      p1.setEstimate(oldP1 + m.getResidual() / m.getPartial(p1));
57      checkValue(m.getResidual(), 0.0);
58      p1.setEstimate(oldP1);
59      checkValue(m.getResidual(), 0.1);
60  
61      double oldP2 = p2.getEstimate();
62      p2.setEstimate(oldP2 + m.getResidual() / m.getPartial(p2));
63      checkValue(m.getResidual(), 0.0);
64      p2.setEstimate(oldP2);
65      checkValue(m.getResidual(), 0.1);
66  
67    }
68  
69    public static Test suite() {
70      return new TestSuite(WeightedMeasurementTest.class);
71    }
72  
73    @Override
74    public void setUp() {
75      p1 = new EstimatedParameter("p1", 1.0);
76      p2 = new EstimatedParameter("p2", 2.0);
77    }
78  
79    @Override
80    public void tearDown() {
81      p1 = null;
82      p2 = null;
83    }
84  
85    private void checkValue(double value, double expected) {
86     assertTrue(Math.abs(value - expected) < 1.0e-10);
87    }
88  
89    private double theoretical() {
90     return 3 * p1.getEstimate() - p2.getEstimate();
91    }
92  
93    private double partial(EstimatedParameter p) {
94      if (p == p1) {
95        return 3.0;
96      } else if (p == p2) {
97        return -1.0;
98      } else {
99        return 0.0;
100     }
101   }
102 
103   private static class MyMeasurement
104     extends WeightedMeasurement {
105 
106     public MyMeasurement(double weight, double measuredValue,
107                          WeightedMeasurementTest testInstance) {
108       super(weight, measuredValue);
109       this.testInstance = testInstance;
110     }
111 
112     @Override
113     public double getTheoreticalValue() {
114       return testInstance.theoretical();
115     }
116 
117     @Override
118     public double getPartial(EstimatedParameter p) {
119       return testInstance.partial(p);
120     }
121 
122     private transient WeightedMeasurementTest testInstance;
123 
124     private static final long serialVersionUID = -246712922500792332L;
125 
126   }
127 
128   private EstimatedParameter p1;
129   private EstimatedParameter p2;
130 
131 }