001 /* =========================================================== 002 * JFreeChart : a free chart library for the Java(tm) platform 003 * =========================================================== 004 * 005 * (C) Copyright 2000-2011, by Object Refinery Limited and Contributors. 006 * 007 * Project Info: http://www.jfree.org/jfreechart/index.html 008 * 009 * This library is free software; you can redistribute it and/or modify it 010 * under the terms of the GNU Lesser General Public License as published by 011 * the Free Software Foundation; either version 2.1 of the License, or 012 * (at your option) any later version. 013 * 014 * This library is distributed in the hope that it will be useful, but 015 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 016 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 017 * License for more details. 018 * 019 * You should have received a copy of the GNU Lesser General Public 020 * License along with this library; if not, write to the Free Software 021 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 022 * USA. 023 * 024 * [Oracle and Java are registered trademarks of Oracle and/or its affiliates. 025 * Other names may be trademarks of their respective owners.] 026 * 027 * ------------------ 028 * MovingAverage.java 029 * ------------------ 030 * (C) Copyright 2003-2009, by Object Refinery Limited. 031 * 032 * Original Author: David Gilbert (for Object Refinery Limited); 033 * Contributor(s): Benoit Xhenseval; 034 * 035 * Changes 036 * ------- 037 * 28-Jan-2003 : Version 1 (DG); 038 * 10-Mar-2003 : Added createPointMovingAverage() method contributed by Benoit 039 * Xhenseval (DG); 040 * 01-Aug-2003 : Added new method for TimeSeriesCollection, and fixed bug in 041 * XYDataset method (DG); 042 * 15-Jul-2004 : Switched getX() with getXValue() and getY() with 043 * getYValue() (DG); 044 * 11-Jan-2005 : Removed deprecated code in preparation for the 1.0.0 045 * release (DG); 046 * 09-Jun-2009 : Tidied up some calls to TimeSeries (DG); 047 * 048 */ 049 050 package org.jfree.data.time; 051 052 import org.jfree.data.xy.XYDataset; 053 import org.jfree.data.xy.XYSeries; 054 import org.jfree.data.xy.XYSeriesCollection; 055 056 /** 057 * A utility class for calculating moving averages of time series data. 058 */ 059 public class MovingAverage { 060 061 /** 062 * Creates a new {@link TimeSeriesCollection} containing a moving average 063 * series for each series in the source collection. 064 * 065 * @param source the source collection. 066 * @param suffix the suffix added to each source series name to create the 067 * corresponding moving average series name. 068 * @param periodCount the number of periods in the moving average 069 * calculation. 070 * @param skip the number of initial periods to skip. 071 * 072 * @return A collection of moving average time series. 073 */ 074 public static TimeSeriesCollection createMovingAverage( 075 TimeSeriesCollection source, String suffix, int periodCount, 076 int skip) { 077 078 if (source == null) { 079 throw new IllegalArgumentException("Null 'source' argument."); 080 } 081 if (periodCount < 1) { 082 throw new IllegalArgumentException("periodCount must be greater " 083 + "than or equal to 1."); 084 } 085 086 TimeSeriesCollection result = new TimeSeriesCollection(); 087 for (int i = 0; i < source.getSeriesCount(); i++) { 088 TimeSeries sourceSeries = source.getSeries(i); 089 TimeSeries maSeries = createMovingAverage(sourceSeries, 090 sourceSeries.getKey() + suffix, periodCount, skip); 091 result.addSeries(maSeries); 092 } 093 return result; 094 095 } 096 097 /** 098 * Creates a new {@link TimeSeries} containing moving average values for 099 * the given series. If the series is empty (contains zero items), the 100 * result is an empty series. 101 * 102 * @param source the source series. 103 * @param name the name of the new series. 104 * @param periodCount the number of periods used in the average 105 * calculation. 106 * @param skip the number of initial periods to skip. 107 * 108 * @return The moving average series. 109 */ 110 public static TimeSeries createMovingAverage(TimeSeries source, 111 String name, int periodCount, int skip) { 112 113 if (source == null) { 114 throw new IllegalArgumentException("Null source."); 115 } 116 if (periodCount < 1) { 117 throw new IllegalArgumentException("periodCount must be greater " 118 + "than or equal to 1."); 119 120 } 121 122 TimeSeries result = new TimeSeries(name); 123 124 if (source.getItemCount() > 0) { 125 126 // if the initial averaging period is to be excluded, then 127 // calculate the index of the 128 // first data item to have an average calculated... 129 long firstSerial = source.getTimePeriod(0).getSerialIndex() + skip; 130 131 for (int i = source.getItemCount() - 1; i >= 0; i--) { 132 133 // get the current data item... 134 RegularTimePeriod period = source.getTimePeriod(i); 135 long serial = period.getSerialIndex(); 136 137 if (serial >= firstSerial) { 138 // work out the average for the earlier values... 139 int n = 0; 140 double sum = 0.0; 141 long serialLimit = period.getSerialIndex() - periodCount; 142 int offset = 0; 143 boolean finished = false; 144 145 while ((offset < periodCount) && (!finished)) { 146 if ((i - offset) >= 0) { 147 TimeSeriesDataItem item = source.getRawDataItem( 148 i - offset); 149 RegularTimePeriod p = item.getPeriod(); 150 Number v = item.getValue(); 151 long currentIndex = p.getSerialIndex(); 152 if (currentIndex > serialLimit) { 153 if (v != null) { 154 sum = sum + v.doubleValue(); 155 n = n + 1; 156 } 157 } 158 else { 159 finished = true; 160 } 161 } 162 offset = offset + 1; 163 } 164 if (n > 0) { 165 result.add(period, sum / n); 166 } 167 else { 168 result.add(period, null); 169 } 170 } 171 172 } 173 } 174 175 return result; 176 177 } 178 179 /** 180 * Creates a new {@link TimeSeries} containing moving average values for 181 * the given series, calculated by number of points (irrespective of the 182 * 'age' of those points). If the series is empty (contains zero items), 183 * the result is an empty series. 184 * <p> 185 * Developed by Benoit Xhenseval (www.ObjectLab.co.uk). 186 * 187 * @param source the source series. 188 * @param name the name of the new series. 189 * @param pointCount the number of POINTS used in the average calculation 190 * (not periods!) 191 * 192 * @return The moving average series. 193 */ 194 public static TimeSeries createPointMovingAverage(TimeSeries source, 195 String name, int pointCount) { 196 197 if (source == null) { 198 throw new IllegalArgumentException("Null 'source'."); 199 } 200 if (pointCount < 2) { 201 throw new IllegalArgumentException("periodCount must be greater " 202 + "than or equal to 2."); 203 } 204 205 TimeSeries result = new TimeSeries(name); 206 double rollingSumForPeriod = 0.0; 207 for (int i = 0; i < source.getItemCount(); i++) { 208 // get the current data item... 209 TimeSeriesDataItem current = source.getRawDataItem(i); 210 RegularTimePeriod period = current.getPeriod(); 211 // FIXME: what if value is null on next line? 212 rollingSumForPeriod += current.getValue().doubleValue(); 213 214 if (i > pointCount - 1) { 215 // remove the point i-periodCount out of the rolling sum. 216 TimeSeriesDataItem startOfMovingAvg = source.getRawDataItem( 217 i - pointCount); 218 rollingSumForPeriod -= startOfMovingAvg.getValue() 219 .doubleValue(); 220 result.add(period, rollingSumForPeriod / pointCount); 221 } 222 else if (i == pointCount - 1) { 223 result.add(period, rollingSumForPeriod / pointCount); 224 } 225 } 226 return result; 227 } 228 229 /** 230 * Creates a new {@link XYDataset} containing the moving averages of each 231 * series in the <code>source</code> dataset. 232 * 233 * @param source the source dataset. 234 * @param suffix the string to append to source series names to create 235 * target series names. 236 * @param period the averaging period. 237 * @param skip the length of the initial skip period. 238 * 239 * @return The dataset. 240 */ 241 public static XYDataset createMovingAverage(XYDataset source, String suffix, 242 long period, long skip) { 243 244 return createMovingAverage(source, suffix, (double) period, 245 (double) skip); 246 247 } 248 249 250 /** 251 * Creates a new {@link XYDataset} containing the moving averages of each 252 * series in the <code>source</code> dataset. 253 * 254 * @param source the source dataset. 255 * @param suffix the string to append to source series names to create 256 * target series names. 257 * @param period the averaging period. 258 * @param skip the length of the initial skip period. 259 * 260 * @return The dataset. 261 */ 262 public static XYDataset createMovingAverage(XYDataset source, 263 String suffix, double period, double skip) { 264 265 if (source == null) { 266 throw new IllegalArgumentException("Null source (XYDataset)."); 267 } 268 269 XYSeriesCollection result = new XYSeriesCollection(); 270 271 for (int i = 0; i < source.getSeriesCount(); i++) { 272 XYSeries s = createMovingAverage(source, i, source.getSeriesKey(i) 273 + suffix, period, skip); 274 result.addSeries(s); 275 } 276 277 return result; 278 279 } 280 281 /** 282 * Creates a new {@link XYSeries} containing the moving averages of one 283 * series in the <code>source</code> dataset. 284 * 285 * @param source the source dataset. 286 * @param series the series index (zero based). 287 * @param name the name for the new series. 288 * @param period the averaging period. 289 * @param skip the length of the initial skip period. 290 * 291 * @return The dataset. 292 */ 293 public static XYSeries createMovingAverage(XYDataset source, 294 int series, String name, double period, double skip) { 295 296 if (source == null) { 297 throw new IllegalArgumentException("Null source (XYDataset)."); 298 } 299 if (period < Double.MIN_VALUE) { 300 throw new IllegalArgumentException("period must be positive."); 301 } 302 if (skip < 0.0) { 303 throw new IllegalArgumentException("skip must be >= 0.0."); 304 } 305 306 XYSeries result = new XYSeries(name); 307 308 if (source.getItemCount(series) > 0) { 309 310 // if the initial averaging period is to be excluded, then 311 // calculate the lowest x-value to have an average calculated... 312 double first = source.getXValue(series, 0) + skip; 313 314 for (int i = source.getItemCount(series) - 1; i >= 0; i--) { 315 316 // get the current data item... 317 double x = source.getXValue(series, i); 318 319 if (x >= first) { 320 // work out the average for the earlier values... 321 int n = 0; 322 double sum = 0.0; 323 double limit = x - period; 324 int offset = 0; 325 boolean finished = false; 326 327 while (!finished) { 328 if ((i - offset) >= 0) { 329 double xx = source.getXValue(series, i - offset); 330 Number yy = source.getY(series, i - offset); 331 if (xx > limit) { 332 if (yy != null) { 333 sum = sum + yy.doubleValue(); 334 n = n + 1; 335 } 336 } 337 else { 338 finished = true; 339 } 340 } 341 else { 342 finished = true; 343 } 344 offset = offset + 1; 345 } 346 if (n > 0) { 347 result.add(x, sum / n); 348 } 349 else { 350 result.add(x, null); 351 } 352 } 353 354 } 355 } 356 357 return result; 358 359 } 360 361 }