Skip to content

Instantly share code, notes, and snippets.

@joaofig
Created August 26, 2016 09:47
Show Gist options
  • Save joaofig/448dc43c1247b0417b7fb85d895c051c to your computer and use it in GitHub Desktop.
Save joaofig/448dc43c1247b0417b7fb85d895c051c to your computer and use it in GitHub Desktop.
ExponentialMovingModel
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