|
| 1 | +package io.sqooba.oss.timeseries.window |
| 2 | + |
| 3 | +import io.sqooba.oss.timeseries.immutable.TSEntry |
| 4 | + |
| 5 | +import scala.collection.mutable |
| 6 | + |
| 7 | +/** An aggregator that does strictly nothing. Used for window creation without aggregation. */ |
| 8 | +object DoNothingAggregator extends TimeUnawareReversibleAggregator[Nothing, Nothing] { |
| 9 | + |
| 10 | + def currentValue: Option[Nothing] = None |
| 11 | + |
| 12 | + def addEntry(entry: TSEntry[Nothing]): Unit = () |
| 13 | + |
| 14 | + def dropEntry(entry: TSEntry[Nothing]): Unit = () |
| 15 | +} |
| 16 | + |
| 17 | +/** A reversible aggregator that keeps track of the total sum of the values |
| 18 | + * present in each entry that is at least partially within the window's domain. |
| 19 | + * |
| 20 | + * Discontinuities in the domain of definition between entries are completely ignored. |
| 21 | + */ |
| 22 | +class SumAggregator[T](implicit n: Numeric[T]) extends TimeUnawareReversibleAggregator[T, T] { |
| 23 | + import n._ |
| 24 | + |
| 25 | + private var sum = n.zero |
| 26 | + |
| 27 | + def currentValue: Option[T] = Some(sum) |
| 28 | + |
| 29 | + def addEntry(entry: TSEntry[T]): Unit = |
| 30 | + sum += entry.value |
| 31 | + |
| 32 | + def dropEntry(entry: TSEntry[T]): Unit = |
| 33 | + sum -= entry.value |
| 34 | + |
| 35 | +} |
| 36 | + |
| 37 | +/** Reversible aggregator that calculates the mean of the values in the window |
| 38 | + * weighted by time of validity. It is therefore time-aware and needs entries |
| 39 | + * to be contained in the window. |
| 40 | + */ |
| 41 | +class MeanAggregator[T](implicit n: Numeric[T]) extends TimeAwareReversibleAggregator[T, Double] { |
| 42 | + import n._ |
| 43 | + |
| 44 | + // Sum of X_i * d_i |
| 45 | + private var sum = .0 |
| 46 | + // Sum of d_i |
| 47 | + private var durations: Long = 0 |
| 48 | + |
| 49 | + def currentValue: Option[Double] = |
| 50 | + // Sum of X_i * d_i / Sum of d_i or None |
| 51 | + if (durations > 0) Some(sum / durations) else None |
| 52 | + |
| 53 | + def addEntry(entry: TSEntry[T]): Unit = { |
| 54 | + sum += weighted(entry) |
| 55 | + durations += entry.validity |
| 56 | + } |
| 57 | + |
| 58 | + def dropEntry(entry: TSEntry[T]): Unit = { |
| 59 | + sum -= weighted(entry) |
| 60 | + durations -= entry.validity |
| 61 | + } |
| 62 | + |
| 63 | + private def weighted(e: TSEntry[T]): Double = e.value.toDouble * e.validity |
| 64 | +} |
| 65 | + |
| 66 | +/** Reversible aggregator that calculates the (biased) standard deviation (e.g. |
| 67 | + * square root of the variance) of the values in the window, weighted by time of |
| 68 | + * validity. It is therefore time-aware and needs entries to be contained in the |
| 69 | + * window. |
| 70 | + */ |
| 71 | +class StdAggregator[T](implicit n: Numeric[T]) extends TimeAwareReversibleAggregator[T, Double] { |
| 72 | + import n._ |
| 73 | + |
| 74 | + // Weighted mean of squares E_w[X^2] |
| 75 | + private val squareMean = new MeanAggregator[T]() |
| 76 | + // Weighted mean E_w[X] |
| 77 | + private val mean = new MeanAggregator[T]() |
| 78 | + |
| 79 | + def currentValue: Option[Double] = |
| 80 | + for { |
| 81 | + mean <- mean.currentValue |
| 82 | + squares <- squareMean.currentValue |
| 83 | + } yield |
| 84 | + // std = sqrt{ E_w[X^2] - E_w[X]^2 } |
| 85 | + Math.sqrt(squares - mean * mean) |
| 86 | + |
| 87 | + def addEntry(entry: TSEntry[T]): Unit = { |
| 88 | + squareMean.addEntry(entry.map(v => v * v)) |
| 89 | + mean.addEntry(entry) |
| 90 | + } |
| 91 | + |
| 92 | + def dropEntry(entry: TSEntry[T]): Unit = { |
| 93 | + squareMean.dropEntry(entry.map(v => v * v)) |
| 94 | + mean.dropEntry(entry) |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +/** A reversible aggregator that keeps track of the minimum of the values |
| 99 | + * present in the window. You can get a maximum aggregator by simply |
| 100 | + * reversing the ordering passed as an implicit. |
| 101 | + * |
| 102 | + * The aggregator uses an ordered internal queue and discards values that |
| 103 | + * can never be the minimum. |
| 104 | + */ |
| 105 | +class MinAggregator[T](implicit n: Ordering[T]) extends TimeUnawareReversibleAggregator[T, T] { |
| 106 | + import n._ |
| 107 | + |
| 108 | + private var minQueue = mutable.Queue[T]() |
| 109 | + |
| 110 | + override def currentValue: Option[T] = minQueue.headOption |
| 111 | + |
| 112 | + def addEntry(e: TSEntry[T]): Unit = { |
| 113 | + // In Scala 2.13, this can be more elegantly solved: |
| 114 | + // minQueue.takeWhileInPlace(_ <= e.value).append(e.value) |
| 115 | + minQueue = minQueue.takeWhile(_ <= e.value) |
| 116 | + minQueue.enqueue(e.value) |
| 117 | + } |
| 118 | + |
| 119 | + def dropEntry(entry: TSEntry[T]): Unit = { |
| 120 | + if (minQueue.head == entry.value) minQueue.dequeue() |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +/** Template aggregator that keeps track of the entire window. It is therefore not |
| 125 | + * efficient for most calculations. |
| 126 | + */ |
| 127 | +abstract class QueueAggregator[T, A] extends TimeUnawareReversibleAggregator[T, A] { |
| 128 | + |
| 129 | + private[window] val queue: mutable.Queue[T] = mutable.Queue.empty |
| 130 | + |
| 131 | + def addEntry(e: TSEntry[T]): Unit = |
| 132 | + queue += e.value |
| 133 | + |
| 134 | + def dropEntry(entry: TSEntry[T]): Unit = |
| 135 | + queue.dequeue() |
| 136 | +} |
0 commit comments