Current section
Files
Jump to
Current section
Files
src/gleam_stats@stats.erl
-module(gleam_stats@stats).
-compile(no_auto_import).
-export([sum/1, mean/1, median/1, hmean/1, gmean/1, var/2, std/2, moment/2, skewness/1, kurtosis/1, zscore/2, percentile/2, iqr/1, freedman_diaconis_rule/1, histogram/2, correlation/2, trim/3, isclose/4, allclose/4, amax/1, amin/1, argmax/1, argmin/1]).
-export_type([range/0]).
-type range() :: {range, float(), float()}.
-spec sum(list(float())) -> float().
sum(Arr) ->
case Arr of
[] ->
0.0;
_@1 ->
_pipe = Arr,
gleam@list:fold(_pipe, 0.0, fun(Acc, A) -> A + Acc end)
end.
-spec mean(list(float())) -> {ok, float()} | {error, binary()}.
mean(Arr) ->
case Arr of
[] ->
_pipe = <<"Invalid input argument: The list is empty."/utf8>>,
{error, _pipe};
_@1 ->
_pipe@1 = Arr,
_pipe@2 = sum(_pipe@1),
_pipe@3 = (fun(A) ->
case gleam@int:to_float(gleam@list:length(Arr)) of
0.0 -> 0.0;
Gleam@denominator -> A
/ Gleam@denominator
end
end)(_pipe@2),
{ok, _pipe@3}
end.
-spec median(list(float())) -> {ok, float()} | {error, binary()}.
median(Arr) ->
case Arr of
[] ->
_pipe = <<"Invalid input argument: The list is empty."/utf8>>,
{error, _pipe};
_@1 ->
Count = gleam@list:length(Arr),
Mid = gleam@list:length(Arr) div 2,
Sorted = gleam@list:sort(Arr, fun gleam@float:compare/2),
case gleam@int:is_odd(Count) of
true ->
{ok, Val0@1} = case gleam@list:at(Sorted, Mid) of
{ok, Val0} -> {ok, Val0};
_try ->
erlang:error(#{gleam_error => assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _try,
module => <<"gleam_stats/stats"/utf8>>,
function => <<"median"/utf8>>,
line => 176})
end,
_pipe@1 = Val0@1,
{ok, _pipe@1};
false ->
{ok, Val0@3} = case gleam@list:at(Sorted, Mid - 1) of
{ok, Val0@2} -> {ok, Val0@2};
_try@1 ->
erlang:error(#{gleam_error => assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _try@1,
module => <<"gleam_stats/stats"/utf8>>,
function => <<"median"/utf8>>,
line => 183})
end,
{ok, Val1@1} = case gleam@list:at(Sorted, Mid) of
{ok, Val1} -> {ok, Val1};
_try@2 ->
erlang:error(#{gleam_error => assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _try@2,
module => <<"gleam_stats/stats"/utf8>>,
function => <<"median"/utf8>>,
line => 184})
end,
_pipe@2 = [Val0@3, Val1@1],
mean(_pipe@2)
end
end.
-spec hmean(list(float())) -> {ok, float()} | {error, binary()}.
hmean(Arr) ->
case Arr of
[] ->
_pipe = <<"Invalid input argument: The list is empty."/utf8>>,
{error, _pipe};
_@1 ->
Xarr = begin
_pipe@1 = Arr,
gleam@list:try_map(_pipe@1, fun(A) -> case A >= 0.0 of
true ->
_pipe@2 = case A of
0.0 -> 0.0;
Gleam@denominator -> 1.0 / Gleam@denominator
end,
{ok, _pipe@2};
false ->
_pipe@3 = <<"The harmonic mean is only defined for positive numbers."/utf8>>,
{error, _pipe@3}
end end)
end,
case Xarr of
{error, String} ->
_pipe@4 = String,
{error, _pipe@4};
{ok, Xarr@1} ->
_pipe@5 = Xarr@1,
_pipe@6 = sum(_pipe@5),
_pipe@7 = (fun(X) -> case X of
0.0 -> 0.0;
Gleam@denominator@1 -> gleam@int:to_float(
gleam@list:length(Xarr@1)
) / Gleam@denominator@1
end end)(_pipe@6),
{ok, _pipe@7}
end
end.
-spec gmean(list(float())) -> {ok, float()} | {error, binary()}.
gmean(Arr) ->
case Arr of
[] ->
_pipe = <<"Invalid input argument: The list is empty."/utf8>>,
{error, _pipe};
_@1 ->
Xval = begin
_pipe@1 = Arr,
gleam@list:try_fold(
_pipe@1,
1.0,
fun(Acc, A) -> case A >= 0.0 of
true ->
_pipe@2 = Acc * A,
{ok, _pipe@2};
false ->
_pipe@3 = <<"The geometric mean is only defined for positive numbers."/utf8>>,
{error, _pipe@3}
end end
)
end,
case Xval of
{error, String} ->
_pipe@4 = String,
{error, _pipe@4};
{ok, Xval@1} ->
_pipe@5 = Xval@1,
_pipe@6 = (fun(X) ->
gleam@float:power(
X,
case gleam@int:to_float(gleam@list:length(Arr)) of
0.0 -> 0.0;
Gleam@denominator -> 1.0
/ Gleam@denominator
end
)
end)(_pipe@5),
{ok, _pipe@6}
end
end.
-spec var(list(float()), integer()) -> {ok, float()} | {error, binary()}.
var(Arr, Ddof) ->
case Arr of
[] ->
_pipe = <<"Invalid input argument: The list is empty."/utf8>>,
{error, _pipe};
_@1 ->
case Ddof < 0 of
true ->
_pipe@1 = <<"Invalid input argument: ddof < 0. Valid input is ddof >= 0."/utf8>>,
{error, _pipe@1};
false ->
{ok, Mean@1} = case mean(Arr) of
{ok, Mean} -> {ok, Mean};
_try ->
erlang:error(#{gleam_error => assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _try,
module => <<"gleam_stats/stats"/utf8>>,
function => <<"var"/utf8>>,
line => 386})
end,
_pipe@2 = Arr,
_pipe@3 = gleam@list:map(
_pipe@2,
fun(A) -> gleam@float:power(A - Mean@1, 2.0) end
),
_pipe@4 = sum(_pipe@3),
_pipe@5 = (fun(A@1) ->
case (gleam@int:to_float(gleam@list:length(Arr))
- gleam@int:to_float(Ddof)) of
0.0 -> 0.0;
Gleam@denominator -> A@1
/ Gleam@denominator
end
end)(_pipe@4),
{ok, _pipe@5}
end
end.
-spec std(list(float()), integer()) -> {ok, float()} | {error, binary()}.
std(Arr, Ddof) ->
case Arr of
[] ->
_pipe = <<"Invalid input argument: The list is empty."/utf8>>,
{error, _pipe};
_@1 ->
case Ddof < 0 of
true ->
_pipe@1 = <<"Invalid input argument: ddof < 0. Valid input is ddof >= 0."/utf8>>,
{error, _pipe@1};
false ->
{ok, Variance@1} = case var(Arr, Ddof) of
{ok, Variance} -> {ok, Variance};
_try ->
erlang:error(#{gleam_error => assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _try,
module => <<"gleam_stats/stats"/utf8>>,
function => <<"std"/utf8>>,
line => 446})
end,
{ok, Stdev@1} = case gleam@float:square_root(Variance@1) of
{ok, Stdev} -> {ok, Stdev};
_try@1 ->
erlang:error(#{gleam_error => assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _try@1,
module => <<"gleam_stats/stats"/utf8>>,
function => <<"std"/utf8>>,
line => 449})
end,
_pipe@2 = Stdev@1,
{ok, _pipe@2}
end
end.
-spec moment(list(float()), integer()) -> {ok, float()} | {error, binary()}.
moment(Arr, N) ->
case Arr of
[] ->
_pipe = <<"Invalid input argument: The list is empty."/utf8>>,
{error, _pipe};
_@1 ->
case N >= 0 of
true ->
case N of
0 ->
_pipe@1 = 1.0,
{ok, _pipe@1};
1 ->
_pipe@2 = 0.0,
{ok, _pipe@2};
_@2 ->
{ok, M1@1} = case mean(Arr) of
{ok, M1} -> {ok, M1};
_try ->
erlang:error(#{gleam_error => assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _try,
module => <<"gleam_stats/stats"/utf8>>,
function => <<"moment"/utf8>>,
line => 519})
end,
_pipe@3 = Arr,
_pipe@4 = gleam@list:map(
_pipe@3,
fun(A) ->
gleam@float:power(
A
- M1@1,
gleam@int:to_float(N)
)
end
),
mean(_pipe@4)
end;
false ->
_pipe@5 = <<"Invalid input argument: n < 0. Valid input is n > 0."/utf8>>,
{error, _pipe@5}
end
end.
-spec skewness(list(float())) -> {ok, float()} | {error, binary()}.
skewness(Arr) ->
case Arr of
[] ->
_pipe = <<"Invalid input argument: The list is empty."/utf8>>,
{error, _pipe};
_@1 ->
{ok, M2@1} = case moment(Arr, 2) of
{ok, M2} -> {ok, M2};
_try ->
erlang:error(#{gleam_error => assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _try,
module => <<"gleam_stats/stats"/utf8>>,
function => <<"skewness"/utf8>>,
line => 585})
end,
{ok, M3@1} = case moment(Arr, 3) of
{ok, M3} -> {ok, M3};
_try@1 ->
erlang:error(#{gleam_error => assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _try@1,
module => <<"gleam_stats/stats"/utf8>>,
function => <<"skewness"/utf8>>,
line => 586})
end,
_pipe@1 = case gleam@float:power(M2@1, 1.5) of
0.0 -> 0.0;
Gleam@denominator -> M3@1 / Gleam@denominator
end,
{ok, _pipe@1}
end.
-spec kurtosis(list(float())) -> {ok, float()} | {error, binary()}.
kurtosis(Arr) ->
case Arr of
[] ->
_pipe = <<"Invalid input argument: The list is empty."/utf8>>,
{error, _pipe};
_@1 ->
{ok, M2@1} = case moment(Arr, 2) of
{ok, M2} -> {ok, M2};
_try ->
erlang:error(#{gleam_error => assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _try,
module => <<"gleam_stats/stats"/utf8>>,
function => <<"kurtosis"/utf8>>,
line => 646})
end,
{ok, M4@1} = case moment(Arr, 4) of
{ok, M4} -> {ok, M4};
_try@1 ->
erlang:error(#{gleam_error => assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _try@1,
module => <<"gleam_stats/stats"/utf8>>,
function => <<"kurtosis"/utf8>>,
line => 647})
end,
_pipe@1 = (case gleam@float:power(M2@1, 2.0) of
0.0 -> 0.0;
Gleam@denominator -> M4@1 / Gleam@denominator
end) - 3.0,
{ok, _pipe@1}
end.
-spec zscore(list(float()), integer()) -> {ok, list(float())} |
{error, binary()}.
zscore(Arr, Ddof) ->
case Arr of
[] ->
_pipe = <<"Invalid input argument: The list is empty."/utf8>>,
{error, _pipe};
_@1 ->
case Ddof < 0 of
true ->
_pipe@1 = <<"Invalid input argument: ddof < 0. Valid input is ddof >= 0."/utf8>>,
{error, _pipe@1};
false ->
{ok, Mean@1} = case mean(Arr) of
{ok, Mean} -> {ok, Mean};
_try ->
erlang:error(#{gleam_error => assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _try,
module => <<"gleam_stats/stats"/utf8>>,
function => <<"zscore"/utf8>>,
line => 700})
end,
{ok, Stdev@1} = case std(Arr, Ddof) of
{ok, Stdev} -> {ok, Stdev};
_try@1 ->
erlang:error(#{gleam_error => assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _try@1,
module => <<"gleam_stats/stats"/utf8>>,
function => <<"zscore"/utf8>>,
line => 701})
end,
_pipe@2 = Arr,
_pipe@3 = gleam@list:map(_pipe@2, fun(A) -> case Stdev@1 of
0.0 -> 0.0;
Gleam@denominator -> (A - Mean@1) / Gleam@denominator
end end),
{ok, _pipe@3}
end
end.
-spec percentile(list(float()), integer()) -> {ok, float()} | {error, binary()}.
percentile(Arr, N) ->
case Arr of
[] ->
_pipe = <<"Invalid input argument: The list is empty."/utf8>>,
{error, _pipe};
_@1 ->
case (N < 0) orelse (N > 100) of
true ->
_pipe@1 = <<"Invalid input argument: n < 0 or n > 100. Valid input is 0 <= n <= 100."/utf8>>,
{error, _pipe@1};
false ->
S = gleam@list:sort(Arr, fun gleam@float:compare/2),
R = (gleam@int:to_float(N) / 100.0) * gleam@int:to_float(
gleam@list:length(Arr)
- 1
),
F = gleam@float:truncate(R),
{ok, Lower@1} = case gleam@list:at(S, F) of
{ok, Lower} -> {ok, Lower};
_try ->
erlang:error(#{gleam_error => assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _try,
module => <<"gleam_stats/stats"/utf8>>,
function => <<"percentile"/utf8>>,
line => 763})
end,
{ok, Upper@1} = case gleam@list:at(S, F + 1) of
{ok, Upper} -> {ok, Upper};
_try@1 ->
erlang:error(#{gleam_error => assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _try@1,
module => <<"gleam_stats/stats"/utf8>>,
function => <<"percentile"/utf8>>,
line => 764})
end,
_pipe@2 = Lower@1 + ((Upper@1 - Lower@1) * (R - gleam@int:to_float(
F
))),
{ok, _pipe@2}
end
end.
-spec iqr(list(float())) -> {ok, float()} | {error, binary()}.
iqr(Arr) ->
case Arr of
[] ->
_pipe = <<"Invalid input argument: The list is empty."/utf8>>,
{error, _pipe};
_@1 ->
Length = gleam@list:length(Arr),
case gleam@int:is_even(Length) of
true ->
{X, Y} = begin
_pipe@1 = Arr,
gleam@list:split(_pipe@1, Length div 2)
end,
{ok, Val0@1} = case median(Y) of
{ok, Val0} -> {ok, Val0};
_try ->
erlang:error(#{gleam_error => assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _try,
module => <<"gleam_stats/stats"/utf8>>,
function => <<"iqr"/utf8>>,
line => 819})
end,
{ok, Val1@1} = case median(X) of
{ok, Val1} -> {ok, Val1};
_try@1 ->
erlang:error(#{gleam_error => assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _try@1,
module => <<"gleam_stats/stats"/utf8>>,
function => <<"iqr"/utf8>>,
line => 820})
end,
_pipe@2 = Val0@1 - Val1@1,
{ok, _pipe@2};
false ->
{X@1, _@2} = begin
_pipe@3 = Arr,
gleam@list:split(_pipe@3, (Length - 1) div 2)
end,
{_@3, Y@1} = begin
_pipe@4 = Arr,
gleam@list:split(_pipe@4, (Length + 1) div 2)
end,
{ok, Val0@3} = case median(Y@1) of
{ok, Val0@2} -> {ok, Val0@2};
_try@2 ->
erlang:error(#{gleam_error => assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _try@2,
module => <<"gleam_stats/stats"/utf8>>,
function => <<"iqr"/utf8>>,
line => 833})
end,
{ok, Val1@3} = case median(X@1) of
{ok, Val1@2} -> {ok, Val1@2};
_try@3 ->
erlang:error(#{gleam_error => assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _try@3,
module => <<"gleam_stats/stats"/utf8>>,
function => <<"iqr"/utf8>>,
line => 834})
end,
_pipe@5 = Val0@3 - Val1@3,
{ok, _pipe@5}
end
end.
-spec freedman_diaconis_rule(list(float())) -> {ok, float()} | {error, binary()}.
freedman_diaconis_rule(Arr) ->
case Arr of
[] ->
_pipe = <<"Invalid input argument: The list is empty."/utf8>>,
{error, _pipe};
_@1 ->
Length = gleam@int:to_float(gleam@list:length(Arr)),
{ok, Iqr@1} = case begin
_pipe@1 = Arr,
iqr(_pipe@1)
end of
{ok, Iqr} -> {ok, Iqr};
_try ->
erlang:error(#{gleam_error => assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _try,
module => <<"gleam_stats/stats"/utf8>>,
function => <<"freedman_diaconis_rule"/utf8>>,
line => 885})
end,
{ok, Lower@1} = case begin
_pipe@2 = Arr,
amin(_pipe@2)
end of
{ok, Lower} -> {ok, Lower};
_try@1 ->
erlang:error(#{gleam_error => assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _try@1,
module => <<"gleam_stats/stats"/utf8>>,
function => <<"freedman_diaconis_rule"/utf8>>,
line => 888})
end,
{ok, Upper@1} = case begin
_pipe@3 = Arr,
amax(_pipe@3)
end of
{ok, Upper} -> {ok, Upper};
_try@2 ->
erlang:error(#{gleam_error => assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _try@2,
module => <<"gleam_stats/stats"/utf8>>,
function => <<"freedman_diaconis_rule"/utf8>>,
line => 891})
end,
Width = case gleam@float:power(Length, 1.0 / 3.0) of
0.0 -> 0.0;
Gleam@denominator -> (2.0 * Iqr@1) / Gleam@denominator
end,
case Width < (case Length of
0.0 -> 0.0;
Gleam@denominator@1 -> (Upper@1 - Lower@1) / Gleam@denominator@1
end) of
true ->
_pipe@4 = <<"The determined bin width is too small. Determine the width manually or in another way."/utf8>>,
{error, _pipe@4};
false ->
_pipe@5 = gleam@float:ceiling(case Width of
0.0 -> 0.0;
Gleam@denominator@2 -> (Upper@1 - Lower@1) / Gleam@denominator@2
end),
{ok, _pipe@5}
end
end.
-spec histogram(list(float()), float()) -> {ok, list({range(), integer()})} |
{error, binary()}.
histogram(Arr, Width) ->
case Arr of
[] ->
_pipe = <<"Invalid input argument: The list is empty."/utf8>>,
{error, _pipe};
_@1 ->
case create_bins(Arr, Width) of
{error, String} ->
_pipe@1 = String,
{error, _pipe@1};
{ok, Bins} ->
_pipe@2 = Bins,
_pipe@3 = bin_elements(_pipe@2, Arr),
{ok, _pipe@3}
end
end.
-spec create_bins(list(float()), float()) -> {ok, list({range(), integer()})} |
{error, binary()}.
create_bins(Arr, Width) ->
case Arr of
[] ->
_pipe = <<"Invalid input argument: The list is empty."/utf8>>,
{error, _pipe};
_@1 ->
case Width < 0.0 of
true ->
_pipe@1 = <<"Invalid input argument: width < 0. Valid input is width > 0."/utf8>>,
{error, _pipe@1};
false ->
{ok, Min@1} = case begin
_pipe@2 = Arr,
amin(_pipe@2)
end of
{ok, Min} -> {ok, Min};
_try ->
erlang:error(#{gleam_error => assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _try,
module => <<"gleam_stats/stats"/utf8>>,
function => <<"create_bins"/utf8>>,
line => 1059})
end,
{ok, Max@1} = case begin
_pipe@3 = Arr,
amax(_pipe@3)
end of
{ok, Max} -> {ok, Max};
_try@1 ->
erlang:error(#{gleam_error => assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _try@1,
module => <<"gleam_stats/stats"/utf8>>,
function => <<"create_bins"/utf8>>,
line => 1062})
end,
Inc = begin
_pipe@4 = case Width of
0.0 -> 0.0;
Gleam@denominator -> ((1.001 * Max@1) - (0.999 * Min@1)) / Gleam@denominator
end,
gleam@float:ceiling(_pipe@4)
end,
_pipe@5 = gleam@list:range(0, gleam@float:round(Inc)),
_pipe@6 = gleam@list:map(
_pipe@5,
fun(X) ->
{{range,
Width
* gleam@int:to_float(X),
Width
* gleam@int:to_float(X + 1)},
0}
end
),
{ok, _pipe@6}
end
end.
-spec find_bin(list({range(), integer()}), float()) -> {ok,
{range(), integer()}} |
{error, nil}.
find_bin(Bins, Key) ->
_pipe = Bins,
gleam@list:find(
_pipe,
fun(B) ->
{range, Cmin, Cmax} = gleam@pair:first(B),
(Key
>= Cmin)
andalso (Key
< Cmax)
end
).
-spec bin_elements(list({range(), integer()}), list(float())) -> list({range(),
integer()}).
bin_elements(Bins, Arr) ->
_pipe = Arr,
gleam@list:fold(
_pipe,
Bins,
fun(Acc, Key) ->
{ok, Bin@1} = case begin
_pipe@1 = Acc,
find_bin(_pipe@1, Key)
end of
{ok, Bin} -> {ok, Bin};
_try ->
erlang:error(#{gleam_error => assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _try,
module => <<"gleam_stats/stats"/utf8>>,
function => <<"bin_elements"/utf8>>,
line => 1093})
end,
{ok, Kv@1} = case gleam@list:key_pop(Acc, gleam@pair:first(Bin@1)) of
{ok, Kv} -> {ok, Kv};
_try@1 ->
erlang:error(#{gleam_error => assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _try@1,
module => <<"gleam_stats/stats"/utf8>>,
function => <<"bin_elements"/utf8>>,
line => 1097})
end,
gleam@list:key_set(
gleam@pair:second(Kv@1),
gleam@pair:first(Bin@1),
gleam@pair:first(Kv@1)
+ 1
)
end
).
-spec correlation(list(float()), list(float())) -> {ok, float()} |
{error, binary()}.
correlation(Xarr, Yarr) ->
Xlen = gleam@list:length(Xarr),
Ylen = gleam@list:length(Yarr),
case {Xlen =< 0, Ylen =< 0} of
{true, true} ->
_pipe = <<"Invlaid input argument: length(xarr) == 0 or length(yarr) == 0. Valid input is length(xarr) > 0 and length(yarr) > 0."/utf8>>,
{error, _pipe};
{_@1, _@2} ->
case Xlen =:= Ylen of
false ->
_pipe@1 = <<"Invalid input argument: length(xarr) != length(yarr). Valid input is when length(xarr) == length(yarr)."/utf8>>,
{error, _pipe@1};
true ->
case (Xlen >= 2) andalso (Ylen >= 2) of
false ->
_pipe@2 = <<"Invalid input argument: length(xarr) < 2 or length(yarr) < 2. Valid input is when length(xarr) >= 2 and length(yarr) >= 2."/utf8>>,
{error, _pipe@2};
true ->
{ok, Xmean@1} = case begin
_pipe@3 = Xarr,
mean(_pipe@3)
end of
{ok, Xmean} -> {ok, Xmean};
_try ->
erlang:error(#{gleam_error => assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _try,
module => <<"gleam_stats/stats"/utf8>>,
function => <<"correlation"/utf8>>,
line => 1181})
end,
{ok, Ymean@1} = case begin
_pipe@4 = Yarr,
mean(_pipe@4)
end of
{ok, Ymean} -> {ok, Ymean};
_try@1 ->
erlang:error(#{gleam_error => assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _try@1,
module => <<"gleam_stats/stats"/utf8>>,
function => <<"correlation"/utf8>>,
line => 1184})
end,
A = begin
_pipe@5 = gleam@list:zip(Xarr, Yarr),
_pipe@6 = gleam@list:map(
_pipe@5,
fun(Z) ->
(gleam@pair:first(Z)
- Xmean@1)
* (gleam@pair:second(Z)
- Ymean@1)
end
),
sum(_pipe@6)
end,
B = begin
_pipe@7 = Xarr,
_pipe@8 = gleam@list:map(
_pipe@7,
fun(X) -> (X - Xmean@1) * (X - Xmean@1) end
),
sum(_pipe@8)
end,
C = begin
_pipe@9 = Yarr,
_pipe@10 = gleam@list:map(
_pipe@9,
fun(Y) -> (Y - Ymean@1) * (Y - Ymean@1) end
),
sum(_pipe@10)
end,
{ok, Val0@1} = case gleam@float:square_root(B * C) of
{ok, Val0} -> {ok, Val0};
_try@2 ->
erlang:error(#{gleam_error => assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _try@2,
module => <<"gleam_stats/stats"/utf8>>,
function => <<"correlation"/utf8>>,
line => 1203})
end,
_pipe@11 = case Val0@1 of
0.0 -> 0.0;
Gleam@denominator -> A / Gleam@denominator
end,
{ok, _pipe@11}
end
end
end.
-spec trim(list(float()), integer(), integer()) -> {ok, list(float())} |
{error, binary()}.
trim(Arr, Min, Max) ->
case Arr of
[] ->
_pipe = <<"Invalid input argument: The list is empty."/utf8>>,
{error, _pipe};
_@1 ->
case (Min >= 0) andalso (Max < gleam@list:length(Arr)) of
false ->
_pipe@1 = <<"Invalid input argument: min < 0 or max < length(arr). Valid input is min > 0 and max < length(arr)."/utf8>>,
{error, _pipe@1};
true ->
_pipe@2 = Arr,
_pipe@3 = gleam@list:drop(_pipe@2, Min),
_pipe@4 = gleam@list:take(_pipe@3, (Max - Min) + 1),
{ok, _pipe@4}
end
end.
-spec isclose(float(), float(), float(), float()) -> boolean().
isclose(A, B, Rtol, Atol) ->
X = gleam@float:absolute_value(A - B),
Y = Atol + (Rtol * gleam@float:absolute_value(B)),
case X =< Y of
true ->
true;
false ->
false
end.
-spec allclose(list(float()), list(float()), float(), float()) -> {ok,
list(boolean())} |
{error, binary()}.
allclose(Xarr, Yarr, Rtol, Atol) ->
Xlen = gleam@list:length(Xarr),
Ylen = gleam@list:length(Yarr),
case Xlen =:= Ylen of
false ->
_pipe = <<"Invalid input argument: length(xarr) != length(yarr). Valid input is when length(xarr) == length(yarr)."/utf8>>,
{error, _pipe};
true ->
_pipe@1 = gleam@list:zip(Xarr, Yarr),
_pipe@2 = gleam@list:map(
_pipe@1,
fun(Z) ->
isclose(
gleam@pair:first(Z),
gleam@pair:second(Z),
Rtol,
Atol
)
end
),
{ok, _pipe@2}
end.
-spec amax(list(float())) -> {ok, float()} | {error, binary()}.
amax(Arr) ->
case Arr of
[] ->
_pipe = <<"Invalid input argument: The list is empty."/utf8>>,
{error, _pipe};
_@1 ->
{ok, Val0@1} = case gleam@list:at(Arr, 0) of
{ok, Val0} -> {ok, Val0};
_try ->
erlang:error(#{gleam_error => assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _try,
module => <<"gleam_stats/stats"/utf8>>,
function => <<"amax"/utf8>>,
line => 1416})
end,
_pipe@1 = Arr,
_pipe@2 = gleam@list:fold(
_pipe@1,
Val0@1,
fun(Acc, A) -> case A > Acc of
true ->
A;
false ->
Acc
end end
),
{ok, _pipe@2}
end.
-spec amin(list(float())) -> {ok, float()} | {error, binary()}.
amin(Arr) ->
case Arr of
[] ->
_pipe = <<"Invalid input argument: The list is empty."/utf8>>,
{error, _pipe};
_@1 ->
{ok, Val0@1} = case gleam@list:at(Arr, 0) of
{ok, Val0} -> {ok, Val0};
_try ->
erlang:error(#{gleam_error => assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _try,
module => <<"gleam_stats/stats"/utf8>>,
function => <<"amin"/utf8>>,
line => 1471})
end,
_pipe@1 = Arr,
_pipe@2 = gleam@list:fold(
_pipe@1,
Val0@1,
fun(Acc, A) -> case A < Acc of
true ->
A;
false ->
Acc
end end
),
{ok, _pipe@2}
end.
-spec argmax(list(float())) -> {ok, list(integer())} | {error, binary()}.
argmax(Arr) ->
case Arr of
[] ->
_pipe = <<"Invalid input argument: The list is empty."/utf8>>,
{error, _pipe};
_@1 ->
{ok, Max@1} = case begin
_pipe@1 = Arr,
amax(_pipe@1)
end of
{ok, Max} -> {ok, Max};
_try ->
erlang:error(#{gleam_error => assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _try,
module => <<"gleam_stats/stats"/utf8>>,
function => <<"argmax"/utf8>>,
line => 1526})
end,
_pipe@2 = Arr,
_pipe@3 = gleam@list:index_map(
_pipe@2,
fun(Index, A) -> case A - Max@1 of
0.0 ->
Index;
_@2 ->
-1
end end
),
_pipe@4 = gleam@list:filter(_pipe@3, fun(Index@1) -> case Index@1 of
-1 ->
false;
_@3 ->
true
end end),
{ok, _pipe@4}
end.
-spec argmin(list(float())) -> {ok, list(integer())} | {error, binary()}.
argmin(Arr) ->
case Arr of
[] ->
_pipe = <<"Invalid input argument: The list is empty."/utf8>>,
{error, _pipe};
_@1 ->
{ok, Min@1} = case begin
_pipe@1 = Arr,
amin(_pipe@1)
end of
{ok, Min} -> {ok, Min};
_try ->
erlang:error(#{gleam_error => assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _try,
module => <<"gleam_stats/stats"/utf8>>,
function => <<"argmin"/utf8>>,
line => 1586})
end,
_pipe@2 = Arr,
_pipe@3 = gleam@list:index_map(
_pipe@2,
fun(Index, A) -> case A - Min@1 of
0.0 ->
Index;
_@2 ->
-1
end end
),
_pipe@4 = gleam@list:filter(_pipe@3, fun(Index@1) -> case Index@1 of
-1 ->
false;
_@3 ->
true
end end),
{ok, _pipe@4}
end.