-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ML] Move
chi2test
to package (#167237)
The `chi2test` utils so fare were only used within data comparison view. We plan to use it with other plugins, so moving it so a separate package in this PR. `SIGNIFICANCE_LEVELS` was updated to include some more digits.
- Loading branch information
Showing
21 changed files
with
1,247 additions
and
1,092 deletions.
There are no files selected for viewing
Validating CODEOWNERS rules …
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# @kbn/ml-chi2test | ||
|
||
`computeChi2PValue` computes the p-value for how similar the datasets are. | ||
Returned value ranges from 0 to 1, with 1 meaning the datasets are identical. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { criticalTableLookup } from './critical_table_lookup'; | ||
import type { Histogram } from './types'; | ||
|
||
/** | ||
* Compute the p-value for how similar the datasets are. | ||
* Returned value ranges from 0 to 1, with 1 meaning the datasets are identical. | ||
* | ||
* @param {Histogram[]} normalizedBaselineTerms - An array of normalized baseline terms (Histogram objects). | ||
* @param {Histogram[]} normalizedDriftedTerms - An array of normalized drifted terms (Histogram objects). | ||
* @returns {number} The p-value indicating the similarity of the datasets. | ||
*/ | ||
export const computeChi2PValue = ( | ||
normalizedBaselineTerms: Histogram[], | ||
normalizedDriftedTerms: Histogram[] | ||
) => { | ||
// Get all unique keys from both arrays | ||
const allKeys: string[] = Array.from( | ||
new Set([ | ||
...normalizedBaselineTerms.map((term) => term.key.toString()), | ||
...normalizedDriftedTerms.map((term) => term.key.toString()), | ||
]) | ||
).slice(0, 100); | ||
|
||
// Calculate the chi-squared statistic and degrees of freedom | ||
let chiSquared: number = 0; | ||
const degreesOfFreedom: number = allKeys.length - 1; | ||
|
||
if (degreesOfFreedom === 0) return 1; | ||
|
||
allKeys.forEach((key) => { | ||
const baselineTerm = normalizedBaselineTerms.find((term) => term.key === key); | ||
const driftedTerm = normalizedDriftedTerms.find((term) => term.key === key); | ||
|
||
const observed: number = driftedTerm?.percentage ?? 0; | ||
const expected: number = baselineTerm?.percentage ?? 0; | ||
chiSquared += Math.pow(observed - expected, 2) / (expected > 0 ? expected : 1e-6); // Prevent divide by zero | ||
}); | ||
|
||
// Use the criticalTableLookup function to determine the p-value | ||
return criticalTableLookup(chiSquared, degreesOfFreedom); | ||
}; |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { CRITICAL_VALUES_TABLE, SIGNIFICANCE_LEVELS } from './constants'; | ||
|
||
/** | ||
* Performs a lookup in a critical values table to determine the significance level | ||
* associated with a given chi-squared statistic and degrees of freedom. | ||
* | ||
* @param {number} chi2Statistic - The chi-squared statistic for which the significance level is to be determined. | ||
* @param {number} df - The degrees of freedom (an integer) for the chi-squared test. | ||
* @returns {number} The significance level corresponding to the chi-squared statistic and degrees of freedom. | ||
* @throws {Error} If df is less than 1 or not an integer. | ||
*/ | ||
export const criticalTableLookup = (chi2Statistic: number, df: number) => { | ||
if (df < 1) return 1; | ||
if (!Number.isInteger(df)) throw Error('Degrees of freedom must be a valid integer'); | ||
|
||
// Get the row index | ||
const rowIndex: number = df - 1; | ||
|
||
// Get the column index | ||
let minDiff: number = Math.abs(CRITICAL_VALUES_TABLE[rowIndex][0] - chi2Statistic); | ||
let columnIndex: number = 0; | ||
for (let j = 1; j < CRITICAL_VALUES_TABLE[rowIndex].length; j++) { | ||
const diff: number = Math.abs(CRITICAL_VALUES_TABLE[rowIndex][j] - chi2Statistic); | ||
if (diff < minDiff) { | ||
minDiff = diff; | ||
columnIndex = j; | ||
} | ||
} | ||
|
||
// Determine the significance level from the column index | ||
const significanceLevel: number = SIGNIFICANCE_LEVELS[columnIndex]; | ||
return significanceLevel; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
export { computeChi2PValue } from './compute_chi_2_pvalue'; | ||
export { criticalTableLookup } from './critical_table_lookup'; | ||
export { CRITICAL_VALUES_TABLE, SIGNIFICANCE_LEVELS } from './constants'; | ||
export type { Histogram } from './types'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
module.exports = { | ||
preset: '@kbn/test', | ||
rootDir: '../../../..', | ||
roots: ['<rootDir>/x-pack/packages/ml/chi2test'], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"type": "shared-common", | ||
"id": "@kbn/ml-chi2test", | ||
"owner": "@elastic/ml-ui" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"name": "@kbn/ml-chi2test", | ||
"private": true, | ||
"version": "1.0.0", | ||
"license": "Elastic License 2.0" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"extends": "../../../../tsconfig.base.json", | ||
"compilerOptions": { | ||
"outDir": "target/types", | ||
"types": [ | ||
"jest", | ||
"node", | ||
"react" | ||
] | ||
}, | ||
"include": [ | ||
"**/*.ts", | ||
"**/*.tsx", | ||
], | ||
"exclude": [ | ||
"target/**/*" | ||
], | ||
"kbn_references": [] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
/** | ||
* Interface for the Histogram type used by computeChi2PValue. | ||
*/ | ||
export interface Histogram { | ||
/** | ||
* The doc count. | ||
*/ | ||
doc_count: number; | ||
/** | ||
* The key. | ||
*/ | ||
key: string | number; | ||
/** | ||
* Optional percentage. | ||
*/ | ||
percentage?: number; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.