Ad
Code
Diff
  • class DataSet {
      constructor(...data) {
        this.data = data;
      }
      get mean() {
        return this._mean = (this._mean !== undefined) ? this._mean
          : this.data.reduce((a, b) => a + b) / this.data.length
      }
      get variance() {
        return this._variance = this._variance !== undefined ? this._variance
          : this.stdDeviation ** 2;
      }
      get stdDeviation() {
        return this._stdDeviation = (this._stdDeviation !== undefined) ? this._stdDeviation
          : Math.sqrt(
            this.data.map(x => x * x).reduce((a, b) => a + b) / this.data.length
            - (this.data.reduce((a, b) => a + b) / this.data.length) ** 2);
      }
      reset() {
        delete this._mean;
        delete this._variance;
        delete this._stdDeviation;
      }
      
    }
    • class DataSet {
    • constructor(...data) {
    • this.data = data;
    • this.mean = this.data.reduce((a,b)=>a+b) / this.data.length;
    • this.variance = this.data.map(x=>x*x).reduce((a,b)=>a+b) / this.data.length - this.mean ** 2;
    • this.stdDeviation = Math.sqrt(this.variance);
    • }
    • setMean() {
    • return this.mean = this.data.reduce((a,b)=>a+b) / this.data.length;
    • get mean() {
    • return this._mean = (this._mean !== undefined) ? this._mean
    • : this.data.reduce((a, b) => a + b) / this.data.length
    • }
    • setVar() {
    • this.stdDeviation = Math.sqrt(this.data.map(x=>x*x).reduce((a,b)=>a+b) / this.data.length - (this.data.reduce((a,b)=>a+b) / this.data.length) ** 2);
    • return this.variance = this.stdDeviation ** 2;
    • get variance() {
    • return this._variance = this._variance !== undefined ? this._variance
    • : this.stdDeviation ** 2;
    • }
    • get stdDeviation() {
    • return this._stdDeviation = (this._stdDeviation !== undefined) ? this._stdDeviation
    • : Math.sqrt(
    • this.data.map(x => x * x).reduce((a, b) => a + b) / this.data.length
    • - (this.data.reduce((a, b) => a + b) / this.data.length) ** 2);
    • }
    • reset() {
    • delete this._mean;
    • delete this._variance;
    • delete this._stdDeviation;
    • }
    • }