AshDyan.Stats (AshDyan v0.6.0)

Copy Markdown View Source

Numeric/statistical helpers shared by the engine, formatter, and third-party analysis modules.

Everything here is Decimal-aware: Decimal structs do not implement Kernel.+/2 or Elixir's structurally-correct ordering, so sums, averages, percentiles, and comparisons all branch on representation. These helpers are public so custom AshDyan.Analysis implementations can reuse them instead of re-implementing the Decimal handling.

Summary

Functions

Decimal-aware addition so cumulative totals work on decimal metrics.

Decimal-aware mean; returns nil for an empty list.

Numeric comparison that works for Decimal, numbers, and nil. Elixir's :asc/:desc sorter compares structs structurally, which is wrong for Decimal (it would order by the underlying map fields, not the value).

Share-of-total percentage for a single value, as a float.

Linear-interpolated percentile of a list (Decimal-aware); nil when empty.

Min of a list, ignoring nils; nil when empty.

Population standard deviation; nil for an empty list.

Sum a list of values, branching on whether they are Decimals. Enum.sum/1 raises ArithmeticError on Decimal structs, so we reduce with Decimal.add.

Population variance; nil for an empty list.

Functions

add_values(a, b)

Decimal-aware addition so cumulative totals work on decimal metrics.

average_numbers(values)

Decimal-aware mean; returns nil for an empty list.

compare_values(a, b)

@spec compare_values(term(), term()) :: :lt | :eq | :gt

Numeric comparison that works for Decimal, numbers, and nil. Elixir's :asc/:desc sorter compares structs structurally, which is wrong for Decimal (it would order by the underlying map fields, not the value).

percentage_of(v, total)

Share-of-total percentage for a single value, as a float.

percentile_of(values, p)

Linear-interpolated percentile of a list (Decimal-aware); nil when empty.

safe_enum(values, fun)

Min of a list, ignoring nils; nil when empty.

stddev(values)

Population standard deviation; nil for an empty list.

sum_values(values)

Sum a list of values, branching on whether they are Decimals. Enum.sum/1 raises ArithmeticError on Decimal structs, so we reduce with Decimal.add.

variance(values)

Population variance; nil for an empty list.