Current section

Files

Jump to
gleam_stats src gleam_stats@distributions@uniform.erl
Raw

src/gleam_stats@distributions@uniform.erl

-module(gleam_stats@distributions@uniform).
-compile(no_auto_import).
-export([uniform_mean/2, uniform_variance/2, uniform_pdf/3, uniform_cdf/3, uniform_random/4]).
-spec check_uniform_parameters(float(), float()) -> {ok, boolean()} |
{error, binary()}.
check_uniform_parameters(A, B) ->
case A =< B of
false ->
_pipe = <<"Invalid input arugment: a > b. Valid input is a <= b."/utf8>>,
{error, _pipe};
true ->
_pipe@1 = true,
{ok, _pipe@1}
end.
-spec uniform_mean(float(), float()) -> {ok, float()} | {error, binary()}.
uniform_mean(A, B) ->
case check_uniform_parameters(A, B) of
{error, String} ->
_pipe = String,
{error, _pipe};
_@1 ->
_pipe@1 = (A + B) / 2.0,
{ok, _pipe@1}
end.
-spec uniform_variance(float(), float()) -> {ok, float()} | {error, binary()}.
uniform_variance(A, B) ->
case check_uniform_parameters(A, B) of
{error, String} ->
_pipe = String,
{error, _pipe};
_@1 ->
{ok, V@1} = case gleam_stats@math:pow(B - A, 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/uniform"/utf8>>,
function => <<"uniform_variance"/utf8>>,
line => 84})
end,
_pipe@1 = V@1 / 12.0,
{ok, _pipe@1}
end.
-spec uniform_pdf(float(), float(), float()) -> {ok, float()} |
{error, binary()}.
uniform_pdf(X, A, B) ->
do_uniform_pdf(X, A, B).
-spec do_uniform_pdf(float(), float(), float()) -> {ok, float()} |
{error, binary()}.
do_uniform_pdf(X, A, B) ->
case check_uniform_parameters(A, B) of
{error, String} ->
_pipe = String,
{error, _pipe};
_@1 ->
case (X >= A) andalso (X =< B) of
true ->
_pipe@1 = case (B - A) of
0.0 -> 0.0;
Gleam@denominator -> 1.0 / Gleam@denominator
end,
{ok, _pipe@1};
false ->
_pipe@2 = 0.0,
{ok, _pipe@2}
end
end.
-spec uniform_cdf(float(), float(), float()) -> {ok, float()} |
{error, binary()}.
uniform_cdf(X, A, B) ->
do_uniform_cdf(X, A, B).
-spec do_uniform_cdf(float(), float(), float()) -> {ok, float()} |
{error, binary()}.
do_uniform_cdf(X, A, B) ->
case check_uniform_parameters(A, B) of
{error, String} ->
_pipe = String,
{error, _pipe};
_@1 ->
case X < A of
true ->
_pipe@1 = 0.0,
{ok, _pipe@1};
false ->
case (X >= A) andalso (X =< B) of
true ->
_pipe@2 = case (B - A) of
0.0 -> 0.0;
Gleam@denominator -> (X - A) / Gleam@denominator
end,
{ok, _pipe@2};
false ->
case X > B of
_@2 ->
_pipe@3 = 1.0,
{ok, _pipe@3}
end
end
end
end.
-spec uniform_random(
gleam@iterator:iterator(integer()),
float(),
float(),
integer()
) -> {ok, {list(float()), gleam@iterator:iterator(integer())}} |
{error, binary()}.
uniform_random(Stream, A, B, M) ->
do_uniform_random(Stream, A, B, M).
-spec do_uniform_random(
gleam@iterator:iterator(integer()),
float(),
float(),
integer()
) -> {ok, {list(float()), gleam@iterator:iterator(integer())}} |
{error, binary()}.
do_uniform_random(Stream, A, B, M) ->
case check_uniform_parameters(A, B) of
{error, String} ->
_pipe = String,
{error, _pipe};
_@1 ->
case M > 0 of
false ->
{error,
<<"Invalid input arugment: m < 0. Valid input is m > 0."/utf8>>};
true ->
{ok, Out@1} = case gleam_stats@generators:take_randints(
Stream,
M
) of
{ok, Out} -> {ok, Out};
_try ->
erlang:error(#{gleam_error => assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _try,
module => <<"gleam_stats/distributions/uniform"/utf8>>,
function => <<"do_uniform_random"/utf8>>,
line => 314})
end,
Numbers = begin
_pipe@1 = gleam@pair:first(Out@1),
gleam@list:map(
_pipe@1,
fun(X) ->
A
+ (case gleam@int:to_float(4294967295) of
0.0 -> 0.0;
Gleam@denominator -> (gleam@int:to_float(X)
* (B
- A))
/ Gleam@denominator
end)
end
)
end,
_pipe@2 = {Numbers, gleam@pair:second(Out@1)},
{ok, _pipe@2}
end
end.