Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky committed Dec 19, 2021
1 parent bc7b87c commit 4a2012d
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 7 deletions.
6 changes: 2 additions & 4 deletions src/stats/critical_values/student_t.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,15 @@ import { getCriticalValue, getCriticalValuesMap } from './map.js'

// Retrieve critical value of the Student's t distribution, two-tailed, for a
// given degrees of freedom
export const getStudentTValue = function (degreesOfFreedom) {
const singleSignificanteLevel = (1 - SIGNIFICANCE_LEVEL) / 2
export const getStudentTValue = function (degreesOfFreedom, significanceLevel) {
const singleSignificanteLevel = (1 - significanceLevel) / 2
return getCriticalValue(
STUDENT_T_MAP,
degreesOfFreedom,
singleSignificanteLevel,
)
}

const SIGNIFICANCE_LEVEL = 0.95

const STUDENT_T_RAW = [
{
significanceLevel: 0.025,
Expand Down
10 changes: 8 additions & 2 deletions src/stats/moe.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export const getAdjustedMoe = function (stdev, length, envDev) {
// combination pair
export const getMoe = function (stdev, length) {
const standardError = stdev / Math.sqrt(length)
const tvalue = getStudentTValue(length - 1)
const tvalue = getStudentTValue(length - 1, SIGNIFICANCE_LEVEL)
const moe = standardError * tvalue
return moe
}
Expand Down Expand Up @@ -122,7 +122,11 @@ export const getLengthForMoe = function (moeTarget, stdev) {
lengths.add(length)
// eslint-disable-next-line fp/no-mutation
length = Math.max(
Math.round(((getStudentTValue(length - 1) * stdev) / moeTarget) ** 2),
Math.round(
((getStudentTValue(length - 1, SIGNIFICANCE_LEVEL) * stdev) /
moeTarget) **
2,
),
MIN_LENGTH,
)
}
Expand All @@ -132,3 +136,5 @@ export const getLengthForMoe = function (moeTarget, stdev) {

// Minimal `length` with a defined t-value
const MIN_LENGTH = 2

const SIGNIFICANCE_LEVEL = 0.95
7 changes: 6 additions & 1 deletion src/stats/similar.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,15 @@ const welchTTest = function ({
const degreesOfFreedom =
(errorSquaredA + errorSquaredB) ** 2 /
(errorSquaredA ** 2 / (lengthA - 1) + errorSquaredB ** 2 / (lengthB - 1))
const tValue = getStudentTValue(Math.floor(degreesOfFreedom))
const tValue = getStudentTValue(
Math.floor(degreesOfFreedom),
SIGNIFICANCE_LEVEL,
)
return tStat < tValue
}

const getErrorSquared = function (stdev, length) {
return (stdev / Math.sqrt(length)) ** 2
}

const SIGNIFICANCE_LEVEL = 0.95

0 comments on commit 4a2012d

Please sign in to comment.