Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
 
 package io.tehuti.metrics.stats;
 
 public class Histogram {
 
     private final BinScheme binScheme;
     private final float[] hist;
     private double count;
 
     public Histogram(BinScheme binScheme) {
         this. = new float[binScheme.bins()];
         this. = 0.0f;
         this. = binScheme;
     }
 
     public void record(double value) {
         this.[.toBin(value)] += 1.0f;
         this. += 1.0f;
     }
 
     public double value(double quantile) {
         if ( == 0.0d)
             return .;
         float sum = 0.0f;
         float quant = (floatquantile;
         for (int i = 0; i < this..length - 1; i++) {
             sum += this.[i];
             if (sum /  > quant)
                 return .fromBin(i);
         }
         return .;
     }
 
     public float[] counts() {
         return this.;
     }
 
     public void clear() {
         for (int i = 0; i < this..lengthi++)
             this.[i] = 0.0f;
         this. = 0;
     }
 
     @Override
     public String toString() {
         StringBuilder b = new StringBuilder('{');
         for (int i = 0; i < this..length - 1; i++) {
             b.append(String.format("%.10f".fromBin(i)));
             b.append(':');
             b.append(String.format("%.0f"this.[i]));
             b.append(',');
         }
         b.append(.);
         b.append(':');
         b.append(this.[this..length - 1]);
         b.append('}');
         return b.toString();
     }
 
     public interface BinScheme {
         public int bins();
 
         public int toBin(double value);
 
         public double fromBin(int bin);
     }
 
     public static class ConstantBinScheme implements BinScheme {
         private final double min;
         private final double max;
         private final int bins;
         private final double bucketWidth;
 
         public ConstantBinScheme(int binsdouble mindouble max) {
             if (bins < 2)
                 throw new IllegalArgumentException("Must have at least 2 bins.");
             this. = min;
             this. = max;
             this. = bins;
             this. = (max - min) / (bins - 2);
         }
 
         public int bins() {
             return this.;
        }
        public double fromBin(int b) {
            if (b == 0)
                return .;
            else if (b ==  - 1)
                return .;
            else
                return  + (b - 1) * ;
        }
        public int toBin(double x) {
            if (x < )
                return 0;
            else if (x > )
                return  - 1;
            else
                return (int) ((x - ) / ) + 1;
        }
    }
    public static class LinearBinScheme implements BinScheme {
        private final int bins;
        private final double max;
        private final double scale;
        public LinearBinScheme(int numBinsdouble max) {
            this. = numBins;
            this. = max;
            this. = max / (numBins * (numBins - 1) / 2);
        }
        public int bins() {
            return this.;
        }
        public double fromBin(int b) {
            if (b == this. - 1) {
                return .;
            } else {
                double unscaled = (b * (b + 1.0)) / 2.0;
                return unscaled * this.;
            }
        }
        public int toBin(double x) {
            if (x < 0.0d) {
                throw new IllegalArgumentException("Values less than 0.0 not accepted.");
            } else if (x > this.) {
                return this. - 1;
            } else {
                double scaled = x / this.;
                return (int) (-0.5 + Math.sqrt(2.0 * scaled + 0.25));
            }
        }
    }
New to GrepCode? Check out our FAQ X