WeightedObservedPoints.java

  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. package org.apache.commons.math3.fitting;

  18. import java.util.List;
  19. import java.util.ArrayList;
  20. import java.io.Serializable;

  21. /**
  22.  * Simple container for weighted observed points used
  23.  * in {@link AbstractCurveFitter curve fitting} algorithms.
  24.  *
  25.  * @since 3.3
  26.  */
  27. public class WeightedObservedPoints implements Serializable {
  28.     /** Serializable version id. */
  29.     private static final long serialVersionUID = 20130813L;

  30.     /** Observed points. */
  31.     private final List<WeightedObservedPoint> observations
  32.         = new ArrayList<WeightedObservedPoint>();

  33.     /**
  34.      * Adds a point to the sample.
  35.      * Calling this method is equivalent to calling
  36.      * {@code add(1.0, x, y)}.
  37.      *
  38.      * @param x Abscissa of the point.
  39.      * @param y Observed value  at {@code x}. After fitting we should
  40.      * have {@code f(x)} as close as possible to this value.
  41.      *
  42.      * @see #add(double, double, double)
  43.      * @see #add(WeightedObservedPoint)
  44.      * @see #toList()
  45.      */
  46.     public void add(double x, double y) {
  47.         add(1d, x, y);
  48.     }

  49.     /**
  50.      * Adds a point to the sample.
  51.      *
  52.      * @param weight Weight of the observed point.
  53.      * @param x Abscissa of the point.
  54.      * @param y Observed value  at {@code x}. After fitting we should
  55.      * have {@code f(x)} as close as possible to this value.
  56.      *
  57.      * @see #add(double, double)
  58.      * @see #add(WeightedObservedPoint)
  59.      * @see #toList()
  60.      */
  61.     public void add(double weight, double x, double y) {
  62.         observations.add(new WeightedObservedPoint(weight, x, y));
  63.     }

  64.     /**
  65.      * Adds a point to the sample.
  66.      *
  67.      * @param observed Observed point to add.
  68.      *
  69.      * @see #add(double, double)
  70.      * @see #add(double, double, double)
  71.      * @see #toList()
  72.      */
  73.     public void add(WeightedObservedPoint observed) {
  74.         observations.add(observed);
  75.     }

  76.     /**
  77.      * Gets a <em>snapshot</em> of the observed points.
  78.      * The list of stored points is copied in order to ensure that
  79.      * modification of the returned instance does not affect this
  80.      * container.
  81.      * Conversely, further modification of this container (through
  82.      * the {@code add} or {@code clear} methods) will not affect the
  83.      * returned list.
  84.      *
  85.      * @return the observed points, in the order they were added to this
  86.      * container.
  87.      *
  88.      * @see #add(double, double)
  89.      * @see #add(double, double, double)
  90.      * @see #add(WeightedObservedPoint)
  91.      */
  92.     public List<WeightedObservedPoint> toList() {
  93.         // The copy is necessary to ensure thread-safety because of the
  94.         // "clear" method (which otherwise would be able to empty the
  95.         // list of points while it is being used by another thread).
  96.         return new ArrayList<WeightedObservedPoint>(observations);
  97.     }

  98.     /**
  99.      * Removes all observations from this container.
  100.      */
  101.     public void clear() {
  102.         observations.clear();
  103.     }
  104. }