Last active
August 7, 2020 00:54
-
-
Save khanlou/6c4f25eb9362a71b1089acb9b7bc7491 to your computer and use it in GitHub Desktop.
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
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