Removing BeanListUnivariate example from test cases. Improving ListUnivariate Serialization Example.

git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/math/trunk@141257 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Mark R. Diggory 2004-06-01 21:28:06 +00:00
parent 03ec3fdca5
commit 82b4bc1943
4 changed files with 176 additions and 13 deletions

View File

@ -15,7 +15,9 @@
*/ */
package org.apache.commons.math.stat.univariate; package org.apache.commons.math.stat.univariate;
import java.io.Serializable;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import org.apache.commons.beanutils.PropertyUtils; import org.apache.commons.beanutils.PropertyUtils;
@ -30,15 +32,25 @@ import org.apache.commons.math.util.NumberTransformer;
* univariate statistics for a List of Java Beans by property. This * univariate statistics for a List of Java Beans by property. This
* implementation uses beanutils' PropertyUtils to get a simple, nested, * implementation uses beanutils' PropertyUtils to get a simple, nested,
* indexed, mapped, or combined property from an element of a List. * indexed, mapped, or combined property from an element of a List.
* @version $Revision: 1.2 $ $Date: 2004/04/24 21:43:26 $ * @version $Revision: 1.1 $ $Date: 2004/06/01 21:28:06 $
*/ */
public class BeanListUnivariateImpl extends ListUnivariateImpl { public class BeanListUnivariateImpl extends ListUnivariateImpl implements Serializable {
/** Serializable version identifier */
static final long serialVersionUID = -6428201899045406285L;
/** /**
* propertyName of the property to get from the bean * propertyName of the property to get from the bean
*/ */
private String propertyName; private String propertyName;
/**
* No argument Constructor
*/
public BeanListUnivariateImpl(){
this(new ArrayList());
}
/** /**
* Construct a BeanListUnivariate with specified * Construct a BeanListUnivariate with specified
* backing list * backing list

View File

@ -22,20 +22,34 @@ import junit.framework.Test;
import junit.framework.TestCase; import junit.framework.TestCase;
import junit.framework.TestSuite; import junit.framework.TestSuite;
import org.apache.commons.math.TestUtils;
import org.apache.commons.math.stat.StatUtils; import org.apache.commons.math.stat.StatUtils;
import org.apache.commons.math.beans.VitalStats;
import org.apache.commons.math.beans.Patient;
/** /**
* Test cases for the {@link BeanListUnivariateImpl} class. * Test cases for the {@link BeanListUnivariateImpl} class.
* *
* @version $Revision: 1.2 $ $Date: 2004/04/24 21:43:26 $ * @version $Revision: 1.1 $ $Date: 2004/06/01 21:28:06 $
*/ */
public final class BeanListUnivariateImplTest extends TestCase { 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; private List patientList = null;
private double tolerance = Double.MIN_VALUE;
public BeanListUnivariateImplTest(String name) { public BeanListUnivariateImplTest(String name) {
super(name); super(name);
@ -121,5 +135,93 @@ public final class BeanListUnivariateImplTest extends TestCase {
u.clear(); u.clear();
assertEquals("total count",0,u.getN(),tolerance); 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;
}
}
} }

View File

@ -15,19 +15,24 @@
*/ */
package org.apache.commons.math.stat.univariate; package org.apache.commons.math.stat.univariate;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import org.apache.commons.math.MathException; import org.apache.commons.math.MathException;
import org.apache.commons.math.stat.univariate.UnivariateStatistic; import org.apache.commons.math.stat.univariate.UnivariateStatistic;
import org.apache.commons.math.stat.univariate.AbstractDescriptiveStatistics; import org.apache.commons.math.stat.univariate.DescriptiveStatistics;
import org.apache.commons.math.util.DefaultTransformer; import org.apache.commons.math.util.DefaultTransformer;
import org.apache.commons.math.util.NumberTransformer; import org.apache.commons.math.util.NumberTransformer;
/** /**
* @version $Revision: 1.3 $ $Date: 2004/05/23 00:33:41 $ * @version $Revision: 1.4 $ $Date: 2004/06/01 21:28:06 $
*/ */
public class ListUnivariateImpl extends AbstractDescriptiveStatistics { public class ListUnivariateImpl extends DescriptiveStatistics implements Serializable {
/** Serializable version identifier */
static final long serialVersionUID = -8837442489133392138L;
/** /**
* Holds a reference to a list - GENERICs are going to make * Holds a reference to a list - GENERICs are going to make
* out lives easier here as we could only accept List<Number> * out lives easier here as we could only accept List<Number>
@ -40,6 +45,13 @@ public class ListUnivariateImpl extends AbstractDescriptiveStatistics {
/** hold the window size **/ /** hold the window size **/
protected int windowSize = DescriptiveStatistics.INFINITE_WINDOW; protected int windowSize = DescriptiveStatistics.INFINITE_WINDOW;
/**
* No argument Constructor
*/
public ListUnivariateImpl(){
this(new ArrayList());
}
/** /**
* Construct a ListUnivariate with a specific List. * Construct a ListUnivariate with a specific List.
* @param list The list that will back this DescriptiveStatistics * @param list The list that will back this DescriptiveStatistics
@ -196,8 +208,8 @@ public class ListUnivariateImpl extends AbstractDescriptiveStatistics {
} }
} }
public int getWindowSize() { public int getWindowSize() {
return windowSize; return windowSize;
} }
} }

View File

@ -18,6 +18,8 @@ package org.apache.commons.math.stat.univariate;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import org.apache.commons.math.TestUtils;
import junit.framework.Test; import junit.framework.Test;
import junit.framework.TestCase; import junit.framework.TestCase;
import junit.framework.TestSuite; import junit.framework.TestSuite;
@ -25,10 +27,11 @@ import junit.framework.TestSuite;
/** /**
* Test cases for the {@link Univariate} class. * Test cases for the {@link Univariate} class.
* *
* @version $Revision: 1.2 $ $Date: 2004/05/23 00:56:15 $ * @version $Revision: 1.3 $ $Date: 2004/06/01 21:28:06 $
*/ */
public final class ListUnivariateImplTest extends TestCase { public final class ListUnivariateImplTest extends TestCase {
private double one = 1; private double one = 1;
private float two = 2; private float two = 2;
private int three = 3; private int three = 3;
@ -134,6 +137,40 @@ public final class ListUnivariateImplTest extends TestCase {
} }
/** test stats */
public void testSerialization() {
DescriptiveStatistics u = null;
try {
u = DescriptiveStatistics.newInstance(ListUnivariateImpl.class);
} catch (InstantiationException e) {
fail(e.getMessage());
} catch (IllegalAccessException e) {
fail(e.getMessage());
}
assertEquals("total count",0,u.getN(),tolerance);
u.addValue(one);
u.addValue(two);
DescriptiveStatistics u2 = (DescriptiveStatistics)TestUtils.serializeAndRecover(u);
u2.addValue(two);
u2.addValue(three);
assertEquals("N",n,u2.getN(),tolerance);
assertEquals("sum",sum,u2.getSum(),tolerance);
assertEquals("sumsq",sumSq,u2.getSumsq(),tolerance);
assertEquals("var",var,u2.getVariance(),tolerance);
assertEquals("std",std,u2.getStandardDeviation(),tolerance);
assertEquals("mean",mean,u2.getMean(),tolerance);
assertEquals("min",min,u2.getMin(),tolerance);
assertEquals("max",max,u2.getMax(),tolerance);
u2.clear();
assertEquals("total count",0,u2.getN(),tolerance);
}
} }