1 16 package org.apache.commons.math.stat.univariate; 17 18 import java.util.ArrayList ; 19 import java.util.List ; 20 21 import junit.framework.Test; 22 import junit.framework.TestCase; 23 import junit.framework.TestSuite; 24 25 import org.apache.commons.math.TestUtils; 26 import org.apache.commons.math.stat.StatUtils; 27 28 33 34 public final class BeanListUnivariateImplTest extends TestCase { 35 36 private double one = 1; 37 private float two = 2; 38 private int three = 3; 39 private double mean = 2; 40 private double sumSq = 18; 41 private double sum = 8; 42 private double var = 0.666666666666666666667; 43 private double std = Math.sqrt(var); 44 private double n = 4; 45 private double min = 1; 46 private double max = 3; 47 private double skewness = 0; 48 private double kurtosis = 0.5; 49 private double tolerance = 10E-15; 50 51 52 private List patientList = null; 53 54 public BeanListUnivariateImplTest(String name) { 55 super(name); 56 } 57 58 public void setUp() { 59 patientList = new ArrayList (); 60 61 VitalStats vs1 = new VitalStats( new Double (120.0), 63 new Double (96.4) ); 64 Patient p1 = new Patient( vs1, new Integer ( 35 ) ); 65 patientList.add( p1 ); 66 67 VitalStats vs2 = new VitalStats( new Double (70.0), 69 new Double (97.4) ); 70 Patient p2 = new Patient( vs2, new Integer ( 23 ) ); 71 patientList.add( p2 ); 72 73 VitalStats vs3 = new VitalStats( new Double (90.0), 75 new Double (98.6) ); 76 Patient p3 = new Patient( vs3, new Integer ( 42 ) ); 77 patientList.add( p3 ); 78 } 79 80 public static Test suite() { 81 TestSuite suite = new TestSuite(BeanListUnivariateImplTest.class); 82 suite.setName("Frequency Tests"); 83 return suite; 84 } 85 86 87 public void testStats() { 88 DescriptiveStatistics u = new BeanListUnivariateImpl( patientList, "age" ); 89 double[] values = {35d, 23d, 42d}; 90 assertEquals("total count",3,u.getN(),tolerance); 91 assertEquals("mean", StatUtils.mean(values), u.getMean(), tolerance); 92 assertEquals("min", StatUtils.min(values), u.getMin(), tolerance); 93 assertEquals("max", StatUtils.max(values), u.getMax(), tolerance); 94 assertEquals("var", StatUtils.variance(values), u.getVariance(), tolerance); 95 u.clear(); 96 assertEquals("total count",0,u.getN(),tolerance); 97 } 98 99 public void testPropStats() { 100 101 DescriptiveStatistics heartU = new BeanListUnivariateImpl( patientList, 102 "vitalStats.heartRate" ); 103 104 assertEquals( "Mean heart rate unexpected", 93.333, 105 heartU.getMean(), 0.001 ); 106 assertEquals( "Max heart rate unexpected", 120.0, 107 heartU.getMax(), 0.001 ); 108 109 DescriptiveStatistics ageU = new BeanListUnivariateImpl( patientList, 110 "age" ); 111 112 assertEquals( "Mean age unexpected", 33.333, 113 ageU.getMean(), 0.001 ); 114 assertEquals( "Max age unexpected", 42.0, 115 ageU.getMax(), 0.001 ); 116 117 } 118 119 public void testSetPropertyName(){ 120 BeanListUnivariateImpl u = new BeanListUnivariateImpl(null); 121 String expected = "property"; 122 u.setPropertyName(expected); 123 assertEquals(expected, u.getPropertyName()); 124 } 125 126 public void testAddValue() { 127 DescriptiveStatistics u = new BeanListUnivariateImpl( patientList, "age" ); 128 u.addValue(10); 129 double[] values = {35d, 23d, 42d, 10d}; 130 assertEquals("total count",4,u.getN(),tolerance); 131 assertEquals("mean", StatUtils.mean(values), u.getMean(), tolerance); 132 assertEquals("min", StatUtils.min(values), u.getMin(), tolerance); 133 assertEquals("max", StatUtils.max(values), u.getMax(), tolerance); 134 assertEquals("var", StatUtils.variance(values), u.getVariance(), tolerance); 135 u.clear(); 136 assertEquals("total count",0,u.getN(),tolerance); 137 } 138 139 140 public void testSerialization() { 141 142 double[] values = {35d, 23d, 42d}; 143 144 DescriptiveStatistics u = new BeanListUnivariateImpl( patientList, "age" ); 145 assertEquals("total count",3,u.getN(),tolerance); 146 assertEquals("mean", StatUtils.mean(values), u.getMean(), tolerance); 147 assertEquals("min", StatUtils.min(values), u.getMin(), tolerance); 148 assertEquals("max", StatUtils.max(values), u.getMax(), tolerance); 149 assertEquals("var", StatUtils.variance(values), u.getVariance(), tolerance); 150 151 152 DescriptiveStatistics u2 = (DescriptiveStatistics)TestUtils.serializeAndRecover(u); 153 assertEquals("total count",3,u2.getN(),tolerance); 154 assertEquals("mean", StatUtils.mean(values), u2.getMean(), tolerance); 155 assertEquals("min", StatUtils.min(values), u2.getMin(), tolerance); 156 assertEquals("max", StatUtils.max(values), u2.getMax(), tolerance); 157 assertEquals("var", StatUtils.variance(values), u2.getVariance(), tolerance); 158 159 u.clear(); 160 assertEquals("total count",0,u.getN(),tolerance); 161 162 u2.clear(); 163 assertEquals("total count",0,u2.getN(),tolerance); 164 165 } 166 167 public class VitalStats { 168 169 private Double heartrate; 170 private Double temperature; 171 172 public VitalStats() { 173 } 174 175 public VitalStats(Double heartrate, Double temperature) { 176 setHeartRate( heartrate ); 177 setTemperature( temperature ); 178 } 179 180 public Double getHeartRate() { 181 return heartrate; 182 } 183 184 public void setHeartRate(Double heartrate) { 185 this.heartrate = heartrate; 186 } 187 188 public Double getTemperature() { 189 return temperature; 190 } 191 192 public void setTemperature(Double temperature) { 193 this.temperature = temperature; 194 } 195 } 196 197 public class Patient { 198 199 private VitalStats vitalStats; 200 private Integer age; 201 202 public Patient() { 203 } 204 205 public Patient(VitalStats vitalStats, Integer age) { 206 setVitalStats( vitalStats ); 207 setAge( age ); 208 } 209 210 public VitalStats getVitalStats() { 211 return( vitalStats ); 212 } 213 214 public void setVitalStats(VitalStats vitalStats) { 215 this.vitalStats = vitalStats; 216 } 217 218 public Integer getAge() { 219 return age; 220 } 221 222 public void setAge(Integer age) { 223 this.age = age; 224 } 225 } 226 } 227 228 | Popular Tags |