Skip to content

[ML] Improve robustness to very low variance data #232

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

Merged
merged 1 commit into from
Oct 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion include/maths/CPeriodicityHypothesisTests.h
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ class MATHS_EXPORT CPeriodicityHypothesisTests {

//! \brief A collection of statistics used during testing.
struct STestStats {
STestStats();
explicit STestStats(double meanMagnitude);
//! Set the various test thresholds.
void setThresholds(double vt, double at, double Rt);
//! Check if the null hypothesis is good enough to not need an
Expand All @@ -233,6 +233,8 @@ class MATHS_EXPORT CPeriodicityHypothesisTests {
double s_NonEmptyBuckets;
//! The average number of measurements per bucket value.
double s_MeasurementsPerBucket;
//! The mean magnitude of the bucket values.
double s_MeanMagnitude;
//! The null hypothesis periodic components.
CPeriodicityHypothesisTestsResult s_H0;
//! The variance estimate of H0.
Expand Down
26 changes: 13 additions & 13 deletions lib/maths/CPeriodicityHypothesisTests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1154,8 +1154,16 @@ CPeriodicityHypothesisTests::best(const TNestedHypothesesVec& hypotheses) const
THypothesisSummaryVec summaries;
summaries.reserve(hypotheses.size());

double meanMagnitude{CBasicStatistics::mean(std::accumulate(
m_BucketValues.begin(), m_BucketValues.end(), TMeanAccumulator{},
[](TMeanAccumulator partial, const TFloatMeanAccumulator& value) {
partial.add(std::fabs(CBasicStatistics::mean(value)),
CBasicStatistics::count(value));
return partial;
}))};

for (const auto& hypothesis : hypotheses) {
STestStats stats;
STestStats stats{meanMagnitude};
stats.s_TrendSegments = static_cast<double>(hypothesis.trendSegments());
CPeriodicityHypothesisTestsResult resultForHypothesis{hypothesis.test(stats)};
if (stats.s_NonEmptyBuckets > stats.s_DF0) {
Expand Down Expand Up @@ -2195,13 +2203,14 @@ bool CPeriodicityHypothesisTests::testAmplitude(const TTimeTimePr2Vec& window,
const double CPeriodicityHypothesisTests::ACCURATE_TEST_POPULATED_FRACTION{0.9};
const double CPeriodicityHypothesisTests::MINIMUM_COEFFICIENT_OF_VARIATION{1e-4};

CPeriodicityHypothesisTests::STestStats::STestStats()
CPeriodicityHypothesisTests::STestStats::STestStats(double meanMagnitude)
: s_TrendSegments(1.0), s_HasPeriod(false), s_HasPartition(false),
s_VarianceThreshold(COMPONENT_SIGNIFICANT_VARIANCE_REDUCTION[E_HighThreshold]),
s_AmplitudeThreshold(SEASONAL_SIGNIFICANT_AMPLITUDE[E_HighThreshold]),
s_AutocorrelationThreshold(SEASONAL_SIGNIFICANT_AUTOCORRELATION[E_HighThreshold]),
s_Range(0.0), s_NonEmptyBuckets(0.0), s_MeasurementsPerBucket(0.0),
s_V0(0.0), s_R0(0.0), s_DF0(0.0), s_StartOfPartition(0) {
s_MeanMagnitude(meanMagnitude), s_V0(0.0), s_R0(0.0), s_DF0(0.0),
s_StartOfPartition(0) {
}

void CPeriodicityHypothesisTests::STestStats::setThresholds(double vt, double at, double Rt) {
Expand All @@ -2211,16 +2220,7 @@ void CPeriodicityHypothesisTests::STestStats::setThresholds(double vt, double at
}

bool CPeriodicityHypothesisTests::STestStats::nullHypothesisGoodEnough() const {
TMeanAccumulator mean;
for (const auto& t : s_T0) {
mean += std::accumulate(t.begin(), t.end(), TMeanAccumulator(),
[](TMeanAccumulator m, double x) {
m.add(std::fabs(x));
return m;
});
}
return std::sqrt(s_V0) <=
MINIMUM_COEFFICIENT_OF_VARIATION * CBasicStatistics::mean(mean);
return std::sqrt(s_V0) <= MINIMUM_COEFFICIENT_OF_VARIATION * s_MeanMagnitude;
}

CPeriodicityHypothesisTests::CNestedHypotheses::CNestedHypotheses(TTestFunc test)
Expand Down
4 changes: 2 additions & 2 deletions lib/maths/CTimeSeriesDecompositionDetail.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1215,14 +1215,14 @@ void CTimeSeriesDecompositionDetail::CComponents::handle(const SAddValue& messag
for (std::size_t i = 1u; i <= m; ++i) {
CSeasonalComponent* component{seasonalComponents[i - 1]};
CComponentErrors* error_{seasonalErrors[i - 1]};
double varianceIncrease{variances[i] / variance / expectedVarianceIncrease};
double varianceIncrease{variance == 0.0 ? 1.0 : variances[i] / variance / expectedVarianceIncrease};
component->add(time, values[i], weight);
error_->add(referenceError, error, predictions[i - 1], varianceIncrease, weight);
}
for (std::size_t i = m + 1; i <= m + n; ++i) {
CCalendarComponent* component{calendarComponents[i - m - 1]};
CComponentErrors* error_{calendarErrors[i - m - 1]};
double varianceIncrease{variances[i] / variance / expectedVarianceIncrease};
double varianceIncrease{variance == 0.0 ? 1.0 : variances[i] / variance / expectedVarianceIncrease};
component->add(time, values[i], weight);
error_->add(referenceError, error, predictions[i - 1], varianceIncrease, weight);
}
Expand Down