Skip to content

Instantly share code, notes, and snippets.

@jamesthompson
Created September 5, 2012 16:10
Show Gist options
  • Save jamesthompson/3639158 to your computer and use it in GitHub Desktop.
Save jamesthompson/3639158 to your computer and use it in GitHub Desktop.
Primitive Chung-Kennedy Filter
def calcStDev(in: List[Double]) = {
def squaredDifference(v1:Double, v2:Double) = math.pow(v1 - v2,2.0)
val mean = in.sum / in.length
val squared = in.foldLeft(0.0)(_ + squaredDifference(_, mean))
math.sqrt(squared / in.length.toDouble)
}
def ckf(in:List[Double], windowSize:Int) : List[Double] = {
val out = for(i <- windowSize until in.length - windowSize) yield {
val forward = calcStDev(in.slice(i, i + windowSize + 1))
val backward = calcStDev(in.slice(i - windowSize - 1, i))
if(forward < backward) forward else backward
}
out.toList
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment