How to use the @mathigon/core.total function in @mathigon/core

To help you get started, we’ve selected a few @mathigon/core examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github mathigon / textbooks / probability / functions.js View on Github external
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);
  }
github mathigon / textbooks / probability / functions.js View on Github external
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);
  }
github mathigon / fermat.js / src / random.ts View on Github external
export function weighted(weights: number[]) {
    const x = Math.random() * total(weights);

    let cum = 0;
    return weights.findIndex((w) => (cum += w) >= x);
  }
github mathigon / fermat.js / src / statistics.ts View on Github external
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;
}
github mathigon / fermat.js / src / statistics.ts View on Github external
export function mean(values: number[]) {
  return values.length ? total(values) / values.length : 0;
}
github mathigon / textbooks / content / statistics / functions.ts View on Github external
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);
}