Skip to content

Commit

Permalink
Remove variance
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky committed Aug 25, 2019
1 parent fa92d4e commit 45e6cae
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 21 deletions.
1 change: 0 additions & 1 deletion src/print/stats/prefix.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,4 @@ const PLUS_MINUS = '±'
const PREFIXES = {
diff: getArrow,
deviation: getPlusMinus,
variance: getPlusMinus,
}
5 changes: 2 additions & 3 deletions src/print/stats/skip.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
// Statistics are now shown if:
// - undefined (e.g. `diff` with no previous benchmark)
// - deviation|variance if not enough samples
// - deviation if not enough samples
export const shouldSkipStat = function({ stat, name, loops }) {
return (
stat === undefined ||
(DEVIATION_STATS.includes(name) && shouldSkipDeviation(stat, loops))
(name === 'deviation' && shouldSkipDeviation(stat, loops))
)
}

const shouldSkipDeviation = function(stat, loops) {
return stat === 0 || loops < MIN_LOOPS
}

const DEVIATION_STATS = ['deviation', 'variance']
const MIN_LOOPS = 10
1 change: 0 additions & 1 deletion src/print/stats/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ export const STAT_TYPES = {
diff: 'percentage',
limit: 'scalar',
deviation: 'percentage',
variance: 'scalar',
count: 'count',
loops: 'count',
repeat: 'count',
Expand Down
1 change: 0 additions & 1 deletion src/report/reporters/debug/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ const STATS = [
{ name: 'diffPretty', shortName: 'dif' },
{ name: 'limitPretty', shortName: 'lmt' },
{ name: 'deviationPretty', shortName: 'dev' },
{ name: 'variancePretty', shortName: 'vrc' },
{ name: 'countPretty', shortName: 'cnt' },
{ name: 'loopsPretty', shortName: 'lps' },
{ name: 'repeatPretty', shortName: 'rpt' },
Expand Down
12 changes: 5 additions & 7 deletions src/stats/compute.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { sortNumbers } from '../utils/sort.js'

import { getMedian, getMean, getVariance, getDeviation } from './methods.js'
import { getMedian, getMean, getDeviation } from './methods.js'
import { getPercentiles } from './percentiles.js'
import { getHistogram } from './histogram.js'

Expand Down Expand Up @@ -28,9 +28,9 @@ const reduceCount = function(totalCount, { count }) {
// Perform the statistical logic.
// Note that when `repeat > 1`, the distribution of the measured function will
// be modified by the looping process and transformed to a bell shape, even if
// if was not one. This means `percentiles`, `histogram`, `variance` and
// `deviation` will have a different meaning: they visualize the measurements of
// the function not function itself.
// if was not one. This means `percentiles`, `histogram` and `deviation` will
// have a different meaning: they visualize the measurements of the function not
// function itself.
// eslint-disable-next-line max-statements
const computeStats = function({ times, count, processes }) {
// Half of the statistics require the array to be sorted
Expand All @@ -50,16 +50,14 @@ const computeStats = function({ times, count, processes }) {
const histogram = getHistogram(times, HISTOGRAM_SIZE)

const mean = getMean(times)
const variance = getVariance(times, mean)
const deviation = getDeviation(variance, mean)
const deviation = getDeviation(times, mean)

return {
median,
mean,
min,
max,
deviation,
variance,
count,
loops,
repeat,
Expand Down
12 changes: 4 additions & 8 deletions src/stats/methods.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,14 @@ const addNumbers = function(numA, numB) {
return numA + numB
}

// Retrieve variance of an array of floats (cannot be NaN nor Infinite).
// Returns Infinity when array is empty.
export const getVariance = function(array, mean) {
return getMean(array.map(num => (num - mean) ** 2))
}

// Same as variance but for standard deviation.
// Retrieve standard deviation of an array of floats (cannot be NaN/Infinity).
// In percentage relative to the mean.
export const getDeviation = function(variance, mean) {
// Returns Infinity when array is empty.
export const getDeviation = function(array, mean) {
if (mean === 0) {
return 0
}

const variance = getMean(array.map(num => (num - mean) ** 2))
return Math.sqrt(variance) / mean
}

0 comments on commit 45e6cae

Please sign in to comment.