1 #ifndef FAIF_TS_PREDICTIONS 2 #define FAIF_TS_PREDICTIONS 9 #include <boost/noncopyable.hpp> 10 #include <boost/tuple/tuple.hpp> 12 #include "TimeSeries.hpp" 15 namespace timeseries {
18 class PredictionVisitor;
35 if( history_.size() == 0)
41 TimeSeriesDigit::const_iterator it =
42 std::lower_bound( history_.begin(), history_.end(), v,
45 if( it != history_.end() ) {
49 return history_.back();
63 DigitTime history_last = std::min(to + 1, 0);
66 for(
DigitTime t = from; t < history_last; ++t)
72 TimeSeriesDigit::const_iterator first = pred.begin();
75 for(;first != pred.end(); ++first)
76 out.push_back( *first );
111 struct KNNDef :
public boost::tuple<int,size_t> {
114 KNNDef(
int k,
size_t ref_size) : boost::tuple<int,size_t>(k, ref_size) {}
116 int getK()
const {
return get<0>(); }
132 :
Prediction(history), definition_(definition)
142 const ARDef&
getARDef()
const {
return definition_; }
153 assert( t < static_cast<int>(predictions_.size()) );
154 return predictions_[t];
161 for(
DigitTime i=0;i<static_cast<int>(definition_.size());++i) {
162 val += definition_[i] *
get(time - i - 1).getValue();
171 if( ! predictions_.empty() )
172 last_predicted = predictions_.back().getTime();
174 if(n > last_predicted) {
175 for(
DigitTime t = last_predicted + 1; t <= n; ++t ) {
177 predictions_.push_back( calculateAR(t) );
185 inline std::ostream& operator<<(std::ostream& os,
const PredictionAR& ar) {
187 std::copy(ar.
getARDef().begin(), ar.
getARDef().end(), std::ostream_iterator<double>(os,
",") );
194 typedef TimeSeriesDigit::const_iterator TSIter;
203 Value series_distance(TSIter begin1, TSIter end1, TSIter begin2, TSIter end2) {
204 if( distance(begin1, end1) > distance(begin2, end2) ) {
210 return std::inner_product( begin1, end1, begin2, 0.0, std::plus<double>(), boost::bind(&element_distance, _1, _2 ) );
213 struct BestRangesFunctor : boost::noncopyable {
215 BestRangesFunctor(TSIter begin, TSIter end, TSIter last_begin,
219 : NUM_NEIGHBOURS(num_neighbours)
221 for(; begin != last_begin; ++begin) {
222 Value dist = series_distance( reference.begin(), reference.end(), begin, end );
227 void add(
Value dist, TSIter it) {
229 best_.insert( make_pair(dist, it) );
230 if( best_.size() > NUM_NEIGHBOURS )
231 best_.erase(--best_.end() );
234 Value avg(
DigitTime offset,
int reference_length, TSIter last_iter)
const {
236 for(BestIter ii = best_.begin(); ii != best_.end(); ++ii ) {
237 TSIter it_best = ii->second;
238 if( distance(it_best, last_iter) > (reference_length + offset) ) {
239 advance(it_best, reference_length + offset );
240 v += it_best->getValue();
244 return v/best_.size();
250 const unsigned int NUM_NEIGHBOURS;
253 std::multimap<Value,TSIter> best_;
254 typedef std::multimap<Value,TSIter>::const_iterator BestIter;
257 inline std::ostream& operator<<(std::ostream& os,
const BestRangesFunctor& b) {
258 for( BestRangesFunctor::BestIter ii = b.best_.begin(); ii != b.best_.end(); ++ii ) {
259 os << (ii->second)->getTime() <<
',';
271 :
Prediction(history), definition_(definition)
274 TimeSeriesDigit::const_iterator reference_begin = historical.end();
275 TimeSeriesDigit::const_iterator reference_end = historical.end();
277 int ref_size =
static_cast<int>(definition.
getRefSize() );
280 if( distance(historical.begin(), historical.end() ) > ref_size )
281 std::advance( reference_begin, - ref_size);
283 reference_begin = historical.begin();
304 assert( t < static_cast<int>(predictions_.size()) );
305 return predictions_[t];
313 predictions_.clear();
319 int min_hist_size =
static_cast<int>( reference_.size() ) + 1;
320 TSIter last_begin = ts.end();
321 if(min_hist_size < static_cast<int>( ts.size() ) )
322 advance(last_begin, -min_hist_size);
324 last_begin = ts.begin();
329 BestRangesFunctor best(ts.begin(), ts.end(), last_begin, reference_, definition_.
getK() );
333 Value v = best.avg(t, static_cast<int>(definition_.
getRefSize()), ts.end() );
343 inline std::ostream& operator<<(std::ostream& os,
const PredictionKNN& knn) {
351 PrintVisitor(std::ostream& os) : os_(os) {}
360 inline std::ostream& operator<<(std::ostream& os,
const Prediction& pred) {
372 #endif //FAIF_TS_PREDICTIONS std::vector< double > ARDef
the AR parameter collection f(t) = a[0]f(t-1) + a[1]f(t-2) + ... a[n-1]f(t-n)
Definition: Predictions.hpp:106
virtual void accept(PredictionVisitor &v) const
Definition: Predictions.hpp:139
bad prediction range exception
Definition: TimeseriesExceptions.hpp:11
int getK() const
Definition: Predictions.hpp:116
virtual void accept(PredictionVisitor &v) const =0
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
double Value
value - real number
Definition: TimeSeries.hpp:49
const DigitTime & getTime() const
Definition: TimeSeries.hpp:138
PredictionAR(const TimeSeriesDigit &history, const ARDef &definition)
Definition: Predictions.hpp:131
const KNNDef & getKNNDef() const
Definition: Predictions.hpp:290
size_t getRefSize() const
Definition: Predictions.hpp:118
Definition: TimeSeries.hpp:151
virtual ~PredictionAR()
Definition: Predictions.hpp:136
Definition: TimeSeries.hpp:131
const TimeValueDigit & getHistoricalValue(DigitTime t) const
Definition: Predictions.hpp:34
virtual void accept(PredictionVisitor &v) const
Definition: Predictions.hpp:293
Definition: Predictions.hpp:267
Definition: Predictions.hpp:21
const TimeSeriesDigit & getHistory() const
Definition: Predictions.hpp:28
TimeSeriesDigit calculatePrediction(DigitTime from, DigitTime to)
Definition: Predictions.hpp:57
const ARDef & getARDef() const
Definition: Predictions.hpp:142
Definition: Predictions.hpp:128
the KNN parameters, k = num_neighbours, ref_size = size of reference block
Definition: Predictions.hpp:111
KNNDef(int k, size_t ref_size)
Definition: Predictions.hpp:114
Definition: Predictions.hpp:95