@tracerbench/stats
Advanced tools
Comparing version 4.5.3 to 4.5.5
@@ -8,3 +8,3 @@ /** | ||
/** | ||
* Apply the passed "_func" to the permutations of the items in listOne and listTwo | ||
* Apply the passed "func" to the permutations of the items in listOne and listTwo | ||
* | ||
@@ -23,4 +23,11 @@ * @param listOne - Array of numbers | ||
*/ | ||
export declare function confidenceInterval(distributionOne: number[], distributionTwo: number[], interval: number): [number, number]; | ||
export declare function confidenceInterval(a: number[], b: number[], confidence: number): { | ||
lower: number; | ||
median: number; | ||
upper: number; | ||
U: number; | ||
zScore: number; | ||
pValue: number; | ||
}; | ||
export {}; | ||
//# sourceMappingURL=confidence-interval.d.ts.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.confidenceInterval = exports.cartesianProduct = void 0; | ||
const d3_array_1 = require("d3-array"); | ||
const jStat = require("jstat"); | ||
@@ -14,3 +15,3 @@ /** | ||
/** | ||
* Apply the passed "_func" to the permutations of the items in listOne and listTwo | ||
* Apply the passed "func" to the permutations of the items in listOne and listTwo | ||
* | ||
@@ -39,17 +40,35 @@ * @param listOne - Array of numbers | ||
*/ | ||
function confidenceInterval(distributionOne, distributionTwo, interval) { | ||
const distributionOneLength = distributionOne.length; | ||
const distributionTwoLength = distributionTwo.length; | ||
const lengthsMultiplied = distributionOneLength * distributionTwoLength; | ||
const sqrtOfSomething = Math.sqrt((lengthsMultiplied * (distributionOneLength + distributionTwoLength + 1)) / | ||
12); | ||
const other = jStat.normal.inv(1 - (1 - interval) / 2, 0, 1) * sqrtOfSomething; | ||
const ca = Math.floor((distributionOneLength * distributionTwoLength) / 2 - other); | ||
const diffs = cartesianProduct(distributionOne, distributionTwo); | ||
return [ | ||
diffs[ca - 1], | ||
diffs[distributionOneLength * distributionTwoLength - ca] | ||
]; | ||
function confidenceInterval(a, b, confidence) { | ||
var _a; | ||
const aLength = a.length; | ||
const bLength = b.length; | ||
const maxU = aLength * bLength; | ||
const meanU = maxU / 2; | ||
const deltas = a | ||
.map((a) => b.map((b) => a - b)) | ||
.flat() | ||
.sort((a, b) => a - b); | ||
const U = deltas.reduce((accum, value) => accum + (value < 0 ? 1 : value == 0 ? 0.5 : 0), 0); | ||
const lowerTail = U <= meanU; | ||
const standadDeviationU = Math.sqrt((maxU * (aLength + bLength + 1)) / 12); | ||
// we are estimating a discrete distribution so bias the mean depending on which tail | ||
// we are computing the pValue for | ||
const continuityCorrection = lowerTail ? 0.5 : -0.5; | ||
const zScore = (U - meanU + continuityCorrection) / standadDeviationU; | ||
// z is symmetrical, so use lower tail and double the cumulative for each tail | ||
// since this is a two tailed test | ||
const pValue = jStat.normal.cdf(-Math.abs(zScore), 0, 1) * 2; | ||
const alpha = 1 - confidence; | ||
const lowerU = Math.round(jStat.normal.inv(alpha / 2, meanU + 0.5, standadDeviationU)); | ||
const upperU = Math.round(jStat.normal.inv(1 - alpha / 2, meanU + 0.5, standadDeviationU)); | ||
return { | ||
lower: deltas[lowerU], | ||
median: (_a = d3_array_1.median(deltas)) !== null && _a !== void 0 ? _a : 0, | ||
upper: deltas[upperU], | ||
zScore, | ||
pValue, | ||
U | ||
}; | ||
} | ||
exports.confidenceInterval = confidenceInterval; | ||
//# sourceMappingURL=confidence-interval.js.map |
@@ -86,10 +86,10 @@ "use strict"; | ||
const ci = confidence_interval_1.confidenceInterval(control, experiment, confidenceLevel); | ||
const isSig = (ci[0] < 0 && 0 < ci[1]) || | ||
(ci[0] > 0 && 0 > ci[1]) || | ||
(ci[0] === 0 && ci[1] === 0) | ||
const isSig = (ci.lower < 0 && 0 < ci.upper) || | ||
(ci.lower > 0 && 0 > ci.upper) || | ||
(ci.lower === 0 && ci.upper === 0) | ||
? false | ||
: true; | ||
return { | ||
min: Math.round(Math.ceil(ci[0] * 100) / 100), | ||
max: Math.round(Math.ceil(ci[1] * 100) / 100), | ||
min: Math.round(Math.ceil(ci.lower * 100) / 100), | ||
max: Math.round(Math.ceil(ci.upper * 100) / 100), | ||
isSig | ||
@@ -96,0 +96,0 @@ }; |
{ | ||
"name": "@tracerbench/stats", | ||
"version": "4.5.3", | ||
"version": "4.5.5", | ||
"description": "Stats class written in TS-Node", | ||
@@ -31,3 +31,3 @@ "keywords": [ | ||
"dependencies": { | ||
"d3-array": "^2.8.0", | ||
"d3-array": "^2.9.1", | ||
"d3-scale": "^3.2.3", | ||
@@ -43,27 +43,27 @@ "fs-extra": "^9.0.1", | ||
"@types/d3-array": "^2.8.0", | ||
"@types/d3-scale": "^3.2.1", | ||
"@types/fs-extra": "^9.0.4", | ||
"@types/node": "^14.14.7", | ||
"@types/d3-scale": "^3.2.2", | ||
"@types/fs-extra": "^9.0.6", | ||
"@types/node": "^14.14.19", | ||
"@types/tmp": "^0.2.0", | ||
"@typescript-eslint/eslint-plugin": "^4.8.1", | ||
"@typescript-eslint/parser": "^4.8.1", | ||
"@typescript-eslint/eslint-plugin": "^4.11.1", | ||
"@typescript-eslint/parser": "^4.11.1", | ||
"chai": "^4.2.0", | ||
"chai-files": "^1.4.0", | ||
"eslint": "^7.13.0", | ||
"eslint-config-prettier": "^6.15.0", | ||
"eslint": "^7.17.0", | ||
"eslint-config-prettier": "^7.1.0", | ||
"eslint-plugin-filenames": "^1.3.2", | ||
"eslint-plugin-import": "^2.22.1", | ||
"eslint-plugin-oclif": "^0.1.0", | ||
"eslint-plugin-prettier": "^3.1.4", | ||
"eslint-plugin-simple-import-sort": "^5.0.3", | ||
"eslint-plugin-prettier": "^3.3.0", | ||
"eslint-plugin-simple-import-sort": "5.0.3", | ||
"mocha": "^8.2.1", | ||
"mock-fs": "^4.13.0", | ||
"nyc": "^15.1.0", | ||
"prettier": "^2.1.2", | ||
"ts-node": "^9.0.0", | ||
"typescript": "^4.0.5", | ||
"typescript-json-schema": "^0.43.0" | ||
"prettier": "^2.2.1", | ||
"ts-node": "^9.1.1", | ||
"typescript": "^4.1.3", | ||
"typescript-json-schema": "^0.47.0" | ||
}, | ||
"engine": "node >= 10", | ||
"gitHead": "21395e4b64a55bf92aeb6cdef441e48f2ee4b854" | ||
"gitHead": "fc452a997ad2db56e451bcf4b98650336e975c80" | ||
} |
@@ -0,1 +1,2 @@ | ||
import { median } from 'd3-array'; | ||
import * as jStat from 'jstat'; | ||
@@ -13,3 +14,3 @@ | ||
/** | ||
* Apply the passed "_func" to the permutations of the items in listOne and listTwo | ||
* Apply the passed "func" to the permutations of the items in listOne and listTwo | ||
* | ||
@@ -43,24 +44,59 @@ * @param listOne - Array of numbers | ||
export function confidenceInterval( | ||
distributionOne: number[], | ||
distributionTwo: number[], | ||
interval: number | ||
): [number, number] { | ||
const distributionOneLength = distributionOne.length; | ||
const distributionTwoLength = distributionTwo.length; | ||
a: number[], | ||
b: number[], | ||
confidence: number | ||
): { | ||
lower: number; | ||
median: number; | ||
upper: number; | ||
U: number; | ||
zScore: number; | ||
pValue: number; | ||
} { | ||
const aLength = a.length; | ||
const bLength = b.length; | ||
const maxU = aLength * bLength; | ||
const meanU = maxU / 2; | ||
const lengthsMultiplied = distributionOneLength * distributionTwoLength; | ||
const sqrtOfSomething = Math.sqrt( | ||
(lengthsMultiplied * (distributionOneLength + distributionTwoLength + 1)) / | ||
12 | ||
const deltas = a | ||
.map((a) => b.map((b) => a - b)) | ||
.flat() | ||
.sort((a, b) => a - b); | ||
const U = deltas.reduce( | ||
(accum, value) => accum + (value < 0 ? 1 : value == 0 ? 0.5 : 0), | ||
0 | ||
); | ||
const other = | ||
jStat.normal.inv(1 - (1 - interval) / 2, 0, 1) * sqrtOfSomething; | ||
const ca = Math.floor( | ||
(distributionOneLength * distributionTwoLength) / 2 - other | ||
const lowerTail = U <= meanU; | ||
const standadDeviationU = Math.sqrt((maxU * (aLength + bLength + 1)) / 12); | ||
// we are estimating a discrete distribution so bias the mean depending on which tail | ||
// we are computing the pValue for | ||
const continuityCorrection = lowerTail ? 0.5 : -0.5; | ||
const zScore = (U - meanU + continuityCorrection) / standadDeviationU; | ||
// z is symmetrical, so use lower tail and double the cumulative for each tail | ||
// since this is a two tailed test | ||
const pValue = jStat.normal.cdf(-Math.abs(zScore), 0, 1) * 2; | ||
const alpha = 1 - confidence; | ||
const lowerU = Math.round( | ||
jStat.normal.inv(alpha / 2, meanU + 0.5, standadDeviationU) | ||
); | ||
const diffs = cartesianProduct(distributionOne, distributionTwo); | ||
return [ | ||
diffs[ca - 1], | ||
diffs[distributionOneLength * distributionTwoLength - ca] | ||
]; | ||
const upperU = Math.round( | ||
jStat.normal.inv(1 - alpha / 2, meanU + 0.5, standadDeviationU) | ||
); | ||
return { | ||
lower: deltas[lowerU], | ||
median: median(deltas) ?? 0, | ||
upper: deltas[upperU], | ||
zScore, | ||
pValue, | ||
U | ||
}; | ||
} |
@@ -218,10 +218,10 @@ import { cross, histogram, mean, quantile } from 'd3-array'; | ||
const isSig = | ||
(ci[0] < 0 && 0 < ci[1]) || | ||
(ci[0] > 0 && 0 > ci[1]) || | ||
(ci[0] === 0 && ci[1] === 0) | ||
(ci.lower < 0 && 0 < ci.upper) || | ||
(ci.lower > 0 && 0 > ci.upper) || | ||
(ci.lower === 0 && ci.upper === 0) | ||
? false | ||
: true; | ||
return { | ||
min: Math.round(Math.ceil(ci[0] * 100) / 100), | ||
max: Math.round(Math.ceil(ci[1] * 100) / 100), | ||
min: Math.round(Math.ceil(ci.lower * 100) / 100), | ||
max: Math.round(Math.ceil(ci.upper * 100) / 100), | ||
isSig | ||
@@ -228,0 +228,0 @@ }; |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
222351
1235
Updatedd3-array@^2.9.1