Skip to content

Instantly share code, notes, and snippets.

@izmailoff
Created February 8, 2016 00:47
Show Gist options
  • Save izmailoff/b609c882994d90f05ac4 to your computer and use it in GitHub Desktop.
Save izmailoff/b609c882994d90f05ac4 to your computer and use it in GitHub Desktop.
Basic statistics functions (not optimized)
object Stats {
type Observations = List[Double]
def mean(xs: Observations) = xs.sum / xs.size
def variance(xs: Observations) = {
val m = mean(xs)
xs.map{ x => math.pow(x - m, 2) }.sum / (xs.size - 1)
}
def stdDev(xs: Observations) = math.sqrt(variance(xs))
def zScore(xs: Observations) = {
val m = mean(xs)
val std = stdDev(xs)
xs.map{ x => (x - m) / std }
}
def pearsonsR(xs: Observations, ys: Observations) =
zScore(xs).zip(zScore(ys)).map{ case(x, y) => x * y }.sum / (xs.size - 1)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment