RMOL Logo Get Revenue Management Optimisation Library at SourceForge.net. Fast, secure and Free Open Source software downloads

MAForecast.cpp

Go to the documentation of this file.
00001 // //////////////////////////////////////////////////////////////////////
00002 // Import section
00003 // //////////////////////////////////////////////////////////////////////
00004 // C
00005 #include <assert.h>
00006 // STL
00007 #include <cmath>
00008 // RMOL Bom
00009 #include <rmol/bom/MAForecast.hpp>
00010 
00011 namespace RMOL {
00012 
00013   // //////////////////////////////////////////////////////////////////////
00014   bool MAForecast::smoothByMovingAverage (const DataList_T& iInputData,
00015                                           DataList_T& ioOutputData,
00016                                           const TimeAbscissa_T& iSpan) {
00017 
00018     const TimeAbscissa_T lInputDateSize = iInputData.size();
00019     if (iSpan == 0 || iSpan >= lInputDateSize) {
00020       return false;
00021     }
00022 
00023     ioOutputData.clear();
00024     ioOutputData.reserve (lInputDateSize);
00025 
00026     const DataType_T lFirstDataElement = iInputData.at(0);
00027     ioOutputData.push_back (lFirstDataElement);
00028 
00029     DataType_T sum = 0;
00030     for (TimeAbscissa_T x = 1; x != lInputDateSize; x++) {
00031       const TimeAbscissa_T p = std::min (x, iSpan);
00032 
00033       if ( p <= iSpan ) {
00034         sum = 0;
00035 
00036         for (TimeAbscissa_T y = 0; y != p; y++) {
00037           sum += iInputData.at(x-y);
00038         }
00039         
00040       } else {
00041         // save cpu cycles: add current
00042         // value and subtract least recent
00043         sum = sum - iInputData.at(x-p) + iInputData.at(x);
00044       }
00045 
00046       ioOutputData.push_back (sum / p);
00047     }
00048 
00049     return true;
00050   }    
00051 
00052 }
SourceForge Logo

Generated on Fri Jul 30 21:53:12 2010 for RMOL by Doxygen 1.6.1