pub struct CompactArrayOfDoublesSketch { /* private fields */ }Expand description
An immutable, serializable snapshot of an ArrayOfDoubles Tuple sketch.
Produced by super::ArrayOfDoublesSketch::compact, by any set
operation’s result, or by Self::deserialize.
Implementations§
Source§impl CompactArrayOfDoublesSketch
impl CompactArrayOfDoublesSketch
Sourcepub fn deserialize(bytes: &[u8]) -> Result<Self, SketchError>
pub fn deserialize(bytes: &[u8]) -> Result<Self, SketchError>
Deserializes bytes produced by Self::serialize. Returns
SketchError::Deserialization if the bytes are truncated, corrupt,
or not an ArrayOfDoubles sketch.
Examples found in repository?
14fn main() {
15 // Two sketches of user IDs, each carrying [sessions, revenue] per user.
16 let mut day1 = ArrayOfDoublesSketchBuilder::new()
17 .lg_k(12)
18 .num_values(2)
19 .build()
20 .unwrap();
21 for id in 0..10_000u64 {
22 day1.update_u64(id, &[1.0, 2.50]).unwrap();
23 }
24
25 let mut day2 = ArrayOfDoublesSketchBuilder::new()
26 .lg_k(12)
27 .num_values(2)
28 .build()
29 .unwrap();
30 for id in 5_000..15_000u64 {
31 day2.update_u64(id, &[1.0, 4.00]).unwrap();
32 }
33
34 println!("Day 1 unique users (estimate): {:.0}", day1.get_estimate());
35 println!("Day 2 unique users (estimate): {:.0}", day2.get_estimate());
36 println!("Values per entry: {}", day1.get_num_values());
37
38 // Union: unique users across both days, with per-user values summed for
39 // anyone who appeared on both.
40 let mut union = ArrayOfDoublesUnionBuilder::new()
41 .lg_k(12)
42 .num_values(2)
43 .build()
44 .unwrap();
45 union.update(&day1).unwrap();
46 union.update(&day2).unwrap();
47 let combined = union.get_result(true);
48 println!(
49 "Total unique users (union estimate): {:.0}",
50 combined.get_estimate()
51 );
52
53 // Per-entry access is what distinguishes Tuple sketches from HLL/Theta/CPC:
54 // scale the retained sample's revenue back up by 1/theta to estimate the
55 // full population total.
56 let retained_revenue: f64 = combined.entries().map(|(_, values)| values[1]).sum();
57 println!(
58 "Estimated total revenue: {:.2} (from {} retained entries, theta = {:.4})",
59 retained_revenue / combined.get_theta(),
60 combined.get_num_retained(),
61 combined.get_theta()
62 );
63
64 // Intersection: users who came back on day 2.
65 let mut intersection = ArrayOfDoublesIntersection::new(2).unwrap();
66 intersection.update(&day1).unwrap();
67 intersection.update(&day2).unwrap();
68 match intersection.get_result(true) {
69 Ok(returning) => println!(
70 "Returning users (intersection estimate): {:.0}",
71 returning.get_estimate()
72 ),
73 Err(e) => println!("No intersection result: {e}"),
74 }
75
76 // A-not-b: users who only came on day 1.
77 let a_not_b = ArrayOfDoublesAnotB::new();
78 let day1_only = a_not_b.compute(&day1, &day2, true).unwrap();
79 println!(
80 "Day-1-only users (a-not-b estimate): {:.0}",
81 day1_only.get_estimate()
82 );
83
84 // Jaccard similarity of the two days' audiences.
85 let similarity = array_of_doubles_jaccard_similarity(&day1, &day2).unwrap();
86 println!(
87 "Jaccard similarity: {:.3} (range [{:.3}, {:.3}])",
88 similarity.estimate, similarity.lower_bound, similarity.upper_bound
89 );
90
91 // Serialize a compact sketch for storage/transmission, then restore it.
92 let compact = day1.compact(true);
93 let bytes = compact.serialize();
94 println!("Serialized day-1 sketch: {} bytes", bytes.len());
95 let restored = CompactArrayOfDoublesSketch::deserialize(&bytes).unwrap();
96 println!(
97 "Restored estimate: {:.0} ({} values per entry)",
98 restored.get_estimate(),
99 restored.get_num_values()
100 );
101}Sourcepub fn serialize(&self) -> Vec<u8> ⓘ
pub fn serialize(&self) -> Vec<u8> ⓘ
Serializes this sketch. Unlike Theta, this family has exactly one
serialization format upstream — there is no compressed variant — and
no ordered parameter: orderedness is fixed when the snapshot was
created (e.g. via
ArrayOfDoublesSketch::compact).
Examples found in repository?
14fn main() {
15 // Two sketches of user IDs, each carrying [sessions, revenue] per user.
16 let mut day1 = ArrayOfDoublesSketchBuilder::new()
17 .lg_k(12)
18 .num_values(2)
19 .build()
20 .unwrap();
21 for id in 0..10_000u64 {
22 day1.update_u64(id, &[1.0, 2.50]).unwrap();
23 }
24
25 let mut day2 = ArrayOfDoublesSketchBuilder::new()
26 .lg_k(12)
27 .num_values(2)
28 .build()
29 .unwrap();
30 for id in 5_000..15_000u64 {
31 day2.update_u64(id, &[1.0, 4.00]).unwrap();
32 }
33
34 println!("Day 1 unique users (estimate): {:.0}", day1.get_estimate());
35 println!("Day 2 unique users (estimate): {:.0}", day2.get_estimate());
36 println!("Values per entry: {}", day1.get_num_values());
37
38 // Union: unique users across both days, with per-user values summed for
39 // anyone who appeared on both.
40 let mut union = ArrayOfDoublesUnionBuilder::new()
41 .lg_k(12)
42 .num_values(2)
43 .build()
44 .unwrap();
45 union.update(&day1).unwrap();
46 union.update(&day2).unwrap();
47 let combined = union.get_result(true);
48 println!(
49 "Total unique users (union estimate): {:.0}",
50 combined.get_estimate()
51 );
52
53 // Per-entry access is what distinguishes Tuple sketches from HLL/Theta/CPC:
54 // scale the retained sample's revenue back up by 1/theta to estimate the
55 // full population total.
56 let retained_revenue: f64 = combined.entries().map(|(_, values)| values[1]).sum();
57 println!(
58 "Estimated total revenue: {:.2} (from {} retained entries, theta = {:.4})",
59 retained_revenue / combined.get_theta(),
60 combined.get_num_retained(),
61 combined.get_theta()
62 );
63
64 // Intersection: users who came back on day 2.
65 let mut intersection = ArrayOfDoublesIntersection::new(2).unwrap();
66 intersection.update(&day1).unwrap();
67 intersection.update(&day2).unwrap();
68 match intersection.get_result(true) {
69 Ok(returning) => println!(
70 "Returning users (intersection estimate): {:.0}",
71 returning.get_estimate()
72 ),
73 Err(e) => println!("No intersection result: {e}"),
74 }
75
76 // A-not-b: users who only came on day 1.
77 let a_not_b = ArrayOfDoublesAnotB::new();
78 let day1_only = a_not_b.compute(&day1, &day2, true).unwrap();
79 println!(
80 "Day-1-only users (a-not-b estimate): {:.0}",
81 day1_only.get_estimate()
82 );
83
84 // Jaccard similarity of the two days' audiences.
85 let similarity = array_of_doubles_jaccard_similarity(&day1, &day2).unwrap();
86 println!(
87 "Jaccard similarity: {:.3} (range [{:.3}, {:.3}])",
88 similarity.estimate, similarity.lower_bound, similarity.upper_bound
89 );
90
91 // Serialize a compact sketch for storage/transmission, then restore it.
92 let compact = day1.compact(true);
93 let bytes = compact.serialize();
94 println!("Serialized day-1 sketch: {} bytes", bytes.len());
95 let restored = CompactArrayOfDoublesSketch::deserialize(&bytes).unwrap();
96 println!(
97 "Restored estimate: {:.0} ({} values per entry)",
98 restored.get_estimate(),
99 restored.get_num_values()
100 );
101}Sourcepub fn get_estimate(&self) -> f64
pub fn get_estimate(&self) -> f64
Returns the current estimate of the number of distinct keys in this sketch.
Examples found in repository?
14fn main() {
15 // Two sketches of user IDs, each carrying [sessions, revenue] per user.
16 let mut day1 = ArrayOfDoublesSketchBuilder::new()
17 .lg_k(12)
18 .num_values(2)
19 .build()
20 .unwrap();
21 for id in 0..10_000u64 {
22 day1.update_u64(id, &[1.0, 2.50]).unwrap();
23 }
24
25 let mut day2 = ArrayOfDoublesSketchBuilder::new()
26 .lg_k(12)
27 .num_values(2)
28 .build()
29 .unwrap();
30 for id in 5_000..15_000u64 {
31 day2.update_u64(id, &[1.0, 4.00]).unwrap();
32 }
33
34 println!("Day 1 unique users (estimate): {:.0}", day1.get_estimate());
35 println!("Day 2 unique users (estimate): {:.0}", day2.get_estimate());
36 println!("Values per entry: {}", day1.get_num_values());
37
38 // Union: unique users across both days, with per-user values summed for
39 // anyone who appeared on both.
40 let mut union = ArrayOfDoublesUnionBuilder::new()
41 .lg_k(12)
42 .num_values(2)
43 .build()
44 .unwrap();
45 union.update(&day1).unwrap();
46 union.update(&day2).unwrap();
47 let combined = union.get_result(true);
48 println!(
49 "Total unique users (union estimate): {:.0}",
50 combined.get_estimate()
51 );
52
53 // Per-entry access is what distinguishes Tuple sketches from HLL/Theta/CPC:
54 // scale the retained sample's revenue back up by 1/theta to estimate the
55 // full population total.
56 let retained_revenue: f64 = combined.entries().map(|(_, values)| values[1]).sum();
57 println!(
58 "Estimated total revenue: {:.2} (from {} retained entries, theta = {:.4})",
59 retained_revenue / combined.get_theta(),
60 combined.get_num_retained(),
61 combined.get_theta()
62 );
63
64 // Intersection: users who came back on day 2.
65 let mut intersection = ArrayOfDoublesIntersection::new(2).unwrap();
66 intersection.update(&day1).unwrap();
67 intersection.update(&day2).unwrap();
68 match intersection.get_result(true) {
69 Ok(returning) => println!(
70 "Returning users (intersection estimate): {:.0}",
71 returning.get_estimate()
72 ),
73 Err(e) => println!("No intersection result: {e}"),
74 }
75
76 // A-not-b: users who only came on day 1.
77 let a_not_b = ArrayOfDoublesAnotB::new();
78 let day1_only = a_not_b.compute(&day1, &day2, true).unwrap();
79 println!(
80 "Day-1-only users (a-not-b estimate): {:.0}",
81 day1_only.get_estimate()
82 );
83
84 // Jaccard similarity of the two days' audiences.
85 let similarity = array_of_doubles_jaccard_similarity(&day1, &day2).unwrap();
86 println!(
87 "Jaccard similarity: {:.3} (range [{:.3}, {:.3}])",
88 similarity.estimate, similarity.lower_bound, similarity.upper_bound
89 );
90
91 // Serialize a compact sketch for storage/transmission, then restore it.
92 let compact = day1.compact(true);
93 let bytes = compact.serialize();
94 println!("Serialized day-1 sketch: {} bytes", bytes.len());
95 let restored = CompactArrayOfDoublesSketch::deserialize(&bytes).unwrap();
96 println!(
97 "Restored estimate: {:.0} ({} values per entry)",
98 restored.get_estimate(),
99 restored.get_num_values()
100 );
101}Sourcepub fn get_lower_bound(&self, num_std_dev: u8) -> Result<f64, SketchError>
pub fn get_lower_bound(&self, num_std_dev: u8) -> Result<f64, SketchError>
Returns the lower bound of the confidence interval around
Self::get_estimate. See
ArrayOfDoublesSketch::get_lower_bound
for the meaning of num_std_dev.
Sourcepub fn get_upper_bound(&self, num_std_dev: u8) -> Result<f64, SketchError>
pub fn get_upper_bound(&self, num_std_dev: u8) -> Result<f64, SketchError>
Returns the upper bound of the confidence interval around
Self::get_estimate. See
ArrayOfDoublesSketch::get_lower_bound
for the meaning of num_std_dev.
Sourcepub fn is_estimation_mode(&self) -> bool
pub fn is_estimation_mode(&self) -> bool
Returns true if this sketch’s theta threshold is below 1.0
(i.e. Self::get_estimate is a statistical estimate rather than an
exact count).
Sourcepub fn is_ordered(&self) -> bool
pub fn is_ordered(&self) -> bool
Returns true if this sketch’s retained entries are sorted by hash
value.
Sourcepub fn get_theta(&self) -> f64
pub fn get_theta(&self) -> f64
Returns the current theta threshold (1.0 if not in estimation mode).
Examples found in repository?
14fn main() {
15 // Two sketches of user IDs, each carrying [sessions, revenue] per user.
16 let mut day1 = ArrayOfDoublesSketchBuilder::new()
17 .lg_k(12)
18 .num_values(2)
19 .build()
20 .unwrap();
21 for id in 0..10_000u64 {
22 day1.update_u64(id, &[1.0, 2.50]).unwrap();
23 }
24
25 let mut day2 = ArrayOfDoublesSketchBuilder::new()
26 .lg_k(12)
27 .num_values(2)
28 .build()
29 .unwrap();
30 for id in 5_000..15_000u64 {
31 day2.update_u64(id, &[1.0, 4.00]).unwrap();
32 }
33
34 println!("Day 1 unique users (estimate): {:.0}", day1.get_estimate());
35 println!("Day 2 unique users (estimate): {:.0}", day2.get_estimate());
36 println!("Values per entry: {}", day1.get_num_values());
37
38 // Union: unique users across both days, with per-user values summed for
39 // anyone who appeared on both.
40 let mut union = ArrayOfDoublesUnionBuilder::new()
41 .lg_k(12)
42 .num_values(2)
43 .build()
44 .unwrap();
45 union.update(&day1).unwrap();
46 union.update(&day2).unwrap();
47 let combined = union.get_result(true);
48 println!(
49 "Total unique users (union estimate): {:.0}",
50 combined.get_estimate()
51 );
52
53 // Per-entry access is what distinguishes Tuple sketches from HLL/Theta/CPC:
54 // scale the retained sample's revenue back up by 1/theta to estimate the
55 // full population total.
56 let retained_revenue: f64 = combined.entries().map(|(_, values)| values[1]).sum();
57 println!(
58 "Estimated total revenue: {:.2} (from {} retained entries, theta = {:.4})",
59 retained_revenue / combined.get_theta(),
60 combined.get_num_retained(),
61 combined.get_theta()
62 );
63
64 // Intersection: users who came back on day 2.
65 let mut intersection = ArrayOfDoublesIntersection::new(2).unwrap();
66 intersection.update(&day1).unwrap();
67 intersection.update(&day2).unwrap();
68 match intersection.get_result(true) {
69 Ok(returning) => println!(
70 "Returning users (intersection estimate): {:.0}",
71 returning.get_estimate()
72 ),
73 Err(e) => println!("No intersection result: {e}"),
74 }
75
76 // A-not-b: users who only came on day 1.
77 let a_not_b = ArrayOfDoublesAnotB::new();
78 let day1_only = a_not_b.compute(&day1, &day2, true).unwrap();
79 println!(
80 "Day-1-only users (a-not-b estimate): {:.0}",
81 day1_only.get_estimate()
82 );
83
84 // Jaccard similarity of the two days' audiences.
85 let similarity = array_of_doubles_jaccard_similarity(&day1, &day2).unwrap();
86 println!(
87 "Jaccard similarity: {:.3} (range [{:.3}, {:.3}])",
88 similarity.estimate, similarity.lower_bound, similarity.upper_bound
89 );
90
91 // Serialize a compact sketch for storage/transmission, then restore it.
92 let compact = day1.compact(true);
93 let bytes = compact.serialize();
94 println!("Serialized day-1 sketch: {} bytes", bytes.len());
95 let restored = CompactArrayOfDoublesSketch::deserialize(&bytes).unwrap();
96 println!(
97 "Restored estimate: {:.0} ({} values per entry)",
98 restored.get_estimate(),
99 restored.get_num_values()
100 );
101}Sourcepub fn get_num_retained(&self) -> u32
pub fn get_num_retained(&self) -> u32
Returns the number of entries retained by this sketch.
Examples found in repository?
14fn main() {
15 // Two sketches of user IDs, each carrying [sessions, revenue] per user.
16 let mut day1 = ArrayOfDoublesSketchBuilder::new()
17 .lg_k(12)
18 .num_values(2)
19 .build()
20 .unwrap();
21 for id in 0..10_000u64 {
22 day1.update_u64(id, &[1.0, 2.50]).unwrap();
23 }
24
25 let mut day2 = ArrayOfDoublesSketchBuilder::new()
26 .lg_k(12)
27 .num_values(2)
28 .build()
29 .unwrap();
30 for id in 5_000..15_000u64 {
31 day2.update_u64(id, &[1.0, 4.00]).unwrap();
32 }
33
34 println!("Day 1 unique users (estimate): {:.0}", day1.get_estimate());
35 println!("Day 2 unique users (estimate): {:.0}", day2.get_estimate());
36 println!("Values per entry: {}", day1.get_num_values());
37
38 // Union: unique users across both days, with per-user values summed for
39 // anyone who appeared on both.
40 let mut union = ArrayOfDoublesUnionBuilder::new()
41 .lg_k(12)
42 .num_values(2)
43 .build()
44 .unwrap();
45 union.update(&day1).unwrap();
46 union.update(&day2).unwrap();
47 let combined = union.get_result(true);
48 println!(
49 "Total unique users (union estimate): {:.0}",
50 combined.get_estimate()
51 );
52
53 // Per-entry access is what distinguishes Tuple sketches from HLL/Theta/CPC:
54 // scale the retained sample's revenue back up by 1/theta to estimate the
55 // full population total.
56 let retained_revenue: f64 = combined.entries().map(|(_, values)| values[1]).sum();
57 println!(
58 "Estimated total revenue: {:.2} (from {} retained entries, theta = {:.4})",
59 retained_revenue / combined.get_theta(),
60 combined.get_num_retained(),
61 combined.get_theta()
62 );
63
64 // Intersection: users who came back on day 2.
65 let mut intersection = ArrayOfDoublesIntersection::new(2).unwrap();
66 intersection.update(&day1).unwrap();
67 intersection.update(&day2).unwrap();
68 match intersection.get_result(true) {
69 Ok(returning) => println!(
70 "Returning users (intersection estimate): {:.0}",
71 returning.get_estimate()
72 ),
73 Err(e) => println!("No intersection result: {e}"),
74 }
75
76 // A-not-b: users who only came on day 1.
77 let a_not_b = ArrayOfDoublesAnotB::new();
78 let day1_only = a_not_b.compute(&day1, &day2, true).unwrap();
79 println!(
80 "Day-1-only users (a-not-b estimate): {:.0}",
81 day1_only.get_estimate()
82 );
83
84 // Jaccard similarity of the two days' audiences.
85 let similarity = array_of_doubles_jaccard_similarity(&day1, &day2).unwrap();
86 println!(
87 "Jaccard similarity: {:.3} (range [{:.3}, {:.3}])",
88 similarity.estimate, similarity.lower_bound, similarity.upper_bound
89 );
90
91 // Serialize a compact sketch for storage/transmission, then restore it.
92 let compact = day1.compact(true);
93 let bytes = compact.serialize();
94 println!("Serialized day-1 sketch: {} bytes", bytes.len());
95 let restored = CompactArrayOfDoublesSketch::deserialize(&bytes).unwrap();
96 println!(
97 "Restored estimate: {:.0} ({} values per entry)",
98 restored.get_estimate(),
99 restored.get_num_values()
100 );
101}Sourcepub fn get_num_values(&self) -> u8
pub fn get_num_values(&self) -> u8
Returns the fixed number of f64 values each retained entry carries.
Examples found in repository?
14fn main() {
15 // Two sketches of user IDs, each carrying [sessions, revenue] per user.
16 let mut day1 = ArrayOfDoublesSketchBuilder::new()
17 .lg_k(12)
18 .num_values(2)
19 .build()
20 .unwrap();
21 for id in 0..10_000u64 {
22 day1.update_u64(id, &[1.0, 2.50]).unwrap();
23 }
24
25 let mut day2 = ArrayOfDoublesSketchBuilder::new()
26 .lg_k(12)
27 .num_values(2)
28 .build()
29 .unwrap();
30 for id in 5_000..15_000u64 {
31 day2.update_u64(id, &[1.0, 4.00]).unwrap();
32 }
33
34 println!("Day 1 unique users (estimate): {:.0}", day1.get_estimate());
35 println!("Day 2 unique users (estimate): {:.0}", day2.get_estimate());
36 println!("Values per entry: {}", day1.get_num_values());
37
38 // Union: unique users across both days, with per-user values summed for
39 // anyone who appeared on both.
40 let mut union = ArrayOfDoublesUnionBuilder::new()
41 .lg_k(12)
42 .num_values(2)
43 .build()
44 .unwrap();
45 union.update(&day1).unwrap();
46 union.update(&day2).unwrap();
47 let combined = union.get_result(true);
48 println!(
49 "Total unique users (union estimate): {:.0}",
50 combined.get_estimate()
51 );
52
53 // Per-entry access is what distinguishes Tuple sketches from HLL/Theta/CPC:
54 // scale the retained sample's revenue back up by 1/theta to estimate the
55 // full population total.
56 let retained_revenue: f64 = combined.entries().map(|(_, values)| values[1]).sum();
57 println!(
58 "Estimated total revenue: {:.2} (from {} retained entries, theta = {:.4})",
59 retained_revenue / combined.get_theta(),
60 combined.get_num_retained(),
61 combined.get_theta()
62 );
63
64 // Intersection: users who came back on day 2.
65 let mut intersection = ArrayOfDoublesIntersection::new(2).unwrap();
66 intersection.update(&day1).unwrap();
67 intersection.update(&day2).unwrap();
68 match intersection.get_result(true) {
69 Ok(returning) => println!(
70 "Returning users (intersection estimate): {:.0}",
71 returning.get_estimate()
72 ),
73 Err(e) => println!("No intersection result: {e}"),
74 }
75
76 // A-not-b: users who only came on day 1.
77 let a_not_b = ArrayOfDoublesAnotB::new();
78 let day1_only = a_not_b.compute(&day1, &day2, true).unwrap();
79 println!(
80 "Day-1-only users (a-not-b estimate): {:.0}",
81 day1_only.get_estimate()
82 );
83
84 // Jaccard similarity of the two days' audiences.
85 let similarity = array_of_doubles_jaccard_similarity(&day1, &day2).unwrap();
86 println!(
87 "Jaccard similarity: {:.3} (range [{:.3}, {:.3}])",
88 similarity.estimate, similarity.lower_bound, similarity.upper_bound
89 );
90
91 // Serialize a compact sketch for storage/transmission, then restore it.
92 let compact = day1.compact(true);
93 let bytes = compact.serialize();
94 println!("Serialized day-1 sketch: {} bytes", bytes.len());
95 let restored = CompactArrayOfDoublesSketch::deserialize(&bytes).unwrap();
96 println!(
97 "Restored estimate: {:.0} ({} values per entry)",
98 restored.get_estimate(),
99 restored.get_num_values()
100 );
101}Sourcepub fn entries(&self) -> impl Iterator<Item = (u64, Vec<f64>)>
pub fn entries(&self) -> impl Iterator<Item = (u64, Vec<f64>)>
Iterates the retained entries as (hash, values) pairs, where
values.len() == self.get_num_values(). Ordered by hash if
Self::is_ordered is true.
The entries are copied out of C++ in two FFI calls up front (cxx
cannot hand back a live C++ iterator), so each item owns its Vec
rather than borrowing from the sketch.
Examples found in repository?
14fn main() {
15 // Two sketches of user IDs, each carrying [sessions, revenue] per user.
16 let mut day1 = ArrayOfDoublesSketchBuilder::new()
17 .lg_k(12)
18 .num_values(2)
19 .build()
20 .unwrap();
21 for id in 0..10_000u64 {
22 day1.update_u64(id, &[1.0, 2.50]).unwrap();
23 }
24
25 let mut day2 = ArrayOfDoublesSketchBuilder::new()
26 .lg_k(12)
27 .num_values(2)
28 .build()
29 .unwrap();
30 for id in 5_000..15_000u64 {
31 day2.update_u64(id, &[1.0, 4.00]).unwrap();
32 }
33
34 println!("Day 1 unique users (estimate): {:.0}", day1.get_estimate());
35 println!("Day 2 unique users (estimate): {:.0}", day2.get_estimate());
36 println!("Values per entry: {}", day1.get_num_values());
37
38 // Union: unique users across both days, with per-user values summed for
39 // anyone who appeared on both.
40 let mut union = ArrayOfDoublesUnionBuilder::new()
41 .lg_k(12)
42 .num_values(2)
43 .build()
44 .unwrap();
45 union.update(&day1).unwrap();
46 union.update(&day2).unwrap();
47 let combined = union.get_result(true);
48 println!(
49 "Total unique users (union estimate): {:.0}",
50 combined.get_estimate()
51 );
52
53 // Per-entry access is what distinguishes Tuple sketches from HLL/Theta/CPC:
54 // scale the retained sample's revenue back up by 1/theta to estimate the
55 // full population total.
56 let retained_revenue: f64 = combined.entries().map(|(_, values)| values[1]).sum();
57 println!(
58 "Estimated total revenue: {:.2} (from {} retained entries, theta = {:.4})",
59 retained_revenue / combined.get_theta(),
60 combined.get_num_retained(),
61 combined.get_theta()
62 );
63
64 // Intersection: users who came back on day 2.
65 let mut intersection = ArrayOfDoublesIntersection::new(2).unwrap();
66 intersection.update(&day1).unwrap();
67 intersection.update(&day2).unwrap();
68 match intersection.get_result(true) {
69 Ok(returning) => println!(
70 "Returning users (intersection estimate): {:.0}",
71 returning.get_estimate()
72 ),
73 Err(e) => println!("No intersection result: {e}"),
74 }
75
76 // A-not-b: users who only came on day 1.
77 let a_not_b = ArrayOfDoublesAnotB::new();
78 let day1_only = a_not_b.compute(&day1, &day2, true).unwrap();
79 println!(
80 "Day-1-only users (a-not-b estimate): {:.0}",
81 day1_only.get_estimate()
82 );
83
84 // Jaccard similarity of the two days' audiences.
85 let similarity = array_of_doubles_jaccard_similarity(&day1, &day2).unwrap();
86 println!(
87 "Jaccard similarity: {:.3} (range [{:.3}, {:.3}])",
88 similarity.estimate, similarity.lower_bound, similarity.upper_bound
89 );
90
91 // Serialize a compact sketch for storage/transmission, then restore it.
92 let compact = day1.compact(true);
93 let bytes = compact.serialize();
94 println!("Serialized day-1 sketch: {} bytes", bytes.len());
95 let restored = CompactArrayOfDoublesSketch::deserialize(&bytes).unwrap();
96 println!(
97 "Restored estimate: {:.0} ({} values per entry)",
98 restored.get_estimate(),
99 restored.get_num_values()
100 );
101}Trait Implementations§
Source§impl ArrayOfDoublesInput for CompactArrayOfDoublesSketch
impl ArrayOfDoublesInput for CompactArrayOfDoublesSketch
Source§fn get_num_values(&self) -> u8
fn get_num_values(&self) -> u8
f64 values each of this sketch’s retained
entries carries. Set operations require all operands to agree.