1dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond/*
2dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * Licensed to the Apache Software Foundation (ASF) under one or more
3dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * contributor license agreements.  See the NOTICE file distributed with
4dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * this work for additional information regarding copyright ownership.
5dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * The ASF licenses this file to You under the Apache License, Version 2.0
6dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * (the "License"); you may not use this file except in compliance with
7dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * the License.  You may obtain a copy of the License at
8dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond *
9dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond *      http://www.apache.org/licenses/LICENSE-2.0
10dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond *
11dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * Unless required by applicable law or agreed to in writing, software
12dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * distributed under the License is distributed on an "AS IS" BASIS,
13dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * See the License for the specific language governing permissions and
15dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * limitations under the License.
16dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond */
17dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
18dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondpackage org.apache.commons.math.estimation;
19dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
20dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondimport java.io.Serializable;
21dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
22dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond/**
23dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * This class represents measurements in estimation problems.
24dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond *
25dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <p>This abstract class implements all the methods needed to handle
26dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * measurements in a general way. It defines neither the {@link
27dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * #getTheoreticalValue getTheoreticalValue} nor the {@link
28dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * #getPartial getPartial} methods, which should be defined by
29dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * sub-classes according to the specific problem.</p>
30dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond *
31dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <p>The {@link #getTheoreticalValue getTheoreticalValue} and {@link
32dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * #getPartial getPartial} methods must always use the current
33dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * estimate of the parameters set by the solver in the problem. These
34dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * parameters can be retrieved through the {@link
35dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * EstimationProblem#getAllParameters
36dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * EstimationProblem.getAllParameters} method if the measurements are
37dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * independent of the problem, or directly if they are implemented as
38dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * inner classes of the problem.</p>
39dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond *
40dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <p>The instances for which the <code>ignored</code> flag is set
41dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * through the {@link #setIgnored setIgnored} method are ignored by the
42dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * solvers. This can be used to reject wrong measurements at some
43dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * steps of the estimation.</p>
44dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond *
45dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @see EstimationProblem
46dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond *
47dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @version $Revision: 811827 $ $Date: 2009-09-06 17:32:50 +0200 (dim. 06 sept. 2009) $
48dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @since 1.2
49dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @deprecated as of 2.0, everything in package org.apache.commons.math.estimation has
50dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * been deprecated and replaced by package org.apache.commons.math.optimization.general
51dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond */
52dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
53dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond@Deprecated
54dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondpublic abstract class WeightedMeasurement implements Serializable {
55dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
56dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /** Serializable version identifier. */
57dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    private static final long serialVersionUID = 4360046376796901941L;
58dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
59dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /** Measurement weight. */
60dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    private final double  weight;
61dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
62dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /** Value of the measurements. */
63dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    private final double  measuredValue;
64dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
65dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /** Ignore measurement indicator. */
66dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    private boolean ignored;
67dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
68dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond  /**
69dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond   * Simple constructor.
70dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond   * Build a measurement with the given parameters, and set its ignore
71dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond   * flag to false.
72dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond   * @param weight weight of the measurement in the least squares problem
73dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond   * (two common choices are either to use 1.0 for all measurements, or to
74dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond   * use a value proportional to the inverse of the variance of the measurement
75dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond   * type)
76dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond   *
77dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond   * @param measuredValue measured value
78dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond   */
79dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond  public WeightedMeasurement(double weight, double measuredValue) {
80dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    this.weight        = weight;
81dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    this.measuredValue = measuredValue;
82dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    ignored            = false;
83dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond  }
84dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
85dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond  /** Simple constructor.
86dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond   *
87dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond   * Build a measurement with the given parameters
88dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond   *
89dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond   * @param weight weight of the measurement in the least squares problem
90dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond   * @param measuredValue measured value
91dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond   * @param ignored true if the measurement should be ignored
92dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond   */
93dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond  public WeightedMeasurement(double weight, double measuredValue,
94dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                             boolean ignored) {
95dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    this.weight        = weight;
96dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    this.measuredValue = measuredValue;
97dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    this.ignored       = ignored;
98dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond  }
99dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
100dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond  /**
101dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond   * Get the weight of the measurement in the least squares problem
102dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond   *
103dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond   * @return weight
104dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond   */
105dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond  public double getWeight() {
106dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    return weight;
107dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond  }
108dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
109dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond  /**
110dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond   * Get the measured value
111dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond   *
112dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond   * @return measured value
113dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond   */
114dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond  public double getMeasuredValue() {
115dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    return measuredValue;
116dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond  }
117dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
118dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond  /**
119dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond   * Get the residual for this measurement
120dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond   * The residual is the measured value minus the theoretical value.
121dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond   *
122dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond   * @return residual
123dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond   */
124dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond  public double getResidual() {
125dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    return measuredValue - getTheoreticalValue();
126dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond  }
127dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
128dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond  /**
129dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond   * Get the theoretical value expected for this measurement
130dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond   * <p>The theoretical value is the value expected for this measurement
131dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond   * if the model and its parameter were all perfectly known.</p>
132dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond   * <p>The value must be computed using the current estimate of the parameters
133dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond   * set by the solver in the problem.</p>
134dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond   *
135dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond   * @return theoretical value
136dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond   */
137dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond  public abstract double getTheoreticalValue();
138dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
139dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond  /**
140dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond   * Get the partial derivative of the {@link #getTheoreticalValue
141dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond   * theoretical value} according to the parameter.
142dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond   * <p>The value must be computed using the current estimate of the parameters
143dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond   * set by the solver in the problem.</p>
144dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond   *
145dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond   * @param parameter parameter against which the partial derivative
146dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond   * should be computed
147dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond   * @return partial derivative of the {@link #getTheoreticalValue
148dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond   * theoretical value}
149dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond   */
150dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond  public abstract double getPartial(EstimatedParameter parameter);
151dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
152dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond  /**
153dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond   * Set the ignore flag to the specified value
154dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond   * Setting the ignore flag to true allow to reject wrong
155dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond   * measurements, which sometimes can be detected only rather late.
156dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond   *
157dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond   * @param ignored value for the ignore flag
158dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond   */
159dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond  public void setIgnored(boolean ignored) {
160dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    this.ignored = ignored;
161dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond  }
162dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
163dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond  /**
164dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond   * Check if this measurement should be ignored
165dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond   *
166dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond   * @return true if the measurement should be ignored
167dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond   */
168dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond  public boolean isIgnored() {
169dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    return ignored;
170dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond  }
171dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
172dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond}
173