GPLIB++
ShortCorr.h
Go to the documentation of this file.
1 #ifndef SHORTCORR_H_
2 #define SHORTCORR_H_
3 #include "FatalException.h"
4 #include "statutils.h"
5 #include <algorithm>
6 #include <numeric>
7 
8 namespace gplib
9  {
10 
11  /** \addtogroup tstools Time series analysis methods */
12  /* @{ */
13 
14  //! Calculate the correlation between a short time series and a master time series
15  /*! The short time series will be shifted along the master and the corresponding correlation will be stored in
16  * the structure referenced by outpebegin, make sure that structure can hold enough data*/
17  template<typename _InputIterator, typename _OutputIterator>
18  void ShortCorr(_InputIterator masterbegin, _InputIterator masterend,
19  _InputIterator shortbegin, _InputIterator shortend,
20  _OutputIterator outbegin)
21  {
22  //check the length of both time series, the "short" time series has to be shorter than the master
23  if (distance(shortbegin, shortend) > distance(masterbegin, masterend))
24  throw FatalException("Short time series longer than master !");
25 
26  typedef std::vector<
27  typename std::iterator_traits<_InputIterator>::value_type>
28  tinvector;
29  _InputIterator currstart = masterbegin;
30  _OutputIterator currout = outbegin;
31  const int shortsize = distance(shortbegin, shortend);
32 
33  // create a local copy of the short time series, so we can do some manipulation
34  tinvector shortts(shortbegin, shortend);
35  SubMean(shortts.begin(), shortts.end());
36  //calculate the zero lag autocorrelation of the short time series
37  tinvector shortauto(shortts.begin(), shortts.end());
38  std::transform(shortauto.begin(), shortauto.end(), shortauto.begin(),
39  shortauto.begin(), std::multiplies<double>());
40  const double shortpower = std::accumulate(shortauto.begin(),
41  shortauto.end(),
42  typename std::iterator_traits<_OutputIterator>::value_type());
43  // go through the time series and calculate the correlation
44  while (currstart + shortsize < masterend)
45  {
46  tinvector currmul(currstart, currstart + shortsize);
47 
48  SubMean(currmul.begin(), currmul.end());
49  tinvector currauto(currmul.begin(), currmul.end());
50  std::transform(currauto.begin(), currauto.end(), currauto.begin(),
51  currauto.begin(), std::multiplies<double>());
52  double currpower = std::accumulate(currauto.begin(),
53  currauto.end(),
54  typename std::iterator_traits<_OutputIterator>::value_type());
55 
56  std::transform(currmul.begin(), currmul.end(), shortts.begin(),
57  currmul.begin(), std::multiplies<double>());
58  *currout = std::accumulate(currmul.begin(), currmul.end(),
59  typename std::iterator_traits<_OutputIterator>::value_type());
60  *currout /= sqrt(shortpower * currpower);
61  ++currout;
62  ++currstart;
63  }
64  }
65  /* @} */
66  }
67 #endif /*SHORTCORR_H_*/
void SubMean(InputIterator begin, InputIterator end, typename std::iterator_traits< InputIterator >::value_type mean)
Substract the mean from each element in the container, mean is passed as a parameter.
Definition: statutils.h:85
void ShortCorr(_InputIterator masterbegin, _InputIterator masterend, _InputIterator shortbegin, _InputIterator shortend, _OutputIterator outbegin)
Calculate the correlation between a short time series and a master time series.
Definition: ShortCorr.h:18
The basic exception class for all errors that arise in gplib.