1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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 public class WeightedMeasurementTest
26 extends TestCase {
27
28 public WeightedMeasurementTest(String name) {
29 super(name);
30 p1 = null;
31 p2 = null;
32 }
33
34 public void testConstruction() {
35 WeightedMeasurement m = new MyMeasurement(3.0, theoretical() + 0.1, this);
36 checkValue(m.getWeight(), 3.0);
37 checkValue(m.getMeasuredValue(), theoretical() + 0.1);
38 }
39
40 public void testIgnored() {
41 WeightedMeasurement m = new MyMeasurement(3.0, theoretical() + 0.1, this);
42 assertTrue(!m.isIgnored());
43 m.setIgnored(true);
44 assertTrue(m.isIgnored());
45 m.setIgnored(false);
46 assertTrue(!m.isIgnored());
47 }
48
49 public void testTheory() {
50 WeightedMeasurement m = new MyMeasurement(3.0, theoretical() + 0.1, this);
51 checkValue(m.getTheoreticalValue(), theoretical());
52 checkValue(m.getResidual(), 0.1);
53
54 double oldP1 = p1.getEstimate();
55 p1.setEstimate(oldP1 + m.getResidual() / m.getPartial(p1));
56 checkValue(m.getResidual(), 0.0);
57 p1.setEstimate(oldP1);
58 checkValue(m.getResidual(), 0.1);
59
60 double oldP2 = p2.getEstimate();
61 p2.setEstimate(oldP2 + m.getResidual() / m.getPartial(p2));
62 checkValue(m.getResidual(), 0.0);
63 p2.setEstimate(oldP2);
64 checkValue(m.getResidual(), 0.1);
65
66 }
67
68 public static Test suite() {
69 return new TestSuite(WeightedMeasurementTest.class);
70 }
71
72 public void setUp() {
73 p1 = new EstimatedParameter("p1", 1.0);
74 p2 = new EstimatedParameter("p2", 2.0);
75 }
76
77 public void tearDown() {
78 p1 = null;
79 p2 = null;
80 }
81
82 private void checkValue(double value, double expected) {
83 assertTrue(Math.abs(value - expected) < 1.0e-10);
84 }
85
86 private double theoretical() {
87 return 3 * p1.getEstimate() - p2.getEstimate();
88 }
89
90 private double partial(EstimatedParameter p) {
91 if (p == p1) {
92 return 3.0;
93 } else if (p == p2) {
94 return -1.0;
95 } else {
96 return 0.0;
97 }
98 }
99
100 private static class MyMeasurement
101 extends WeightedMeasurement {
102
103 public MyMeasurement(double weight, double measuredValue,
104 WeightedMeasurementTest testInstance) {
105 super(weight, measuredValue);
106 this.testInstance = testInstance;
107 }
108
109 public double getTheoreticalValue() {
110 return testInstance.theoretical();
111 }
112
113 public double getPartial(EstimatedParameter p) {
114 return testInstance.partial(p);
115 }
116
117 private transient WeightedMeasurementTest testInstance;
118
119 private static final long serialVersionUID = -246712922500792332L;
120
121 }
122
123 private EstimatedParameter p1;
124 private EstimatedParameter p2;
125
126 }