Skip to content

Commit

Permalink
attempt at output standardization for approximate counting (algorithm…
Browse files Browse the repository at this point in the history
…-archivists#913)

* attempt at output standardization for approximate counting

* further standardization
  • Loading branch information
leios authored Nov 22, 2021
1 parent 465e1e8 commit edbf50b
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 23 deletions.
15 changes: 10 additions & 5 deletions contents/approximate_counting/code/c/approximate_counting.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,19 +63,24 @@ void test_approximation_count(size_t n_trials, size_t n_items, double a,
}
double avg = sum / n_trials;

assert(fabs((avg - n_items) / n_items) < threshold);
if (fabs((avg - n_items) / n_items) < threshold){
printf("passed\n");
}
else{
printf("failed\n");
}
}

int main()
{
srand(time(NULL));

printf("Counting Tests, 100 trials\n");
printf("testing 1000, a = 30, 10%% error\n");
printf("[#]\nCounting Tests, 100 trials\n");
printf("[#]\ntesting 1,000, a = 30, 10%% error\n");
test_approximation_count(100, 1000, 30, 0.1);
printf("testing 12345, a = 10, 10%% error\n");
printf("[#]\ntesting 12,345, a = 10, 10%% error\n");
test_approximation_count(100, 12345, 10, 0.1);
printf("testing 222222, a = 0.5, 20%% error\n");
printf("[#]\ntesting 222,222, a = 0.5, 20%% error\n");
test_approximation_count(100, 222222, 0.5, 0.2);

return 0;
Expand Down
10 changes: 5 additions & 5 deletions contents/approximate_counting/code/cpp/approximate_counting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,17 @@ auto test_approximate_count(
for (auto i = 0; i < n_trials; ++i)
sum += approximate_count(n_items, a);
const auto avg = sum / n_trials;
return std::abs((avg - n_items) / n_items) < threshold ? "pass" : "fail";
return std::abs((avg - n_items) / n_items) < threshold ? "passed" : "failed";
}

int main() {
std::cout << "Counting Tests, 100 trials\n";
std::cout << "[#]\nCounting Tests, 100 trials\n";

std::cout << "testing 1,000, a = 30, 10% error "
std::cout << "[#]\ntesting 1,000, a = 30, 10% error \n"
<< test_approximate_count(100, 1000, 30, 0.1) << "\n";
std::cout << "testing 12,345, a = 10, 10% error "
std::cout << "[#]\ntesting 12,345, a = 10, 10% error \n"
<< test_approximate_count(100, 12345, 10, 0.1) << "\n";
// Note : with a lower a, we need more trials, so a higher % error here.
std::cout << "testing 222,222, a = 0.5, 20% error "
std::cout << "[#]\ntesting 222,222, a = 0.5, 20% error \n"
<< test_approximate_count(100, 222222, 0.5, 0.2) << "\n";
}
26 changes: 16 additions & 10 deletions contents/approximate_counting/code/julia/approximate_counting.jl
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,21 @@ function test_approximate_count(n_trials, n_items, a, threshold)

avg = sum(samples)/n_trials

@test (abs((avg - n_items) / n_items) < threshold)
if (abs((avg - n_items) / n_items) < threshold)
println("passed")
else
println("failed")
end
end

@testset "Counting Tests, 100 trials" begin
println("testing 1,000, a = 30, 10% error")
test_approximate_count(100, 1000, 30, 0.1)
println("testing 12,345, a = 10, 10% error")
test_approximate_count(100, 12345, 10, 0.1)
# Note: with a lower a, we need more trials, so a higher % error here.
println("testing 222,222, a = 0.5, 20% error")
test_approximate_count(100, 222222, 0.5, 0.2)
end
println("[#]\nCounting Tests, 100 trials")

println("[#]\ntesting 1,000, a = 30, 10% error")
test_approximate_count(100, 1000, 30, 0.1)

println("[#]\ntesting 12,345, a = 10, 10% error")
test_approximate_count(100, 12345, 10, 0.1)

# Note: with a lower a, we need more trials, so a higher % error here.
println("[#]\ntesting 222,222, a = 0.5, 20% error")
test_approximate_count(100, 222222, 0.5, 0.2)
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,13 @@ def test_approximate_count(n_trials, n_items, a, threshold):

if abs((avg - n_items)/n_items) < threshold:
print("passed")
else:
print("failed")

print("testing 1,000, a = 30, 10% error")
print("[#]\nCounting Tests, 100 trials")
print("[#]\ntesting 1,000, a = 30, 10% error")
test_approximate_count(100, 1000, 30, 0.1)
print("testing 12,345, a = 10, 10% error")
print("[#]\ntesting 12,345, a = 10, 10% error")
test_approximate_count(100, 12345, 10, 0.1)
print("testing 222,222, a = 0.5, 20% error")
print("[#]\ntesting 222,222, a = 0.5, 20% error")
test_approximate_count(100, 222222, 0.5, 0.2)

0 comments on commit edbf50b

Please sign in to comment.