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
9
* http://www.apache.org/licenses/LICENSE-2.0
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.
18
package org.apache.commons.math.estimation;
20
import org.apache.commons.math.estimation.EstimatedParameter;
21
import org.apache.commons.math.estimation.WeightedMeasurement;
23
import junit.framework.*;
26
public class WeightedMeasurementTest
29
public WeightedMeasurementTest(String name) {
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);
41
public void testIgnored() {
42
WeightedMeasurement m = new MyMeasurement(3.0, theoretical() + 0.1, this);
43
assertTrue(!m.isIgnored());
45
assertTrue(m.isIgnored());
47
assertTrue(!m.isIgnored());
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);
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);
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);
69
public static Test suite() {
70
return new TestSuite(WeightedMeasurementTest.class);
75
p1 = new EstimatedParameter("p1", 1.0);
76
p2 = new EstimatedParameter("p2", 2.0);
80
public void tearDown() {
85
private void checkValue(double value, double expected) {
86
assertTrue(Math.abs(value - expected) < 1.0e-10);
89
private double theoretical() {
90
return 3 * p1.getEstimate() - p2.getEstimate();
93
private double partial(EstimatedParameter p) {
103
private static class MyMeasurement
104
extends WeightedMeasurement {
106
public MyMeasurement(double weight, double measuredValue,
107
WeightedMeasurementTest testInstance) {
108
super(weight, measuredValue);
109
this.testInstance = testInstance;
113
public double getTheoreticalValue() {
114
return testInstance.theoretical();
118
public double getPartial(EstimatedParameter p) {
119
return testInstance.partial(p);
122
private transient WeightedMeasurementTest testInstance;
124
private static final long serialVersionUID = -246712922500792332L;
128
private EstimatedParameter p1;
129
private EstimatedParameter p2;