1/**2*3* Licensed to the Apache Software Foundation (ASF) under one4* or more contributor license agreements. See the NOTICE file5* distributed with this work for additional information6* regarding copyright ownership. The ASF licenses this file7* to you under the Apache License, Version 2.0 (the8* "License"); you may not use this file except in compliance9* with the License. You may obtain a copy of the License at10*11* http://www.apache.org/licenses/LICENSE-2.012*13* Unless required by applicable law or agreed to in writing, software14* distributed under the License is distributed on an "AS IS" BASIS,15* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.16* See the License for the specific language governing permissions and17* limitations under the License.18*/19 20packageorg.apache.hadoop.hbase.util; 21 22/**23* This class maintains mean and variation for any sequence of input provided to it.24* It is initialized with number of rolling periods which basically means the number of past25* inputs whose data will be considered to maintain mean and variation.26* It will use O(N) memory to maintain these statistics, where N is number of look up periods it27* was initialized with.28* If zero is passed during initialization then it will maintain mean and variance from the29* start. It will use O(1) memory only. But note that since it will maintain mean / variance30* from the start the statistics may behave like constants and may ignore short trends.31* All operations are O(1) except the initialization which is O(N).32*/33publicclassRollingStatCalculator { 34privatedoublecurrentSum; 35privatedoublecurrentSqrSum; 36// Total number of data values whose statistic is currently present37privatelongnumberOfDataValues; 38privateintrollingPeriod; 39privateintcurrentIndexPosition; 40// to be used only if we have non-zero rolling period41privatelong[] dataValues; 42 43/**44* Creates a RollingStatCalculator with given number of rolling periods.45* @param rollingPeriod46*/47publicRollingStatCalculator(introllingPeriod) { 48this.rollingPeriod = rollingPeriod; 49this.dataValues = fillWithZeros(rollingPeriod); 50this.currentSum = 0.0; 51this.currentSqrSum = 0.0; 52this.currentIndexPosition = 0; 53this.numberOfDataValues = 0; 54 } 55 56/**57* Inserts given data value to array of data values to be considered for statistics calculation58* @param data59*/60publicvoidinsertDataValue(longdata) { 61// if current number of data points already equals rolling period and rolling period is62// non-zero then remove one data and update the statistics63if(numberOfDataValues >= rollingPeriod && rollingPeriod > 0) { 64this.removeData(dataValues[currentIndexPosition]); 65 } 66 numberOfDataValues++; 67 currentSum = currentSum + (double)data; 68 currentSqrSum = currentSqrSum + ((double)data * data); 69if(rollingPeriod >0) 70 { 71 dataValues[currentIndexPosition] = data; 72 currentIndexPosition = (currentIndexPosition + 1) % rollingPeriod; 73 } 74 } 75 76/**77* Update the statistics after removing the given data value78* @param data79*/80privatevoidremoveData(longdata) { 81 currentSum = currentSum - (double)data; 82 currentSqrSum = currentSqrSum - ((double)data * data); 83 numberOfDataValues--; 84 } 85 86/**87* @return mean of the data values that are in the current list of data values88*/89publicdoublegetMean() { 90returnthis.currentSum / (double)numberOfDataValues; 91 } 92 93/**94* @return deviation of the data values that are in the current list of data values95*/96publicdoublegetDeviation() { 97doublevariance = (currentSqrSum - (currentSum*currentSum)/(double)(numberOfDataValues))/ 98 numberOfDataValues; 99returnMath.sqrt(variance); 100 } 101 102/**103* @param size104* @return an array of given size initialized with zeros105*/106privatelong[] fillWithZeros(intsize) { 107long[] zeros =newlong[size]; 108for(inti=0; i<size; i++) { 109 zeros[i] = 0L; 110 } 111returnzeros; 112 } 113 }