Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- export class Lab1Component implements OnInit {
- public array: number[] = [2, 4, 4, 4.1, 5, 5, 5.5, 6, 7, 7.5, 7.5, 8, 8, 8, 8];
- public element: number;
- public invalidInput = false;
- public values = {
- 'average': 0,
- 'moda': 0,
- 'median': 0,
- 'dispersion': 0,
- 'squareDeviation': 0,
- 'area': 0,
- 'fixedDispersion': 0,
- 'fixedSquareDeviation': 0,
- 'asymmetry': 0,
- 'exses': 0
- };
- constructor() {
- }
- ngOnInit() {
- console.log(this.array);
- this.calculate();
- }
- addElement() {
- if (!isNullOrUndefined(this.element)) {
- this.array.push(this.element);
- this.invalidInput = false;
- this.element = null;
- this.calculate();
- } else {
- this.invalidInput = true;
- }
- }
- clear() {
- this.array = [];
- }
- calculate() {
- const sorted = this.array.sort((a, b) => a - b);
- this.values.average = this.array.reduce((previousValue, currentValue) => previousValue + currentValue) / this.array.length;
- this.values.moda = this.array.reduce((a, b, i, arr) =>
- (arr.filter(v => v === a).length >= arr.filter(v => v === b).length ? a : b), null);
- if (this.array.length % 2 === 0) {
- this.values.median = (this.array[this.array.length / 2] + this.array[this.array.length / 2 - 1]) / 2;
- } else {
- this.values.median = this.array[(this.array.length - 1) / 2];
- }
- const occurency = this.array.reduce(function (acc, curr) {
- if (typeof acc[curr] === 'undefined') {
- acc[curr] = 1;
- } else {
- acc[curr] += 1;
- }
- return acc;
- }, {});
- console.log(occurency);
- const set = Array.from(new Set(this.array));
- this.values.dispersion = ( set.map(value => Math.pow(value, 2) * occurency[value])
- .reduce((previousValue, currentValue) => previousValue + currentValue) / this.array.length )
- - Math.pow(this.values.average, 2);
- this.values.fixedDispersion = this.array.length / ( this.array.length - 1 ) * this.values.dispersion;
- this.values.squareDeviation = Math.sqrt(this.values.dispersion);
- this.values.fixedSquareDeviation = Math.sqrt(this.values.fixedDispersion);
- this.values.area = sorted[sorted.length - 1] - sorted[0];
- this.values.asymmetry = 1 / this.array.length * set.map(value => Math.pow(value - this.values.average, 3) * occurency[value])
- .reduce((previousValue, currentValue) => previousValue + currentValue) /
- Math.pow(this.values.squareDeviation, 3);
- this.values.exses = 1 / this.array.length * set.map(value => Math.pow(value - this.values.average, 4) * occurency[value])
- .reduce((previousValue, currentValue) => previousValue + currentValue) /
- Math.pow(this.values.squareDeviation, 4);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement