001 /* =========================================================== 002 * JFreeChart : a free chart library for the Java(tm) platform 003 * =========================================================== 004 * 005 * (C) Copyright 2000-2008, 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 * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 025 * in the United States and other countries.] 026 * 027 * ------------------ 028 * MovingAverage.java 029 * ------------------ 030 * (C) Copyright 2003-2008, 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 * 047 */ 048 049 package org.jfree.data.time; 050 051 import org.jfree.data.xy.XYDataset; 052 import org.jfree.data.xy.XYSeries; 053 import org.jfree.data.xy.XYSeriesCollection; 054 055 /** 056 * A utility class for calculating moving averages of time series data. 057 */ 058 public class MovingAverage { 059 060 /** 061 * Creates a new {@link TimeSeriesCollection} containing a moving average 062 * series for each series in the source collection. 063 * 064 * @param source the source collection. 065 * @param suffix the suffix added to each source series name to create the 066 * corresponding moving average series name. 067 * @param periodCount the number of periods in the moving average 068 * calculation. 069 * @param skip the number of initial periods to skip. 070 * 071 * @return A collection of moving average time series. 072 */ 073 public static TimeSeriesCollection createMovingAverage( 074 TimeSeriesCollection source, String suffix, int periodCount, 075 int skip) { 076 077 if (source == null) { 078 throw new IllegalArgumentException("Null 'source' argument."); 079 } 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 088 for (int i = 0; i < source.getSeriesCount(); i++) { 089 TimeSeries sourceSeries = source.getSeries(i); 090 TimeSeries maSeries = createMovingAverage(sourceSeries, 091 sourceSeries.getKey() + suffix, periodCount, skip); 092 result.addSeries(maSeries); 093 } 094 095 return result; 096 097 } 098 099 /** 100 * Creates a new {@link TimeSeries} containing moving average values for 101 * the given series. If the series is empty (contains zero items), the 102 * result is an empty series. 103 * 104 * @param source the source series. 105 * @param name the name of the new series. 106 * @param periodCount the number of periods used in the average 107 * calculation. 108 * @param skip the number of initial periods to skip. 109 * 110 * @return The moving average series. 111 */ 112 public static TimeSeries createMovingAverage(TimeSeries source, 113 String name, int periodCount, int skip) { 114 115 if (source == null) { 116 throw new IllegalArgumentException("Null source."); 117 } 118 119 if (periodCount < 1) { 120 throw new IllegalArgumentException("periodCount must be greater " + 121 "than or equal to 1."); 122 123 } 124 125 TimeSeries result = new TimeSeries(name, source.getTimePeriodClass()); 126 127 if (source.getItemCount() > 0) { 128 129 // if the initial averaging period is to be excluded, then 130 // calculate the index of the 131 // first data item to have an average calculated... 132 long firstSerial 133 = source.getDataItem(0).getPeriod().getSerialIndex() + skip; 134 135 for (int i = source.getItemCount() - 1; i >= 0; i--) { 136 137 // get the current data item... 138 TimeSeriesDataItem current = source.getDataItem(i); 139 RegularTimePeriod period = current.getPeriod(); 140 long serial = period.getSerialIndex(); 141 142 if (serial >= firstSerial) { 143 // work out the average for the earlier values... 144 int n = 0; 145 double sum = 0.0; 146 long serialLimit = period.getSerialIndex() - periodCount; 147 int offset = 0; 148 boolean finished = false; 149 150 while ((offset < periodCount) && (!finished)) { 151 if ((i - offset) >= 0) { 152 TimeSeriesDataItem item = source.getDataItem( 153 i - offset); 154 RegularTimePeriod p = item.getPeriod(); 155 Number v = item.getValue(); 156 long currentIndex = p.getSerialIndex(); 157 if (currentIndex > serialLimit) { 158 if (v != null) { 159 sum = sum + v.doubleValue(); 160 n = n + 1; 161 } 162 } 163 else { 164 finished = true; 165 } 166 } 167 offset = offset + 1; 168 } 169 if (n > 0) { 170 result.add(period, sum / n); 171 } 172 else { 173 result.add(period, null); 174 } 175 } 176 177 } 178 } 179 180 return result; 181 182 } 183 184 /** 185 * Creates a new {@link TimeSeries} containing moving average values for 186 * the given series, calculated by number of points (irrespective of the 187 * 'age' of those points). If the series is empty (contains zero items), 188 * the result is an empty series. 189 * <p> 190 * Developed by Benoit Xhenseval (www.ObjectLab.co.uk). 191 * 192 * @param source the source series. 193 * @param name the name of the new series. 194 * @param pointCount the number of POINTS used in the average calculation 195 * (not periods!) 196 * 197 * @return The moving average series. 198 */ 199 public static TimeSeries createPointMovingAverage(TimeSeries source, 200 String name, int pointCount) { 201 202 if (source == null) { 203 throw new IllegalArgumentException("Null 'source'."); 204 } 205 206 if (pointCount < 2) { 207 throw new IllegalArgumentException("periodCount must be greater " + 208 "than or equal to 2."); 209 } 210 211 TimeSeries result = new TimeSeries(name, source.getTimePeriodClass()); 212 double rollingSumForPeriod = 0.0; 213 for (int i = 0; i < source.getItemCount(); i++) { 214 // get the current data item... 215 TimeSeriesDataItem current = source.getDataItem(i); 216 RegularTimePeriod period = current.getPeriod(); 217 rollingSumForPeriod += current.getValue().doubleValue(); 218 219 if (i > pointCount - 1) { 220 // remove the point i-periodCount out of the rolling sum. 221 TimeSeriesDataItem startOfMovingAvg = source.getDataItem( 222 i - pointCount); 223 rollingSumForPeriod -= startOfMovingAvg.getValue() 224 .doubleValue(); 225 result.add(period, rollingSumForPeriod / pointCount); 226 } 227 else if (i == pointCount - 1) { 228 result.add(period, rollingSumForPeriod / pointCount); 229 } 230 } 231 return result; 232 } 233 234 /** 235 * Creates a new {@link XYDataset} containing the moving averages of each 236 * series in the <code>source</code> dataset. 237 * 238 * @param source the source dataset. 239 * @param suffix the string to append to source series names to create 240 * target series names. 241 * @param period the averaging period. 242 * @param skip the length of the initial skip period. 243 * 244 * @return The dataset. 245 */ 246 public static XYDataset createMovingAverage(XYDataset source, String suffix, 247 long period, long skip) { 248 249 return createMovingAverage(source, suffix, (double) period, 250 (double) skip); 251 252 } 253 254 255 /** 256 * Creates a new {@link XYDataset} containing the moving averages of each 257 * series in the <code>source</code> dataset. 258 * 259 * @param source the source dataset. 260 * @param suffix the string to append to source series names to create 261 * target series names. 262 * @param period the averaging period. 263 * @param skip the length of the initial skip period. 264 * 265 * @return The dataset. 266 */ 267 public static XYDataset createMovingAverage(XYDataset source, 268 String suffix, double period, double skip) { 269 270 if (source == null) { 271 throw new IllegalArgumentException("Null source (XYDataset)."); 272 } 273 274 XYSeriesCollection result = new XYSeriesCollection(); 275 276 for (int i = 0; i < source.getSeriesCount(); i++) { 277 XYSeries s = createMovingAverage(source, i, source.getSeriesKey(i) 278 + suffix, period, skip); 279 result.addSeries(s); 280 } 281 282 return result; 283 284 } 285 286 /** 287 * Creates a new {@link XYSeries} containing the moving averages of one 288 * series in the <code>source</code> dataset. 289 * 290 * @param source the source dataset. 291 * @param series the series index (zero based). 292 * @param name the name for the new series. 293 * @param period the averaging period. 294 * @param skip the length of the initial skip period. 295 * 296 * @return The dataset. 297 */ 298 public static XYSeries createMovingAverage(XYDataset source, 299 int series, String name, double period, double skip) { 300 301 if (source == null) { 302 throw new IllegalArgumentException("Null source (XYDataset)."); 303 } 304 if (period < Double.MIN_VALUE) { 305 throw new IllegalArgumentException("period must be positive."); 306 } 307 if (skip < 0.0) { 308 throw new IllegalArgumentException("skip must be >= 0.0."); 309 310 } 311 312 XYSeries result = new XYSeries(name); 313 314 if (source.getItemCount(series) > 0) { 315 316 // if the initial averaging period is to be excluded, then 317 // calculate the lowest x-value to have an average calculated... 318 double first = source.getXValue(series, 0) + skip; 319 320 for (int i = source.getItemCount(series) - 1; i >= 0; i--) { 321 322 // get the current data item... 323 double x = source.getXValue(series, i); 324 325 if (x >= first) { 326 // work out the average for the earlier values... 327 int n = 0; 328 double sum = 0.0; 329 double limit = x - period; 330 int offset = 0; 331 boolean finished = false; 332 333 while (!finished) { 334 if ((i - offset) >= 0) { 335 double xx = source.getXValue(series, i - offset); 336 Number yy = source.getY(series, i - offset); 337 if (xx > limit) { 338 if (yy != null) { 339 sum = sum + yy.doubleValue(); 340 n = n + 1; 341 } 342 } 343 else { 344 finished = true; 345 } 346 } 347 else { 348 finished = true; 349 } 350 offset = offset + 1; 351 } 352 if (n > 0) { 353 result.add(x, sum / n); 354 } 355 else { 356 result.add(x, null); 357 } 358 } 359 360 } 361 } 362 363 return result; 364 365 } 366 367 }