Skip to content

Commit d0aa6ba

Browse files
authored
Merge pull request #3 from isroman/master
Remove ambiguous ceil and floor functions, replace random with rand
2 parents e5305cb + 4a8c984 commit d0aa6ba

File tree

2 files changed

+5
-32
lines changed

2 files changed

+5
-32
lines changed

src/quantile.erl

+1-20
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
-ifdef(TEST).
55
-include_lib("eunit/include/eunit.hrl").
6-
-export([ceil/1]).
76
-define(DEBUG, true).
87
-endif.
98

@@ -54,16 +53,6 @@ ranks(Size, Quantile) ->
5453
[ceil(Size * Quantile)]
5554
end.
5655

57-
-spec ceil(float()) -> integer().
58-
ceil(X) ->
59-
T = erlang:trunc(X),
60-
case (X - T) of
61-
Neg when Neg < 0 -> T;
62-
Pos when Pos > 0 -> T + 1;
63-
_ -> T
64-
end.
65-
66-
6756
% TESTS
6857
rank_average(Size, Quantile) ->
6958
case ranks(Size, Quantile) of
@@ -81,8 +70,7 @@ quantile_test_() ->
8170
fun test_setup/0,
8271
fun test_teardown/1,
8372
[
84-
{"test ceil", fun ceil_test/0}
85-
,{"test ranks", fun ranks_test/0}
73+
{"test ranks", fun ranks_test/0}
8674
,{"test natural", fun natural_test/0}
8775
,{"test even", fun even_test/0}
8876
,{"test quantiles", fun quantile_test/0}
@@ -115,13 +103,6 @@ even_test() ->
115103
,?assertEqual(false, even(11))
116104
,?assertEqual(false, even(-11)).
117105

118-
ceil_test() ->
119-
?assertEqual(1, ceil(1.0))
120-
,?assertEqual(2, ceil(1.1))
121-
,?assertEqual(2, ceil(1.8))
122-
,?assertEqual(-1, ceil(-1.8))
123-
,?assertEqual(-1, ceil(-1.1)).
124-
125106
quantile_test() ->
126107
Samples1 = [3.0, 6.0, 7.0, 8.0, 8.0, 10.0, 13.0, 15.0, 16.0, 20.0]
127108
,?assertEqual(7.0, quantile(1/4, Samples1))

src/quantile_estimator.erl

+4-12
Original file line numberDiff line numberDiff line change
@@ -160,14 +160,6 @@ quantile(Phi, Invariant, [Next = #group{g = Gi, delta = Deltai}|DataStructure],
160160
quantile(Phi, Invariant, DataStructure, N, Rank + Gi, Next)
161161
end.
162162

163-
floor(X) ->
164-
T = erlang:trunc(X),
165-
case (X - T) of
166-
Neg when Neg < 0 -> T - 1;
167-
Pos when Pos > 0 -> T;
168-
_ -> T
169-
end.
170-
171163
clamp(X) when X >= 0 -> X;
172164
clamp(X) when X < 0 -> 0.
173165

@@ -273,7 +265,7 @@ test_quantile() ->
273265
% we create a set of 1000 random values and test if the guarantees are met
274266
Invariant = f_biased(0.001),
275267
N = 1000,
276-
Samples = [random:uniform()||_ <- lists:seq(1, N)],
268+
Samples = [rand:uniform()||_ <- lists:seq(1, N)],
277269
Data = lists:foldl(fun(Sample, Stats) -> insert(Sample, Stats) end, quantile_estimator:new(Invariant), Samples),
278270
% error_logger:info_msg("D:~p\n", [D]),
279271
validate(Samples, Invariant, Data),
@@ -288,7 +280,7 @@ test_compression_biased() ->
288280
% we create a set of 1000 random values and test if the guarantees are met
289281
Invariant = f_biased(0.01),
290282
N = 2000,
291-
Samples = [random:uniform()||_ <- lists:seq(1, N)],
283+
Samples = [rand:uniform()||_ <- lists:seq(1, N)],
292284
Data = lists:foldl(fun(Sample, Stats) -> insert(Sample, Stats) end, quantile_estimator:new(Invariant), Samples),
293285
DL = Data#quantile_estimator.data,
294286
validate(Samples, Invariant, Data),
@@ -298,7 +290,7 @@ test_comression_targeted() ->
298290
% we create a set of 1000 random values and test if the guarantees are met
299291
Invariant = quantile_estimator:f_targeted([{0.05, 0.005}, {0.5, 0.02}, {0.95, 0.005}]),
300292
N = 2000,
301-
Samples = [random:uniform()||_ <- lists:seq(1, N)],
293+
Samples = [rand:uniform()||_ <- lists:seq(1, N)],
302294
Data = lists:foldl(fun(Sample, Stats) -> insert(Sample, Stats) end, quantile_estimator:new(Invariant), Samples),
303295
DL = Data#quantile_estimator.data,
304296
validate(Samples, Invariant, Data),
@@ -339,7 +331,7 @@ validate(Samples, Invariant, Estimate = #quantile_estimator{samples_count = N, d
339331
Index(quantile:quantile(Q, SamplesSort), SamplesSort),
340332
Index(quantile(Q, Estimate), SamplesSort),
341333
abs(Index(quantile:quantile(Q, SamplesSort), SamplesSort) - Index(quantile(Q, Estimate), SamplesSort)),
342-
quantile:ceil(Invariant(Index(quantile(Q, Estimate), SamplesSort), N))
334+
ceil(Invariant(Index(quantile(Q, Estimate), SamplesSort), N))
343335
} || Q <- Quantiles],
344336
% [error_logger:info_msg("QReal:~p,~p,~p\n", [Q, N, quantile:quantile(Q, SamplesSort)]) || Q <- [0.0, 1.0]],
345337
% [error_logger:info_msg("QEst:~p,~p,~p\n", [Q, N, quantile(Q, Invariant, Estimate)]) || Q <- [0.0, 1.0]],

0 commit comments

Comments
 (0)