Class WeightedMeanAndSum<V extends java.lang.Number,​W extends java.lang.Number>

java.lang.Object
org.opentrafficsim.base.WeightedMeanAndSum<V,​W>
Type Parameters:
V - value type
W - weight type

public class WeightedMeanAndSum<V extends java.lang.Number,​W extends java.lang.Number>
extends java.lang.Object
Utility to calculate a weighted mean and/or sum. This can be used as part of a process or loop with information being accumulated in the object. This is even a memory friendly method as this class only stores 2 double values internally.

Copyright (c) 2013-2019 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
BSD-style license. See OpenTrafficSim License.

Version:
$Revision$, $LastChangedDate$, by $Author$, initial version 8 okt. 2018
Author:
Alexander Verbraeck, Peter Knoppers, Wouter Schakel
  • Constructor Summary

    Constructors 
    Constructor Description
    WeightedMeanAndSum()
    Constructor.
  • Method Summary

    Modifier and Type Method Description
    WeightedMeanAndSum<V,​W> add​(java.lang.Iterable<V> values, java.lang.Iterable<W> weights)
    Adds a weighted value for each element.
    <S> WeightedMeanAndSum<V,​W> add​(java.util.Collection<S> collection, java.util.function.Function<S,​V> values, java.util.function.Function<S,​W> weights)
    Adds each value with a weight given by a function.
    WeightedMeanAndSum<V,​W> add​(java.util.Collection<V> collection, java.util.function.Function<V,​W> weights)
    Adds each value with a weight given by a function.
    WeightedMeanAndSum<V,​W> add​(java.util.Map<V,​W> map)
    Adds each weighted value from a map.
    WeightedMeanAndSum<V,​W> add​(V[] values, W[] weights)
    Adds a weighted value for each element.
    WeightedMeanAndSum<V,​W> add​(V value, W weight)
    Adds a value with weight.
    double getMean()
    Returns the weighted mean of available data.
    double getSum()
    Returns the weighted sum of available data.
    double getWeightSum()
    Returns the sum of the weights.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

  • Method Details

    • getMean

      public final double getMean()
      Returns the weighted mean of available data.
      Returns:
      double; weighted mean of available data
    • getSum

      public final double getSum()
      Returns the weighted sum of available data.
      Returns:
      double; weighted sum of available data
    • getWeightSum

      public final double getWeightSum()
      Returns the sum of the weights.
      Returns:
      double; sum of the weights
    • add

      public final WeightedMeanAndSum<V,​W> add​(V value, W weight)
      Adds a value with weight.
      Parameters:
      value - V; value
      weight - W; weight
      Returns:
      this WeightedMeanAndSum for method chaining
    • add

      public final WeightedMeanAndSum<V,​W> add​(java.lang.Iterable<V> values, java.lang.Iterable<W> weights)
      Adds a weighted value for each element. Note that iteration order is pivotal in correct operations. This method should not be used with instances of LinkedHashMap or LinkedHashSet.
      Parameters:
      values - Iterable<V>; values
      weights - Iterable<W>; weights
      Returns:
      this WeightedMeanAndSum<V, W> for method chaining
      Throws:
      java.lang.IllegalArgumentException - if the number of values and weights are unequal
    • add

      public final WeightedMeanAndSum<V,​W> add​(V[] values, W[] weights)
      Adds a weighted value for each element.
      Parameters:
      values - V[]; values
      weights - W[]; weights
      Returns:
      this WeightedMeanAndSum<V, W> for method chaining
    • add

      public final WeightedMeanAndSum<V,​W> add​(java.util.Map<V,​W> map)
      Adds each weighted value from a map.
      Parameters:
      map - Map<V, W>; map
      Returns:
      this WeightedMeanAndSum<V, W> for method chaining
    • add

      public final WeightedMeanAndSum<V,​W> add​(java.util.Collection<V> collection, java.util.function.Function<V,​W> weights)
      Adds each value with a weight given by a function.
      Parameters:
      collection - Collection<V>; values
      weights - Function<V, W>; weights
      Returns:
      this WeightedMeanAndSum<V, W> for method chaining
    • add

      public final <S> WeightedMeanAndSum<V,​W> add​(java.util.Collection<S> collection, java.util.function.Function<S,​V> values, java.util.function.Function<S,​W> weights)
      Adds each value with a weight given by a function.
      Type Parameters:
      S - type of source object
      Parameters:
      collection - Collection<S>; collection of source objects
      values - Function<S, V>; values
      weights - Function<S, W>; weights
      Returns:
      this WeightedMeanAndSum<V, W> for method chaining