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