pointStats.kt

From GM-RKB
Jump to navigation Jump to search

A pointStats.kt is a Kotlin-based IID point estimation system.



References

2023

  • Gabor Melli
/* pointStats.kt (v1.0) - a Kotlin-based class that calculates several point statistics.
** Return results as public variables.
*/

import kotlin.math.pow
import kotlin.math.sqrt

data class PointStats(
    val values: DoubleArray
) {
    val n: Int = values.size
    val sum: Double = values.sum()
    val average: Double = sum / n
    val min: Double = values.minOrNull() ?: 0.0
    val max: Double = values.maxOrNull() ?: 0.0
    val median: Double = values.sorted().let { (it[n / 2] + it[(n - 1) / 2]) / 2 }
    val sumSq: Double = values.map { it.pow(2) }.sum()
    val sumSqDev: Double = values.map { (average - it).pow(2) }.sum()
    val varS: Double = sumSqDev / (n - 1)
    val stdDevS: Double = sqrt(varS)

    companion object {
        @JvmStatic
        fun main(args: Array<String>) {
            val numList = doubleArrayOf(2.0, 20.5, 24.5, 19.0)
            val inst = PointStats(numList)
            println("cnt=${inst.n}\tsum=${inst.sum}\tavg=${inst.average}\tmin=${inst.min}\tmax=${inst.max}\tmedian=${inst.median}\tsumSq=${inst.sumSq}\tsumSqDev=${inst.sumSqDev}\tvarS=${inst.varS}\tstdDevS=${inst.stdDevS}")
        }
    }
}