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

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

  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

  function updateWeights({
    row,
    label,
    learningRate,
    intercept,
    weights
  }) {
    const predictedValue = (0, _predictValue.default)({
      row,
      intercept,
      weights
    });
    const error = label - predictedValue;
    const newIntercept = intercept + learningRate * error * predictedValue * (1 - predictedValue);
    const newWeights = weights.map((w, index) => w + learningRate * error * predictedValue * (1 - predictedValue) * row[index]);
    return {
      newWeights,
      newIntercept,
      error
    };
  }

  const modelTrainer = function modelTrainer({
    learningRate,
    initialWeights,
    initialIntercept = 0
  }) {
    return source$ => source$.pipe((0, _operators.scan)(([intercept, weights, sumOfErrorSquared], [row, label]) => {
      const predictedValue = (0, _predictValue.default)({
        row,
        intercept,
        weights: weights || initialWeights || row.map(() => 0)
      });
      const {
        newWeights,
        newIntercept,
        error
      } = updateWeights({
        row,
        label,
        learningRate,
        intercept,
        weights: weights || initialWeights || row.map(() => 0)
      });
      return [newIntercept, newWeights, sumOfErrorSquared + error ** 2];
    }, [initialIntercept, null, 0]), (0, _operators.map)(([intercept, weights, sumOfErrorSquared]) => ({
      intercept,
      weights,
      sumOfErrorSquared
    })));
  };

  const testExports = {
    updateWeights
  };
  _exports.testExports = testExports;
  var _default = modelTrainer;
  _exports.default = _default;
});
//# sourceMappingURL=modelTrainer.js.map