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
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-2023 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved.
BSD-style license. See OpenTrafficSim License.
- Author:
- Alexander Verbraeck, Peter Knoppers, Wouter Schakel
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionfinal WeightedMeanAndSum<V,
W> Adds a weighted value for each element.final <S> WeightedMeanAndSum<V,
W> Adds each value with a weight given by a function.final WeightedMeanAndSum<V,
W> Adds each value with a weight given by a function.final WeightedMeanAndSum<V,
W> Adds each weighted value from a map.final WeightedMeanAndSum<V,
W> Adds a weighted value for each element.final WeightedMeanAndSum<V,
W> Adds a value with weight.final double
getMean()
Returns the weighted mean of available data.final double
getSum()
Returns the weighted sum of available data.final double
Returns the sum of the weights.
-
Constructor Details
-
WeightedMeanAndSum
public WeightedMeanAndSum()Constructor.
-
-
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
Adds a value with weight.- Parameters:
value
- V; valueweight
- W; weight- Returns:
- this WeightedMeanAndSum for method chaining
-
add
Adds a weighted value for each element. Note that iteration order is pivotal in correct operations. This method should not be used with instances ofLinkedHashMap
orLinkedHashSet
.- 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
Adds a weighted value for each element.- Parameters:
values
- V[]; valuesweights
- W[]; weights- Returns:
- this WeightedMeanAndSum<V, W> for method chaining
-
add
Adds each weighted value from a map.- Parameters:
map
- Map<V, W>; map- Returns:
- this WeightedMeanAndSum<V, W> for method chaining
-
add
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
-