Class VarianceCalculatorLong

Object
VarianceCalculatorLong

public class VarianceCalculatorLong extends Object
Calculates variance efficiently, as successive values are added, using long to store sums internally.

This is similar to VarianceCalculatorDouble but is designed for int values rather than doubles.

Efficiency occurs by maintaining a running sum, sum-of-squares and count as values are added.

This is useful for calculating variance in sequential order in an online manner.

Author:
Owen Feehan
  • Constructor Details

    • VarianceCalculatorLong

      public VarianceCalculatorLong(long sum, long sumSquares, long count)
      Creates a new VarianceCalculatorLong instance.
      Parameters:
      sum - The running sum of values.
      sumSquares - The running sum of squares of values.
      count - The running count of values.
    • VarianceCalculatorLong

      public VarianceCalculatorLong()
  • Method Details

    • add

      public void add(int value)
      Adds a value to the running sum.
      Parameters:
      value - the value to add.
    • add

      public void add(int value, int instances)
      Adds a multiple instances of a value to the running sum.
      Parameters:
      value - the value to add.
      instances - how many instances of value to add.
    • subtract

      public VarianceCalculatorLong subtract(VarianceCalculatorLong toSubtract)
      Subtracts the running-sums and count from another VarianceCalculatorLong from the current object's state.

      This occurs immutably, and the currently object's state is unaffected.

      Parameters:
      toSubtract - the other VarianceCalculatorLong whose state is subtracted.
      Returns:
      a newly created VarianceCalculatorLong whose state is the current object minus toSubtract.
    • mean

      public double mean()
      Calculate the mean, based on the current state of running-sums.
      Returns:
      the mean.
    • variance

      public double variance()
      Calculate the variance, based on the current state of running-sums.
      Returns:
      the variance.
    • getCount

      public long getCount()
      The running count of values.