(function (global, factory) {
  if (typeof define === "function" && define.amd) {
    define(["exports", "rxjs/operators"], factory);
  } else if (typeof exports !== "undefined") {
    factory(exports, require("rxjs/operators"));
  } else {
    var mod = {
      exports: {}
    };
    factory(mod.exports, global.operators);
    global.variance = mod.exports;
  }
})(typeof globalThis !== "undefined" ? globalThis : typeof self !== "undefined" ? self : this, function (_exports, _operators) {
  "use strict";

  Object.defineProperty(_exports, "__esModule", {
    value: true
  });
  _exports.default = void 0;

  function reducer({
    index,
    mean,
    m2
  }, nextNum) {
    if (index === 0) return {
      index: index + 1,
      mean: nextNum,
      m2: 0
    };
    const delta = nextNum - mean;
    const newMean = mean + delta / (index + 1);
    const delta2 = nextNum - newMean;
    return {
      index: index + 1,
      mean: newMean,
      m2: m2 + delta * delta2
    };
  }

  const variance = function variance(initialState, sample = true) {
    return source$ => source$.pipe((0, _operators.scan)(reducer, initialState || {
      index: 0,
      mean: 0,
      m2: null
    }), (0, _operators.filter)(({
      index
    }) => index > 1), (0, _operators.map)(({
      index,
      m2
    }) => sample ? m2 / (index - 1) : m2 / index));
  };

  var _default = variance;
  _exports.default = _default;
});
//# sourceMappingURL=variance.js.map