Current section
Files
Jump to
Current section
Files
src/gleam_stats@distributions@geometric.erl
-module(gleam_stats@distributions@geometric).
-compile(no_auto_import).
-export([geometric_mean/1, geometric_variance/1, geometric_pmf/2, geometric_cdf/2, geometric_random/3]).
-spec check_geometric_parameters(float()) -> {ok, boolean()} | {error, binary()}.
check_geometric_parameters(P) ->
case (0.0 < P) andalso (P =< 1.0) of
false ->
_pipe = <<"Invalid input argument: p <= 0 or p > 1. Valid input is 0 < p <= 1."/utf8>>,
{error, _pipe};
true ->
_pipe@1 = true,
{ok, _pipe@1}
end.
-spec geometric_mean(float()) -> {ok, float()} | {error, binary()}.
geometric_mean(P) ->
case check_geometric_parameters(P) of
{error, String} ->
_pipe = String,
{error, _pipe};
_@1 ->
_pipe@1 = case P of
0.0 -> 0.0;
Gleam@denominator -> (1.0 - P) / Gleam@denominator
end,
{ok, _pipe@1}
end.
-spec geometric_variance(float()) -> {ok, float()} | {error, binary()}.
geometric_variance(P) ->
case check_geometric_parameters(P) of
{error, String} ->
_pipe = String,
{error, _pipe};
_@1 ->
{ok, V@1} = case gleam_stats@math:pow(P, 2.0) of
{ok, V} -> {ok, V};
_try ->
erlang:error(#{gleam_error => assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _try,
module => <<"gleam_stats/distributions/geometric"/utf8>>,
function => <<"geometric_variance"/utf8>>,
line => 85})
end,
_pipe@1 = case V@1 of
0.0 -> 0.0;
Gleam@denominator -> (1.0 - P) / Gleam@denominator
end,
{ok, _pipe@1}
end.
-spec geometric_pmf(integer(), float()) -> {ok, float()} | {error, binary()}.
geometric_pmf(X, P) ->
do_geometric_pmf(X, P).
-spec do_geometric_pmf(integer(), float()) -> {ok, float()} | {error, binary()}.
do_geometric_pmf(X, P) ->
case check_geometric_parameters(P) of
{error, String} ->
_pipe = String,
{error, _pipe};
_@1 ->
case X >= 0 of
true ->
{ok, V@1} = case gleam_stats@math:pow(
1.0
- P,
gleam@int:to_float(X)
) of
{ok, V} -> {ok, V};
_try ->
erlang:error(#{gleam_error => assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _try,
module => <<"gleam_stats/distributions/geometric"/utf8>>,
function => <<"do_geometric_pmf"/utf8>>,
line => 144})
end,
_pipe@1 = V@1 * P,
{ok, _pipe@1};
_@2 ->
_pipe@2 = 0.0,
{ok, _pipe@2}
end
end.
-spec geometric_cdf(integer(), float()) -> {ok, float()} | {error, binary()}.
geometric_cdf(X, P) ->
do_geometric_cdf(X, P).
-spec do_geometric_cdf(integer(), float()) -> {ok, float()} | {error, binary()}.
do_geometric_cdf(X, P) ->
case check_geometric_parameters(P) of
{error, String} ->
_pipe = String,
{error, _pipe};
_@1 ->
case X >= 0 of
true ->
{ok, V@1} = case gleam_stats@math:pow(
1.0
- P,
gleam@int:to_float(X)
+ 1.0
) of
{ok, V} -> {ok, V};
_try ->
erlang:error(#{gleam_error => assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _try,
module => <<"gleam_stats/distributions/geometric"/utf8>>,
function => <<"do_geometric_cdf"/utf8>>,
line => 214})
end,
_pipe@1 = 1.0 - V@1,
{ok, _pipe@1};
false ->
_pipe@2 = 0.0,
{ok, _pipe@2}
end
end.
-spec geometric_random(gleam@iterator:iterator(integer()), float(), integer()) -> {ok,
{list(integer()),
gleam@iterator:iterator(integer())}} |
{error, binary()}.
geometric_random(Stream, P, M) ->
do_geometric_random(Stream, P, M).
-spec do_geometric_random(
gleam@iterator:iterator(integer()),
float(),
integer()
) -> {ok, {list(integer()), gleam@iterator:iterator(integer())}} |
{error, binary()}.
do_geometric_random(Stream, P, M) ->
case check_geometric_parameters(P) of
{error, String} ->
_pipe = String,
{error, _pipe};
_@1 ->
case M > 0 of
false ->
_pipe@1 = <<"Invalid input arugment: m < 0. Valid input is m > 0."/utf8>>,
{error, _pipe@1};
true ->
{ok, Out@1} = case gleam_stats@distributions@uniform:uniform_random(
Stream,
0.0,
1.0,
M
) of
{ok, Out} -> {ok, Out};
_try ->
erlang:error(#{gleam_error => assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _try,
module => <<"gleam_stats/distributions/geometric"/utf8>>,
function => <<"do_geometric_random"/utf8>>,
line => 293})
end,
Numbers = begin
_pipe@2 = gleam@pair:first(Out@1),
gleam@list:map(
_pipe@2,
fun(X) ->
{ok, X1@1} = case gleam_stats@math:log(X) of
{ok, X1} -> {ok, X1};
_try@1 ->
erlang:error(#{gleam_error => assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _try@1,
module => <<"gleam_stats/distributions/geometric"/utf8>>,
function => <<"do_geometric_random"/utf8>>,
line => 299})
end,
{ok, X2@1} = case gleam_stats@math:log(1.0 - P) of
{ok, X2} -> {ok, X2};
_try@2 ->
erlang:error(#{gleam_error => assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _try@2,
module => <<"gleam_stats/distributions/geometric"/utf8>>,
function => <<"do_geometric_random"/utf8>>,
line => 300})
end,
gleam@float:round(
gleam_stats@math:floor(case X2@1 of
0.0 -> 0.0;
Gleam@denominator -> X1@1 / Gleam@denominator
end)
)
end
)
end,
_pipe@3 = {Numbers, gleam@pair:second(Out@1)},
{ok, _pipe@3}
end
end.