Package org.opentrafficsim.base
Class WeightedMeanAndSum<V extends Number,W extends Number>
- java.lang.Object
-
- org.opentrafficsim.base.WeightedMeanAndSum<V,W>
-
- Type Parameters:
V- value typeW- 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 Summary
Constructors Constructor Description WeightedMeanAndSum()Constructor.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description WeightedMeanAndSum<V,W>add(Iterable<V> values, Iterable<W> weights)Adds a weighted value for each element.<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.WeightedMeanAndSum<V,W>add(Collection<V> collection, Function<V,W> weights)Adds each value with a weight given by a function.WeightedMeanAndSum<V,W>add(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.doublegetMean()Returns the weighted mean of available data.doublegetSum()Returns the weighted sum of available data.doublegetWeightSum()Returns the sum of the weights.
-
-
-
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; valueweight- 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 ofLinkedHashMaporLinkedHashSet.- Parameters:
values- Iterable<V>; valuesweights- 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[]; valuesweights- 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>; valuesweights- 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 objectsvalues- Function<S, V>; valuesweights- Function<S, W>; weights- Returns:
- this WeightedMeanAndSum<V, W> for method chaining
-
-