Skip to content

Commit

Permalink
fix jsdoc
Browse files Browse the repository at this point in the history
  • Loading branch information
walterra committed Sep 26, 2023
1 parent 3248153 commit 4e15d9e
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 2 deletions.
7 changes: 5 additions & 2 deletions x-pack/packages/ml/chi2test/compute_chi_2_pvalue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ 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 normalizedBaselineTerms
* @param normalizedDriftedTerms
*
* @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[],
Expand Down Expand Up @@ -41,5 +43,6 @@ export const computeChi2PValue = (
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);
};
3 changes: 3 additions & 0 deletions x-pack/packages/ml/chi2test/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1024,6 +1024,9 @@ export const CRITICAL_VALUES_TABLE = [
],
];

/**
* Signifance levels used by `computeChi2PValue`.
*/
export const SIGNIFICANCE_LEVELS = [
0.000001, 0.000032, 0.001, 0.01, 0.02, 0.03, 0.04, 0.05, 0.06, 0.07, 0.08, 0.09, 0.1, 0.11, 0.12,
0.13, 0.14, 0.15, 0.16, 0.17, 0.18, 0.19, 0.2, 0.21, 0.22, 0.23, 0.24, 0.25, 0.26, 0.27, 0.28,
Expand Down
10 changes: 10 additions & 0 deletions x-pack/packages/ml/chi2test/critical_table_lookup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@

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');
Expand All @@ -25,6 +34,7 @@ export const criticalTableLookup = (chi2Statistic: number, df: number) => {
}
}

// Determine the significance level from the column index
const significanceLevel: number = SIGNIFICANCE_LEVELS[columnIndex];
return significanceLevel;
};
12 changes: 12 additions & 0 deletions x-pack/packages/ml/chi2test/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,20 @@
* 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;
}

0 comments on commit 4e15d9e

Please sign in to comment.