View Javadoc

1   /*
2    * Copyright 2003-2004 The Apache Software Foundation.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.apache.commons.math.stat.descriptive;
17  
18  import java.io.Serializable;
19  
20  import org.apache.commons.math.util.ResizableDoubleArray;
21  
22  /**
23   * Default implementation of
24   * {@link org.apache.commons.math.stat.descriptive.DescriptiveStatistics}.
25   *
26   * @version $Revision: 348519 $ $Date: 2005-11-23 12:12:18 -0700 (Wed, 23 Nov 2005) $
27   */
28  public class DescriptiveStatisticsImpl extends DescriptiveStatistics implements Serializable {
29  
30      /** Serializable version identifier */
31      private static final long serialVersionUID = -1868088725461221010L;
32      
33      /** hold the window size **/
34      protected int windowSize;
35      
36      /** 
37       *  Stored data values
38       */
39      protected ResizableDoubleArray eDA;
40  
41      /**
42       * Construct a DescriptiveStatisticsImpl with infinite window
43       */
44      public DescriptiveStatisticsImpl() {
45          this(INFINITE_WINDOW);
46      }
47      
48      /**
49       * Construct a DescriptiveStatisticsImpl with finite window
50       * @param window the finite window size.
51       */
52      public DescriptiveStatisticsImpl(int window) {
53          super();
54          eDA = new ResizableDoubleArray();
55          setWindowSize(window);
56      }
57  
58      /**
59       * Access the window size.
60       * @return the current window size.
61       */
62      public int getWindowSize() {
63          return windowSize;
64      }
65      
66      /**
67       * @see org.apache.commons.math.stat.descriptive.DescriptiveStatistics#getValues()
68       */
69      public double[] getValues() {
70  
71          double[] copiedArray = new double[eDA.getNumElements()];
72          System.arraycopy(
73              eDA.getElements(),
74              0,
75              copiedArray,
76              0,
77              eDA.getNumElements());
78          return copiedArray;
79      }
80      
81      /**
82       * @see org.apache.commons.math.stat.descriptive.DescriptiveStatistics#getElement(int)
83       */
84      public double getElement(int index) {
85          return eDA.getElement(index);
86      }
87  
88      /**
89       * @see org.apache.commons.math.stat.descriptive.DescriptiveStatistics#getN()
90       */
91      public long getN() {
92          return eDA.getNumElements();
93      }
94  
95      /**
96       * @see org.apache.commons.math.stat.descriptive.DescriptiveStatistics#addValue(double)
97       */
98      public void addValue(double v) {
99          if (windowSize != INFINITE_WINDOW) {
100             if (getN() == windowSize) {
101                 eDA.addElementRolling(v);
102             } else if (getN() < windowSize) {
103                 eDA.addElement(v);
104             }
105         } else {
106             eDA.addElement(v);
107         }
108     }
109 
110     /**
111      * @see org.apache.commons.math.stat.descriptive.DescriptiveStatistics#clear()
112      */
113     public void clear() {
114         eDA.clear();
115     }
116 
117     /**
118      * @see org.apache.commons.math.stat.descriptive.DescriptiveStatistics#setWindowSize(int)
119      */
120     public void setWindowSize(int windowSize) {
121         if (windowSize < 1) {
122             if (windowSize != INFINITE_WINDOW) {
123                 throw new IllegalArgumentException("window size must be positive.");
124             }
125         }
126         
127         this.windowSize = windowSize;
128 
129         // We need to check to see if we need to discard elements
130         // from the front of the array.  If the windowSize is less than 
131         // the current number of elements.
132         if (windowSize != INFINITE_WINDOW && windowSize < eDA.getNumElements()) {
133             eDA.discardFrontElements(eDA.getNumElements() - windowSize);
134         }
135     }
136 
137     /**
138      * Apply the given statistic to this univariate collection.
139      * @param stat the statistic to apply
140      * @return the computed value of the statistic.
141      */
142     public double apply(UnivariateStatistic stat) {
143         return stat.evaluate(eDA.getValues(), eDA.start(), eDA.getNumElements());
144     }
145 }