Skip to content
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

Anomaly likelihood #11

Merged
merged 68 commits into from
Nov 29, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
68 commits
Select commit Hold shift + click to select a range
f31ad4b
Likelihood: initial implementation
breznak Apr 4, 2017
4b212f0
MovingAverage: add getter for max window size
breznak Apr 11, 2017
6146075
Likelihood: simplify & cleanup design
breznak Apr 11, 2017
7e440f7
Likelihood: use circular_buffers
breznak Apr 11, 2017
85fe8c1
Likelihood: use boost::circular_buffer
breznak Apr 11, 2017
1c7fe2f
remove old comment
breznak Apr 11, 2017
38228dc
Likelihood: review 1 - fix include accumulate, inner_product
breznak Apr 18, 2017
5850fa4
Likelihood: review 1- fix min needs cast to ulong for both params
breznak Apr 18, 2017
7dbae09
Likelihood: review 1 - fix assert
breznak Apr 18, 2017
1ff58e9
Likelihood: add header file
breznak Apr 18, 2017
e01c7fd
Anomaly: add Likelihood implementation
breznak Apr 18, 2017
16cdf2e
Anomaly: fix uninitialized var likelihood
breznak Apr 18, 2017
35c0f76
Likelihood: initial test
breznak Apr 18, 2017
ef48703
Likelihood: fix header separation
breznak Apr 19, 2017
1a6acfb
Likelihood: update first test
breznak Apr 19, 2017
50afb71
review 1: Anomaly: fix likelihood score as (1-likelihood)
breznak Apr 23, 2017
bae48b2
Likelihood: review 2: remove rawValue
breznak May 4, 2017
1db76da
MovingAverage: rev2: rename to getMaxWindowSize()
breznak May 4, 2017
89f4110
Likelihood: rev2: move mean/var helpers to cpp only
breznak May 4, 2017
257bb3f
Likelihood: rev2: use NTA_CHECK
breznak May 4, 2017
46272fe
Likelihood: rev2: move docs to header file
breznak May 4, 2017
821d18f
Likelihood rev2: cast to UInt
breznak May 4, 2017
b93a912
Likelihood: fix filterLikelihoods()
breznak May 4, 2017
20e7e57
Likelihood: rev2: optimize vector allocation
breznak May 4, 2017
4c3ffe7
Likelihood: TMP comment out offending check
breznak May 4, 2017
357a55f
Likelihood: add FIXME about (not) using timestamps
breznak May 4, 2017
7867357
Likelihood: fix reserve instead of resize
breznak May 5, 2017
9e67c9d
Likelihood: forgoten fixes
breznak May 5, 2017
fdb38b9
Likelihood: replace some magic numbers with constants
breznak May 5, 2017
a4928f5
Likelihood: use trailing _ for private methods & members
breznak May 20, 2017
eeb4e53
Move circularBufferToWindow() from header to cpp only
breznak May 20, 2017
9033802
Likelihood: move calcSkipRecords_ from header to cpp
breznak May 20, 2017
8131ee8
Likelihood: make computeLogLikelihood public
breznak May 20, 2017
6a42a31
Likelihood: remove nullDistribution() as it's only used once
breznak May 20, 2017
d4af414
Anomaly: drop (raw) inputValue as it's not used
breznak May 20, 2017
5cc23d9
Anomaly: use Real instead of Real32
breznak May 20, 2017
66fada1
Anomaly: align timestamp (int, default -1) with Likelihood
breznak May 20, 2017
f20692e
Likelihood: use Real instead of Real32
breznak May 20, 2017
41509b2
Likelihood: check timestamp is monotonically increasing
breznak May 20, 2017
9e2b72f
Likelihood: honor timestamp
breznak May 20, 2017
632c910
Likelihood: make public const members public
breznak May 20, 2017
3c1d466
Likelihood: fix runningLikelihood always pushed 0.5
breznak May 20, 2017
d541ad9
Likelihood: fix formating of docstrings
breznak May 20, 2017
1d07292
Likelihood: doc estimateNormal() takes raw anomalyScores
breznak May 20, 2017
f508e03
Likelihood: optimize: use const, pass reference&
breznak May 20, 2017
e66241f
Likelihood: TODO comment on aggregation
breznak May 21, 2017
a2c3f05
Likelihood: fix typo
breznak May 21, 2017
fc72bb5
Likelihood: fix CI error in assert
breznak May 22, 2017
5a4b2fa
Likelihood: fix runningLikelihoods size
breznak Jun 15, 2017
85f6667
Merge branch 'master' into anomaly_likelihood
breznak Jan 10, 2018
212caf7
Merge branch 'master' into anomaly_likelihood
breznak Jan 18, 2018
014976d
Merge branch 'master_community' into anomaly_likelihood
breznak Aug 30, 2018
4f05972
Merge branch 'master_community' into anomaly_likelihood
breznak Nov 26, 2018
f4213a2
Anomaly: fix merge duplicate code
breznak Nov 26, 2018
508fa58
AnomalyLikelihood: replace boost::circular_buffer with SlidingWindow
breznak Nov 26, 2018
a7d369a
Numenta copyright not needed, retrigger build
breznak Nov 26, 2018
55572bc
Merge branch 'master' into anomaly_likelihood
breznak Nov 26, 2018
e2ace2e
Merge branch 'master_community' into anomaly_likelihood
breznak Nov 27, 2018
8c83578
Merge branch 'master' into anomaly_likelihood
breznak Nov 27, 2018
7a5bdc5
Merge branch 'master_community' into anomaly_likelihood
breznak Nov 28, 2018
178e0a0
Merge branch 'anomaly_likelihood' of https://github.com/breznak/nupic…
breznak Nov 28, 2018
fe94243
Merge branch 'master' into anomaly_likelihood
breznak Nov 28, 2018
925a191
Merge branch 'master' of https://github.com/htm-community/nupic.core …
breznak Nov 28, 2018
caa5cf8
Merge branch 'master' into anomaly_likelihood
breznak Nov 28, 2018
bdb6fd6
Merge branch 'master' into anomaly_likelihood
breznak Nov 28, 2018
000048d
Merge branch 'anomaly_likelihood' of https://github.com/breznak/nupic…
breznak Nov 28, 2018
64b2a13
Travis use gcc 7
breznak Nov 28, 2018
21f3447
Revert "Travis use gcc 7"
breznak Nov 28, 2018
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
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@ set(src_lib_static_nupiccore_solo nupic_core_solo)

