Skip to content

Commit

Permalink
Refactor variance
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky committed Nov 14, 2021
1 parent 7703abb commit 832e2cc
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 10 deletions.
5 changes: 3 additions & 2 deletions src/stats/compute.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { getLengthFromLoops } from './length.js'
import { getMoe, getRmoe } from './moe.js'
import { getOutliersPercentages } from './outliers/main.js'
import { getSortedMedian, getQuantiles } from './quantile.js'
import { getStdev, getRstdev } from './stdev.js'
import { getVariance, getStdev, getRstdev } from './stdev.js'
import { getMean } from './sum.js'

// Aggregate measures into discrete statistics.
Expand Down Expand Up @@ -121,7 +121,8 @@ const getPrecisionStats = function ({
return getPerfectPrecisionStats(mean)
}

const stdev = getStdev(measures, { minIndex, maxIndex, mean })
const variance = getVariance(measures, { minIndex, maxIndex, mean })
const stdev = getStdev(variance)
const rstdev = getRstdev(stdev, mean)
const moe = getMoe(stdev, length)
const rmoe = getRmoe(moe, mean)
Expand Down
16 changes: 8 additions & 8 deletions src/stats/stdev.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
// Retrieve standard deviation of an array of floats (cannot be NaN/Infinity).
// Retrieve variance of an array of floats (cannot be NaN/Infinity).
// Array must not be empty.
// We use the absolute standard deviation, as opposed to making it relative to
// the mean (as a percentage)
// We use the absolute variance, as opposed to making it relative to the mean
// (as a percentage)
// - It makes it easier to understand:
// - the spread of a given combination
// - its relation to moe and distribution-related stats such as percentiles
// (that are also not percentages)
// - On the flipside, it makes it harder to compare combinations (since they
// most likely have different means)
export const getStdev = function (array, { minIndex, maxIndex, mean }) {
const variance = getVariance(array, { minIndex, maxIndex, mean })
return Math.sqrt(variance)
}

export const getVariance = function (
array,
{ minIndex = 0, maxIndex = array.length - 1, mean },
Expand All @@ -35,6 +30,11 @@ const getSumDeviation = function ({ array, minIndex, maxIndex, mean }) {
return sum
}

// Retrieve standard deviation
export const getStdev = function (variance) {
return Math.sqrt(variance)
}

// Retrieve stdev relative to the mean.
// This is more useful than stdev when comparing different combinations, or when
// targetting a specific precision threshold.
Expand Down

0 comments on commit 832e2cc

Please sign in to comment.