Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function compute(str) {
let max = clamp(Math.floor(str.length / 2), 1, 8);
let poss = generatePossibilities(max);
let result = 1;
for (let i = 2; i <= max; ++i) {
let values = poss[i - 1];
let count = values.length;
let observed = values.map(v => findCount(str, v));
let sum = total(observed);
let chi = total(observed.map(o => square(o - sum / count) / sum * count));
let deg = count - 1;
result = Math.min(result, random.chiCDF(chi, deg));
}
return clamp(result, 0, 1);
}
function compute(str) {
let max = clamp(Math.floor(str.length / 2), 1, 8);
let poss = generatePossibilities(max);
let result = 1;
for (let i = 2; i <= max; ++i) {
let values = poss[i - 1];
let count = values.length;
let observed = values.map(v => findCount(str, v));
let sum = total(observed);
let chi = total(observed.map(o => square(o - sum / count) / sum * count));
let deg = count - 1;
result = Math.min(result, random.chiCDF(chi, deg));
}
return clamp(result, 0, 1);
}
export function weighted(weights: number[]) {
const x = Math.random() * total(weights);
let cum = 0;
return weights.findIndex((w) => (cum += w) >= x);
}
export function covariance(aX: number[], aY: number[]) {
if (aX.length !== aY.length) throw new Error('Array length mismatch.');
const sum = aX.reduce((a, v, i) => a + v * aY[i], 0);
return (sum - total(aX) * total(aY) / aX.length) / aX.length;
}
export function mean(values: number[]) {
return values.length ? total(values) / values.length : 0;
}
function compute(str: string) {
const max = clamp(Math.floor(str.length / 2), 1, 8);
const poss = generatePossibilitiesCached(max);
let result = 1;
for (let i = 2; i <= max; ++i) {
let values = poss[i - 1];
let count = values.length;
let observed = values.map(v => findCount(str, v));
let sum = total(observed);
let chi = total(observed.map(o => ((o - sum / count) ** 2) / sum * count));
let deg = count - 1;
result = Math.min(result, Random.chiCDF(chi, deg));
}
return clamp(result, 0, 1);
}