File tree Expand file tree Collapse file tree 2 files changed +24
-0
lines changed
core/src/main/kotlin/org/jetbrains/kotlinx/dataframe/math Expand file tree Collapse file tree 2 files changed +24
-0
lines changed Original file line number Diff line number Diff line change @@ -4,6 +4,7 @@ import org.jetbrains.kotlinx.dataframe.api.ddof_default
4
4
import org.jetbrains.kotlinx.dataframe.api.skipNA_default
5
5
import org.jetbrains.kotlinx.dataframe.impl.renderType
6
6
import java.math.BigDecimal
7
+ import java.math.BigInteger
7
8
import kotlin.reflect.KType
8
9
import kotlin.reflect.full.withNullability
9
10
@@ -26,6 +27,8 @@ internal fun <T : Number> Iterable<T?>.std(
26
27
Int ::class , Short ::class , Byte ::class -> (this as Iterable <Int >).std(ddof)
27
28
Long ::class -> (this as Iterable <Long >).std(ddof)
28
29
BigDecimal ::class -> (this as Iterable <BigDecimal >).std(ddof)
30
+ BigInteger ::class -> (this as Iterable <BigInteger >).std(ddof)
31
+ Number ::class -> (this as Iterable <Number >).map { it.toDouble() }.std(skipNA, ddof)
29
32
Nothing ::class -> Double .NaN
30
33
else -> throw IllegalArgumentException (" Unable to compute the std for type ${renderType(type)} " )
31
34
}
@@ -47,3 +50,6 @@ public fun Iterable<Long>.std(ddof: Int = ddof_default): Double = varianceAndMea
47
50
48
51
@JvmName(" bigDecimalStd" )
49
52
public fun Iterable<BigDecimal>.std (ddof : Int = ddof_default): Double = varianceAndMean().std(ddof)
53
+
54
+ @JvmName(" bigIntegerStd" )
55
+ public fun Iterable<BigInteger>.std (ddof : Int = ddof_default): Double = varianceAndMean().std(ddof)
Original file line number Diff line number Diff line change @@ -4,6 +4,7 @@ package org.jetbrains.kotlinx.dataframe.math
4
4
5
5
import org.jetbrains.kotlinx.dataframe.api.skipNA_default
6
6
import java.math.BigDecimal
7
+ import java.math.BigInteger
7
8
import kotlin.math.sqrt
8
9
9
10
public data class BasicStats (val count : Int , val mean : Double , val variance : Double ) {
@@ -114,3 +115,20 @@ public fun Iterable<BigDecimal>.varianceAndMean(): BasicStats {
114
115
}
115
116
return BasicStats (count, mean.toDouble(), variance.toDouble())
116
117
}
118
+
119
+ @JvmName(" bigIntegerVarianceAndMean" )
120
+ public fun Iterable<BigInteger>.varianceAndMean (): BasicStats {
121
+ var count = 0
122
+ var sum = BigInteger .ZERO
123
+ for (element in this ) {
124
+ sum + = element
125
+ count++
126
+ }
127
+ val mean = sum.toDouble() / count
128
+ var variance = .0
129
+ for (element in this ) {
130
+ val diff = element.toDouble() - mean
131
+ variance + = diff * diff
132
+ }
133
+ return BasicStats (count, mean, variance)
134
+ }
You can’t perform that action at this time.
0 commit comments