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

org.kalasim.misc.Math.kt Maven / Gradle / Ivy

There is a newer version: 1.0.3
Show newest version
package org.kalasim.misc

import org.apache.commons.math3.distribution.EnumeratedDistribution
import org.apache.commons.math3.stat.descriptive.*
import org.apache.commons.math3.stat.descriptive.rank.Median
import org.kalasim.monitors.ValueTimeline

fun Collection.median() = Median().evaluate(toDoubleArray())

val DescriptiveStatistics.median: Double
    get() = Median().evaluate(values)


// copied from krangl
fun  List.cumSum(): Iterable =
    drop(1).fold(listOf(first().toDouble())) { list, curVal ->
        list + (list.last().toDouble() + curVal.toDouble())
    }

fun List.merge(): StatisticalSummaryValues = AggregateSummaryStatistics.aggregate(this)

fun  List>.mergeStats() = map { it.statisticsSummary() }.merge()
//
//fun List.mergeStats() = flatMap { it.statisticsSummary().pmf }.groupBy { it.first }
//        .map { it.key to it.value.sumOf { it.second } }.asCMPairList().run {
//            EnumeratedDistribution(this)
//        }

fun  List>.merge() =
    flatMap { it.pmf }
        .groupBy { it.first }
        .map { kv -> kv.key to kv.value.sumOf { it.second } }
        .asCMPairList()
        .run {
            EnumeratedDistribution(this)
        }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy