Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ECDF Stat #832

Merged
merged 17 commits into from
Aug 2, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add an interpolation to ECDF.
  • Loading branch information
ASmirnov-HORIS committed Jul 27, 2023
commit c66b8ef22ca8487e1f23668461dba19d588f4962
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class ECDF {
fun plotSpecList(): List<MutableMap<String, Any>> {
return listOf(
basic(),
withInterpolation(),
)
}

Expand Down Expand Up @@ -39,4 +40,30 @@ class ECDF {
return plotSpec

}

private fun withInterpolation(): MutableMap<String, Any> {
val spec = """
{
'kind': 'plot',
'mapping': {
'x': 'sepal length (cm)'
},
'ggtitle': {
'text': 'Interpolation'
},
'layers': [
{
'geom': 'step',
'stat': 'ecdf',
'n': 10
}
]
}
""".trimIndent()

val plotSpec = HashMap(parsePlotSpec(spec))
plotSpec["data"] = Iris.df
return plotSpec

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ import org.jetbrains.letsPlot.core.plot.base.DataFrame
import org.jetbrains.letsPlot.core.plot.base.StatContext
import org.jetbrains.letsPlot.core.plot.base.data.TransformVar

class ECDFStat : BaseStat(DEF_MAPPING) {
class ECDFStat(
private val n: Int?
) : BaseStat(DEF_MAPPING) {

override fun consumes(): List<Aes<*>> {
return listOf(Aes.X)
Expand All @@ -23,8 +25,16 @@ class ECDFStat : BaseStat(DEF_MAPPING) {

val xs = data.getNumeric(TransformVar.X)
val xValues = xs.filter { it?.isFinite() ?: false }.map { it!! }
if (xValues.isEmpty()) {
return withEmptyStatValues()
}

val ecdf: (Double) -> Double = { t -> xValues.count { x -> x <= t }.toDouble() / xValues.size }
val statX = xValues.distinct()
val statX = if (n == null) {
xValues.distinct()
} else {
linspace(xValues.min(), xValues.max(), n)
}
val statY = statX.map { ecdf(it) }

return DataFrame.Builder()
Expand All @@ -33,6 +43,13 @@ class ECDFStat : BaseStat(DEF_MAPPING) {
.build()
}

private fun linspace(start: Double, stop: Double, num: Int): List<Double> {
if (num <= 0) return emptyList()
if (num == 1) return listOf(start)
val step = (stop - start) / (num - 1)
return List(num) { start + it * step }
}

companion object {
private val DEF_MAPPING: Map<Aes<*>, DataFrame.Variable> = mapOf(
Aes.X to Stats.X,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,10 @@ object Option {
const val LINE_QUANTILES = "quantiles"
}

object ECDF {
const val N = "n"
}

object Summary {
const val QUANTILES = "quantiles"
const val FUN = "fun"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import org.jetbrains.letsPlot.core.spec.Option.Stat.DensityRidges
import org.jetbrains.letsPlot.core.spec.Option.Stat.YDensity
import org.jetbrains.letsPlot.core.spec.Option.Stat.QQ
import org.jetbrains.letsPlot.core.spec.Option.Stat.QQLine
import org.jetbrains.letsPlot.core.spec.Option.Stat.ECDF
import org.jetbrains.letsPlot.core.spec.Option.Stat.Summary

object StatProto {
Expand Down Expand Up @@ -117,7 +118,11 @@ object StatProto {

StatKind.QQ2_LINE -> return configureQQ2LineStat(options)

StatKind.ECDF -> return ECDFStat()
StatKind.ECDF -> {
return ECDFStat(
n = options.getInteger(ECDF.N)
)
}

StatKind.SUMMARY -> return configureSummaryStat(options)

Expand Down