org.nield.kotlinstatistics.IntegerStatistics.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 IntArray.descriptiveStatistics get(): Descriptives = DescriptiveStatistics().apply { forEach { addValue(it.toDouble()) } }.let(::ApacheDescriptives)
fun IntArray.geometricMean() = StatUtils.geometricMean(asSequence().map { it.toDouble() }.toList().toDoubleArray() )
fun IntArray.median() = percentile(50.0)
fun IntArray.percentile(percentile: Double) = StatUtils.percentile(asSequence().map { it.toDouble() }.toList().toDoubleArray(), percentile)
fun IntArray.variance() = StatUtils.variance(asSequence().map { it.toDouble() }.toList().toDoubleArray())
fun IntArray.sumOfSquares() = StatUtils.sumSq(asSequence().map { it.toDouble() }.toList().toDoubleArray())
fun IntArray.normalize() = StatUtils.normalize(asSequence().map { it.toDouble() }.toList().toDoubleArray())
val IntArray.kurtosis get() = descriptiveStatistics.kurtosis
val IntArray.skewness get() = descriptiveStatistics.skewness
// AGGREGATION OPERATORS
inline fun Sequence.sumBy(crossinline keySelector: (T) -> K, crossinline intSelector: (T) -> Int) =
groupApply(keySelector, intSelector) { it.sum() }
inline fun Iterable.sumBy(crossinline keySelector: (T) -> K, crossinline intSelector: (T) -> Int) =
asSequence().sumBy(keySelector, intSelector)
fun Sequence>.sumBy() =
groupApply({it.first}, {it.second}) { it.sum() }
fun Iterable>.sumBy() = asSequence().sumBy()
inline fun Sequence.averageBy(crossinline keySelector: (T) -> K, crossinline intSelector: (T) -> Int) =
groupApply(keySelector, intSelector) { it.average() }
inline fun Iterable.averageBy(crossinline keySelector: (T) -> K, crossinline intSelector: (T) -> Int) =
asSequence().averageBy(keySelector, intSelector)
fun Sequence>.averageBy() =
groupApply({it.first}, {it.second}) { it.average() }
fun Iterable>.averageBy() = asSequence().averageBy()
// bin operators
inline fun Sequence.binByInt(binSize: Int,
crossinline valueSelector: (T) -> Int,
rangeStart: Int? = null
): BinModel, Int> = toList().binByInt(binSize, valueSelector, { it }, rangeStart)
inline fun Sequence.binByInt(binSize: Int,
crossinline valueSelector: (T) -> Int,
crossinline groupOp: (List) -> G,
rangeStart: Int? = null
) = toList().binByInt(binSize, valueSelector, groupOp, rangeStart)
inline fun Iterable.binByInt(binSize: Int,
crossinline valueSelector: (T) -> Int,
rangeStart: Int? = null
): BinModel, Int> = toList().binByInt(binSize, valueSelector, { it }, rangeStart)
inline fun Iterable.binByInt(binSize: Int,
crossinline valueSelector: (T) -> Int,
crossinline groupOp: (List) -> G,
rangeStart: Int? = null
) = toList().binByInt(binSize, valueSelector, groupOp, rangeStart)
inline fun List.binByInt(binSize: Int,
crossinline valueSelector: (T) -> Int,
rangeStart: Int? = null
): BinModel, Int> = binByInt(binSize, valueSelector, { it }, rangeStart)
inline fun List.binByInt(binSize: Int,
crossinline valueSelector: (T) -> Int,
crossinline groupOp: (List) -> G,
rangeStart: Int? = 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 - 1
add(currentRangeStart..currentRangeEnd)
currentRangeStart = currentRangeEnd + 1
}
}
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)
}