fix(growth): use centile and percentile correctly

1. The calculation made in `Grownth.Calc.Centile.compute/4` converts a
   given z-score into the expected measurement value.
   This can be used to get specific values for key z-scores, such as,
   -3, -2, 2 and 3.
2. To calculate the percentile of a given z-score the system uses the
   cumulative distribution function.
This commit is contained in:
2024-08-28 13:03:45 +00:00
parent 824a70452f
commit 5c044e69d9
3 changed files with 56 additions and 30 deletions

View File

@@ -1,9 +1,9 @@
defmodule Growth.Score.Scorer do
@moduledoc """
Behaviour defining common interface to calculate z-score and centile for a given measurement.
Behaviour defining common interface to calculate z-score and percentile for a given measurement.
"""
alias Growth.Calc.Centile
alias Growth.Calc.Percentile
alias Growth.Calc.ZScore
@callback measure_name() :: atom()
@@ -18,7 +18,7 @@ defmodule Growth.Score.Scorer do
@spec result(module(), Growth.t()) :: Growth.t()
@doc """
Calculate z-score and centile values for the given indicator and add them to the growth measurement `results`.
Calculate z-score and percentile values for the given indicator and add them to the growth measurement `results`.
"""
def result(indicator, growth) do
result =
@@ -58,24 +58,22 @@ defmodule Growth.Score.Scorer do
|> Enum.reject(&is_nil/1)
end
@spec scores(module(), Growth.t(), ZScore.l(), ZScore.m(), ZScore.s()) ::
{Growth.measure(), number() | :na, nil}
@doc """
Calculate the z-score and centile of an indicator measurement.
Calculate the z-score and percentile of an indicator measurement.
"""
@spec scores(module(), Growth.t(), ZScore.l(), ZScore.m(), ZScore.s()) ::
{Growth.measure(), Growth.measure()}
def scores(indicator, growth, l, m, s) do
measure = Map.get(growth, indicator.measure_name())
{
z_score(measure, l, m, s),
centile(measure, l, m, s)
}
growth
|> Map.get(indicator.measure_name())
|> z_score(l, m, s)
|> then(fn score -> {score, percentile(score)} end)
end
@spec z_score(Growth.measure(), ZScore.l(), ZScore.m(), ZScore.s()) :: Growth.measure()
@doc """
Check `Growth.Calc.ZScore.compute/4`.
"""
@spec z_score(Growth.measure(), ZScore.l(), ZScore.m(), ZScore.s()) :: Growth.measure()
def z_score(nil, _, _, _) do
nil
end
@@ -84,15 +82,15 @@ defmodule Growth.Score.Scorer do
ZScore.compute(value, l, m, s)
end
@spec centile(Growth.measure(), ZScore.l(), ZScore.m(), ZScore.s()) :: number() | :na | nil
@doc """
Check `Growth.Calc.Centile.compute/4`.
Check `Growth.Calc.Percentile.compute/1`.
"""
def centile(nil, _, _, _) do
@spec percentile(Growth.measure()) :: Growth.measure()
def percentile(nil) do
nil
end
def centile(value, l, m, s) do
Centile.compute(value, l, m, s)
def percentile(score) do
Percentile.compute(score)
end
end