stats
Statistical analysis framework for performing basic statistical analysis of data. The data is analysed in a single pass, when a data value is pushed to the RunningStat or RunningRegress objects
RunningStat calculates for a single data set
- n (data count)
- min (smallest value)
- max (largest value)
- sum
- mean
- variance
- varianceS (sample var)
- standardDeviation
- standardDeviationS (sample stddev)
- skewness (the third statistical moment)
- kurtosis (the fourth statistical moment)
RunningRegress calculates for two sets of data
- n
- slope
- intercept
- correlation
Procs have been provided to calculate statistics on arrays and sequences.
However, if more than a single statistical calculation is required, it is more efficient to push the data once to the RunningStat object, and call the numerous statistical procs for the RunningStat object.
var rs: RunningStat rs.push(MySeqOfData) rs.mean() rs.variance() rs.skewness() rs.kurtosis()
Example:
static:
block:
var statistics: RunningStat ## Must be "var"
statistics.push(@[1.0, 2.0, 1.0, 4.0, 1.0, 4.0, 1.0, 2.0])
doAssert statistics.n == 8
template `===`(a, b: float): bool = (abs(a - b) < 1e-9)
doAssert statistics.mean() === 2.0
doAssert statistics.variance() === 1.5
doAssert statistics.varianceS() === 1.714285714285715
doAssert statistics.skewness() === 0.8164965809277261
doAssert statistics.skewnessS() === 1.018350154434631
doAssert statistics.kurtosis() === -1.0
doAssert statistics.kurtosisS() === -0.7000000000000008 Imports
Types
RunningStat = object n*: int ## number of pushed data min*, max*, sum*: float ## self-explaining mom1, mom2, mom3, mom4: float ## statistical moments, mom1 is mean
- an accumulator for statistical data Source Edit
RunningRegress = object n*: int ## number of pushed data x_stats*: RunningStat ## stats for first set of data y_stats*: RunningStat ## stats for second set of data s_xy: float ## accumulated data for combined xy
- an accumulator for regression calculations Source Edit
Procs
proc clear(s: var RunningStat) {...}{.raises: [], tags: [].}- reset
sSource Edit proc push(s: var RunningStat; x: float) {...}{.raises: [], tags: [].}- pushes a value
xfor processing Source Edit proc push(s: var RunningStat; x: int) {...}{.raises: [], tags: [].}-
pushes a value
xfor processing.
Source Editxis simply converted tofloatand the other push operation is called. proc push(s: var RunningStat; x: openArray[float | int])
-
pushes all values of
xfor processing.Int values of
Source Editxare simply converted tofloatand the other push operation is called. proc mean(s: RunningStat): float {...}{.raises: [], tags: [].}- computes the current mean of
sSource Edit proc variance(s: RunningStat): float {...}{.raises: [], tags: [].}- computes the current population variance of
sSource Edit proc varianceS(s: RunningStat): float {...}{.raises: [], tags: [].}- computes the current sample variance of
sSource Edit proc standardDeviation(s: RunningStat): float {...}{.raises: [], tags: [].}- computes the current population standard deviation of
sSource Edit proc standardDeviationS(s: RunningStat): float {...}{.raises: [], tags: [].}- computes the current sample standard deviation of
sSource Edit proc skewness(s: RunningStat): float {...}{.raises: [], tags: [].}- computes the current population skewness of
sSource Edit proc skewnessS(s: RunningStat): float {...}{.raises: [], tags: [].}- computes the current sample skewness of
sSource Edit proc kurtosis(s: RunningStat): float {...}{.raises: [], tags: [].}- computes the current population kurtosis of
sSource Edit proc kurtosisS(s: RunningStat): float {...}{.raises: [], tags: [].}- computes the current sample kurtosis of
sSource Edit proc `+`(a, b: RunningStat): RunningStat {...}{.raises: [], tags: [].}-
combine two RunningStats.
Useful if performing parallel analysis of data series and need to re-combine parallel result sets
Source Edit proc `+=`(a: var RunningStat; b: RunningStat) {...}{.inline, raises: [], tags: [].}- add a second RunningStats
btoaSource Edit proc `$`(a: RunningStat): string {...}{.raises: [], tags: [].}- produces a string representation of the
RunningStat. The exact format is currently unspecified and subject to change. Currently it contains:- the number of probes
- min, max values
- sum, mean and standard deviation.
proc mean[T](x: openArray[T]): float
- computes the mean of
xSource Edit proc variance[T](x: openArray[T]): float
- computes the population variance of
xSource Edit proc varianceS[T](x: openArray[T]): float
- computes the sample variance of
xSource Edit proc standardDeviation[T](x: openArray[T]): float
- computes the population standardDeviation of
xSource Edit proc standardDeviationS[T](x: openArray[T]): float
- computes the sample standardDeviation of
xSource Edit proc skewness[T](x: openArray[T]): float
- computes the population skewness of
xSource Edit proc skewnessS[T](x: openArray[T]): float
- computes the sample skewness of
xSource Edit proc kurtosis[T](x: openArray[T]): float
- computes the population kurtosis of
xSource Edit proc kurtosisS[T](x: openArray[T]): float
- computes the sample kurtosis of
xSource Edit proc clear(r: var RunningRegress) {...}{.raises: [], tags: [].}- reset
rSource Edit proc push(r: var RunningRegress; x, y: float) {...}{.raises: [], tags: [].}- pushes two values
xandyfor processing Source Edit proc push(r: var RunningRegress; x, y: int) {...}{.inline, raises: [], tags: [].}-
pushes two values
xandyfor processing.
Source Editxandyare converted tofloatand the other push operation is called. proc push(r: var RunningRegress; x, y: openArray[float | int])
- pushes two sets of values
xandyfor processing. Source Edit proc slope(r: RunningRegress): float {...}{.raises: [], tags: [].}- computes the current slope of
rSource Edit proc intercept(r: RunningRegress): float {...}{.raises: [], tags: [].}- computes the current intercept of
rSource Edit proc correlation(r: RunningRegress): float {...}{.raises: [], tags: [].}- computes the current correlation of the two data sets pushed into
rSource Edit proc `+`(a, b: RunningRegress): RunningRegress {...}{.raises: [], tags: [].}-
combine two
RunningRegressobjects.Useful if performing parallel analysis of data series and need to re-combine parallel result sets
Source Edit proc `+=`(a: var RunningRegress; b: RunningRegress) {...}{.raises: [], tags: [].}- add RunningRegress
btoaSource Edit
© 2006–2021 Andreas Rumpf
Licensed under the MIT License.
https://nim-lang.org/docs/stats.html