Created
December 6, 2017 02:37
-
-
Save Skrylar/42300800e29f81f2c69bebeba38bbe6d 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
# not sure why this isn't built-in | |
template inc(self: var float; other: float) = | |
self = self + other | |
type | |
Averager* = object | |
count: int | |
x: float | |
proc feed*(self: var Averager; value: float) = | |
inc self.x, value | |
inc self.count | |
proc done*(self: var Averager): float = | |
result = self.x / self.count.float | |
self.count = 0 | |
self.x = 0 | |
type | |
Deviator* = object | |
count: int | |
x: float | |
proc feed*(self: var Deviator; value, mean: float) = | |
let val = (value - mean) | |
inc self.x, pow(val, 2) | |
inc self.count | |
proc done*(self: var Deviator; population: bool = true): float = | |
if population: | |
result = self.x / self.count.float | |
else: | |
result = self.x / (self.count - 1).float | |
self.count = 0 | |
self.x = 0 | |
type | |
Correlator* = object | |
## Computes Pearson R from a stream of x/y values. | |
xy, x2, y2: float | |
proc feed*(self: var Correlator; x, y: float) = | |
inc self.xy, x * y | |
inc self.x2, pow(x, 2) | |
inc self.y2, pow(y, 2) | |
proc done*(self: var Correlator): float = | |
result = self.xy / sqrt(self.x2 * self.y2) | |
self.xy = 0 | |
self.x2 = 0 | |
self.y2 = 0 | |
proc linear_regression*(r, mx, my, sx, sy: float; slope, intercept: out float) = | |
## Computes the slope and intercept for a linear regression line given correlation R, means of x and y, and standard deviations of x and y. | |
slope = r * sy / sx | |
intercept = my - slope * mx |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment