Skip to content

Instantly share code, notes, and snippets.

@khanlou
Last active Aug 7, 2020
Embed
What would you like to do?
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