Source Edit

Statistical analysis framework for performing basic statistical analysis of data. The data is analysed in a single pass, when it is pushed to a RunningStat or RunningRegress object.

RunningStat calculates for a single data set

  • n (data count)
  • min (smallest value)
  • max (largest value)
  • sum
  • mean
  • variance
  • varianceS (sample variance)
  • standardDeviation
  • standardDeviationS (sample standard deviation)
  • skewness (the third statistical moment)
  • kurtosis (the fourth statistical moment)

RunningRegress calculates for two sets of data

  • n (data count)
  • slope
  • intercept
  • correlation

Procs are provided to calculate statistics on openArrays.

However, if more than a single statistical calculation is required, it is more efficient to push the data once to a RunningStat object and then call the numerous statistical procs for the RunningStat object:

Example:

  1. import std/stats
  2. from std/math import almostEqual
  3. template `~=`(a, b: float): bool = almostEqual(a, b)
  4. var statistics: RunningStat # must be var
  5. statistics.push(@[1.0, 2.0, 1.0, 4.0, 1.0, 4.0, 1.0, 2.0])
  6. doAssert statistics.n == 8
  7. doAssert statistics.mean() ~= 2.0
  8. doAssert statistics.variance() ~= 1.5
  9. doAssert statistics.varianceS() ~= 1.714285714285715
  10. doAssert statistics.skewness() ~= 0.8164965809277261
  11. doAssert statistics.skewnessS() ~= 1.018350154434631
  12. doAssert statistics.kurtosis() ~= -1.0
  13. doAssert statistics.kurtosisS() ~= -0.7000000000000008

Imports

math

Types

  1. RunningRegress = object
  2. n*: int ## amount of pushed data
  3. x_stats*: RunningStat ## stats for the first set of data
  4. y_stats*: RunningStat ## stats for the second set of data
  5. ## accumulated data for combined xy

An accumulator for regression calculations. Source Edit

  1. RunningStat = object
  2. n*: int ## amount of pushed data
  3. min*, max*, sum*: float ## self-explaining
  4. ## statistical moments, mom1 is mean

An accumulator for statistical data. Source Edit

Procs

  1. proc `$`(a: RunningStat): string {....raises: [], tags: [], forbids: [].}

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.

Source Edit

  1. proc `+`(a, b: RunningRegress): RunningRegress {....raises: [], tags: [],
  2. forbids: [].}

Combines two RunningRegress objects.

Useful when performing parallel analysis of data series and needing to re-combine parallel result sets

Source Edit

  1. proc `+`(a, b: RunningStat): RunningStat {....raises: [], tags: [], forbids: [].}

Combines two RunningStats.

Useful when performing parallel analysis of data series and needing to re-combine parallel result sets.

Source Edit

  1. proc `+=`(a: var RunningRegress; b: RunningRegress) {....raises: [], tags: [],
  2. forbids: [].}

Adds the RunningRegress b to a. Source Edit

  1. proc `+=`(a: var RunningStat; b: RunningStat) {.inline, ...raises: [], tags: [],
  2. forbids: [].}

Adds the RunningStat b to a. Source Edit

  1. proc clear(r: var RunningRegress) {....raises: [], tags: [], forbids: [].}

Resets r. Source Edit

  1. proc clear(s: var RunningStat) {....raises: [], tags: [], forbids: [].}

Resets s. Source Edit

  1. proc correlation(r: RunningRegress): float {....raises: [], tags: [], forbids: [].}

Computes the current correlation of the two data sets pushed into r. Source Edit

  1. proc intercept(r: RunningRegress): float {....raises: [], tags: [], forbids: [].}

Computes the current intercept of r. Source Edit

  1. proc kurtosis(s: RunningStat): float {....raises: [], tags: [], forbids: [].}

Computes the current population kurtosis of s. Source Edit

  1. proc kurtosis[T](x: openArray[T]): float

Computes the population kurtosis of x. Source Edit

  1. proc kurtosisS(s: RunningStat): float {....raises: [], tags: [], forbids: [].}

Computes the current sample kurtosis of s. Source Edit

  1. proc kurtosisS[T](x: openArray[T]): float

Computes the sample kurtosis of x. Source Edit

  1. proc mean(s: RunningStat): float {....raises: [], tags: [], forbids: [].}

Computes the current mean of s. Source Edit

  1. proc mean[T](x: openArray[T]): float

Computes the mean of x. Source Edit

  1. proc push(r: var RunningRegress; x, y: float) {....raises: [], tags: [],
  2. forbids: [].}

Pushes two values x and y for processing. Source Edit

  1. proc push(r: var RunningRegress; x, y: int) {.inline, ...raises: [], tags: [],
  2. forbids: [].}

Pushes two values x and y for processing.

x and y are converted to float and the other push operation is called.

Source Edit

  1. proc push(r: var RunningRegress; x, y: openArray[float | int])

Pushes two sets of values x and y for processing. Source Edit

  1. proc push(s: var RunningStat; x: float) {....raises: [], tags: [], forbids: [].}

Pushes a value x for processing. Source Edit

  1. proc push(s: var RunningStat; x: int) {....raises: [], tags: [], forbids: [].}

Pushes a value x for processing.

x is simply converted to float and the other push operation is called.

Source Edit

  1. proc push(s: var RunningStat; x: openArray[float | int])

Pushes all values of x for processing.

Int values of x are simply converted to float and the other push operation is called.

Source Edit

  1. proc skewness(s: RunningStat): float {....raises: [], tags: [], forbids: [].}

Computes the current population skewness of s. Source Edit

  1. proc skewness[T](x: openArray[T]): float

Computes the population skewness of x. Source Edit

  1. proc skewnessS(s: RunningStat): float {....raises: [], tags: [], forbids: [].}

Computes the current sample skewness of s. Source Edit

  1. proc skewnessS[T](x: openArray[T]): float

Computes the sample skewness of x. Source Edit

  1. proc slope(r: RunningRegress): float {....raises: [], tags: [], forbids: [].}

Computes the current slope of r. Source Edit

  1. proc standardDeviation(s: RunningStat): float {....raises: [], tags: [],
  2. forbids: [].}

Computes the current population standard deviation of s. Source Edit

  1. proc standardDeviation[T](x: openArray[T]): float

Computes the population standard deviation of x. Source Edit

  1. proc standardDeviationS(s: RunningStat): float {....raises: [], tags: [],
  2. forbids: [].}

Computes the current sample standard deviation of s. Source Edit

  1. proc standardDeviationS[T](x: openArray[T]): float

Computes the sample standard deviation of x. Source Edit

  1. proc variance(s: RunningStat): float {....raises: [], tags: [], forbids: [].}

Computes the current population variance of s. Source Edit

  1. proc variance[T](x: openArray[T]): float

Computes the population variance of x. Source Edit

  1. proc varianceS(s: RunningStat): float {....raises: [], tags: [], forbids: [].}

Computes the current sample variance of s. Source Edit

  1. proc varianceS[T](x: openArray[T]): float

Computes the sample variance of x. Source Edit