Created
February 8, 2016 00:47
-
-
Save izmailoff/b609c882994d90f05ac4 to your computer and use it in GitHub Desktop.
Basic statistics functions (not optimized)
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
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