org.nield.kotlinstatistics.NumberStatistics.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
The newest version!
package org.nield.kotlinstatistics
import org.apache.commons.math3.stat.StatUtils
import org.apache.commons.math3.stat.descriptive.DescriptiveStatistics
//regression
inline fun Iterable.simpleRegression(crossinline xSelector: (T) -> Number, crossinline ySelector: (T) -> Number) = asSequence().simpleRegression(xSelector, ySelector)
typealias ASR = org.apache.commons.math3.stat.regression.SimpleRegression
inline fun Sequence.simpleRegression(crossinline xSelector: (T) -> Number, crossinline ySelector: (T) -> Number): SimpleRegression {
val r = ASR()
forEach { r.addData(xSelector(it).toDouble(), ySelector(it).toDouble()) }
return ApacheSimpleRegression(r)
}
fun Sequence>.simpleRegression() = simpleRegression({it.first},{it.second})
fun Iterable>.simpleRegression() = simpleRegression({it.first},{it.second})
// Simple number vector ops
val Iterable.descriptiveStatistics: Descriptives get() = DescriptiveStatistics().apply { forEach { addValue(it.toDouble()) } }.let(::ApacheDescriptives)
val Sequence.descriptiveStatistics: Descriptives get() = DescriptiveStatistics().apply { forEach { addValue(it.toDouble()) } }.let(::ApacheDescriptives)
val Array.descriptiveStatistics: Descriptives get() = DescriptiveStatistics().apply { forEach { addValue(it.toDouble()) } }.let(::ApacheDescriptives)
fun Iterable.geometricMean() = asSequence().geometricMean()
fun Sequence.geometricMean() = StatUtils.geometricMean(map { it.toDouble() }.toList().toDoubleArray())
fun Array.geometricMean() = asSequence().geometricMean()
fun Iterable.median() = asSequence().median()
fun Sequence.median() = map { it.toDouble() }.percentile(50.0)
fun Array.median() = asSequence().median()
fun Iterable.percentile(percentile: Double) = asSequence().percentile(percentile)
fun Sequence.percentile(percentile: Double) = StatUtils.percentile(map { it.toDouble() }.toList().toDoubleArray(), percentile)
fun Array.percentile(percentile: Double) = asSequence().percentile(percentile)
fun Iterable.variance() = asSequence().variance()
fun Sequence.variance() = StatUtils.variance(map { it.toDouble() }.toList().toDoubleArray())
fun Array.variance() = asSequence().variance()
fun Iterable.sumOfSquares() = asSequence().sumOfSquares()
fun Sequence.sumOfSquares() = StatUtils.sumSq(map { it.toDouble() }.toList().toDoubleArray())
fun Array.sumOfSquares() = asSequence().sumOfSquares()
fun Iterable.standardDeviation() = descriptiveStatistics.standardDeviation
fun Sequence.standardDeviation() = descriptiveStatistics.standardDeviation
fun Array.standardDeviation() = descriptiveStatistics.standardDeviation
fun Iterable.normalize() = asSequence().normalize()
fun Sequence.normalize() = StatUtils.normalize(map { it.toDouble() }.toList().toDoubleArray())
fun Array.normalize() = asSequence().normalize()
val Iterable.kurtosis get() = descriptiveStatistics.kurtosis
val Sequence.kurtosis get() = descriptiveStatistics.kurtosis
val Array.kurtosis get() = descriptiveStatistics.kurtosis
val Iterable.skewness get() = descriptiveStatistics.skewness
val Sequence.skewness get() = descriptiveStatistics.skewness
val Array.skewness get() = descriptiveStatistics.skewness
// slicing operations
inline fun Sequence.descriptiveStatisticsBy(crossinline keySelector: (T) -> K, crossinline valueSelector: (T) -> Number) =
groupApply(keySelector, valueSelector) { it.descriptiveStatistics }
inline fun Iterable.descriptiveStatisticsBy(crossinline keySelector: (T) -> K, crossinline valueSelector: (T) -> Number) =
asSequence().descriptiveStatisticsBy(keySelector, valueSelector)
fun Sequence>.descriptiveStatisticsBy() =
groupApply({it.first}, {it.second}) { it.descriptiveStatistics }
fun Iterable>.descriptiveStatisticsBy() = asSequence().descriptiveStatisticsBy()
inline fun Sequence.medianBy(crossinline keySelector: (T) -> K, crossinline valueSelector: (T) -> Number) =
groupApply(keySelector, valueSelector) { it.median() }
fun Sequence>.medianBy() =
groupApply({it.first}, {it.second}) { it.median() }
inline fun Iterable.medianBy(crossinline keySelector: (T) -> K, crossinline valueSelector: (T) -> Number) =
asSequence().medianBy(keySelector, valueSelector)
fun Iterable>.medianBy() = asSequence().medianBy()
inline fun Sequence.percentileBy(percentile: Double, crossinline keySelector: (T) -> K, crossinline valueSelector: (T) -> Number) =
groupApply(keySelector, valueSelector) { it.percentile(percentile) }
fun Sequence>.percentileBy(percentile: Double) =
groupApply({it.first}, {it.second}) { it.percentile(percentile) }
inline fun Iterable.percentileBy(percentile: Double, crossinline keySelector: (T) -> K, crossinline valueSelector: (T) -> Number) =
asSequence().percentileBy(percentile, keySelector, valueSelector)
fun Iterable>.percentileBy(percentile: Double) = asSequence().percentileBy(percentile)
inline fun Sequence.varianceBy(crossinline keySelector: (T) -> K, crossinline valueSelector: (T) -> Number) =
groupApply(keySelector, valueSelector) { it.variance() }
fun Sequence>.varianceBy() =
groupApply({it.first}, {it.second}) { it.variance() }
inline fun Iterable.varianceBy(crossinline keySelector: (T) -> K, crossinline valueSelector: (T) -> Number) =
asSequence().varianceBy(keySelector, valueSelector)
fun Iterable>.varianceBy() = asSequence().varianceBy()
inline fun Sequence.standardDeviationBy(crossinline keySelector: (T) -> K, crossinline valueSelector: (T) -> Number) =
groupApply(keySelector, valueSelector) { it.standardDeviation() }
fun Sequence>.standardDeviationBy() =
groupApply({it.first}, {it.second}) { it.standardDeviation() }
inline fun Iterable.standardDeviationBy(crossinline keySelector: (T) -> K, crossinline valueSelector: (T) -> Number) =
asSequence().standardDeviationBy(keySelector, valueSelector)
fun Iterable>.standardDeviationBy() = asSequence().standardDeviationBy()
inline fun Sequence.geometricMeanBy(crossinline keySelector: (T) -> K, crossinline valueSelector: (T) -> Number) =
groupApply(keySelector, valueSelector) { it.geometricMean() }
fun Sequence>.geometricMeanBy() =
groupApply({it.first}, {it.second}) { it.geometricMean() }
inline fun Iterable.geometricMeanBy(crossinline keySelector: (T) -> K, crossinline valueSelector: (T) -> Number) =
asSequence().standardDeviationBy(keySelector, valueSelector)
fun Iterable>.geometricMeanBy() = asSequence().standardDeviationBy()