|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License; |
| 4 | + * you may not use this file except in compliance with the Elastic License. |
| 5 | + */ |
| 6 | + |
| 7 | +#ifndef INCLUDED_ml_maths_CExpandingWindow_h |
| 8 | +#define INCLUDED_ml_maths_CExpandingWindow_h |
| 9 | + |
| 10 | +#include <core/CFloatStorage.h> |
| 11 | +#include <core/CoreTypes.h> |
| 12 | +#include <core/CVectorRange.h> |
| 13 | + |
| 14 | +#include <maths/CBasicStatistics.h> |
| 15 | +#include <maths/ImportExport.h> |
| 16 | + |
| 17 | +#include <cstddef> |
| 18 | +#include <functional> |
| 19 | +#include <vector> |
| 20 | + |
| 21 | +namespace ml |
| 22 | +{ |
| 23 | +namespace core |
| 24 | +{ |
| 25 | +class CStatePersistInserter; |
| 26 | +class CStateRestoreTraverser; |
| 27 | +} |
| 28 | + |
| 29 | +namespace maths |
| 30 | +{ |
| 31 | + |
| 32 | +//! \brief Implements a fixed memory expanding time window. |
| 33 | +//! |
| 34 | +//! DESCRIPTION:\n |
| 35 | +//! As the window expands it compresses by merging adjacent values |
| 36 | +//! and maintaining means of merged values. It cycles through a |
| 37 | +//! sequence of increasing compression factors, which are determined |
| 38 | +//! by a sequence of increasing bucketing lengths supplied to the |
| 39 | +//! constructor. At the point it overflows, i.e. time since the |
| 40 | +//! beginning of the window exceeds "size" x "maximum bucket length", |
| 41 | +//! it will re-initialize the bucketing and update the start time. |
| 42 | +class MATHS_EXPORT CExpandingWindow |
| 43 | +{ |
| 44 | + public: |
| 45 | + using TDoubleVec = std::vector<double>; |
| 46 | + using TTimeVec = std::vector<core_t::TTime>; |
| 47 | + using TTimeCRng = core::CVectorRange<const TTimeVec>; |
| 48 | + using TFloatMeanAccumulator = CBasicStatistics::SSampleMean<CFloatStorage>::TAccumulator; |
| 49 | + using TFloatMeanAccumulatorVec = std::vector<TFloatMeanAccumulator>; |
| 50 | + using TPredictor = std::function<double (core_t::TTime)>; |
| 51 | + |
| 52 | + public: |
| 53 | + CExpandingWindow(core_t::TTime bucketLength, |
| 54 | + TTimeCRng bucketLengths, |
| 55 | + std::size_t size, |
| 56 | + double decayRate = 0.0); |
| 57 | + |
| 58 | + //! Initialize by reading state from \p traverser. |
| 59 | + bool acceptRestoreTraverser(core::CStateRestoreTraverser &traverser); |
| 60 | + |
| 61 | + //! Persist state by passing information to \p inserter. |
| 62 | + void acceptPersistInserter(core::CStatePersistInserter &inserter) const; |
| 63 | + |
| 64 | + //! Get the start time of the sketch. |
| 65 | + core_t::TTime startTime() const; |
| 66 | + |
| 67 | + //! Get the end time of the sketch. |
| 68 | + core_t::TTime endTime() const; |
| 69 | + |
| 70 | + //! Get the current bucket length. |
| 71 | + core_t::TTime bucketLength() const; |
| 72 | + |
| 73 | + //! Get the bucket values. |
| 74 | + const TFloatMeanAccumulatorVec &values() const; |
| 75 | + |
| 76 | + //! Get the bucket values minus the values from \p trend. |
| 77 | + TFloatMeanAccumulatorVec valuesMinusPrediction(const TPredictor &predictor) const; |
| 78 | + |
| 79 | + //! Set the start time to \p time. |
| 80 | + void initialize(core_t::TTime time); |
| 81 | + |
| 82 | + //! Age the bucket values to account for \p time elapsed time. |
| 83 | + void propagateForwardsByTime(double time); |
| 84 | + |
| 85 | + //! Add \p value at \p time. |
| 86 | + void add(core_t::TTime time, double value, double weight = 1.0); |
| 87 | + |
| 88 | + //! Check if we need to compress by increasing the bucket span. |
| 89 | + bool needToCompress(core_t::TTime time) const; |
| 90 | + |
| 91 | + //! Get a checksum for this object. |
| 92 | + uint64_t checksum(uint64_t seed = 0) const; |
| 93 | + |
| 94 | + //! Debug the memory used by this object. |
| 95 | + void debugMemoryUsage(core::CMemoryUsage::TMemoryUsagePtr mem) const; |
| 96 | + |
| 97 | + //! Get the memory used by this object. |
| 98 | + std::size_t memoryUsage() const; |
| 99 | + |
| 100 | + private: |
| 101 | + //! The rate at which the bucket values are aged. |
| 102 | + double m_DecayRate; |
| 103 | + |
| 104 | + //! The data bucketing length. |
| 105 | + core_t::TTime m_BucketLength; |
| 106 | + |
| 107 | + //! The bucket lengths to test. |
| 108 | + TTimeCRng m_BucketLengths; |
| 109 | + |
| 110 | + //! The index in m_BucketLengths of the current bucketing interval. |
| 111 | + std::size_t m_BucketLengthIndex; |
| 112 | + |
| 113 | + //! The time of the first data point. |
| 114 | + core_t::TTime m_StartTime; |
| 115 | + |
| 116 | + //! The bucket values. |
| 117 | + TFloatMeanAccumulatorVec m_BucketValues; |
| 118 | + |
| 119 | + //! The mean value time modulo the data bucketing length. |
| 120 | + TFloatMeanAccumulator m_MeanOffset; |
| 121 | +}; |
| 122 | + |
| 123 | +} |
| 124 | +} |
| 125 | + |
| 126 | +#endif // INCLUDED_ml_maths_CExpandingWindow_h |
0 commit comments