1 16 package org.apache.commons.math.stat.descriptive; 17 18 import java.io.Serializable ; 19 import java.util.ArrayList ; 20 import java.util.List ; 21 22 import org.apache.commons.math.MathException; 23 import org.apache.commons.math.stat.descriptive.UnivariateStatistic; 24 import org.apache.commons.math.stat.descriptive.DescriptiveStatistics; 25 import org.apache.commons.math.util.DefaultTransformer; 26 import org.apache.commons.math.util.NumberTransformer; 27 28 31 public class ListUnivariateImpl extends DescriptiveStatistics implements Serializable { 32 33 34 static final long serialVersionUID = -8837442489133392138L; 35 36 40 protected List list; 41 42 43 protected NumberTransformer transformer; 44 45 46 protected int windowSize = DescriptiveStatistics.INFINITE_WINDOW; 47 48 51 public ListUnivariateImpl(){ 52 this(new ArrayList ()); 53 } 54 55 59 public ListUnivariateImpl(List list) { 60 this(list, new DefaultTransformer()); 61 } 62 63 68 public ListUnivariateImpl(List list, NumberTransformer transformer) { 69 super(); 70 this.list = list; 71 this.transformer = transformer; 72 } 73 74 77 public double[] getValues() { 78 79 int length = list.size(); 80 81 86 if (windowSize != DescriptiveStatistics.INFINITE_WINDOW && 87 windowSize < list.size()) 88 { 89 length = list.size() - Math.max(0, list.size() - windowSize); 90 } 91 92 double[] copiedArray = new double[length]; 94 95 for (int i = 0; i < copiedArray.length; i++) { 96 copiedArray[i] = getElement(i); 97 } 98 return copiedArray; 99 } 100 101 104 public double getElement(int index) { 105 106 double value = Double.NaN; 107 108 int calcIndex = index; 109 110 if (windowSize != DescriptiveStatistics.INFINITE_WINDOW && 111 windowSize < list.size()) 112 { 113 calcIndex = (list.size() - windowSize) + index; 114 } 115 116 117 try { 118 value = transformer.transform(list.get(calcIndex)); 119 } catch (MathException e) { 120 e.printStackTrace(); 121 } 122 123 return value; 124 } 125 126 129 public long getN() { 130 int n = 0; 131 132 if (windowSize != DescriptiveStatistics.INFINITE_WINDOW) { 133 if (list.size() > windowSize) { 134 n = windowSize; 135 } else { 136 n = list.size(); 137 } 138 } else { 139 n = list.size(); 140 } 141 return n; 142 } 143 144 147 public void addValue(double v) { 148 list.add(new Double (v)); 149 } 150 151 155 public void addObject(Object o) { 156 list.add(o); 157 } 158 159 164 public void clear() { 165 list.clear(); 166 } 167 168 173 public double apply(UnivariateStatistic stat) { 174 double[] v = this.getValues(); 175 176 if (v != null) { 177 return stat.evaluate(v, 0, v.length); 178 } 179 return Double.NaN; 180 } 181 182 186 public NumberTransformer getTransformer() { 187 return transformer; 188 } 189 190 194 public void setTransformer(NumberTransformer transformer) { 195 this.transformer = transformer; 196 } 197 198 201 public synchronized void setWindowSize(int windowSize) { 202 this.windowSize = windowSize; 203 int extra = list.size() - windowSize; 206 for (int i = 0; i < extra; i++) { 207 list.remove(0); 208 } 209 } 210 211 214 public int getWindowSize() { 215 return windowSize; 216 } 217 218 } | Popular Tags |