Advertisement
Guest User

Untitled

a guest
Mar 21st, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.73 KB | None | 0 0
  1. export class Lab1Component implements OnInit {
  2.  
  3.   public array: number[] = [2, 4, 4, 4.1, 5, 5, 5.5, 6, 7, 7.5, 7.5, 8, 8, 8, 8];
  4.   public element: number;
  5.   public invalidInput = false;
  6.   public values = {
  7.     'average': 0,
  8.     'moda': 0,
  9.     'median': 0,
  10.     'dispersion': 0,
  11.     'squareDeviation': 0,
  12.     'area': 0,
  13.     'fixedDispersion': 0,
  14.     'fixedSquareDeviation': 0,
  15.     'asymmetry': 0,
  16.     'exses': 0
  17.   };
  18.  
  19.   constructor() {
  20.   }
  21.  
  22.   ngOnInit() {
  23.     console.log(this.array);
  24.     this.calculate();
  25.   }
  26.  
  27.   addElement() {
  28.     if (!isNullOrUndefined(this.element)) {
  29.       this.array.push(this.element);
  30.       this.invalidInput = false;
  31.       this.element = null;
  32.       this.calculate();
  33.     } else {
  34.       this.invalidInput = true;
  35.     }
  36.   }
  37.  
  38.   clear() {
  39.     this.array = [];
  40.   }
  41.  
  42.   calculate() {
  43.     const sorted = this.array.sort((a, b) => a - b);
  44.     this.values.average = this.array.reduce((previousValue, currentValue) => previousValue + currentValue) / this.array.length;
  45.     this.values.moda = this.array.reduce((a, b, i, arr) =>
  46.       (arr.filter(v => v === a).length >= arr.filter(v => v === b).length ? a : b), null);
  47.     if (this.array.length % 2 === 0) {
  48.       this.values.median = (this.array[this.array.length / 2] + this.array[this.array.length / 2 - 1]) / 2;
  49.     } else {
  50.       this.values.median = this.array[(this.array.length - 1) / 2];
  51.     }
  52.     const occurency = this.array.reduce(function (acc, curr) {
  53.       if (typeof acc[curr] === 'undefined') {
  54.         acc[curr] = 1;
  55.       } else {
  56.         acc[curr] += 1;
  57.       }
  58.       return acc;
  59.     }, {});
  60.     console.log(occurency);
  61.     const set = Array.from(new Set(this.array));
  62.     this.values.dispersion = ( set.map(value => Math.pow(value, 2) * occurency[value])
  63.         .reduce((previousValue, currentValue) => previousValue + currentValue) / this.array.length )
  64.       - Math.pow(this.values.average, 2);
  65.     this.values.fixedDispersion = this.array.length / ( this.array.length - 1 ) * this.values.dispersion;
  66.     this.values.squareDeviation = Math.sqrt(this.values.dispersion);
  67.     this.values.fixedSquareDeviation = Math.sqrt(this.values.fixedDispersion);
  68.     this.values.area = sorted[sorted.length - 1] - sorted[0];
  69.  
  70.     this.values.asymmetry = 1 / this.array.length * set.map(value => Math.pow(value - this.values.average, 3) * occurency[value])
  71.       .reduce((previousValue, currentValue) => previousValue + currentValue) /
  72.       Math.pow(this.values.squareDeviation, 3);
  73.     this.values.exses = 1 / this.array.length * set.map(value => Math.pow(value - this.values.average, 4) * occurency[value])
  74.         .reduce((previousValue, currentValue) => previousValue + currentValue) /
  75.       Math.pow(this.values.squareDeviation, 4);
  76.   }
  77.  
  78.  
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement