How to use the jstat.mean function in jstat

To help you get started, we’ve selected a few jstat 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 zhilu-nanjing / financial-cell / test / mycode / myfunc.js View on Github external
function variance(array) {
        let avg = jStat.mean(array)
        let sum = 0;
        for (let i = 0; i < array.length; i++) {
            sum += Math.pow((array[i] - avg), 2);
        }
        return sum/(array.length - 1);
    };
github zhilu-nanjing / financial-cell / src / calc / expression_fn / lib / statistical.js View on Github external
exports.TRIMMEAN = function (range, percent) {
  range = parseNumberArray(flatten(range));
  percent = parseNumber(percent);
  if (anyIsError(range, percent)) {
    return Error(ERROR_VALUE);
  }
  let trim = mathTrig.FLOORMATH(range.length * percent, 2) / 2;
  return jStat.mean(utils.initial(utils.rest(range.sort(function (a, b) {
    return a - b;
  }), trim), trim));
};
github zhilu-nanjing / financial-cell / src / calc / expression_fn / lib / statistical.js View on Github external
export function LINEST(data_y, data_x) {
  data_y = parseNumberArray(flatten(data_y));
  data_x = parseNumberArray(flatten(data_x));
  if (anyIsError(data_y, data_x)) {
    return Error(ERROR_VALUE);
  }
  let ymean = jStat.mean(data_y);
  let xmean = jStat.mean(data_x);
  let n = data_x.length;
  let num = 0;
  let den = 0;
  for (let i = 0; i < n; i++) {
    num += (data_x[i] - xmean) * (data_y[i] - ymean);
    den += Math.pow(data_x[i] - xmean, 2);
  }
  let m = num / den;
  let b = ymean - m * xmean;
  return [m, b];
};
github zhilu-nanjing / financial-cell / src / calc / expression_fn / lib / statistical.js View on Github external
export function COVARIANCE__P(array1, array2) {
  array1 = parseNumberArray(flatten(array1));
  array2 = parseNumberArray(flatten(array2));
  if (anyIsError(array1, array2)) {
    return Error(ERROR_VALUE);
  }
  let mean1 = jStat.mean(array1);
  let mean2 = jStat.mean(array2);
  let result = 0;
  let n = array1.length;
  for (let i = 0; i < n; i++) {
    result += (array1[i] - mean1) * (array2[i] - mean2);
  }
  return result / n;
};
github zhilu-nanjing / financial-cell / src / calc / expression_fn / lib / statistical.js View on Github external
export function FORECAST(x, data_y, data_x) {
  x = parseNumber(x);
  data_y = parseNumberArray(flatten(data_y));
  data_x = parseNumberArray(flatten(data_x));
  if (anyIsError(x, data_y, data_x)) {
    return Error(ERROR_VALUE);
  }
  let xmean = jStat.mean(data_x);
  let ymean = jStat.mean(data_y);
  let n = data_x.length;
  let num = 0;
  let den = 0;
  for (let i = 0; i < n; i++) {
    num += (data_x[i] - xmean) * (data_y[i] - ymean);
    den += Math.pow(data_x[i] - xmean, 2);
  }
  let b = num / den;
  let a = ymean - b * xmean;
  return a + b * x;
};
github zhilu-nanjing / financial-cell / src / calc / expression_fn / lib / statistical.js View on Github external
export function COVARIANCE__P(array1, array2) {
  array1 = parseNumberArray(flatten(array1));
  array2 = parseNumberArray(flatten(array2));
  if (anyIsError(array1, array2)) {
    return Error(ERROR_VALUE);
  }
  let mean1 = jStat.mean(array1);
  let mean2 = jStat.mean(array2);
  let result = 0;
  let n = array1.length;
  for (let i = 0; i < n; i++) {
    result += (array1[i] - mean1) * (array2[i] - mean2);
  }
  return result / n;
};
github zhilu-nanjing / financial-cell / src / calc / expression_fn / lib / statistical.js View on Github external
exports.T.TEST = function(data_x, data_y) {
  data_x = parseNumberArray(flatten(data_x));
  data_y = parseNumberArray(flatten(data_y));
  if (anyIsError(data_x, data_y)) {
    return Error(ERROR_VALUE);
  }

  let mean_x = jStat.mean(data_x);
  let mean_y = jStat.mean(data_y);
  let s_x = 0;
  let s_y = 0;
  let i;

  for (i = 0; i < data_x.length; i++) {
    s_x += Math.pow(data_x[i] - mean_x, 2);
  }
  for (i = 0; i < data_y.length; i++) {
    s_y += Math.pow(data_y[i] - mean_y, 2);
  }

  s_x = s_x / (data_x.length-1);
  s_y = s_y / (data_y.length-1);

  let t = Math.abs(mean_x - mean_y) / Math.sqrt(s_x/data_x.length + s_y/data_y.length);
github zhilu-nanjing / financial-cell / src / calc / expression_fn / lib / statistical.js View on Github external
exports.T.TEST = function(data_x, data_y) {
  data_x = parseNumberArray(flatten(data_x));
  data_y = parseNumberArray(flatten(data_y));
  if (anyIsError(data_x, data_y)) {
    return Error(ERROR_VALUE);
  }

  let mean_x = jStat.mean(data_x);
  let mean_y = jStat.mean(data_y);
  let s_x = 0;
  let s_y = 0;
  let i;

  for (i = 0; i < data_x.length; i++) {
    s_x += Math.pow(data_x[i] - mean_x, 2);
  }
  for (i = 0; i < data_y.length; i++) {
    s_y += Math.pow(data_y[i] - mean_y, 2);
  }

  s_x = s_x / (data_x.length-1);
  s_y = s_y / (data_y.length-1);

  let t = Math.abs(mean_x - mean_y) / Math.sqrt(s_x/data_x.length + s_y/data_y.length);
github zhilu-nanjing / financial-cell / src / calc / expression_fn / lib / statistical.js View on Github external
exports.SKEW = function() {
  let range = parseNumberArray(flatten(arguments));
  if (range instanceof Error) {
    return range;
  }
  let mean = jStat.mean(range);
  let n = range.length;
  let sigma = 0;
  for (let i = 0; i < n; i++) {
    sigma += Math.pow(range[i] - mean, 3);
  }
  return n * sigma / ((n - 1) * (n - 2) * Math.pow(jStat.stdev(range, true), 3));
};