datasketches 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/CHANGELOG.md +3 -0
- data/LICENSE +310 -0
- data/NOTICE +11 -0
- data/README.md +126 -0
- data/ext/datasketches/cpc_wrapper.cpp +50 -0
- data/ext/datasketches/ext.cpp +12 -0
- data/ext/datasketches/extconf.rb +11 -0
- data/ext/datasketches/hll_wrapper.cpp +69 -0
- data/lib/datasketches.rb +9 -0
- data/lib/datasketches/version.rb +3 -0
- data/vendor/datasketches-cpp/CMakeLists.txt +126 -0
- data/vendor/datasketches-cpp/LICENSE +311 -0
- data/vendor/datasketches-cpp/MANIFEST.in +19 -0
- data/vendor/datasketches-cpp/NOTICE +11 -0
- data/vendor/datasketches-cpp/README.md +42 -0
- data/vendor/datasketches-cpp/common/CMakeLists.txt +45 -0
- data/vendor/datasketches-cpp/common/include/MurmurHash3.h +173 -0
- data/vendor/datasketches-cpp/common/include/binomial_bounds.hpp +458 -0
- data/vendor/datasketches-cpp/common/include/bounds_binomial_proportions.hpp +291 -0
- data/vendor/datasketches-cpp/common/include/ceiling_power_of_2.hpp +41 -0
- data/vendor/datasketches-cpp/common/include/common_defs.hpp +51 -0
- data/vendor/datasketches-cpp/common/include/conditional_back_inserter.hpp +68 -0
- data/vendor/datasketches-cpp/common/include/conditional_forward.hpp +70 -0
- data/vendor/datasketches-cpp/common/include/count_zeros.hpp +114 -0
- data/vendor/datasketches-cpp/common/include/inv_pow2_table.hpp +107 -0
- data/vendor/datasketches-cpp/common/include/memory_operations.hpp +57 -0
- data/vendor/datasketches-cpp/common/include/serde.hpp +196 -0
- data/vendor/datasketches-cpp/common/test/CMakeLists.txt +38 -0
- data/vendor/datasketches-cpp/common/test/catch.hpp +17618 -0
- data/vendor/datasketches-cpp/common/test/catch_runner.cpp +7 -0
- data/vendor/datasketches-cpp/common/test/test_allocator.cpp +31 -0
- data/vendor/datasketches-cpp/common/test/test_allocator.hpp +108 -0
- data/vendor/datasketches-cpp/common/test/test_runner.cpp +29 -0
- data/vendor/datasketches-cpp/common/test/test_type.hpp +137 -0
- data/vendor/datasketches-cpp/cpc/CMakeLists.txt +74 -0
- data/vendor/datasketches-cpp/cpc/include/compression_data.hpp +6022 -0
- data/vendor/datasketches-cpp/cpc/include/cpc_common.hpp +62 -0
- data/vendor/datasketches-cpp/cpc/include/cpc_compressor.hpp +147 -0
- data/vendor/datasketches-cpp/cpc/include/cpc_compressor_impl.hpp +742 -0
- data/vendor/datasketches-cpp/cpc/include/cpc_confidence.hpp +167 -0
- data/vendor/datasketches-cpp/cpc/include/cpc_sketch.hpp +311 -0
- data/vendor/datasketches-cpp/cpc/include/cpc_sketch_impl.hpp +810 -0
- data/vendor/datasketches-cpp/cpc/include/cpc_union.hpp +102 -0
- data/vendor/datasketches-cpp/cpc/include/cpc_union_impl.hpp +346 -0
- data/vendor/datasketches-cpp/cpc/include/cpc_util.hpp +137 -0
- data/vendor/datasketches-cpp/cpc/include/icon_estimator.hpp +274 -0
- data/vendor/datasketches-cpp/cpc/include/kxp_byte_lookup.hpp +81 -0
- data/vendor/datasketches-cpp/cpc/include/u32_table.hpp +84 -0
- data/vendor/datasketches-cpp/cpc/include/u32_table_impl.hpp +266 -0
- data/vendor/datasketches-cpp/cpc/test/CMakeLists.txt +44 -0
- data/vendor/datasketches-cpp/cpc/test/compression_test.cpp +67 -0
- data/vendor/datasketches-cpp/cpc/test/cpc_sketch_test.cpp +381 -0
- data/vendor/datasketches-cpp/cpc/test/cpc_union_test.cpp +149 -0
- data/vendor/datasketches-cpp/fi/CMakeLists.txt +54 -0
- data/vendor/datasketches-cpp/fi/include/frequent_items_sketch.hpp +319 -0
- data/vendor/datasketches-cpp/fi/include/frequent_items_sketch_impl.hpp +484 -0
- data/vendor/datasketches-cpp/fi/include/reverse_purge_hash_map.hpp +114 -0
- data/vendor/datasketches-cpp/fi/include/reverse_purge_hash_map_impl.hpp +345 -0
- data/vendor/datasketches-cpp/fi/test/CMakeLists.txt +44 -0
- data/vendor/datasketches-cpp/fi/test/frequent_items_sketch_custom_type_test.cpp +84 -0
- data/vendor/datasketches-cpp/fi/test/frequent_items_sketch_test.cpp +360 -0
- data/vendor/datasketches-cpp/fi/test/items_sketch_string_from_java.sk +0 -0
- data/vendor/datasketches-cpp/fi/test/items_sketch_string_utf8_from_java.sk +0 -0
- data/vendor/datasketches-cpp/fi/test/longs_sketch_from_java.sk +0 -0
- data/vendor/datasketches-cpp/fi/test/reverse_purge_hash_map_test.cpp +47 -0
- data/vendor/datasketches-cpp/hll/CMakeLists.txt +92 -0
- data/vendor/datasketches-cpp/hll/include/AuxHashMap-internal.hpp +303 -0
- data/vendor/datasketches-cpp/hll/include/AuxHashMap.hpp +83 -0
- data/vendor/datasketches-cpp/hll/include/CompositeInterpolationXTable-internal.hpp +811 -0
- data/vendor/datasketches-cpp/hll/include/CompositeInterpolationXTable.hpp +40 -0
- data/vendor/datasketches-cpp/hll/include/CouponHashSet-internal.hpp +291 -0
- data/vendor/datasketches-cpp/hll/include/CouponHashSet.hpp +59 -0
- data/vendor/datasketches-cpp/hll/include/CouponList-internal.hpp +417 -0
- data/vendor/datasketches-cpp/hll/include/CouponList.hpp +91 -0
- data/vendor/datasketches-cpp/hll/include/CubicInterpolation-internal.hpp +233 -0
- data/vendor/datasketches-cpp/hll/include/CubicInterpolation.hpp +43 -0
- data/vendor/datasketches-cpp/hll/include/HarmonicNumbers-internal.hpp +90 -0
- data/vendor/datasketches-cpp/hll/include/HarmonicNumbers.hpp +48 -0
- data/vendor/datasketches-cpp/hll/include/Hll4Array-internal.hpp +335 -0
- data/vendor/datasketches-cpp/hll/include/Hll4Array.hpp +69 -0
- data/vendor/datasketches-cpp/hll/include/Hll6Array-internal.hpp +124 -0
- data/vendor/datasketches-cpp/hll/include/Hll6Array.hpp +55 -0
- data/vendor/datasketches-cpp/hll/include/Hll8Array-internal.hpp +158 -0
- data/vendor/datasketches-cpp/hll/include/Hll8Array.hpp +56 -0
- data/vendor/datasketches-cpp/hll/include/HllArray-internal.hpp +706 -0
- data/vendor/datasketches-cpp/hll/include/HllArray.hpp +136 -0
- data/vendor/datasketches-cpp/hll/include/HllSketch-internal.hpp +462 -0
- data/vendor/datasketches-cpp/hll/include/HllSketchImpl-internal.hpp +149 -0
- data/vendor/datasketches-cpp/hll/include/HllSketchImpl.hpp +85 -0
- data/vendor/datasketches-cpp/hll/include/HllSketchImplFactory.hpp +170 -0
- data/vendor/datasketches-cpp/hll/include/HllUnion-internal.hpp +287 -0
- data/vendor/datasketches-cpp/hll/include/HllUtil.hpp +239 -0
- data/vendor/datasketches-cpp/hll/include/RelativeErrorTables-internal.hpp +112 -0
- data/vendor/datasketches-cpp/hll/include/RelativeErrorTables.hpp +46 -0
- data/vendor/datasketches-cpp/hll/include/coupon_iterator-internal.hpp +56 -0
- data/vendor/datasketches-cpp/hll/include/coupon_iterator.hpp +43 -0
- data/vendor/datasketches-cpp/hll/include/hll.hpp +669 -0
- data/vendor/datasketches-cpp/hll/include/hll.private.hpp +32 -0
- data/vendor/datasketches-cpp/hll/test/AuxHashMapTest.cpp +79 -0
- data/vendor/datasketches-cpp/hll/test/CMakeLists.txt +51 -0
- data/vendor/datasketches-cpp/hll/test/CouponHashSetTest.cpp +130 -0
- data/vendor/datasketches-cpp/hll/test/CouponListTest.cpp +181 -0
- data/vendor/datasketches-cpp/hll/test/CrossCountingTest.cpp +93 -0
- data/vendor/datasketches-cpp/hll/test/HllArrayTest.cpp +191 -0
- data/vendor/datasketches-cpp/hll/test/HllSketchTest.cpp +389 -0
- data/vendor/datasketches-cpp/hll/test/HllUnionTest.cpp +313 -0
- data/vendor/datasketches-cpp/hll/test/IsomorphicTest.cpp +141 -0
- data/vendor/datasketches-cpp/hll/test/TablesTest.cpp +44 -0
- data/vendor/datasketches-cpp/hll/test/ToFromByteArrayTest.cpp +168 -0
- data/vendor/datasketches-cpp/hll/test/array6_from_java.sk +0 -0
- data/vendor/datasketches-cpp/hll/test/compact_array4_from_java.sk +0 -0
- data/vendor/datasketches-cpp/hll/test/compact_set_from_java.sk +0 -0
- data/vendor/datasketches-cpp/hll/test/list_from_java.sk +0 -0
- data/vendor/datasketches-cpp/hll/test/updatable_array4_from_java.sk +0 -0
- data/vendor/datasketches-cpp/hll/test/updatable_set_from_java.sk +0 -0
- data/vendor/datasketches-cpp/kll/CMakeLists.txt +58 -0
- data/vendor/datasketches-cpp/kll/include/kll_helper.hpp +150 -0
- data/vendor/datasketches-cpp/kll/include/kll_helper_impl.hpp +319 -0
- data/vendor/datasketches-cpp/kll/include/kll_quantile_calculator.hpp +67 -0
- data/vendor/datasketches-cpp/kll/include/kll_quantile_calculator_impl.hpp +169 -0
- data/vendor/datasketches-cpp/kll/include/kll_sketch.hpp +559 -0
- data/vendor/datasketches-cpp/kll/include/kll_sketch_impl.hpp +1131 -0
- data/vendor/datasketches-cpp/kll/test/CMakeLists.txt +44 -0
- data/vendor/datasketches-cpp/kll/test/kll_sketch_custom_type_test.cpp +154 -0
- data/vendor/datasketches-cpp/kll/test/kll_sketch_float_one_item_v1.sk +0 -0
- data/vendor/datasketches-cpp/kll/test/kll_sketch_from_java.sk +0 -0
- data/vendor/datasketches-cpp/kll/test/kll_sketch_test.cpp +685 -0
- data/vendor/datasketches-cpp/kll/test/kll_sketch_validation.cpp +229 -0
- data/vendor/datasketches-cpp/pyproject.toml +17 -0
- data/vendor/datasketches-cpp/python/CMakeLists.txt +61 -0
- data/vendor/datasketches-cpp/python/README.md +78 -0
- data/vendor/datasketches-cpp/python/jupyter/CPCSketch.ipynb +345 -0
- data/vendor/datasketches-cpp/python/jupyter/FrequentItemsSketch.ipynb +354 -0
- data/vendor/datasketches-cpp/python/jupyter/HLLSketch.ipynb +346 -0
- data/vendor/datasketches-cpp/python/jupyter/KLLSketch.ipynb +463 -0
- data/vendor/datasketches-cpp/python/jupyter/ThetaSketchNotebook.ipynb +396 -0
- data/vendor/datasketches-cpp/python/src/__init__.py +2 -0
- data/vendor/datasketches-cpp/python/src/cpc_wrapper.cpp +90 -0
- data/vendor/datasketches-cpp/python/src/datasketches.cpp +40 -0
- data/vendor/datasketches-cpp/python/src/fi_wrapper.cpp +123 -0
- data/vendor/datasketches-cpp/python/src/hll_wrapper.cpp +136 -0
- data/vendor/datasketches-cpp/python/src/kll_wrapper.cpp +209 -0
- data/vendor/datasketches-cpp/python/src/theta_wrapper.cpp +162 -0
- data/vendor/datasketches-cpp/python/src/vector_of_kll.cpp +488 -0
- data/vendor/datasketches-cpp/python/src/vo_wrapper.cpp +140 -0
- data/vendor/datasketches-cpp/python/tests/__init__.py +0 -0
- data/vendor/datasketches-cpp/python/tests/cpc_test.py +64 -0
- data/vendor/datasketches-cpp/python/tests/fi_test.py +110 -0
- data/vendor/datasketches-cpp/python/tests/hll_test.py +131 -0
- data/vendor/datasketches-cpp/python/tests/kll_test.py +119 -0
- data/vendor/datasketches-cpp/python/tests/theta_test.py +121 -0
- data/vendor/datasketches-cpp/python/tests/vector_of_kll_test.py +148 -0
- data/vendor/datasketches-cpp/python/tests/vo_test.py +101 -0
- data/vendor/datasketches-cpp/sampling/CMakeLists.txt +48 -0
- data/vendor/datasketches-cpp/sampling/include/var_opt_sketch.hpp +392 -0
- data/vendor/datasketches-cpp/sampling/include/var_opt_sketch_impl.hpp +1752 -0
- data/vendor/datasketches-cpp/sampling/include/var_opt_union.hpp +239 -0
- data/vendor/datasketches-cpp/sampling/include/var_opt_union_impl.hpp +645 -0
- data/vendor/datasketches-cpp/sampling/test/CMakeLists.txt +43 -0
- data/vendor/datasketches-cpp/sampling/test/binaries_from_java.txt +67 -0
- data/vendor/datasketches-cpp/sampling/test/var_opt_sketch_test.cpp +509 -0
- data/vendor/datasketches-cpp/sampling/test/var_opt_union_test.cpp +358 -0
- data/vendor/datasketches-cpp/sampling/test/varopt_sketch_long_sampling.sk +0 -0
- data/vendor/datasketches-cpp/sampling/test/varopt_sketch_string_exact.sk +0 -0
- data/vendor/datasketches-cpp/sampling/test/varopt_union_double_sampling.sk +0 -0
- data/vendor/datasketches-cpp/setup.py +94 -0
- data/vendor/datasketches-cpp/theta/CMakeLists.txt +57 -0
- data/vendor/datasketches-cpp/theta/include/theta_a_not_b.hpp +73 -0
- data/vendor/datasketches-cpp/theta/include/theta_a_not_b_impl.hpp +83 -0
- data/vendor/datasketches-cpp/theta/include/theta_intersection.hpp +88 -0
- data/vendor/datasketches-cpp/theta/include/theta_intersection_impl.hpp +130 -0
- data/vendor/datasketches-cpp/theta/include/theta_sketch.hpp +533 -0
- data/vendor/datasketches-cpp/theta/include/theta_sketch_impl.hpp +939 -0
- data/vendor/datasketches-cpp/theta/include/theta_union.hpp +122 -0
- data/vendor/datasketches-cpp/theta/include/theta_union_impl.hpp +109 -0
- data/vendor/datasketches-cpp/theta/test/CMakeLists.txt +45 -0
- data/vendor/datasketches-cpp/theta/test/theta_a_not_b_test.cpp +244 -0
- data/vendor/datasketches-cpp/theta/test/theta_compact_empty_from_java.sk +0 -0
- data/vendor/datasketches-cpp/theta/test/theta_compact_estimation_from_java.sk +0 -0
- data/vendor/datasketches-cpp/theta/test/theta_compact_single_item_from_java.sk +0 -0
- data/vendor/datasketches-cpp/theta/test/theta_intersection_test.cpp +218 -0
- data/vendor/datasketches-cpp/theta/test/theta_sketch_test.cpp +438 -0
- data/vendor/datasketches-cpp/theta/test/theta_union_test.cpp +97 -0
- data/vendor/datasketches-cpp/theta/test/theta_update_empty_from_java.sk +0 -0
- data/vendor/datasketches-cpp/theta/test/theta_update_estimation_from_java.sk +0 -0
- data/vendor/datasketches-cpp/tuple/CMakeLists.txt +104 -0
- data/vendor/datasketches-cpp/tuple/include/array_of_doubles_a_not_b.hpp +52 -0
- data/vendor/datasketches-cpp/tuple/include/array_of_doubles_a_not_b_impl.hpp +32 -0
- data/vendor/datasketches-cpp/tuple/include/array_of_doubles_intersection.hpp +52 -0
- data/vendor/datasketches-cpp/tuple/include/array_of_doubles_intersection_impl.hpp +31 -0
- data/vendor/datasketches-cpp/tuple/include/array_of_doubles_sketch.hpp +179 -0
- data/vendor/datasketches-cpp/tuple/include/array_of_doubles_sketch_impl.hpp +238 -0
- data/vendor/datasketches-cpp/tuple/include/array_of_doubles_union.hpp +81 -0
- data/vendor/datasketches-cpp/tuple/include/array_of_doubles_union_impl.hpp +43 -0
- data/vendor/datasketches-cpp/tuple/include/bounds_on_ratios_in_sampled_sets.hpp +135 -0
- data/vendor/datasketches-cpp/tuple/include/bounds_on_ratios_in_theta_sketched_sets.hpp +135 -0
- data/vendor/datasketches-cpp/tuple/include/jaccard_similarity.hpp +172 -0
- data/vendor/datasketches-cpp/tuple/include/theta_a_not_b_experimental.hpp +53 -0
- data/vendor/datasketches-cpp/tuple/include/theta_a_not_b_experimental_impl.hpp +33 -0
- data/vendor/datasketches-cpp/tuple/include/theta_comparators.hpp +48 -0
- data/vendor/datasketches-cpp/tuple/include/theta_constants.hpp +34 -0
- data/vendor/datasketches-cpp/tuple/include/theta_helpers.hpp +54 -0
- data/vendor/datasketches-cpp/tuple/include/theta_intersection_base.hpp +59 -0
- data/vendor/datasketches-cpp/tuple/include/theta_intersection_base_impl.hpp +121 -0
- data/vendor/datasketches-cpp/tuple/include/theta_intersection_experimental.hpp +78 -0
- data/vendor/datasketches-cpp/tuple/include/theta_intersection_experimental_impl.hpp +43 -0
- data/vendor/datasketches-cpp/tuple/include/theta_set_difference_base.hpp +54 -0
- data/vendor/datasketches-cpp/tuple/include/theta_set_difference_base_impl.hpp +80 -0
- data/vendor/datasketches-cpp/tuple/include/theta_sketch_experimental.hpp +393 -0
- data/vendor/datasketches-cpp/tuple/include/theta_sketch_experimental_impl.hpp +481 -0
- data/vendor/datasketches-cpp/tuple/include/theta_union_base.hpp +60 -0
- data/vendor/datasketches-cpp/tuple/include/theta_union_base_impl.hpp +84 -0
- data/vendor/datasketches-cpp/tuple/include/theta_union_experimental.hpp +88 -0
- data/vendor/datasketches-cpp/tuple/include/theta_union_experimental_impl.hpp +47 -0
- data/vendor/datasketches-cpp/tuple/include/theta_update_sketch_base.hpp +259 -0
- data/vendor/datasketches-cpp/tuple/include/theta_update_sketch_base_impl.hpp +389 -0
- data/vendor/datasketches-cpp/tuple/include/tuple_a_not_b.hpp +57 -0
- data/vendor/datasketches-cpp/tuple/include/tuple_a_not_b_impl.hpp +33 -0
- data/vendor/datasketches-cpp/tuple/include/tuple_intersection.hpp +104 -0
- data/vendor/datasketches-cpp/tuple/include/tuple_intersection_impl.hpp +43 -0
- data/vendor/datasketches-cpp/tuple/include/tuple_sketch.hpp +496 -0
- data/vendor/datasketches-cpp/tuple/include/tuple_sketch_impl.hpp +587 -0
- data/vendor/datasketches-cpp/tuple/include/tuple_union.hpp +109 -0
- data/vendor/datasketches-cpp/tuple/include/tuple_union_impl.hpp +47 -0
- data/vendor/datasketches-cpp/tuple/test/CMakeLists.txt +53 -0
- data/vendor/datasketches-cpp/tuple/test/aod_1_compact_empty_from_java.sk +1 -0
- data/vendor/datasketches-cpp/tuple/test/aod_1_compact_estimation_from_java.sk +0 -0
- data/vendor/datasketches-cpp/tuple/test/aod_1_compact_non_empty_no_entries_from_java.sk +0 -0
- data/vendor/datasketches-cpp/tuple/test/aod_2_compact_exact_from_java.sk +0 -0
- data/vendor/datasketches-cpp/tuple/test/aod_3_compact_empty_from_java.sk +1 -0
- data/vendor/datasketches-cpp/tuple/test/array_of_doubles_sketch_test.cpp +298 -0
- data/vendor/datasketches-cpp/tuple/test/theta_a_not_b_experimental_test.cpp +250 -0
- data/vendor/datasketches-cpp/tuple/test/theta_compact_empty_from_java.sk +0 -0
- data/vendor/datasketches-cpp/tuple/test/theta_compact_estimation_from_java.sk +0 -0
- data/vendor/datasketches-cpp/tuple/test/theta_compact_single_item_from_java.sk +0 -0
- data/vendor/datasketches-cpp/tuple/test/theta_intersection_experimental_test.cpp +224 -0
- data/vendor/datasketches-cpp/tuple/test/theta_jaccard_similarity_test.cpp +144 -0
- data/vendor/datasketches-cpp/tuple/test/theta_sketch_experimental_test.cpp +247 -0
- data/vendor/datasketches-cpp/tuple/test/theta_union_experimental_test.cpp +44 -0
- data/vendor/datasketches-cpp/tuple/test/tuple_a_not_b_test.cpp +289 -0
- data/vendor/datasketches-cpp/tuple/test/tuple_intersection_test.cpp +235 -0
- data/vendor/datasketches-cpp/tuple/test/tuple_jaccard_similarity_test.cpp +98 -0
- data/vendor/datasketches-cpp/tuple/test/tuple_sketch_allocation_test.cpp +102 -0
- data/vendor/datasketches-cpp/tuple/test/tuple_sketch_test.cpp +249 -0
- data/vendor/datasketches-cpp/tuple/test/tuple_union_test.cpp +187 -0
- metadata +302 -0
@@ -0,0 +1,396 @@
|
|
1
|
+
{
|
2
|
+
"cells": [
|
3
|
+
{
|
4
|
+
"cell_type": "markdown",
|
5
|
+
"metadata": {},
|
6
|
+
"source": [
|
7
|
+
"## Theta Sketch Examples"
|
8
|
+
]
|
9
|
+
},
|
10
|
+
{
|
11
|
+
"cell_type": "markdown",
|
12
|
+
"metadata": {},
|
13
|
+
"source": [
|
14
|
+
"### Basic Sketch Usage"
|
15
|
+
]
|
16
|
+
},
|
17
|
+
{
|
18
|
+
"cell_type": "code",
|
19
|
+
"execution_count": 1,
|
20
|
+
"metadata": {},
|
21
|
+
"outputs": [],
|
22
|
+
"source": [
|
23
|
+
"from datasketches import theta_sketch, update_theta_sketch, compact_theta_sketch\n",
|
24
|
+
"from datasketches import theta_union, theta_intersection, theta_a_not_b"
|
25
|
+
]
|
26
|
+
},
|
27
|
+
{
|
28
|
+
"cell_type": "markdown",
|
29
|
+
"metadata": {},
|
30
|
+
"source": [
|
31
|
+
"To start, we'll create a sketch with 1 million points in order to demonstrate basic sketch operations."
|
32
|
+
]
|
33
|
+
},
|
34
|
+
{
|
35
|
+
"cell_type": "code",
|
36
|
+
"execution_count": 2,
|
37
|
+
"metadata": {},
|
38
|
+
"outputs": [
|
39
|
+
{
|
40
|
+
"name": "stdout",
|
41
|
+
"output_type": "stream",
|
42
|
+
"text": [
|
43
|
+
"### Update Theta sketch summary:\n",
|
44
|
+
" lg nominal size : 12\n",
|
45
|
+
" lg current size : 13\n",
|
46
|
+
" num retained keys : 6560\n",
|
47
|
+
" resize factor : 8\n",
|
48
|
+
" sampling probability : 1\n",
|
49
|
+
" seed hash : 37836\n",
|
50
|
+
" ordered? : false\n",
|
51
|
+
" theta (fraction) : 0.00654224\n",
|
52
|
+
" theta (raw 64-bit) : 60341508738660257\n",
|
53
|
+
" estimation mode? : true\n",
|
54
|
+
" estimate : 1.00271e+06\n",
|
55
|
+
" lower bound 95% conf : 978261\n",
|
56
|
+
" upper bound 95% conf : 1.02778e+06\n",
|
57
|
+
"### End sketch summary\n",
|
58
|
+
"\n"
|
59
|
+
]
|
60
|
+
}
|
61
|
+
],
|
62
|
+
"source": [
|
63
|
+
"n = 1000000\n",
|
64
|
+
"k = 12\n",
|
65
|
+
"sk1 = update_theta_sketch(k)\n",
|
66
|
+
"for i in range(0, n):\n",
|
67
|
+
" sk1.update(i)\n",
|
68
|
+
"print(sk1)"
|
69
|
+
]
|
70
|
+
},
|
71
|
+
{
|
72
|
+
"cell_type": "markdown",
|
73
|
+
"metadata": {},
|
74
|
+
"source": [
|
75
|
+
"The summary contains most data fo interest, but we can also query for specific information. And in this case, since we know the exact number of distinct items presented ot the sketch, we can look at the estimate, upper, and lower bounds as a percentage of the exact value."
|
76
|
+
]
|
77
|
+
},
|
78
|
+
{
|
79
|
+
"cell_type": "code",
|
80
|
+
"execution_count": 3,
|
81
|
+
"metadata": {},
|
82
|
+
"outputs": [
|
83
|
+
{
|
84
|
+
"name": "stdout",
|
85
|
+
"output_type": "stream",
|
86
|
+
"text": [
|
87
|
+
"Upper bound (1 std. dev) as % of true value:\t 101.5208\n",
|
88
|
+
"Sketch estimate as % of true value:\t\t 100.2715\n",
|
89
|
+
"Lower bound (1 std. dev) as % of true value:\t 99.0374\n"
|
90
|
+
]
|
91
|
+
}
|
92
|
+
],
|
93
|
+
"source": [
|
94
|
+
"print(\"Upper bound (1 std. dev) as % of true value:\\t\", round(100*sk1.get_upper_bound(1) / n, 4))\n",
|
95
|
+
"print(\"Sketch estimate as % of true value:\\t\\t\", round(100*sk1.get_estimate() / n, 4))\n",
|
96
|
+
"print(\"Lower bound (1 std. dev) as % of true value:\\t\", round(100*sk1.get_lower_bound(1) / n, 4))"
|
97
|
+
]
|
98
|
+
},
|
99
|
+
{
|
100
|
+
"cell_type": "markdown",
|
101
|
+
"metadata": {},
|
102
|
+
"source": [
|
103
|
+
"We can serialize and reconstruct the sketch. If we compact the sketch prior to serialization, we can still query the rebuilt sketch but cannot update it further."
|
104
|
+
]
|
105
|
+
},
|
106
|
+
{
|
107
|
+
"cell_type": "code",
|
108
|
+
"execution_count": 4,
|
109
|
+
"metadata": {},
|
110
|
+
"outputs": [
|
111
|
+
{
|
112
|
+
"data": {
|
113
|
+
"text/plain": [
|
114
|
+
"52504"
|
115
|
+
]
|
116
|
+
},
|
117
|
+
"execution_count": 4,
|
118
|
+
"metadata": {},
|
119
|
+
"output_type": "execute_result"
|
120
|
+
}
|
121
|
+
],
|
122
|
+
"source": [
|
123
|
+
"sk1_bytes = sk1.compact().serialize()\n",
|
124
|
+
"len(sk1_bytes)"
|
125
|
+
]
|
126
|
+
},
|
127
|
+
{
|
128
|
+
"cell_type": "code",
|
129
|
+
"execution_count": 5,
|
130
|
+
"metadata": {},
|
131
|
+
"outputs": [
|
132
|
+
{
|
133
|
+
"name": "stdout",
|
134
|
+
"output_type": "stream",
|
135
|
+
"text": [
|
136
|
+
"Estimate: \t\t 1002714.745231455\n",
|
137
|
+
"Estimation mode: \t True\n"
|
138
|
+
]
|
139
|
+
}
|
140
|
+
],
|
141
|
+
"source": [
|
142
|
+
"new_sk1 = theta_sketch.deserialize(sk1_bytes)\n",
|
143
|
+
"print(\"Estimate: \\t\\t\", new_sk1.get_estimate())\n",
|
144
|
+
"print(\"Estimation mode: \\t\", new_sk1.is_estimation_mode())"
|
145
|
+
]
|
146
|
+
},
|
147
|
+
{
|
148
|
+
"cell_type": "markdown",
|
149
|
+
"metadata": {},
|
150
|
+
"source": [
|
151
|
+
"### Sketch Unions"
|
152
|
+
]
|
153
|
+
},
|
154
|
+
{
|
155
|
+
"cell_type": "markdown",
|
156
|
+
"metadata": {},
|
157
|
+
"source": [
|
158
|
+
"Theta Sketch unions make use of a separate union object. The union will accept input sketches with different values of $k$.\n",
|
159
|
+
"\n",
|
160
|
+
"For this example, we will create a sketch with distinct values that partially overlap those in `sk1`."
|
161
|
+
]
|
162
|
+
},
|
163
|
+
{
|
164
|
+
"cell_type": "code",
|
165
|
+
"execution_count": 6,
|
166
|
+
"metadata": {},
|
167
|
+
"outputs": [
|
168
|
+
{
|
169
|
+
"name": "stdout",
|
170
|
+
"output_type": "stream",
|
171
|
+
"text": [
|
172
|
+
"### Update Theta sketch summary:\n",
|
173
|
+
" lg nominal size : 13\n",
|
174
|
+
" lg current size : 14\n",
|
175
|
+
" num retained keys : 12488\n",
|
176
|
+
" resize factor : 8\n",
|
177
|
+
" sampling probability : 1\n",
|
178
|
+
" seed hash : 37836\n",
|
179
|
+
" ordered? : false\n",
|
180
|
+
" theta (fraction) : 0.0123336\n",
|
181
|
+
" theta (raw 64-bit) : 113757656857900725\n",
|
182
|
+
" estimation mode? : true\n",
|
183
|
+
" estimate : 1.01252e+06\n",
|
184
|
+
" lower bound 95% conf : 994626\n",
|
185
|
+
" upper bound 95% conf : 1.03073e+06\n",
|
186
|
+
"### End sketch summary\n",
|
187
|
+
"\n"
|
188
|
+
]
|
189
|
+
}
|
190
|
+
],
|
191
|
+
"source": [
|
192
|
+
"offset = int(3 * n / 4)\n",
|
193
|
+
"sk2 = update_theta_sketch(k+1)\n",
|
194
|
+
"for i in range(0, n):\n",
|
195
|
+
" sk2.update(i + offset)\n",
|
196
|
+
"print(sk2)"
|
197
|
+
]
|
198
|
+
},
|
199
|
+
{
|
200
|
+
"cell_type": "markdown",
|
201
|
+
"metadata": {},
|
202
|
+
"source": [
|
203
|
+
"We can now feed the sketches into the union. As constructed, the exact number of unique values presented to the two sketches is $\\frac{7}{4}n$."
|
204
|
+
]
|
205
|
+
},
|
206
|
+
{
|
207
|
+
"cell_type": "code",
|
208
|
+
"execution_count": null,
|
209
|
+
"metadata": {},
|
210
|
+
"outputs": [],
|
211
|
+
"source": []
|
212
|
+
},
|
213
|
+
{
|
214
|
+
"cell_type": "code",
|
215
|
+
"execution_count": 7,
|
216
|
+
"metadata": {},
|
217
|
+
"outputs": [
|
218
|
+
{
|
219
|
+
"name": "stdout",
|
220
|
+
"output_type": "stream",
|
221
|
+
"text": [
|
222
|
+
"Union estimate as % of true value: 99.6787\n"
|
223
|
+
]
|
224
|
+
}
|
225
|
+
],
|
226
|
+
"source": [
|
227
|
+
"union = theta_union(k)\n",
|
228
|
+
"union.update(sk1)\n",
|
229
|
+
"union.update(sk2)\n",
|
230
|
+
"result = union.get_result()\n",
|
231
|
+
"print(\"Union estimate as % of true value: \", round(100*result.get_estimate()/(1.75*n), 4))"
|
232
|
+
]
|
233
|
+
},
|
234
|
+
{
|
235
|
+
"cell_type": "markdown",
|
236
|
+
"metadata": {},
|
237
|
+
"source": [
|
238
|
+
"### Sketch Intersections"
|
239
|
+
]
|
240
|
+
},
|
241
|
+
{
|
242
|
+
"cell_type": "markdown",
|
243
|
+
"metadata": {},
|
244
|
+
"source": [
|
245
|
+
"Beyond unions, theta sketches also support intersctions through the use of an intersection object. These set intersections can have vastly superior error bounds than the classic inclusion-exclusion rule used with sketches like HLL."
|
246
|
+
]
|
247
|
+
},
|
248
|
+
{
|
249
|
+
"cell_type": "code",
|
250
|
+
"execution_count": 8,
|
251
|
+
"metadata": {},
|
252
|
+
"outputs": [
|
253
|
+
{
|
254
|
+
"name": "stdout",
|
255
|
+
"output_type": "stream",
|
256
|
+
"text": [
|
257
|
+
"Has result: True\n",
|
258
|
+
"### Compact Theta sketch summary:\n",
|
259
|
+
" num retained keys : 1668\n",
|
260
|
+
" seed hash : 37836\n",
|
261
|
+
" ordered? : true\n",
|
262
|
+
" theta (fraction) : 0.00654224\n",
|
263
|
+
" theta (raw 64-bit) : 60341508738660257\n",
|
264
|
+
" estimation mode? : true\n",
|
265
|
+
" estimate : 254959\n",
|
266
|
+
" lower bound 95% conf : 242739\n",
|
267
|
+
" upper bound 95% conf : 267789\n",
|
268
|
+
"### End sketch summary\n",
|
269
|
+
"\n"
|
270
|
+
]
|
271
|
+
}
|
272
|
+
],
|
273
|
+
"source": [
|
274
|
+
"intersection = theta_intersection()\n",
|
275
|
+
"intersection.update(sk1)\n",
|
276
|
+
"intersection.update(sk2)\n",
|
277
|
+
"print(\"Has result: \", intersection.has_result())\n",
|
278
|
+
"result = intersection.get_result()\n",
|
279
|
+
"print(result)"
|
280
|
+
]
|
281
|
+
},
|
282
|
+
{
|
283
|
+
"cell_type": "markdown",
|
284
|
+
"metadata": {},
|
285
|
+
"source": [
|
286
|
+
"In this case, we expect the sets to have an overlap of $\\frac{1}{4}n$."
|
287
|
+
]
|
288
|
+
},
|
289
|
+
{
|
290
|
+
"cell_type": "code",
|
291
|
+
"execution_count": 9,
|
292
|
+
"metadata": {},
|
293
|
+
"outputs": [
|
294
|
+
{
|
295
|
+
"name": "stdout",
|
296
|
+
"output_type": "stream",
|
297
|
+
"text": [
|
298
|
+
"Intersection estimate as % of true value: 101.9834\n"
|
299
|
+
]
|
300
|
+
}
|
301
|
+
],
|
302
|
+
"source": [
|
303
|
+
"print(\"Intersection estimate as % of true value: \", round(100*result.get_estimate()/(0.25*n), 4))"
|
304
|
+
]
|
305
|
+
},
|
306
|
+
{
|
307
|
+
"cell_type": "markdown",
|
308
|
+
"metadata": {},
|
309
|
+
"source": [
|
310
|
+
"### Set Subtraction (A-not-B)"
|
311
|
+
]
|
312
|
+
},
|
313
|
+
{
|
314
|
+
"cell_type": "markdown",
|
315
|
+
"metadata": {},
|
316
|
+
"source": [
|
317
|
+
"Finally, we have the set subtraction operation. Unlike `theta_union` and `theta_intersection`, `theta_a_not_b` always takes as input 2 sketches at a time, namely $a$ and $b$, and directly returns the result as a sketch."
|
318
|
+
]
|
319
|
+
},
|
320
|
+
{
|
321
|
+
"cell_type": "code",
|
322
|
+
"execution_count": 10,
|
323
|
+
"metadata": {},
|
324
|
+
"outputs": [
|
325
|
+
{
|
326
|
+
"name": "stdout",
|
327
|
+
"output_type": "stream",
|
328
|
+
"text": [
|
329
|
+
"### Compact Theta sketch summary:\n",
|
330
|
+
" num retained keys : 4892\n",
|
331
|
+
" seed hash : 37836\n",
|
332
|
+
" ordered? : true\n",
|
333
|
+
" theta (fraction) : 0.00654224\n",
|
334
|
+
" theta (raw 64-bit) : 60341508738660257\n",
|
335
|
+
" estimation mode? : true\n",
|
336
|
+
" estimate : 747756\n",
|
337
|
+
" lower bound 95% conf : 726670\n",
|
338
|
+
" upper bound 95% conf : 769452\n",
|
339
|
+
"### End sketch summary\n",
|
340
|
+
"\n"
|
341
|
+
]
|
342
|
+
}
|
343
|
+
],
|
344
|
+
"source": [
|
345
|
+
"anb = theta_a_not_b()\n",
|
346
|
+
"result = anb.compute(sk1, sk2)\n",
|
347
|
+
"print(result)"
|
348
|
+
]
|
349
|
+
},
|
350
|
+
{
|
351
|
+
"cell_type": "markdown",
|
352
|
+
"metadata": {},
|
353
|
+
"source": [
|
354
|
+
"By using the same two sketches as before, the expected result here is $\\frac{3}{4}n$."
|
355
|
+
]
|
356
|
+
},
|
357
|
+
{
|
358
|
+
"cell_type": "code",
|
359
|
+
"execution_count": 11,
|
360
|
+
"metadata": {},
|
361
|
+
"outputs": [
|
362
|
+
{
|
363
|
+
"name": "stdout",
|
364
|
+
"output_type": "stream",
|
365
|
+
"text": [
|
366
|
+
"A-not-B estimate as % of true value: 99.7008\n"
|
367
|
+
]
|
368
|
+
}
|
369
|
+
],
|
370
|
+
"source": [
|
371
|
+
"print(\"A-not-B estimate as % of true value: \", round(100*result.get_estimate()/(0.75*n), 4))"
|
372
|
+
]
|
373
|
+
}
|
374
|
+
],
|
375
|
+
"metadata": {
|
376
|
+
"kernelspec": {
|
377
|
+
"display_name": "Python 3",
|
378
|
+
"language": "python",
|
379
|
+
"name": "python3"
|
380
|
+
},
|
381
|
+
"language_info": {
|
382
|
+
"codemirror_mode": {
|
383
|
+
"name": "ipython",
|
384
|
+
"version": 3
|
385
|
+
},
|
386
|
+
"file_extension": ".py",
|
387
|
+
"mimetype": "text/x-python",
|
388
|
+
"name": "python",
|
389
|
+
"nbconvert_exporter": "python",
|
390
|
+
"pygments_lexer": "ipython3",
|
391
|
+
"version": "3.7.0"
|
392
|
+
}
|
393
|
+
},
|
394
|
+
"nbformat": 4,
|
395
|
+
"nbformat_minor": 2
|
396
|
+
}
|
@@ -0,0 +1,90 @@
|
|
1
|
+
/*
|
2
|
+
* Licensed to the Apache Software Foundation (ASF) under one
|
3
|
+
* or more contributor license agreements. See the NOTICE file
|
4
|
+
* distributed with this work for additional information
|
5
|
+
* regarding copyright ownership. The ASF licenses this file
|
6
|
+
* to you under the Apache License, Version 2.0 (the
|
7
|
+
* "License"); you may not use this file except in compliance
|
8
|
+
* with the License. You may obtain a copy of the License at
|
9
|
+
*
|
10
|
+
* http://www.apache.org/licenses/LICENSE-2.0
|
11
|
+
*
|
12
|
+
* Unless required by applicable law or agreed to in writing,
|
13
|
+
* software distributed under the License is distributed on an
|
14
|
+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
15
|
+
* KIND, either express or implied. See the License for the
|
16
|
+
* specific language governing permissions and limitations
|
17
|
+
* under the License.
|
18
|
+
*/
|
19
|
+
|
20
|
+
#include <sstream>
|
21
|
+
#include <pybind11/pybind11.h>
|
22
|
+
|
23
|
+
#include "cpc_sketch.hpp"
|
24
|
+
#include "cpc_union.hpp"
|
25
|
+
#include "cpc_common.hpp"
|
26
|
+
#include "common_defs.hpp"
|
27
|
+
|
28
|
+
namespace py = pybind11;
|
29
|
+
|
30
|
+
namespace datasketches {
|
31
|
+
namespace python {
|
32
|
+
|
33
|
+
cpc_sketch* cpc_sketch_deserialize(py::bytes skBytes) {
|
34
|
+
std::string skStr = skBytes; // implicit cast
|
35
|
+
return new cpc_sketch(cpc_sketch::deserialize(skStr.c_str(), skStr.length()));
|
36
|
+
}
|
37
|
+
|
38
|
+
py::object cpc_sketch_serialize(const cpc_sketch& sk) {
|
39
|
+
auto serResult = sk.serialize();
|
40
|
+
return py::bytes((char*)serResult.data(), serResult.size());
|
41
|
+
}
|
42
|
+
|
43
|
+
cpc_sketch* cpc_union_get_result(const cpc_union& u) {
|
44
|
+
return new cpc_sketch(u.get_result());
|
45
|
+
}
|
46
|
+
|
47
|
+
}
|
48
|
+
}
|
49
|
+
|
50
|
+
namespace dspy = datasketches::python;
|
51
|
+
|
52
|
+
void init_cpc(py::module &m) {
|
53
|
+
using namespace datasketches;
|
54
|
+
|
55
|
+
py::class_<cpc_sketch>(m, "cpc_sketch")
|
56
|
+
.def(py::init<uint8_t, uint64_t>(), py::arg("lg_k")=CPC_DEFAULT_LG_K, py::arg("seed")=DEFAULT_SEED)
|
57
|
+
.def(py::init<const cpc_sketch&>())
|
58
|
+
.def("__str__", &cpc_sketch::to_string,
|
59
|
+
"Produces a string summary of the sketch")
|
60
|
+
.def("to_string", &cpc_sketch::to_string,
|
61
|
+
"Produces a string summary of the sketch")
|
62
|
+
.def("serialize", &dspy::cpc_sketch_serialize,
|
63
|
+
"Serializes the sketch into a bytes object")
|
64
|
+
.def_static("deserialize", &dspy::cpc_sketch_deserialize,
|
65
|
+
"Reads a bytes object and returns the corresponding cpc_sketch")
|
66
|
+
.def<void (cpc_sketch::*)(uint64_t)>("update", &cpc_sketch::update, py::arg("datum"),
|
67
|
+
"Updates the sketch with the given 64-bit integer value")
|
68
|
+
.def<void (cpc_sketch::*)(double)>("update", &cpc_sketch::update, py::arg("datum"),
|
69
|
+
"Updates the sketch with the given 64-bit floating point")
|
70
|
+
.def<void (cpc_sketch::*)(const std::string&)>("update", &cpc_sketch::update, py::arg("datum"),
|
71
|
+
"Updates the sketch with the given string")
|
72
|
+
.def("is_empty", &cpc_sketch::is_empty,
|
73
|
+
"Returns True if the sketch is empty, otherwise Dalse")
|
74
|
+
.def("get_estimate", &cpc_sketch::get_estimate,
|
75
|
+
"Estimate of the distinct count of the input stream")
|
76
|
+
.def("get_lower_bound", &cpc_sketch::get_lower_bound, py::arg("kappa"),
|
77
|
+
"Returns an approximate lower bound on the estimate for kappa values in {1, 2, 3}, roughly corresponding to standard deviations")
|
78
|
+
.def("get_upper_bound", &cpc_sketch::get_upper_bound, py::arg("kappa"),
|
79
|
+
"Returns an approximate upper bound on the estimate for kappa values in {1, 2, 3}, roughly corresponding to standard deviations")
|
80
|
+
;
|
81
|
+
|
82
|
+
py::class_<cpc_union>(m, "cpc_union")
|
83
|
+
.def(py::init<uint8_t, uint64_t>(), py::arg("lg_k"), py::arg("seed")=DEFAULT_SEED)
|
84
|
+
.def(py::init<const cpc_union&>())
|
85
|
+
.def("update", (void (cpc_union::*)(const cpc_sketch&)) &cpc_union::update, py::arg("sketch"),
|
86
|
+
"Updates the union with the provided CPC sketch")
|
87
|
+
.def("get_result", &dspy::cpc_union_get_result,
|
88
|
+
"Returns a CPC sketch with the result of the union")
|
89
|
+
;
|
90
|
+
}
|