Skip to content

Instantly share code, notes, and snippets.

@luish
Created May 11, 2016 08:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save luish/e5049de1c373139af88b8849bbc8ddee to your computer and use it in GitHub Desktop.
Save luish/e5049de1c373139af88b8849bbc8ddee to your computer and use it in GitHub Desktop.
import Foundation
struct Math {
static func standardDeviation(values: [Double]) -> Double {
let squaredAvgDiff = values.map { pow($0 - average(values), 2)}
let sum = squaredAvgDiff.reduce(0) {$0 + $1}
return sqrt(sum / Double(values.count))
}
static func average(values: [Double]) -> Double {
return values.reduce(0) {$0 + $1} / Double(values.count)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment