Class WeightedMeanAndSum<V extends Number,​W extends Number>

  • Type Parameters:
    V - value type
    W - weight type

    public class WeightedMeanAndSum<V extends Number,​W extends Number>
    extends 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-2020 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 Detail

      • WeightedMeanAndSum

        public WeightedMeanAndSum()
        Constructor.
    • Method Detail

      • 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​(Iterable<V> values,
                                                       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:
        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​(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​(Collection<V> collection,
                                                       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​(Collection<S> collection,
                                                           Function<S,​V> values,
                                                           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