1 #ifndef FAIF_TIME_SERIES 2 #define FAIF_TIME_SERIES 6 #if defined(_MSC_VER) && (_MSC_VER >= 1400) 8 #pragma warning(disable:4100) 9 #pragma warning(disable:4512) 10 #pragma warning(disable:4127) 11 #pragma warning(disable:4245) 13 #pragma warning(disable:4996) 24 #include <boost/bind.hpp> 25 #include <boost/tuple/tuple.hpp> 26 #include <boost/tuple/tuple_comparison.hpp> 27 #include <boost/date_time/posix_time/posix_time.hpp> 32 namespace timeseries {
41 boost::posix_time::ptime start(boost::gregorian::date(1970,1,1),boost::posix_time::time_duration(0,0,0));
42 return (real_time-start).total_seconds();
58 : boost::tuple<RealTime,Value,Quality>(t,v,q)
61 const RealTime&
getTime()
const {
return get<0>(); }
63 const Value&
getValue()
const {
return get<1>(); }
69 inline std::ostream& operator<<(std::ostream& os,
const TimeValueReal& time_val) {
78 typedef std::vector<TimeValueReal>::iterator iterator;
79 typedef std::vector<TimeValueReal>::const_iterator const_iterator;
95 TimeSeriesReal(
const long* time_begin,
const long* time_end,
const Value* value_begin);
99 TimeSeriesReal(
const RealTime& start_time,
const RealDuration& delta,
100 const Value* value_begin,
const Value* value_end );
106 std::vector<TimeValueReal>::operator=(t);
114 return std::accumulate( begin(), end(), 0.0, boost::bind( std::plus<double>(), _1, boost::bind(&
TimeValueReal::getValue, _2) ) );
118 double getAvg()
const {
return empty() ? 0.0 : getSum()/size(); }
121 double getIntegral()
const;
125 inline std::ostream& operator<<(std::ostream& os,
const TimeSeriesReal& time_series) {
126 std::copy( time_series.begin(), time_series.end(), std::ostream_iterator<TimeValueReal>(os,
" ") );
135 : boost::tuple<DigitTime,Value,Quality>(t,v,q)
138 const DigitTime&
getTime()
const {
return get<0>(); }
145 inline std::ostream& operator<<(std::ostream& os,
const TimeValueDigit& time_val) {
153 typedef std::vector<TimeValueDigit>::iterator iterator;
154 typedef std::vector<TimeValueDigit>::const_iterator const_iterator;
175 std::vector<TimeValueDigit>::operator=(t);
190 return std::accumulate( begin(), end(), 0.0,
196 return std::accumulate( begin(), end(), 0.0,
197 boost::bind( std::plus<double>(), _1,
198 boost::bind( std::multiplies<double>(),
206 double getAvg()
const {
return empty() ? 0.0 : getSum()/size(); }
212 double getSigma()
const {
return empty()? 0.0 : std::sqrt( getSigmaSquared() )/ size(); }
222 typedef TimeSeriesDigit::const_iterator TSIter;
224 TSIter it1 = ts1.begin();
225 TSIter it2 = ts2.begin();
227 double sum_abs = 0.0;
230 while( it1 != ts1.end() || it2 != ts2.end() ) {
233 if(it2 == ts2.end() || (it1 != ts1.end() && it1->getTime() < it2->getTime() ) ) {
234 sum_abs += std::abs(it1->getValue() );
236 }
else if(it1 == ts1.end() || it2->getTime() < it1->getTime() ) {
237 sum_abs += std::abs(it2->getValue() );
240 sum_abs += std::abs(it1->getValue() - it2->getValue() );
245 return count > 0 ? sum_abs/(double)count : 0.0;
251 typedef TimeSeriesDigit::const_iterator TSIter;
253 TSIter it1 = ts1.begin();
254 TSIter it2 = ts2.begin();
256 double sum_rel = 0.0;
259 while( it1 != ts1.end() || it2 != ts2.end() ) {
261 if(it2 == ts2.end() || (it1 != ts1.end() && it1->getTime() < it2->getTime() ) ) {
265 }
else if(it1 == ts1.end() || it2->getTime() < it1->getTime() ) {
270 double m = ( it1->getValue() + it2->getValue() ) / 2.0;
272 if( ( m > (2 * std::numeric_limits<double>::epsilon() ) )
275 sum_rel += std::abs( (it1->getValue() - it2->getValue() )/ m );
281 return count > 0 ? sum_rel/(double)count : 0.0;
285 inline std::ostream& operator<<(std::ostream& os,
const TimeSeriesDigit& time_series) {
286 std::copy( time_series.begin(), time_series.end(), std::ostream_iterator<TimeValueDigit>(os,
" ") );
298 struct FromTimestampValueGenerator {
300 TimeValueReal operator()(
const long& time,
const double& value) {
301 return TimeValueReal( boost::posix_time::from_time_t(time), value );
307 struct FromValueGenerator {
308 FromValueGenerator(
const RealTime& t,
const RealDuration& d)
309 : curr_(t), delta_(d) {}
313 RealTime out = curr_;
325 FromTimestampValueGenerator gen;
326 std::transform( begin, end, input2, back_inserter(*
this), gen );
331 const Value* value_begin,
const Value* value_end ) {
333 FromValueGenerator gen(start_time, delta);
334 std::transform( value_begin, value_end, back_inserter(*
this), gen );
339 typedef std::vector<TimeValueReal>::const_iterator RealConstIter;
342 double trapeziumSquare(
double a,
double b,
double h) {
347 double trapeziumSquare(
const std::vector<TimeValueReal>::const_iterator it) {
348 RealConstIter it_next(it);
350 long len_sec = boost::posix_time::time_period(it_next->getTime(), it->getTime()).length().total_seconds();
351 return trapeziumSquare(it->getValue(), it_next->getValue(), std::abs(len_sec) );
360 std::vector<TimeValueReal>::const_iterator last = end();
363 for(RealConstIter it = begin(); it != last; ++it ) {
364 sum += trapeziumSquare( it );
384 struct NextStepFunctor {
386 NextStepFunctor() : curr_(-1) { }
391 typedef TimeSeriesDigit::const_iterator TSIter;
393 struct CalcCorrelationFunctor {
395 double avg1 = 0.0,
double avg2 = 0.0,
double sig1 = 1.0,
double sig2 = 1.0)
396 : ts1_(ts1), ts2_(ts2), offset_(0), avg1_(avg1), avg2_(avg2), sig1_(sig1), sig2_(sig2)
402 TSIter it1 = ts1_.begin();
403 TSIter it2 = ts2_.begin();
404 while( it1 != ts1_.end() && it2 != ts2_.end() && it1->getTime() != (it2->getTime() + offset_) ) {
405 if( it1->getTime() < (it2->getTime() + offset_) ) ++it1;
410 for(; it1 != ts1_.end() && it2 != ts2_.end(); ++it1, ++it2 ) {
411 val += it1->getValue() * it2->getValue();
415 val -= count * avg1_* avg2_;
417 val /= (count - 1)* sig1_ * sig2_;
419 val /= sig1_ * sig2_;
437 NextStepFunctor functor;
438 std::transform( begin, end, std::back_inserter(*
this), functor );
445 std::transform( ts.begin(), ts.end(), std::back_inserter(*
this), boost::bind(&moveFun, _1, offset ) );
456 CalcCorrelationFunctor gen(*
this, *
this);
457 std::generate_n(back_inserter(out), scope, gen );
465 generate_n(back_inserter(out), scope, gen );
473 #endif //FAIF_TIME_SERIES TimeSeriesDigit correlation(const TimeSeriesDigit &ts1, const TimeSeriesDigit &ts2, int scope)
Definition: TimeSeries.hpp:462
TimeSeriesReal(const_iterator begin, const_iterator end)
c-tor from a range
Definition: TimeSeries.hpp:91
double getAvgAbsDiff(const TimeSeriesDigit &ts1, const TimeSeriesDigit &ts2)
Definition: TimeSeries.hpp:220
timeseries - time hold as RealTime
Definition: TimeSeries.hpp:76
double getSumSquared() const
Definition: TimeSeries.hpp:195
double getIntegral() const
Definition: TimeSeries.hpp:356
double Quality
quality - real 0..1
Definition: TimeSeries.hpp:51
const Value & getValue() const
Definition: TimeSeries.hpp:140
int DigitTime
digit time type. DigitTime < 0 past, DigitTime >= 0 future, DigitTime == 0 now.
Definition: TimeSeries.hpp:47
TimeSeriesReal(const TimeValueReal *begin, const TimeValueReal *end)
c-tor from a range
Definition: TimeSeries.hpp:88
double getSum() const
Definition: TimeSeries.hpp:113
TimeSeriesDigit()
c-tor, empty timeseries
Definition: TimeSeries.hpp:157
TimeValueDigit(DigitTime t=0, const Value &v=0.0, const Quality &q=0.0)
Definition: TimeSeries.hpp:134
TimeSeriesDigit(const_iterator begin, const_iterator end)
c-tor, from range
Definition: TimeSeries.hpp:162
double Value
value - real number
Definition: TimeSeries.hpp:49
TimeSeriesDigit & operator=(const TimeSeriesDigit &t)
Definition: TimeSeries.hpp:174
const RealTime & getTime() const
Definition: TimeSeries.hpp:61
TimeSeriesReal & operator=(const TimeSeriesReal &t)
Definition: TimeSeries.hpp:105
const DigitTime & getTime() const
Definition: TimeSeries.hpp:138
const Quality & getQuality() const
Definition: TimeSeries.hpp:65
TimeSeriesReal(const TimeSeriesReal &t)
copy c-tor
Definition: TimeSeries.hpp:85
double getSigmaSquared() const
Definition: TimeSeries.hpp:209
double getAvgRelDiff(const TimeSeriesDigit &ts1, const TimeSeriesDigit &ts2)
Definition: TimeSeries.hpp:249
TimeValueReal(RealTime t, const Value &v, const Quality &q=0.0)
Definition: TimeSeries.hpp:57
double getAvg() const
Definition: TimeSeries.hpp:118
TimeSeriesDigit autoCorrelationE(int scope) const
Definition: TimeSeries.hpp:454
double getAvg() const
Definition: TimeSeries.hpp:206
const Quality & getQuality() const
Definition: TimeSeries.hpp:142
double getSum() const
Definition: TimeSeries.hpp:189
timeseries value, single value in given real time. Plain old data
Definition: TimeSeries.hpp:54
Definition: TimeSeries.hpp:151
double getSigma() const
Definition: TimeSeries.hpp:212
Definition: TimeSeries.hpp:131
~TimeSeriesReal()
Definition: TimeSeries.hpp:110
TimeSeriesReal()
empty collection
Definition: TimeSeries.hpp:82
const Value & getValue() const
Definition: TimeSeries.hpp:63
TimeSeriesDigit(const TimeValueDigit *begin, const TimeValueDigit *end)
c-tor, from range
Definition: TimeSeries.hpp:159
long to_time_t(const RealTime &real_time)
convert from RealTime to posix_t (num of seconds from 1970)
Definition: TimeSeries.hpp:40
boost::posix_time::time_duration RealDuration
the real time duration
Definition: TimeSeries.hpp:37
boost::posix_time::ptime RealTime
the real time type
Definition: TimeSeries.hpp:35
TimeSeriesDigit(const TimeSeriesDigit &t)
copy c-tor
Definition: TimeSeries.hpp:168