001 /* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 018 package org.apache.commons.math.estimation; 019 020 import org.apache.commons.math.estimation.EstimatedParameter; 021 import org.apache.commons.math.estimation.WeightedMeasurement; 022 023 import junit.framework.*; 024 025 @Deprecated 026 public class WeightedMeasurementTest 027 extends TestCase { 028 029 public WeightedMeasurementTest(String name) { 030 super(name); 031 p1 = null; 032 p2 = null; 033 } 034 035 public void testConstruction() { 036 WeightedMeasurement m = new MyMeasurement(3.0, theoretical() + 0.1, this); 037 checkValue(m.getWeight(), 3.0); 038 checkValue(m.getMeasuredValue(), theoretical() + 0.1); 039 } 040 041 public void testIgnored() { 042 WeightedMeasurement m = new MyMeasurement(3.0, theoretical() + 0.1, this); 043 assertTrue(!m.isIgnored()); 044 m.setIgnored(true); 045 assertTrue(m.isIgnored()); 046 m.setIgnored(false); 047 assertTrue(!m.isIgnored()); 048 } 049 050 public void testTheory() { 051 WeightedMeasurement m = new MyMeasurement(3.0, theoretical() + 0.1, this); 052 checkValue(m.getTheoreticalValue(), theoretical()); 053 checkValue(m.getResidual(), 0.1); 054 055 double oldP1 = p1.getEstimate(); 056 p1.setEstimate(oldP1 + m.getResidual() / m.getPartial(p1)); 057 checkValue(m.getResidual(), 0.0); 058 p1.setEstimate(oldP1); 059 checkValue(m.getResidual(), 0.1); 060 061 double oldP2 = p2.getEstimate(); 062 p2.setEstimate(oldP2 + m.getResidual() / m.getPartial(p2)); 063 checkValue(m.getResidual(), 0.0); 064 p2.setEstimate(oldP2); 065 checkValue(m.getResidual(), 0.1); 066 067 } 068 069 public static Test suite() { 070 return new TestSuite(WeightedMeasurementTest.class); 071 } 072 073 @Override 074 public void setUp() { 075 p1 = new EstimatedParameter("p1", 1.0); 076 p2 = new EstimatedParameter("p2", 2.0); 077 } 078 079 @Override 080 public void tearDown() { 081 p1 = null; 082 p2 = null; 083 } 084 085 private void checkValue(double value, double expected) { 086 assertTrue(Math.abs(value - expected) < 1.0e-10); 087 } 088 089 private double theoretical() { 090 return 3 * p1.getEstimate() - p2.getEstimate(); 091 } 092 093 private double partial(EstimatedParameter p) { 094 if (p == p1) { 095 return 3.0; 096 } else if (p == p2) { 097 return -1.0; 098 } else { 099 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 }