All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.javanut.pronghorn.util.ma.RunningStdDevRoller Maven / Gradle / Ivy

Go to download

Ring buffer based queuing utility for applications that require high performance and/or a small footprint. Well suited for embedded and stream based processing.

There is a newer version: 1.1.27
Show newest version
package com.javanut.pronghorn.util.ma;

public class RunningStdDevRoller {

    public final RunningStdDev[] buckets;
    public final int samplesPerBucket;
    
    private int activeIdx;
    private int activeSampleCount;
    
    private final int bucketCount;
    private final int bucketMask;
    
    private RunningStdDevListener listener;
    
    public RunningStdDevRoller(int bucketsInBits, int samplesPerBucket) {
               
        this.bucketCount = 1<<(bucketsInBits);
        this.bucketMask = bucketCount-1;
        
        this.buckets = new RunningStdDev[bucketCount];
        int i = bucketCount;
        while (--i>=0) {
            this.buckets[i] = new RunningStdDev();
        }
        
        this.samplesPerBucket = samplesPerBucket;
        this.activeSampleCount = 0;
        
    }
        
    public void sample(double value) {
      
        RunningStdDev.sample( buckets[activeIdx], value);
      
        if (++activeSampleCount == samplesPerBucket) {  
    
            processFinishedStdDev(buckets[activeSampleCount-1]);
            
            activeIdx = ++activeIdx & bucketMask;
            activeSampleCount = 0;
                        
        }
    }
    
    public void setListener(RunningStdDevListener listener) {
        this.listener = listener;
    }
    
    protected void processFinishedStdDev(RunningStdDev runningStdDev) {
        if (null!=listener) {
            listener.process(runningStdDev);
        }
    }

    public double mean(int bucketsBack) {
        return RunningStdDev.mean(buckets[bucketMask&(bucketCount+activeIdx-bucketsBack)]);
    }
   
    public double stdDeviation(int bucketsBack) {
        return RunningStdDev.stdDeviation(buckets[bucketMask&(bucketCount+activeIdx-bucketsBack)]);
    }
    
    public double maxSample(int bucketsBack) {
        return RunningStdDev.maxSample(buckets[bucketMask&(bucketCount+activeIdx-bucketsBack)]);
    }
    
    public double minSample(int bucketsBack) {
        return RunningStdDev.minSample(buckets[bucketMask&(bucketCount+activeIdx-bucketsBack)]);
    }
    
    public double probabilityDensity(int bucketsBack, double x) {
        return RunningStdDev.probabilityDensity(buckets[bucketMask&(bucketCount+activeIdx-bucketsBack)], x);
    }
    
    
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy