exponentialMovingAverageWithAlpha()
Create an exponential average of
BigDecimal
values, with the given alpha
.Implementation Notes
This implementation is suitable for Stream<BigDecimal>
, for a version that takes user-specified mapping function see exponentialMovingAverageWithAlphaBy()
.
By default, nulls are ignored and play no part in calculations, see treatNullAs()
and treatNullAsZero()
below for ways to change this behavior.
Signatures
exponentialMovingAverageWithAlpha(double alpha)
alpha
- The alpha value to use, which must be between 0 and 1, exclusive
Additional Methods
Method | Purpose |
---|---|
treatNullAsZero() | When encountering a null value in a stream, treat it as BigDecimal.ZERO instead. See example. |
treatNullAs(BigDecimal replacement) | When encountering a null value in a stream, treat it as the given replacement value instead. See example. |
withOriginal() | Include the original input value from the stream in addition to the calculated value in a WithOriginal record. See example. |
Examples
Exponential moving average of alpha of 0.3
Stream
.of("10.5", "15.2", "8.7", "12.0", "9.8")
.map(BigDecimal::new)
.gather(Gatherers4j.exponentialMovingAverageWithAlpha(0.3))
.toList();
// [
// BigDecimal("10.5")
// BigDecimal("11.91")
// BigDecimal("10.947")
// BigDecimal("11.2629")
// BigDecimal("10.82403")
// ]
Showing nulls are ignored by default
Stream
.of(null, null, "10.5", null, "15.2", "8.7", "12.0", "9.8")
.map(it -> it == null ? null : new BigDecimal(it))
.gather(Gatherers4j.exponentialMovingAverageWithAlpha(0.3))
.toList();
// [
// BigDecimal("10.5")
// BigDecimal("11.91")
// BigDecimal("10.947")
// BigDecimal("11.2629")
// BigDecimal("10.82403")
// ]
Treating null as zero
Stream
.of(null, null, "10.5", null, "15.2", "8.7", "12.0", "9.8")
.map(it -> it == null ? null : new BigDecimal(it))
.gather(Gatherers4j
.exponentialMovingAverageWithAlpha(0.3)
.treatNullAsZero()
)
.toList();
// [
// BigDecimal("0")
// BigDecimal("0.0")
// BigDecimal("3.15")
// BigDecimal("2.205")
// BigDecimal("6.1035")
// BigDecimal("6.88245")
// BigDecimal("8.417715")
// BigDecimal("8.8324005")
// ]
Replacing null with another BigDecimal
Stream
.of(null, null, "10.5", null, "15.2", "8.7", "12.0", "9.8")
.map(it -> it == null ? null : new BigDecimal(it))
.gather(Gatherers4j
.exponentialMovingAverageWithAlpha(0.3)
.treatNullAs(BigDecimal.ONE)
)
.toList();
// [
// BigDecimal("1")
// BigDecimal("1.0")
// BigDecimal("3.85")
// BigDecimal("2.995")
// BigDecimal("6.6565")
// BigDecimal("7.26955")
// BigDecimal("8.688685")
// BigDecimal("9.0220795")
// ]
Emitting a record containing the original and calculated values
Stream
.of("10.5", "15.2", "8.7", "12.0", "9.8")
.map(BigDecimal::new)
.gather(Gatherers4j
.exponentialMovingAverageWithAlpha(0.3)
.withOriginal()
)
.toList();
// [
// WithOriginal[original=10.5, calculated=10.5]
// WithOriginal[original=15.2, calculated=11.91]
// WithOriginal[original=8.7, calculated=10.947]
// WithOriginal[original=12.0, calculated=11.2629]
// WithOriginal[original=9.8, calculated=10.82403]
// ]