Created
August 26, 2016 09:47
-
-
Save joaofig/448dc43c1247b0417b7fb85d895c051c to your computer and use it in GitHub Desktop.
ExponentialMovingModel
This file contains hidden or 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
using System; | |
namespace Sagaceco.TimeSeries.Patterns.Models | |
{ | |
public class ExponentialMovingModel | |
{ | |
private double average = 0.0; | |
private double variance = 0.0; | |
public ExponentialMovingModel() | |
{ | |
Weight = 0.1; | |
} | |
public ExponentialMovingModel(double weight) | |
{ | |
Weight = weight; | |
} | |
public double Weight { get; set; } | |
public double Average | |
{ | |
get { return average; } | |
} | |
public double Variance | |
{ | |
get { return variance; } | |
} | |
public void Update(double x) | |
{ | |
if( average == 0.0 && variance == 0.0 ) | |
{ | |
average = x; | |
} | |
else | |
{ | |
double diff = x - average; | |
double incr = Weight * diff; | |
average = average + incr; | |
variance = (1 - Weight) * (variance + diff * incr); | |
} | |
} | |
public bool IsOutlier( double radius, double x ) | |
{ | |
return Math.Abs( x - average ) > radius * Math.Sqrt( variance ); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment