Skip to content

Commit d219bea

Browse files
authored
[ML] Change point detection and prediction (#92)
Closes #6.
1 parent d5211a7 commit d219bea

File tree

90 files changed

+5636
-2106
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

90 files changed

+5636
-2106
lines changed

docs/CHANGELOG.asciidoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
Improve and use periodic boundary condition for seasonal component modeling ({pull}84[#84])
3131
Improve robustness w.r.t. outliers of detection and initialisation of seasonal components ({pull}90[#90])
32+
Explicit change point detection and modelling ({pull}92[#92])
3233

3334
=== Bug Fixes
3435

include/core/CContainerPrinter.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,7 +274,7 @@ class CORE_EXPORT CContainerPrinter : private CNonInstantiatable {
274274
return *value;
275275
}
276276

277-
//! Print a boost::shared_pointer.
277+
//! Print a std::shared_pointer.
278278
template<typename T>
279279
static std::string printElement(const std::shared_ptr<T>& value) {
280280
if (value == std::shared_ptr<T>()) {

include/core/CRapidJsonWriterBase.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ class CRapidJsonWriterBase
7474
using TValue = rapidjson::Value;
7575
using TDocumentWeakPtr = std::weak_ptr<TDocument>;
7676
using TValuePtr = std::shared_ptr<TValue>;
77-
7877
using TPoolAllocatorPtr = std::shared_ptr<CRapidJsonPoolAllocator>;
7978
using TPoolAllocatorPtrStack = std::stack<TPoolAllocatorPtr>;
8079
using TStrPoolAllocatorPtrMap = boost::unordered_map<std::string, TPoolAllocatorPtr>;

include/core/Constants.h

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,38 +16,41 @@ namespace ml {
1616
namespace core {
1717
namespace constants {
1818

19+
//! A minute in seconds.
20+
const core_t::TTime MINUTE{60};
21+
1922
//! An hour in seconds.
20-
const core_t::TTime HOUR = 3600;
23+
const core_t::TTime HOUR{3600};
2124

2225
//! A day in seconds.
23-
const core_t::TTime DAY = 86400;
26+
const core_t::TTime DAY{86400};
2427

2528
//! A (two day) weekend in seconds.
26-
const core_t::TTime WEEKEND = 172800;
29+
const core_t::TTime WEEKEND{172800};
2730

2831
//! Five weekdays in seconds.
29-
const core_t::TTime WEEKDAYS = 432000;
32+
const core_t::TTime WEEKDAYS{432000};
3033

3134
//! A week in seconds.
32-
const core_t::TTime WEEK = 604800;
35+
const core_t::TTime WEEK{604800};
3336

3437
//! A (364 day) year in seconds.
35-
const core_t::TTime YEAR = 31449600;
38+
const core_t::TTime YEAR{31449600};
3639

3740
//! Log of min double.
38-
const double LOG_MIN_DOUBLE = std::log(std::numeric_limits<double>::min());
41+
const double LOG_MIN_DOUBLE{std::log(std::numeric_limits<double>::min())};
3942

4043
//! Log of max double.
41-
const double LOG_MAX_DOUBLE = std::log(std::numeric_limits<double>::max());
44+
const double LOG_MAX_DOUBLE{std::log(std::numeric_limits<double>::max())};
4245

4346
//! Log of double epsilon.
44-
const double LOG_DOUBLE_EPSILON = std::log(std::numeric_limits<double>::epsilon());
47+
const double LOG_DOUBLE_EPSILON{std::log(std::numeric_limits<double>::epsilon())};
4548

4649
//! Log of two.
47-
const double LOG_TWO = 0.693147180559945;
50+
const double LOG_TWO{0.693147180559945};
4851

4952
//! Log of two pi.
50-
const double LOG_TWO_PI = 1.83787706640935;
53+
const double LOG_TWO_PI{1.83787706640935};
5154

5255
#ifdef Windows
5356
const char PATH_SEPARATOR = '\\';

include/maths/CBasicStatistics.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,14 @@ class MATHS_EXPORT CBasicStatistics {
201201
}
202202
}
203203

204+
//! Update the moments with the collection \p x.
205+
template<typename U, std::size_t N>
206+
void add(const core::CSmallVector<U, N>& x) {
207+
for (const auto& xi : x) {
208+
this->add(xi);
209+
}
210+
}
211+
204212
//! Update the moments with the collection \p x.
205213
template<typename U>
206214
void add(const std::vector<SSampleCentralMoments<U, ORDER>>& x) {

include/maths/CCalendarComponent.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@ class MATHS_EXPORT CCalendarComponent : private CDecompositionComponent {
8282
//! Clear all data.
8383
void clear();
8484

85+
//! Linearly scale the component's by \p scale.
86+
void linearScale(core_t::TTime time, double scale);
87+
8588
//! Adds a value \f$(t, f(t))\f$ to this component.
8689
//!
8790
//! \param[in] time The time of the point.

include/maths/CCalendarComponentAdaptiveBucketing.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ class MATHS_EXPORT CCalendarComponentAdaptiveBucketing : private CAdaptiveBucket
6767
//! allocated memory.
6868
void clear();
6969

70+
//! Linearly scale the bucket values by \p scale.
71+
void linearScale(double scale);
72+
7073
//! Add the function value at \p time.
7174
//!
7275
//! \param[in] time The time of \p value.

include/maths/CModel.h

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,11 @@ using TForecastPushDatapointFunc = std::function<void(SErrorBar)>;
4444
class MATHS_EXPORT CModelParams {
4545
public:
4646
CModelParams(core_t::TTime bucketLength,
47-
const double& learnRate,
48-
const double& decayRate,
49-
double minimumSeasonalVarianceScale);
47+
double learnRate,
48+
double decayRate,
49+
double minimumSeasonalVarianceScale,
50+
core_t::TTime minimumTimeToDetectChange,
51+
core_t::TTime maximumTimeToTestForChange);
5052

5153
//! Get the bucket length.
5254
core_t::TTime bucketLength() const;
@@ -63,6 +65,15 @@ class MATHS_EXPORT CModelParams {
6365
//! Get the minimum seasonal variance scale.
6466
double minimumSeasonalVarianceScale() const;
6567

68+
//! Check if we should start testing for a change point in the model.
69+
bool testForChange(core_t::TTime changeInterval) const;
70+
71+
//! Get the minimum time to detect a change point in the model.
72+
core_t::TTime minimumTimeToDetectChange(void) const;
73+
74+
//! Get the maximum time to test for a change point in the model.
75+
core_t::TTime maximumTimeToTestForChange(void) const;
76+
6677
//! Set the probability that the bucket will be empty for the model.
6778
void probabilityBucketEmpty(double probability);
6879

@@ -78,6 +89,10 @@ class MATHS_EXPORT CModelParams {
7889
double m_DecayRate;
7990
//! The minimum seasonal variance scale.
8091
double m_MinimumSeasonalVarianceScale;
92+
//! The minimum time permitted to detect a change in the model.
93+
core_t::TTime m_MinimumTimeToDetectChange;
94+
//! The maximum time permitted to test for a change in the model.
95+
core_t::TTime m_MaximumTimeToTestForChange;
8196
//! The probability that a bucket will be empty for the model.
8297
double m_ProbabilityBucketEmpty;
8398
};
@@ -90,8 +105,6 @@ class MATHS_EXPORT CModelAddSamplesParams {
90105

91106
public:
92107
CModelAddSamplesParams();
93-
CModelAddSamplesParams(const CModelAddSamplesParams&) = delete;
94-
const CModelAddSamplesParams& operator=(const CModelAddSamplesParams&) = delete;
95108

96109
//! Set whether or not the data are integer valued.
97110
CModelAddSamplesParams& integer(bool integer);
@@ -145,8 +158,6 @@ class MATHS_EXPORT CModelProbabilityParams {
145158

146159
public:
147160
CModelProbabilityParams();
148-
CModelProbabilityParams(const CModelAddSamplesParams&) = delete;
149-
const CModelProbabilityParams& operator=(const CModelAddSamplesParams&) = delete;
150161

151162
//! Set the tag for the entity for which to compute the probability.
152163
CModelProbabilityParams& tag(std::size_t tag);
@@ -254,6 +265,9 @@ class MATHS_EXPORT CModel {
254265
E_Reset //!< Model reset.
255266
};
256267

268+
//! Combine the results \p lhs and \p rhs.
269+
static EUpdateResult combine(EUpdateResult lhs, EUpdateResult rhs);
270+
257271
public:
258272
CModel(const CModelParams& params);
259273
virtual ~CModel() = default;

0 commit comments

Comments
 (0)