Impala
Impalaistheopensource,nativeanalyticdatabaseforApacheHadoop.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
StatsHelper.java
Go to the documentation of this file.
1 // Copyright 2014 Cloudera Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 package com.cloudera.impala.util;
16 
28 public class StatsHelper<T extends Number> {
29 
30  private long count_ = 0;
31 
32  // Current mean
33  private double mean_ = 0.0d;
34 
35  // Sum of the square differences from the mean
36  private double m2_ = 0.0d;
37 
38  public void addSample(T val) {
39  ++count_;
40  mean_ += (val.doubleValue() - mean_) / count_;
41  m2_ += Math.pow(val.doubleValue() - mean_, 2);
42  }
43 
44  public long count() { return count_; }
45 
46  public double mean() {
47  return count_ > 0 ? mean_ : 0.0;
48  }
49 
50  public double variance() {
51  return count_ > 1 ? m2_ / (count_ - 1) : 0.0d;
52  }
53 
54  public double stddev() {
55  return Math.sqrt(variance());
56  }
57 }