xb5_bag_utils (xb5 v1.0.1)
View SourceAdditional utils for operating over xb5_bag contents.
Summary
Types
A percentile value between 0.0 (0th) and 1.0 (100th).
The result of a percentile bracket calculation.
The method used to calculate a percentile bracket.
An option for percentile functions. Currently only {method, Method} is supported; see percentile_bracket_method/0.
Functions
Calculates a percentile value in O(log n) time, using linear interpolation if
the method is either inclusive or exclusive (default is inclusive, see
percentile_bracket_method/0) and the bracket doesn't fall on one exact
element. Returns {value, Result} or none.
Returns the percentile bracket for Percentile in Root node in O(log n)
time, using the method selected (default is inclusive; see
percentile_bracket_method/0).
Returns the percentile rank
of Element in non-empty Root as a float in [0.0, 1.0], in O(log n) time.
Types
-type percentile() :: 0 | 1 | float().
A percentile value between 0.0 (0th) and 1.0 (100th).
-type percentile_bracket(Element) :: {exact, Element} | {between, Element, Element, T :: float()} | none.
The result of a percentile bracket calculation.
{exact, Element}-- the percentile falls exactly on an element.{between, A, B, T}-- the percentile falls between elements A and B, whereTis a float in(0.0, 1.0)representing the interpolation factor.none-- the percentile cannot be calculated (empty bag, or out of range for the chosen method).
-type percentile_bracket_method() :: inclusive | exclusive | nearest_rank.
The method used to calculate a percentile bracket.
inclusive(default) -- Pos = 1 + (N - 1) * P. Equivalent to Excel PERCENTILE.INC (Hyndman-Fan Type 7). Covers the full[0.0, 1.0]range.exclusive-- Pos = (N + 1) * P. Equivalent to Excel PERCENTILE.EXC (Hyndman-Fan Type 6). Returnsnonefor percentiles outside the representable range.nearest_rank-- Pos = ceil(P * N). As described in Wikipedia. Always returns an exact element (no interpolation). Returnsnonefor percentile0.
-type percentile_bracket_opt() :: {method, percentile_bracket_method()}.
An option for percentile functions. Currently only {method, Method} is supported; see percentile_bracket_method/0.
Functions
-spec percentile(Percentile, Size, Root, ValueFun, Opts) -> ValueWrap | none when Percentile :: percentile(), Size :: non_neg_integer(), Root :: xb5_bag_node:t(Element), ValueFun :: fun((Element | InterpolationResult) -> ValueWrap), InterpolationResult :: number(), Opts :: [percentile_bracket_opt()].
Calculates a percentile value in O(log n) time, using linear interpolation if
the method is either inclusive or exclusive (default is inclusive, see
percentile_bracket_method/0) and the bracket doesn't fall on one exact
element. Returns {value, Result} or none.
Raises a {bracket_value_not_a_number, #{value => Bound, bracket => Bracket}}
error if linear interpolation is required but the bracketing elements are not
numbers.
-spec percentile_bracket(Percentile, Size, Root, Opts) -> Bracket when Percentile :: percentile(), Size :: non_neg_integer(), Root :: xb5_bag_node:t(Element), Opts :: [percentile_bracket_opt()], Bracket :: percentile_bracket(Element).
Returns the percentile bracket for Percentile in Root node in O(log n)
time, using the method selected (default is inclusive; see
percentile_bracket_method/0).
Returns {exact, Element} when the percentile falls exactly on an element,
{between, A, B, T} when it falls between two elements, or none if the bag
is empty.
-spec percentile_rank(Element, Size, Root) -> Rank when Size :: pos_integer(), Root :: xb5_bag_node:t(Element), Rank :: float().
Returns the percentile rank
of Element in non-empty Root as a float in [0.0, 1.0], in O(log n) time.
Element does not have to be in the bag.