Removing BeanListUnivariate example from test cases. Improving ListUnivariate Serialization Example.
git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/math/trunk/src/experimental@141257 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
72edfbfa4d
commit
9e99ec37d4
|
@ -0,0 +1,148 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2003-2004 The Apache Software Foundation.
|
||||||
|
*
|
||||||
|
* Licensed 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.math.stat.univariate;
|
||||||
|
|
||||||
|
import java.io.Serializable;
|
||||||
|
import java.lang.reflect.InvocationTargetException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.apache.commons.beanutils.PropertyUtils;
|
||||||
|
import org.apache.commons.beanutils.DynaBean;
|
||||||
|
import org.apache.commons.beanutils.BasicDynaClass;
|
||||||
|
import org.apache.commons.beanutils.DynaProperty;
|
||||||
|
import org.apache.commons.math.MathException;
|
||||||
|
import org.apache.commons.math.util.NumberTransformer;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This implementation of DescriptiveStatistics uses commons-beanutils to gather
|
||||||
|
* univariate statistics for a List of Java Beans by property. This
|
||||||
|
* implementation uses beanutils' PropertyUtils to get a simple, nested,
|
||||||
|
* indexed, mapped, or combined property from an element of a List.
|
||||||
|
* @version $Revision: 1.1 $ $Date: 2004/06/01 21:28:06 $
|
||||||
|
*/
|
||||||
|
public class BeanListUnivariateImpl extends ListUnivariateImpl implements Serializable {
|
||||||
|
|
||||||
|
/** Serializable version identifier */
|
||||||
|
static final long serialVersionUID = -6428201899045406285L;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* propertyName of the property to get from the bean
|
||||||
|
*/
|
||||||
|
private String propertyName;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* No argument Constructor
|
||||||
|
*/
|
||||||
|
public BeanListUnivariateImpl(){
|
||||||
|
this(new ArrayList());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Construct a BeanListUnivariate with specified
|
||||||
|
* backing list
|
||||||
|
* @param list Backing List
|
||||||
|
*/
|
||||||
|
public BeanListUnivariateImpl(List list) {
|
||||||
|
this(list, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Construct a BeanListUnivariate with specified
|
||||||
|
* backing list and propertyName
|
||||||
|
* @param list Backing List
|
||||||
|
* @param propertyName Bean propertyName
|
||||||
|
*/
|
||||||
|
public BeanListUnivariateImpl(List list, String propertyName) {
|
||||||
|
super(list);
|
||||||
|
setPropertyName(propertyName);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return propertyName
|
||||||
|
*/
|
||||||
|
public String getPropertyName() {
|
||||||
|
return propertyName;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param propertyName Name of Property
|
||||||
|
*/
|
||||||
|
public void setPropertyName(String propertyName) {
|
||||||
|
this.propertyName = propertyName;
|
||||||
|
this.transformer = new NumberTransformer() {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @see org.apache.commons.math.util.NumberTransformer#transform(java.lang.Object)
|
||||||
|
*/
|
||||||
|
public double transform(final Object o) throws MathException {
|
||||||
|
try {
|
||||||
|
return (
|
||||||
|
(Number) PropertyUtils.getProperty(
|
||||||
|
o,
|
||||||
|
getPropertyName()))
|
||||||
|
.doubleValue();
|
||||||
|
} catch (IllegalAccessException e) {
|
||||||
|
throw new MathException(
|
||||||
|
"IllegalAccessException in Transformation: "
|
||||||
|
+ e.getMessage(),
|
||||||
|
e);
|
||||||
|
} catch (InvocationTargetException e) {
|
||||||
|
throw new MathException(
|
||||||
|
"InvocationTargetException in Transformation: "
|
||||||
|
+ e.getMessage(),
|
||||||
|
e);
|
||||||
|
} catch (NoSuchMethodException e) {
|
||||||
|
throw new MathException(
|
||||||
|
"oSuchMethodException in Transformation: "
|
||||||
|
+ e.getMessage(),
|
||||||
|
e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a {@link org.apache.commons.beanutils.DynaBean} with a
|
||||||
|
* {@link org.apache.commons.beanutils.DynaProperty} named
|
||||||
|
* <code>propertyName,</code> sets the value of the property to <code>v</code>
|
||||||
|
* and adds the DynaBean to the underlying list.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public void addValue(double v) {
|
||||||
|
DynaProperty[] props = new DynaProperty[] {
|
||||||
|
new DynaProperty(propertyName, Double.class)
|
||||||
|
};
|
||||||
|
BasicDynaClass dynaClass = new BasicDynaClass(null, null, props);
|
||||||
|
DynaBean dynaBean = null;
|
||||||
|
try {
|
||||||
|
dynaBean = dynaClass.newInstance();
|
||||||
|
} catch (Exception ex) { // InstantiationException, IllegalAccessException
|
||||||
|
throw new RuntimeException(ex); // should never happen
|
||||||
|
}
|
||||||
|
dynaBean.set(propertyName, new Double(v));
|
||||||
|
addObject(dynaBean);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Adds a bean to this list.
|
||||||
|
*
|
||||||
|
* @param bean Bean to add to the list
|
||||||
|
*/
|
||||||
|
public void addObject(Object bean) {
|
||||||
|
list.add(bean);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,227 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2003-2004 The Apache Software Foundation.
|
||||||
|
*
|
||||||
|
* Licensed 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.math.stat.univariate;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import junit.framework.Test;
|
||||||
|
import junit.framework.TestCase;
|
||||||
|
import junit.framework.TestSuite;
|
||||||
|
|
||||||
|
import org.apache.commons.math.TestUtils;
|
||||||
|
import org.apache.commons.math.stat.StatUtils;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test cases for the {@link BeanListUnivariateImpl} class.
|
||||||
|
*
|
||||||
|
* @version $Revision: 1.1 $ $Date: 2004/06/01 21:28:06 $
|
||||||
|
*/
|
||||||
|
|
||||||
|
public final class BeanListUnivariateImplTest extends TestCase {
|
||||||
|
|
||||||
|
private double one = 1;
|
||||||
|
private float two = 2;
|
||||||
|
private int three = 3;
|
||||||
|
private double mean = 2;
|
||||||
|
private double sumSq = 18;
|
||||||
|
private double sum = 8;
|
||||||
|
private double var = 0.666666666666666666667;
|
||||||
|
private double std = Math.sqrt(var);
|
||||||
|
private double n = 4;
|
||||||
|
private double min = 1;
|
||||||
|
private double max = 3;
|
||||||
|
private double skewness = 0;
|
||||||
|
private double kurtosis = 0.5;
|
||||||
|
private double tolerance = 10E-15;
|
||||||
|
|
||||||
|
|
||||||
|
private List patientList = null;
|
||||||
|
|
||||||
|
public BeanListUnivariateImplTest(String name) {
|
||||||
|
super(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setUp() {
|
||||||
|
patientList = new ArrayList();
|
||||||
|
|
||||||
|
// Create and add patient bean 1
|
||||||
|
VitalStats vs1 = new VitalStats( new Double(120.0),
|
||||||
|
new Double(96.4) );
|
||||||
|
Patient p1 = new Patient( vs1, new Integer( 35 ) );
|
||||||
|
patientList.add( p1 );
|
||||||
|
|
||||||
|
// Create and add patient bean 2
|
||||||
|
VitalStats vs2 = new VitalStats( new Double(70.0),
|
||||||
|
new Double(97.4) );
|
||||||
|
Patient p2 = new Patient( vs2, new Integer( 23 ) );
|
||||||
|
patientList.add( p2 );
|
||||||
|
|
||||||
|
// Create and add patient bean 3
|
||||||
|
VitalStats vs3 = new VitalStats( new Double(90.0),
|
||||||
|
new Double(98.6) );
|
||||||
|
Patient p3 = new Patient( vs3, new Integer( 42 ) );
|
||||||
|
patientList.add( p3 );
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Test suite() {
|
||||||
|
TestSuite suite = new TestSuite(BeanListUnivariateImplTest.class);
|
||||||
|
suite.setName("Frequency Tests");
|
||||||
|
return suite;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** test stats */
|
||||||
|
public void testStats() {
|
||||||
|
DescriptiveStatistics u = new BeanListUnivariateImpl( patientList, "age" );
|
||||||
|
double[] values = {35d, 23d, 42d};
|
||||||
|
assertEquals("total count",3,u.getN(),tolerance);
|
||||||
|
assertEquals("mean", StatUtils.mean(values), u.getMean(), tolerance);
|
||||||
|
assertEquals("min", StatUtils.min(values), u.getMin(), tolerance);
|
||||||
|
assertEquals("max", StatUtils.max(values), u.getMax(), tolerance);
|
||||||
|
assertEquals("var", StatUtils.variance(values), u.getVariance(), tolerance);
|
||||||
|
u.clear();
|
||||||
|
assertEquals("total count",0,u.getN(),tolerance);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testPropStats() {
|
||||||
|
|
||||||
|
DescriptiveStatistics heartU = new BeanListUnivariateImpl( patientList,
|
||||||
|
"vitalStats.heartRate" );
|
||||||
|
|
||||||
|
assertEquals( "Mean heart rate unexpected", 93.333,
|
||||||
|
heartU.getMean(), 0.001 );
|
||||||
|
assertEquals( "Max heart rate unexpected", 120.0,
|
||||||
|
heartU.getMax(), 0.001 );
|
||||||
|
|
||||||
|
DescriptiveStatistics ageU = new BeanListUnivariateImpl( patientList,
|
||||||
|
"age" );
|
||||||
|
|
||||||
|
assertEquals( "Mean age unexpected", 33.333,
|
||||||
|
ageU.getMean(), 0.001 );
|
||||||
|
assertEquals( "Max age unexpected", 42.0,
|
||||||
|
ageU.getMax(), 0.001 );
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testSetPropertyName(){
|
||||||
|
BeanListUnivariateImpl u = new BeanListUnivariateImpl(null);
|
||||||
|
String expected = "property";
|
||||||
|
u.setPropertyName(expected);
|
||||||
|
assertEquals(expected, u.getPropertyName());
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testAddValue() {
|
||||||
|
DescriptiveStatistics u = new BeanListUnivariateImpl( patientList, "age" );
|
||||||
|
u.addValue(10);
|
||||||
|
double[] values = {35d, 23d, 42d, 10d};
|
||||||
|
assertEquals("total count",4,u.getN(),tolerance);
|
||||||
|
assertEquals("mean", StatUtils.mean(values), u.getMean(), tolerance);
|
||||||
|
assertEquals("min", StatUtils.min(values), u.getMin(), tolerance);
|
||||||
|
assertEquals("max", StatUtils.max(values), u.getMax(), tolerance);
|
||||||
|
assertEquals("var", StatUtils.variance(values), u.getVariance(), tolerance);
|
||||||
|
u.clear();
|
||||||
|
assertEquals("total count",0,u.getN(),tolerance);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** test stats */
|
||||||
|
public void testSerialization() {
|
||||||
|
|
||||||
|
double[] values = {35d, 23d, 42d};
|
||||||
|
|
||||||
|
DescriptiveStatistics u = new BeanListUnivariateImpl( patientList, "age" );
|
||||||
|
assertEquals("total count",3,u.getN(),tolerance);
|
||||||
|
assertEquals("mean", StatUtils.mean(values), u.getMean(), tolerance);
|
||||||
|
assertEquals("min", StatUtils.min(values), u.getMin(), tolerance);
|
||||||
|
assertEquals("max", StatUtils.max(values), u.getMax(), tolerance);
|
||||||
|
assertEquals("var", StatUtils.variance(values), u.getVariance(), tolerance);
|
||||||
|
|
||||||
|
|
||||||
|
DescriptiveStatistics u2 = (DescriptiveStatistics)TestUtils.serializeAndRecover(u);
|
||||||
|
assertEquals("total count",3,u2.getN(),tolerance);
|
||||||
|
assertEquals("mean", StatUtils.mean(values), u2.getMean(), tolerance);
|
||||||
|
assertEquals("min", StatUtils.min(values), u2.getMin(), tolerance);
|
||||||
|
assertEquals("max", StatUtils.max(values), u2.getMax(), tolerance);
|
||||||
|
assertEquals("var", StatUtils.variance(values), u2.getVariance(), tolerance);
|
||||||
|
|
||||||
|
u.clear();
|
||||||
|
assertEquals("total count",0,u.getN(),tolerance);
|
||||||
|
|
||||||
|
u2.clear();
|
||||||
|
assertEquals("total count",0,u2.getN(),tolerance);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public class VitalStats {
|
||||||
|
|
||||||
|
private Double heartrate;
|
||||||
|
private Double temperature;
|
||||||
|
|
||||||
|
public VitalStats() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public VitalStats(Double heartrate, Double temperature) {
|
||||||
|
setHeartRate( heartrate );
|
||||||
|
setTemperature( temperature );
|
||||||
|
}
|
||||||
|
|
||||||
|
public Double getHeartRate() {
|
||||||
|
return heartrate;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setHeartRate(Double heartrate) {
|
||||||
|
this.heartrate = heartrate;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Double getTemperature() {
|
||||||
|
return temperature;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setTemperature(Double temperature) {
|
||||||
|
this.temperature = temperature;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public class Patient {
|
||||||
|
|
||||||
|
private VitalStats vitalStats;
|
||||||
|
private Integer age;
|
||||||
|
|
||||||
|
public Patient() {
|
||||||
|
}
|
||||||
|
|
||||||
|
public Patient(VitalStats vitalStats, Integer age) {
|
||||||
|
setVitalStats( vitalStats );
|
||||||
|
setAge( age );
|
||||||
|
}
|
||||||
|
|
||||||
|
public VitalStats getVitalStats() {
|
||||||
|
return( vitalStats );
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setVitalStats(VitalStats vitalStats) {
|
||||||
|
this.vitalStats = vitalStats;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Integer getAge() {
|
||||||
|
return age;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAge(Integer age) {
|
||||||
|
this.age = age;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue