-
Notifications
You must be signed in to change notification settings - Fork 62
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[ML] Store calendar periodicity historical error statistics in compressed format #127
Merged
tveasey
merged 7 commits into
elastic:master
from
tveasey:enhancement/deflate-calendar-periodicity-test
Jun 27, 2018
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
5f48eeb
Split CTrendTests into the two component tests
tveasey 45f3597
Store cyclic calendar test statistics in compressed format
tveasey 3ab4786
Merge branch 'master' into enhancement/deflate-calendar-periodicity-test
tveasey 3f932a7
Remove out-of-date comment
tveasey c282914
Update change log
tveasey e38a168
Review comments
tveasey a0f9cf1
Fix formatting
tveasey File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
#ifndef INCLUDED_ml_maths_CCalendarCyclicTest_h | ||
#define INCLUDED_ml_maths_CCalendarCyclicTest_h | ||
|
||
#include <core/CMemoryUsage.h> | ||
#include <core/CoreTypes.h> | ||
|
||
#include <maths/CCalendarFeature.h> | ||
#include <maths/CQuantileSketch.h> | ||
#include <maths/ImportExport.h> | ||
#include <maths/MathsTypes.h> | ||
|
||
#include <boost/optional.hpp> | ||
|
||
#include <cstdint> | ||
#include <vector> | ||
|
||
namespace ml { | ||
namespace core { | ||
class CStatePersistInserter; | ||
class CStateRestoreTraverser; | ||
} | ||
namespace maths { | ||
|
||
//! \brief The basic idea of this test is to see if there is stronger | ||
//! than expected temporal correlation between large prediction errors | ||
//! and calendar features. | ||
//! | ||
//! DESCRIPTION:\n | ||
//! This maintains prediction error statistics for a collection of | ||
//! calendar features. These are things like "day of month", | ||
//! ("day of week", "week month") pairs and so on. The test checks to | ||
//! see if the number of large prediction errors is statistically high, | ||
//! i.e. are there many more errors exceeding a specified percentile | ||
//! than one would expect given that this is expected to be binomial. | ||
//! Amongst features with statistically significant frequencies of large | ||
//! errors it returns the feature with the highest mean prediction error. | ||
class MATHS_EXPORT CCalendarCyclicTest { | ||
public: | ||
using TOptionalFeature = boost::optional<CCalendarFeature>; | ||
|
||
public: | ||
explicit CCalendarCyclicTest(double decayRate = 0.0); | ||
|
||
//! Initialize by reading state from \p traverser. | ||
bool acceptRestoreTraverser(core::CStateRestoreTraverser& traverser); | ||
|
||
//! Persist state by passing information to \p inserter. | ||
void acceptPersistInserter(core::CStatePersistInserter& inserter) const; | ||
|
||
//! Age the bucket values to account for \p time elapsed time. | ||
void propagateForwardsByTime(double time); | ||
|
||
//! Add \p error at \p time. | ||
void add(core_t::TTime time, double error, double weight = 1.0); | ||
|
||
//! Check if there are calendar components. | ||
TOptionalFeature test() const; | ||
|
||
//! Get a checksum for this object. | ||
std::uint64_t checksum(std::uint64_t seed = 0) const; | ||
|
||
//! Debug the memory used by this object. | ||
void debugMemoryUsage(core::CMemoryUsage::TMemoryUsagePtr mem) const; | ||
|
||
//! Get the memory used by this object. | ||
std::size_t memoryUsage() const; | ||
|
||
private: | ||
using TTimeVec = std::vector<core_t::TTime>; | ||
using TByte = unsigned char; | ||
using TByteVec = std::vector<TByte>; | ||
|
||
//! \brief Records the daily error statistics. | ||
struct MATHS_EXPORT SErrorStats { | ||
//! Get a checksum for this object. | ||
std::uint64_t checksum() const; | ||
//! Convert to a delimited string. | ||
std::string toDelimited() const; | ||
//! Initialize from a delimited string. | ||
bool fromDelimited(const std::string& str); | ||
|
||
std::uint32_t s_Count = 0; | ||
std::uint32_t s_LargeErrorCount = 0; | ||
CFloatStorage s_LargeErrorSum = 0.0; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. #include <core/CFloatStorage.h> There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For historical reasons this is forwarded in to the maths namespace in |
||
}; | ||
using TErrorStatsVec = std::vector<SErrorStats>; | ||
|
||
private: | ||
//! Winsorise \p error. | ||
double winsorise(double error) const; | ||
|
||
//! Get the significance of \p x large errors given \p n samples. | ||
double significance(double n, double x) const; | ||
|
||
//! Convert to a compressed representation. | ||
void deflate(const TErrorStatsVec& stats); | ||
|
||
//! Extract from the compressed representation. | ||
TErrorStatsVec inflate() const; | ||
|
||
private: | ||
//! The rate at which the error counts are aged. | ||
double m_DecayRate; | ||
|
||
//! Used to estimate large error thresholds. | ||
CQuantileSketch m_ErrorQuantiles; | ||
|
||
//! The start time of the bucket to which the last error | ||
//! was added. | ||
core_t::TTime m_CurrentBucketTime; | ||
|
||
//! The start time of the earliest bucket for which we have | ||
//! error statistics. | ||
core_t::TTime m_CurrentBucketIndex; | ||
|
||
//! The bucket statistics currently being updated. | ||
SErrorStats m_CurrentBucketErrorStats; | ||
|
||
//! The compressed error statistics. | ||
//! | ||
//! \note We always persist the errors in uncompressed format. | ||
TByteVec m_CompressedBucketErrorStats; | ||
}; | ||
} | ||
} | ||
|
||
#endif // INCLUDED_ml_maths_CCalendarCyclicTest_h |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#include <core/CMemoryUsage.h>