1 /** 2 * 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, software 14 * 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 and 17 * limitations under the License. 18 */ 19 20 package org.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 past 25 * 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 it 27 * was initialized with. 28 * If zero is passed during initialization then it will maintain mean and variance from the 29 * start. It will use O(1) memory only. But note that since it will maintain mean / variance 30 * 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 */ 33 public class RollingStatCalculator { 34 private double currentSum; 35 private double currentSqrSum; 36 // Total number of data values whose statistic is currently present 37 private long numberOfDataValues; 38 private int rollingPeriod; 39 private int currentIndexPosition; 40 // to be used only if we have non-zero rolling period 41 private long [] dataValues; 42 43 /** 44 * Creates a RollingStatCalculator with given number of rolling periods. 45 * @param rollingPeriod 46 */ 47 public RollingStatCalculator(int rollingPeriod) { 48 this.rollingPeriod = rollingPeriod; 49 this.dataValues = fillWithZeros(rollingPeriod); 50 this.currentSum = 0.0; 51 this.currentSqrSum = 0.0; 52 this.currentIndexPosition = 0; 53 this.numberOfDataValues = 0; 54 } 55 56 /** 57 * Inserts given data value to array of data values to be considered for statistics calculation 58 * @param data 59 */ 60 public void insertDataValue(long data) { 61 // if current number of data points already equals rolling period and rolling period is 62 // non-zero then remove one data and update the statistics 63 if(numberOfDataValues >= rollingPeriod && rollingPeriod > 0) { 64 this.removeData(dataValues[currentIndexPosition]); 65 } 66 numberOfDataValues++; 67 currentSum = currentSum + (double)data; 68 currentSqrSum = currentSqrSum + ((double)data * data); 69 if (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 value 78 * @param data 79 */ 80 private void removeData(long data) { 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 values 88 */ 89 public double getMean() { 90 return this.currentSum / (double)numberOfDataValues; 91 } 92 93 /** 94 * @return deviation of the data values that are in the current list of data values 95 */ 96 public double getDeviation() { 97 double variance = (currentSqrSum - (currentSum*currentSum)/(double)(numberOfDataValues))/ 98 numberOfDataValues; 99 return Math.sqrt(variance); 100 } 101 102 /** 103 * @param size 104 * @return an array of given size initialized with zeros 105 */ 106 private long [] fillWithZeros(int size) { 107 long [] zeros = new long [size]; 108 for (int i=0; i<size; i++) { 109 zeros[i] = 0L; 110 } 111 return zeros; 112 } 113 }