MATH-1014

Container for storing observations, to allow separating the curve fitting
algorithms from the data to be fitted.


git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1516059 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Gilles Sadowski 2013-08-21 02:03:30 +00:00
parent 897f5e29b9
commit 0155238a3c
2 changed files with 239 additions and 0 deletions

View File

@ -0,0 +1,113 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.math3.fitting;
import java.util.List;
import java.util.ArrayList;
import java.io.Serializable;
/**
* Simple container for weighted observed points used
* in {@link AbstractCurveFitter curve fitting} algorithms.
*
* @version $Id$
* @since 3.3
*/
public class WeightedObservedPoints implements Serializable {
/** Serializable version id. */
private static final long serialVersionUID = 20130813L;
/** Observed points. */
private final List<WeightedObservedPoint> observations
= new ArrayList<WeightedObservedPoint>();
/**
* Adds a point to the sample.
* Calling this method is equivalent to calling
* {@code add(1.0, x, y)}.
*
* @param x Abscissa of the point.
* @param y Observed value at {@code x}. After fitting we should
* have {@code f(x)} as close as possible to this value.
*
* @see #add(double, double, double)
* @see #add(WeightedObservedPoint)
* @see #getObservations()
*/
public void add(double x, double y) {
add(1d, x, y);
}
/**
* Adds a point to the sample.
*
* @param weight Weight of the observed point.
* @param x Abscissa of the point.
* @param y Observed value at {@code x}. After fitting we should
* have {@code f(x)} as close as possible to this value.
*
* @see #add(double, double)
* @see #add(WeightedObservedPoint)
* @see #getObservations()
*/
public void add(double weight, double x, double y) {
observations.add(new WeightedObservedPoint(weight, x, y));
}
/**
* Adds a point to the sample.
*
* @param observed Observed point to add.
*
* @see #add(double, double)
* @see #add(double, double, double)
* @see #getObservations()
*/
public void add(WeightedObservedPoint observed) {
observations.add(observed);
}
/**
* Gets a <em>snapshot</em> of the observed points.
* The list of stored points is copied in order to ensure that
* modification of the returned instance does not affect this
* container.
* Conversely, further modification of this container (through
* the {@code add} or {@code clear} methods) will not affect the
* returned list.
*
* @return the observed points, in the order they were added to this
* container.
*
* @see #add(double, double)
* @see #add(double, double, double)
* @see #add(WeightedObservedPoint)
*/
public List<WeightedObservedPoint> toList() {
// The copy is necessary to ensure thread-safety because of the
// "clear" method (which otherwise would be able to empty the
// list of points while it is being used by another thread).
return new ArrayList<WeightedObservedPoint>(observations);
}
/**
* Removes all observations from this container.
*/
public void clear() {
observations.clear();
}
}

View File

@ -0,0 +1,126 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.math3.fitting;
import java.util.List;
import org.junit.Assert;
import org.junit.Test;
import org.apache.commons.math3.util.Precision;
/**
* Tests {@link WeightedObservedPoints}.
*
* @version $Id$
*/
public class WeightedObservedPointsTest {
@Test
public void testAdd1() {
final WeightedObservedPoints store = new WeightedObservedPoints();
final double x = 1.2;
final double y = 34.56;
final double w = 0.789;
store.add(w, x, y);
Assert.assertTrue(lastElementIsSame(store, new WeightedObservedPoint(w, x, y)));
}
@Test
public void testAdd2() {
final WeightedObservedPoints store = new WeightedObservedPoints();
final double x = 1.2;
final double y = 34.56;
final double w = 0.789;
store.add(new WeightedObservedPoint(w, x, y));
Assert.assertTrue(lastElementIsSame(store, new WeightedObservedPoint(w, x, y)));
}
@Test
public void testAdd3() {
final WeightedObservedPoints store = new WeightedObservedPoints();
final double x = 1.2;
final double y = 34.56;
store.add(x, y);
Assert.assertTrue(lastElementIsSame(store, new WeightedObservedPoint(1, x, y)));
}
@Test
public void testClear() {
final WeightedObservedPoints store = new WeightedObservedPoints();
store.add(new WeightedObservedPoint(1, 2, 3));
store.add(new WeightedObservedPoint(2, -1, -2));
Assert.assertTrue(store.toList().size() == 2);
store.clear();
Assert.assertTrue(store.toList().size() == 0);
}
// Ensure that an instance returned by "toList()" is independent from
// the original container.
@Test
public void testToListCopy() {
final WeightedObservedPoints store = new WeightedObservedPoints();
store.add(new WeightedObservedPoint(1, 2, 3));
store.add(new WeightedObservedPoint(2, -3, -4));
final List<WeightedObservedPoint> list = store.toList();
Assert.assertTrue(list.size() == 2);
// Adding an element to "list" has no impact on "store".
list.add(new WeightedObservedPoint(1.2, 3.4, 5.6));
Assert.assertFalse(list.size() == store.toList().size());
// Clearing "store" has no impact on "list".
store.clear();
Assert.assertFalse(list.size() == 0);
}
/**
* Checks that the contents of the last element is equal to the
* contents of {@code p}.
*
* @param store Container.
* @param point Observation.
* @return {@code true} if both elements have the same contents.
*/
private boolean lastElementIsSame(WeightedObservedPoints store,
WeightedObservedPoint point) {
final List<WeightedObservedPoint> list = store.toList();
final WeightedObservedPoint lastPoint = list.get(list.size() - 1);
if (!Precision.equals(lastPoint.getX(), point.getX())) {
return false;
}
if (!Precision.equals(lastPoint.getY(), point.getY())) {
return false;
}
if (!Precision.equals(lastPoint.getWeight(), point.getWeight())) {
return false;
}
return true;
}
}