set(src_nupiccore_srcs
nupic/algorithms/Anomaly.cpp
nupic/algorithms/AnomalyLikelihood.cpp
nupic/algorithms/BacktrackingTMCpp.cpp
nupic/algorithms/BitHistory.cpp
nupic/algorithms/Cell.cpp
Expand Down
59 changes: 28 additions & 31 deletions src/nupic/algorithms/Anomaly.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@
using namespace std;

namespace nupic {

namespace algorithms {

namespace anomaly {

Real32 computeRawAnomalyScore(const vector<UInt> &active,
const vector<UInt> &predicted) {

Real computeRawAnomalyScore(const vector<UInt>& active,
const vector<UInt>& predicted)
{
// Return 0 if no active columns are present
if (active.size() == 0) {
return 0.0f;
Expand All @@ -53,39 +53,38 @@ Real32 computeRawAnomalyScore(const vector<UInt> &active,
set_intersection(active_.begin(), active_.end(), predicted_.begin(),
predicted_.end(), back_inserter(predictedActiveCols));

return (active.size() - predictedActiveCols.size()) / Real32(active.size());
return (active.size() - predictedActiveCols.size()) / Real(active.size());
}

Anomaly::Anomaly(UInt slidingWindowSize, AnomalyMode mode,
Real32 binaryAnomalyThreshold)
: binaryThreshold_(binaryAnomalyThreshold) {
NTA_ASSERT(binaryAnomalyThreshold >= 0 && binaryAnomalyThreshold <= 1)
<< "binaryAnomalyThreshold must be within [0.0,1.0]";
Anomaly::Anomaly(UInt slidingWindowSize, AnomalyMode mode, Real binaryAnomalyThreshold)
: binaryThreshold_(binaryAnomalyThreshold)
{
NTA_CHECK(binaryAnomalyThreshold >= 0 && binaryAnomalyThreshold <= 1) << "binaryAnomalyThreshold must be within [0.0,1.0]";
mode_ = mode;
if (slidingWindowSize > 0) {
movingAverage_.reset(new nupic::util::MovingAverage(slidingWindowSize));
}

// Not implemented. Fail.
NTA_ASSERT(mode_ == AnomalyMode::PURE)
<< "C++ Anomaly implemented only for PURE mode!";
}

Real32 Anomaly::compute(const vector<UInt> &active,
const vector<UInt> &predicted, Real64 inputValue,
UInt timestamp) {
Real32 anomalyScore = computeRawAnomalyScore(active, predicted);
Real32 score = anomalyScore;
switch (mode_) {
case AnomalyMode::PURE:
score = anomalyScore;
break;
case AnomalyMode::LIKELIHOOD:
case AnomalyMode::WEIGHTED:
// Not implemented. Fail
NTA_ASSERT(mode_ == AnomalyMode::PURE)
<< "C++ Anomaly implemented only for PURE mode!";
break;

Real Anomaly::compute(const vector<UInt>& active, const vector<UInt>& predicted, int timestamp)
{
Real anomalyScore = computeRawAnomalyScore(active, predicted);
Real likelihood = 0.5;
Real score = anomalyScore;
switch(mode_)
{
case AnomalyMode::PURE:
score = anomalyScore;
break;
case AnomalyMode::LIKELIHOOD:
likelihood = likelihood_.anomalyProbability(anomalyScore, timestamp);
score = 1 - likelihood;
break;
case AnomalyMode::WEIGHTED:
likelihood = likelihood_.anomalyProbability(anomalyScore, timestamp);
score = anomalyScore * (1 - likelihood);
break;
}

if (movingAverage_) {
Expand All @@ -100,7 +99,5 @@ Real32 Anomaly::compute(const vector<UInt> &active,
}

} // namespace anomaly

} // namespace algorithms

} // namespace nupic
22 changes: 7 additions & 15 deletions src/nupic/algorithms/Anomaly.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,16 @@
#define NUPIC_ALGORITHMS_ANOMALY_HPP

#include <memory> // Needed for smart pointer templates
#include <nupic/algorithms/AnomalyLikelihood.hpp>
#include <nupic/types/Types.hpp>
#include <nupic/utils/MovingAverage.hpp> // Needed for for smart pointer templates
#include <vector>

namespace nupic {

namespace util {
class MovingAverage; // Forward declaration
}

namespace algorithms {

namespace anomaly {


/**
* Computes the raw anomaly score.
*
Expand Down Expand Up @@ -93,24 +89,20 @@ class Anomaly {
* @param active: array of active column indices
* @param predicted: array of columns indices predicted in this step
* (used for anomaly in step T+1)
* @param inputValue: (optional) value of current input to encoders
* (eg "cat" for category encoder)
* (used in anomaly-likelihood)
* @param timestamp: (optional) date timestamp when the sample occured
* (used in anomaly-likelihood)
* @return the computed anomaly score; Real32 0..1
*/
Real32 compute(const std::vector<UInt> &active,
const std::vector<UInt> &predicted, Real64 inputValue = 0,
UInt timestamp = 0);
Real compute(const std::vector<UInt> &active,
const std::vector<UInt> &predicted,
int timestamp = 0);

private:
AnomalyMode mode_;
Real32 binaryThreshold_;
std::unique_ptr<nupic::util::MovingAverage> movingAverage_;
AnomalyLikelihood likelihood_; //TODO which params/how pass them to constructor?
};
} // namespace anomaly
} // namespace algorithms
} // namespace nupic
}}} //end-ns

#endif // NUPIC_ALGORITHMS_ANOMALY_HPP
Loading