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
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
Refactor ECDFStat.
  • Loading branch information
ASmirnov-HORIS committed Jul 27, 2023
commit d53551aea580962d9e7075daa74c978e9026aff3
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import org.jetbrains.letsPlot.core.plot.base.Aes
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
import org.jetbrains.letsPlot.core.plot.base.stat.math3.UniformDistribution

class ECDFStat : BaseStat(DEF_MAPPING) {

Expand All @@ -22,28 +21,18 @@ class ECDFStat : BaseStat(DEF_MAPPING) {
return withEmptyStatValues()
}

val statData = buildStat(data.getNumeric(TransformVar.X))
val xs = data.getNumeric(TransformVar.X)
val xValues = xs.filter { it?.isFinite() ?: false }.map { it!! }
val ecdf: (Double) -> Double = { t -> xValues.count { x -> x <= t }.toDouble() / xValues.size }
val statX = xValues.distinct()
val statY = statX.map { ecdf(it) }

return DataFrame.Builder()
.putNumeric(Stats.X, statData.getValue(Stats.X))
.putNumeric(Stats.Y, statData.getValue(Stats.Y))
.putNumeric(Stats.X, statX)
.putNumeric(Stats.Y, statY)
.build()
}

private fun buildStat(
xs: List<Double?>
): MutableMap<DataFrame.Variable, List<Double>> {
val statX = xs.filter { it?.isFinite() ?: false }.map { it!! }.sorted()
val t = (1..statX.size).map { (it - 0.5) / statX.size }
val dist = UniformDistribution(0.0, 1.0)
val statY = t.map { dist.inverseCumulativeProbability(it) }

return mutableMapOf(
Stats.X to statX,
Stats.Y to statY
)
}

companion object {
private val DEF_MAPPING: Map<Aes<*>, DataFrame.Variable> = mapOf(
Aes.X to Stats.X,
Expand Down