|
| 1 | +/* |
| 2 | + * * |
| 3 | + * Copyright 2014 Comcast Cable Communications Management, LLC |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + * / |
| 17 | + */ |
| 18 | + |
| 19 | +(function () { |
| 20 | + 'use strict'; |
| 21 | + |
| 22 | + /** |
| 23 | + * Performs statistical calculations. |
| 24 | + * @param data - array of values used for computation. |
| 25 | + * @param iqrFilter - boolean to either perform iqrFiltering or slicing. |
| 26 | + * @param start - slicing start value |
| 27 | + * @param end - slicing end value. |
| 28 | + * @param callbackComplete - callback function for test suite for complete event. |
| 29 | + */ |
| 30 | + function statisticalCalculator(data, iqrFilter, start, end, callbackComplete) { |
| 31 | + this.data = data; |
| 32 | + this.isIqrFilter = iqrFilter; |
| 33 | + //start is the value used for slicing the slowest data with given percentage. |
| 34 | + this.start = start; |
| 35 | + //end is the value used for slicing the fastest data with given percentage. |
| 36 | + this.end = end; |
| 37 | + this.clientCallbackComplete = callbackComplete; |
| 38 | + } |
| 39 | + |
| 40 | + /** |
| 41 | + * This function is used to do the calculations either with IQRFiltering or go with Slicing |
| 42 | + */ |
| 43 | + statisticalCalculator.prototype.getResults = function () { |
| 44 | + this.data = this.data.sort(this.numericComparator); |
| 45 | + var dataset = (this.isIqrFilter) ? this.interQuartileRange(data) : this.slicing(this.data, this.start, this.end); |
| 46 | + var peakValue = dataset[dataset.length - 1]; |
| 47 | + var mean = this.meanCalculator(dataset); |
| 48 | + |
| 49 | + var results = { |
| 50 | + stats: mean, |
| 51 | + peakValue: peakValue |
| 52 | + }; |
| 53 | + this.clientCallbackComplete(results); |
| 54 | + }; |
| 55 | + |
| 56 | + /** |
| 57 | + * Calculate the interquartile range of an array of data points |
| 58 | + * |
| 59 | + * https://en.wikipedia.org/wiki/Interquartile_range |
| 60 | + * In descriptive statistics, the interquartile range (IQR), also called |
| 61 | + * the midspread or middle fifty, or technically H-spread, is a measure of |
| 62 | + * statistical dispersion, being equal to the difference between the upper and |
| 63 | + * lower quartiles,[1][2] IQR = Q3 − Q1. In other words, the IQR is the 1st quartile subtracted from the 3rd quartile |
| 64 | + * The interquartile range is often used to find outliers in data. Outliers are observations |
| 65 | + * that fall below Q1 - 1.5(IQR) or above Q3 + 1.5(IQR) |
| 66 | + */ |
| 67 | + statisticalCalculator.prototype.interQuartileRange = function (a) { |
| 68 | + var length = a.length, |
| 69 | + dataSetWithOutliersRemoved = [], |
| 70 | + isEven = (a.length) % 2, |
| 71 | + iqr, |
| 72 | + i, |
| 73 | + fQMedian, |
| 74 | + tQMedian, |
| 75 | + n; |
| 76 | + |
| 77 | + if (isEven === 0) { |
| 78 | + n = a.length; |
| 79 | + fQMedian = this.medianCalculator(a.slice(0, (a.length) / 2)); |
| 80 | + tQMedian = this.medianCalculator(a.slice((a.length) / 2, n)); |
| 81 | + iqr = tQMedian - fQMedian; |
| 82 | + |
| 83 | + } else { |
| 84 | + n = a.length - 1; |
| 85 | + a.splice((n / 2), 1); |
| 86 | + |
| 87 | + fQMedian = this.medianCalculator(a.slice(0, (a.length) / 2)); |
| 88 | + tQMedian = this.medianCalculator(a.slice(((a.length) / 2) + 1, n + 1)); |
| 89 | + iqr = tQMedian - fQMedian; |
| 90 | + } |
| 91 | + var lowerOutliers = fQMedian - (1.5 * iqr); |
| 92 | + var upperOutliers = tQMedian + (1.5 * iqr); |
| 93 | + |
| 94 | + for (i = 0; i < length; i++) { |
| 95 | + |
| 96 | + if (a[i] > lowerOutliers && a[i] < upperOutliers) { |
| 97 | + dataSetWithOutliersRemoved.push(a[i]); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + return dataSetWithOutliersRemoved; |
| 102 | + }; |
| 103 | + |
| 104 | + /** |
| 105 | + * Simple JavaScript comparator to help with sorting. |
| 106 | + * @param a |
| 107 | + * @param b |
| 108 | + * @returns {number} |
| 109 | + * @constructor |
| 110 | + */ |
| 111 | + statisticalCalculator.prototype.numericComparator = function (a, b) { |
| 112 | + return (a - b); |
| 113 | + }; |
| 114 | + |
| 115 | + /** |
| 116 | + * Calculates the Median. |
| 117 | + * @param a |
| 118 | + * @returns {number} |
| 119 | + */ |
| 120 | + statisticalCalculator.prototype.medianCalculator = function (a) { |
| 121 | + var n = a.length - 1; |
| 122 | + var median = ((a[Math.floor(n / 2)] + a[Math.ceil(n / 2)]) / 2); |
| 123 | + return median; |
| 124 | + }; |
| 125 | + |
| 126 | + /** |
| 127 | + * Calculates the sum and mean of an array |
| 128 | + * @param arr |
| 129 | + * @returns {{sum: *, mean: number}} |
| 130 | + */ |
| 131 | + statisticalCalculator.prototype.meanCalculator = function (arr) { |
| 132 | + var sum = arr.reduce(function (a, b) { |
| 133 | + return a + b; |
| 134 | + }, 0); |
| 135 | + var mean = sum / arr.length; |
| 136 | + return { |
| 137 | + sum: sum, |
| 138 | + mean: mean |
| 139 | + }; |
| 140 | + }; |
| 141 | + |
| 142 | + /** |
| 143 | + * Function takes in array slices the top 30% and bottom 10% of data |
| 144 | + * @param a |
| 145 | + * @returns {*} |
| 146 | + */ |
| 147 | + statisticalCalculator.prototype.slicing = function (a, start, end) { |
| 148 | + var dataLength = a.length; |
| 149 | + var start = Math.round(dataLength * start); |
| 150 | + var end = Math.round(dataLength * end); |
| 151 | + var sliceData = a.slice(start, end); |
| 152 | + return sliceData; |
| 153 | + }; |
| 154 | + |
| 155 | + window.statisticalCalculator = statisticalCalculator; |
| 156 | + |
| 157 | +})(); |
0 commit comments