All Downloads are FREE. Search and download functionalities are using the official Maven repository.

tri.timeseries.TimeSeriesAnalytics.kt Maven / Gradle / Ivy

/*-
 * #%L
 * coda-data
 * --
 * Copyright (C) 2020 - 2021 Elisha Peterson
 * --
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */
package tri.timeseries

import tri.covid19.reports.percentChangeTo
import kotlin.math.log2
import kotlin.math.max

/** Sliding window of at least n entries. Results in (size-n+1) entries. */
fun List.slidingWindow(n: Int, includePartialList: Boolean = false) = when {
    !includePartialList -> (n..size).map { subList(it - n, it) }
    else -> indices.map { subList(max(0, it - n + 1), it + 1) }
}

/** Compute diffs between entries. */
fun List.deltas(offset: Int = 1) = (0 until size).map {
    get(it) - getOrElse(it - offset) { 0.0 }
}

/** Construct partial sums of values. */
fun List.partialSums(): List {
    val res = mutableListOf()
    var sum = 0.0
    forEach {
        sum += it
        res += sum
    }
    return res
}

/** Compute changes from prior values, with given offset. The result has [offset] fewer entries. */
fun List.changes(offset: Int = 1): List = (offset until size).map { get(it) - get(it - offset) }

/** Compute percent change from last to next. */
fun List.percentChanges(offset: Int = 1): List = (0 until size).map {
    (getOrElse(it - offset) { Double.NaN }).percentChangeTo(get(it))
}

/** Compute growth rates between entries (ratio of successive entries). Can produce infinity. */
fun List.growthRates(day0: Int = 0) = (1 until size).map {
    when (day0) {
        0 -> get(it) / get(it - 1)
        else -> (get(it) - get(maxOf(0, it-day0))) / (get(it - 1) - get(maxOf(0, it-day0)))
    }
}

/** Compute growth percentage between entries (ratio of change to average). */
fun List.symmetricGrowth() = (1 until size).map { (get(it) - get(it - 1)) / (.5 * (get(it) + get(it - 1))) }

/**
 * Compute doubling time based on constant growth rates.
 * @param sinceDaysAgo how many days ago is "day 0" for computation of growth rates.
 */
fun List.doublingTimes(sinceDaysAgo: Int = 0) = growthRates(sinceDaysAgo).map {
    1/log2(it)
}

/** Compute average over n entries. The first n-1 entries have partial averages if [includePartialList] is true. */
fun List.movingAverage(bucket: Int, nonZero: Boolean = false, includePartialList: Boolean = false) = slidingWindow(bucket, includePartialList).map {
    if (!nonZero) it.average() else it.filter { it != 0.0 }.average()
}
/** Compute sum over n entries. The first n-1 entries have partial sums if [includePartialList] is true. */
fun List.movingSum(bucket: Int, includePartialList: Boolean = false) = slidingWindow(bucket, includePartialList).map { it.sum() }
/** Ratio of n days (top) over m days (bottom). */
fun List.growthRatio(topBucket: Int, bottomBucket: Int): List {
    val top = movingSum(topBucket, false)
    val bottom = movingSum(bottomBucket, false)
    val size = minOf(top.size, bottom.size)
    val topLast = top.takeLast(size)
    val bottomLast = bottom.takeLast(size)
    val res = topLast.mapIndexed { i, v -> v / bottomLast[i] }
    return res
}





© 2015 - 2025 Weber Informatics LLC | Privacy Policy