ex_trainer/lib/growth/calc/percentile.ex
Joao P Dubas 5c044e69d9
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.
2024-08-28 13:03:45 +00:00

31 lines
867 B
Elixir

defmodule Growth.Calc.Percentile do
@moduledoc """
Convert the z-score of a given measurement into a cumulative percentile.
This calculation is described in the [cumulative distribution function][https://en.wikipedia.org/wiki/Error_function#Cumulative_distribution_function].
## Examples
iex> z_scores = [-1.0, 0.0, 1.0]
iex> Enum.map(z_scores, &apply(Growth.Calc.Percentile, :compute, :&1))
[0.15865525393145707, 0.5, 0.8413447460685429]
"""
@doc """
Convert the z-score of a given measurement into a percentile representation, ranging from 0 to 1.
## Examples
iex> Growth.Calc.Percentile.compute(2.0)
0.9772498680518208
iex> Growth.Calc.Percentile.compute(-2.0)
0.02275013194817921
"""
@spec compute(number()) :: number()
def compute(z_score) do
0.5 * (:math.erf(z_score / :math.sqrt(2)) + 1)
end
end