Current section

Files

Jump to
gleam_stats gen test gleam_stats_examples_test.erl
Raw

gen/test/gleam_stats_examples_test.erl

-module(gleam_stats_examples_test).
-compile(no_auto_import).
-export([example_sum_test/0, example_mean_test/0, example_median_test/0, example_hmean_test/0, example_gmean_test/0, example_var_test/0, example_std_test/0, example_moment_test/0, example_skewness_test/0, example_kurtosis_test/0, example_zscore_test/0, example_percentile_test/0, example_iqr_test/0, example_freedman_diaconis_rule_test/0, example_range_test/0, example_bin_test/0, example_histogram_test/0, example_correlation_test/0, example_trim_test/0, example_isclose_test/0, example_allclose_test/0, example_amax_test/0, example_amin_test/0, example_argmax_test/0, example_argmin_test/0]).
-spec example_sum_test() -> gleam@should:expectation().
example_sum_test() ->
gleam@should:equal(gleam_stats@stats:sum([]), 0.0),
gleam@should:equal(gleam_stats@stats:sum([1.0, 2.0, 3.0]), 6.0).
-spec example_mean_test() -> gleam@should:expectation().
example_mean_test() ->
gleam@should:equal(gleam_stats@stats:mean([]), {error, nil}),
gleam@should:equal(gleam_stats@stats:mean([1.0, 2.0, 3.0]), {ok, 2.0}).
-spec example_median_test() -> gleam@should:expectation().
example_median_test() ->
gleam@should:equal(gleam_stats@stats:median([]), {error, nil}),
gleam@should:equal(gleam_stats@stats:median([1.0, 2.0, 3.0]), {ok, 2.0}),
gleam@should:equal(
gleam_stats@stats:median([1.0, 2.0, 3.0, 4.0]),
{ok, 2.5}
).
-spec example_hmean_test() -> gleam@should:expectation().
example_hmean_test() ->
gleam@should:equal(gleam_stats@stats:hmean([]), {error, nil}),
gleam@should:equal(gleam_stats@stats:hmean([1.0, 3.0, 6.0]), {ok, 2.0}).
-spec example_gmean_test() -> gleam@should:expectation().
example_gmean_test() ->
gleam@should:equal(gleam_stats@stats:gmean([]), {error, nil}),
gleam@should:equal(gleam_stats@stats:gmean([1.0, 3.0, 9.0]), {ok, 3.0}).
-spec example_var_test() -> gleam@should:expectation().
example_var_test() ->
Ddof = 1,
gleam@should:equal(gleam_stats@stats:var([], Ddof), {error, nil}),
gleam@should:equal(gleam_stats@stats:var([1.0, 2.0, 3.0], Ddof), {ok, 1.0}).
-spec example_std_test() -> gleam@should:expectation().
example_std_test() ->
Ddof = 1,
gleam@should:equal(gleam_stats@stats:std([], Ddof), {error, nil}),
gleam@should:equal(gleam_stats@stats:std([1.0, 2.0, 3.0], Ddof), {ok, 1.0}).
-spec example_moment_test() -> gleam@should:expectation().
example_moment_test() ->
gleam@should:equal(gleam_stats@stats:moment([], 0), {error, nil}),
gleam@should:equal(
gleam_stats@stats:moment([0.0, 1.0, 2.0, 3.0, 4.0], 0),
{ok, 1.0}
),
gleam@should:equal(
gleam_stats@stats:moment([0.0, 1.0, 2.0, 3.0, 4.0], 1),
{ok, 0.0}
),
gleam@should:equal(
gleam_stats@stats:moment([0.0, 1.0, 2.0, 3.0, 4.0], 2),
{ok, 2.0}
).
-spec example_skewness_test() -> gleam@should:expectation().
example_skewness_test() ->
gleam@should:equal(gleam_stats@stats:skewness([]), {error, nil}),
gleam@should:equal(
gleam_stats@stats:skewness([1.0, 2.0, 3.0, 4.0]),
{ok, 0.0}
),
gleam@should:be_true((fun(X) -> case X of
{ok, X@1} ->
X@1 > 0.0
end end)(gleam_stats@stats:skewness([1.0, 1.0, 1.0, 2.0]))).
-spec example_kurtosis_test() -> gleam@should:expectation().
example_kurtosis_test() ->
gleam@should:equal(gleam_stats@stats:skewness([]), {error, nil}),
gleam@should:equal(
gleam_stats@stats:kurtosis([1.0, 1.0, 1.0, 1.0]),
{ok, -3.0}
),
gleam@should:be_true((fun(X) -> case X of
{ok, X@1} ->
X@1 > -3.0
end end)(gleam_stats@stats:kurtosis([1.0, 1.0, 1.0, 2.0]))).
-spec example_zscore_test() -> gleam@should:expectation().
example_zscore_test() ->
gleam@should:equal(gleam_stats@stats:zscore([], 1), {error, nil}),
gleam@should:equal(
gleam_stats@stats:zscore([1.0, 2.0, 3.0], 1),
{ok, [-1.0, 0.0, 1.0]}
).
-spec example_percentile_test() -> gleam@should:expectation().
example_percentile_test() ->
gleam@should:equal(gleam_stats@stats:percentile([], 40), {error, nil}),
gleam@should:equal(
gleam_stats@stats:percentile([15.0, 20.0, 35.0, 40.0, 50.0], 40),
{ok, 29.0}
).
-spec example_iqr_test() -> gleam@should:expectation().
example_iqr_test() ->
gleam@should:equal(gleam_stats@stats:iqr([]), {error, nil}),
gleam@should:equal(
gleam_stats@stats:iqr([1.0, 2.0, 3.0, 4.0, 5.0]),
{ok, 3.0}
).
-spec example_freedman_diaconis_rule_test() -> gleam@should:expectation().
example_freedman_diaconis_rule_test() ->
gleam@should:equal(
gleam_stats@stats:freedman_diaconis_rule([]),
{error, nil}
),
gleam@should:equal(
gleam_stats@stats:freedman_diaconis_rule(
gleam@list:map(
gleam@list:range(0, 1000),
fun(X) -> gleam@int:to_float(X) end
)
),
{ok, 10.0}
).
-spec example_range_test() -> gleam@should:expectation().
example_range_test() ->
Range = {range, 0.0, 1.0},
{range, Min, Max} = Range,
gleam@should:equal(Min, 0.0),
gleam@should:equal(Max, 1.0).
-spec example_bin_test() -> gleam@should:expectation().
example_bin_test() ->
Bin = {{range, 0.0, 1.0}, 999},
{range, Min, Max} = gleam@pair:first(Bin),
gleam@should:equal(Min, 0.0),
gleam@should:equal(Max, 1.0),
Count = gleam@pair:second(Bin),
gleam@should:equal(Count, 999).
-spec example_histogram_test() -> gleam@should:expectation().
example_histogram_test() ->
gleam@should:equal(gleam_stats@stats:histogram([], 1.0), {error, nil}),
gleam@should:equal(
gleam_stats@stats:histogram(
gleam@list:map(
gleam@list:range(0, 100),
fun(X) -> gleam@int:to_float(X) end
),
25.0
),
{ok,
[{{range, 0.0, 25.0}, 25},
{{range, 25.0, 50.0}, 25},
{{range, 50.0, 75.0}, 25},
{{range, 75.0, 100.0}, 25}]}
).
-spec example_correlation_test() -> gleam@should:expectation().
example_correlation_test() ->
gleam@should:equal(gleam_stats@stats:correlation([], []), {error, nil}),
Xarr0 = gleam@list:map(
gleam@list:range(0, 100),
fun(X) -> gleam@int:to_float(X) end
),
Yarr0 = gleam@list:map(
gleam@list:range(0, 100),
fun(X@1) -> gleam@int:to_float(X@1) end
),
gleam@should:equal(gleam_stats@stats:correlation(Xarr0, Yarr0), {ok, 1.0}),
Xarr0@1 = gleam@list:map(
gleam@list:range(0, 100),
fun(X@2) -> -1.0 * gleam@int:to_float(X@2) end
),
Yarr0@1 = gleam@list:map(
gleam@list:range(0, 100),
fun(X@3) -> gleam@int:to_float(X@3) end
),
gleam@should:equal(
gleam_stats@stats:correlation(Xarr0@1, Yarr0@1),
{ok, -1.0}
).
-spec example_trim_test() -> gleam@should:expectation().
example_trim_test() ->
gleam@should:equal(gleam_stats@stats:trim([], 0, 0), {error, nil}),
gleam@should:equal(
gleam_stats@stats:trim([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], 1, 4),
{ok, [2.0, 3.0, 4.0, 5.0]}
).
-spec example_isclose_test() -> gleam@should:expectation().
example_isclose_test() ->
Val = 99.0,
Ref_val = 100.0,
Rtol = 0.01,
Atol = 0.10,
gleam@should:be_true(gleam_stats@stats:isclose(Val, Ref_val, Rtol, Atol)).
-spec example_allclose_test() -> gleam@should:expectation().
example_allclose_test() ->
Val = 99.0,
Ref_val = 100.0,
Xarr = gleam@list:repeat(Val, 42),
Yarr = gleam@list:repeat(Ref_val, 42),
Rtol = 0.01,
Atol = 0.10,
gleam@should:equal((fun(Zarr) -> case Zarr of
{ok, Arr} ->
{ok, gleam@list:all(Arr, fun(A) -> A end)};
_ ->
{error, nil}
end end)(gleam_stats@stats:allclose(Xarr, Yarr, Rtol, Atol)), {ok,
true}).
-spec example_amax_test() -> gleam@should:expectation().
example_amax_test() ->
gleam@should:equal(gleam_stats@stats:amax([]), {error, nil}),
gleam@should:equal(
gleam_stats@stats:amax([4.0, 4.0, 3.0, 2.0, 1.0]),
{ok, 4.0}
).
-spec example_amin_test() -> gleam@should:expectation().
example_amin_test() ->
gleam@should:equal(gleam_stats@stats:amin([]), {error, nil}),
gleam@should:equal(
gleam_stats@stats:amin([4.0, 4.0, 3.0, 2.0, 1.0]),
{ok, 1.0}
).
-spec example_argmax_test() -> gleam@should:expectation().
example_argmax_test() ->
gleam@should:equal(gleam_stats@stats:argmax([]), {error, nil}),
gleam@should:equal(
gleam_stats@stats:argmax([4.0, 4.0, 3.0, 2.0, 1.0]),
{ok, [0, 1]}
).
-spec example_argmin_test() -> gleam@should:expectation().
example_argmin_test() ->
gleam@should:equal(gleam_stats@stats:argmin([]), {error, nil}),
gleam@should:equal(
gleam_stats@stats:argmin([4.0, 4.0, 3.0, 2.0, 1.0]),
{ok, [4]}
).