org.nield.kotlinstatistics.LongStatistics.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of kotlin-statistics Show documentation
Show all versions of kotlin-statistics Show documentation
Statistical and analytical extensions for Kotlin
package org.nield.kotlinstatistics
import org.apache.commons.math3.stat.StatUtils
import org.apache.commons.math3.stat.descriptive.DescriptiveStatistics
val LongArray.descriptiveStatistics: Descriptives get() = DescriptiveStatistics().apply { forEach { addValue(it.toDouble()) } }.let(::ApacheDescriptives)
fun LongArray.geometricMean() = StatUtils.geometricMean(asSequence().map { it.toDouble() }.toList().toDoubleArray() )
fun LongArray.median() = percentile(50.0)
fun LongArray.percentile(percentile: Double) = StatUtils.percentile(asSequence().map { it.toDouble() }.toList().toDoubleArray(), percentile)
fun LongArray.variance() = StatUtils.variance(asSequence().map { it.toDouble() }.toList().toDoubleArray())
fun LongArray.sumOfSquares() = StatUtils.sumSq(asSequence().map { it.toDouble() }.toList().toDoubleArray())
fun LongArray.standardDeviation() = descriptiveStatistics.standardDeviation
fun LongArray.normalize() = StatUtils.normalize(asSequence().map { it.toDouble() }.toList().toDoubleArray())
val LongArray.kurtosis get() = descriptiveStatistics.kurtosis
val LongArray.skewness get() = descriptiveStatistics.skewness
// AGGREGATION OPERATORS
inline fun Sequence.sumBy(crossinline keySelector: (T) -> K, crossinline longSelector: (T) -> Long) =
groupApply(keySelector, longSelector) { it.sum() }
inline fun Iterable.sumBy(crossinline keySelector: (T) -> K, crossinline longSelector: (T) -> Long) =
asSequence().sumBy(keySelector, longSelector)
fun Sequence>.sumBy() =
groupApply({it.first}, {it.second}) { it.sum() }
fun Iterable>.sumBy() = asSequence().sumBy()
inline fun Sequence.averageBy(crossinline keySelector: (T) -> K, crossinline longSelector: (T) -> Long) =
groupApply(keySelector, longSelector) { it.average() }
inline fun Iterable.averageBy(crossinline keySelector: (T) -> K, crossinline valueSelector: (T) -> Long) =
asSequence().averageBy(keySelector, valueSelector)
fun Sequence>.averageBy() =
groupApply({it.first}, {it.second}) { it.average() }
fun Iterable>.averageBy() = asSequence().averageBy()
// bin operators
inline fun Sequence.binByLong(binSize: Long,
crossinline valueSelector: (T) -> Long,
rangeStart: Long? = null
) = toList().binByLong(binSize, valueSelector, rangeStart)
inline fun Sequence.binByLong(binSize: Long,
crossinline valueSelector: (T) -> Long,
crossinline groupOp: (List) -> G,
rangeStart: Long? = null
) = toList().binByLong(binSize, valueSelector, groupOp, rangeStart)
inline fun Iterable.binByLong(binSize: Long,
crossinline valueSelector: (T) -> Long,
rangeStart: Long? = null
): BinModel, Long> = toList().binByLong(binSize, valueSelector, { it }, rangeStart)
inline fun Iterable.binByLong(binSize: Long,
crossinline valueSelector: (T) -> Long,
crossinline groupOp: (List) -> G,
rangeStart: Long? = null
) = toList().binByLong(binSize, valueSelector, groupOp, rangeStart)
inline fun List.binByLong(binSize: Long,
crossinline valueSelector: (T) -> Long,
rangeStart: Long? = null
): BinModel, Long> = binByLong(binSize, valueSelector, { it }, rangeStart)
inline fun List.binByLong(binSize: Long,
crossinline valueSelector: (T) -> Long,
crossinline groupOp: (List) -> G,
rangeStart: Long? = null
): BinModel {
val groupedByC = asSequence().groupBy(valueSelector)
val minC = rangeStart?:groupedByC.keys.min()!!
val maxC = groupedByC.keys.max()!!
val bins = mutableListOf>().apply {
var currentRangeStart = minC
var currentRangeEnd = minC
while (currentRangeEnd < maxC) {
currentRangeEnd = currentRangeStart + binSize - 1L
add(currentRangeStart..currentRangeEnd)
currentRangeStart = currentRangeEnd + 1L
}
}
return bins.asSequence()
.map { it to mutableListOf() }
.map { binWithList ->
groupedByC.entries.asSequence()
.filter { it.key in binWithList.first }
.forEach { binWithList.second.addAll(it.value) }
binWithList
}.map { Bin(it.first, groupOp(it.second)) }
.toList()
.let(::BinModel)
}