Skip to content

Instantly share code, notes, and snippets.

@khanlou
Last active August 7, 2020 00:54
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 khanlou/6c4f25eb9362a71b1089acb9b7bc7491 to your computer and use it in GitHub Desktop.
Save khanlou/6c4f25eb9362a71b1089acb9b7bc7491 to your computer and use it in GitHub Desktop.
struct Statistics {
private(set) var count: Double = 0
private(set) var sum: Double = 0
private(set) var sumOfSquares: Double = 0
var average: Double {
sum / count
}
var variance: Double {
sumOfSquares/count - pow(average, 2)
}
var standardDeviation: Double {
sqrt(variance)
}
@discardableResult
mutating func add(_ newValue: Double) -> (void: (), outlier: Bool) {
count += 1
sum += newValue
sumOfSquares += pow(newValue, 2)
return (
void: (),
outlier: newValue > average + standardDeviation * 3 || newValue < average - standardDeviation * 3 // this should use IQR
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment