diff --git a/datasketches/src/theta/jaccard_similarity.rs b/datasketches/src/theta/jaccard_similarity.rs new file mode 100644 index 0000000..0c1937f --- /dev/null +++ b/datasketches/src/theta/jaccard_similarity.rs @@ -0,0 +1,75 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +//! Jaccard similarity for Theta sketches. + +use crate::error::Error; +use crate::hash::DEFAULT_UPDATE_SEED; +use crate::theta::ThetaSketchView; +use crate::thetacommon::jaccard_similarity::JaccardSimilarity; +use crate::thetacommon::jaccard_similarity::JaccardSimilarityOperator; + +/// Jaccard similarity operator for Theta sketches. +/// +/// This is a stateless operator other than its configured hash seed. The returned +/// [`JaccardSimilarity`] contains the estimate and its 95.4% confidence interval. +/// +/// # Examples +/// +/// ``` +/// # use datasketches::theta::{ThetaJaccardSimilarity, ThetaSketchBuilder}; +/// let mut a = ThetaSketchBuilder::default().build(); +/// let mut b = ThetaSketchBuilder::default().build(); +/// a.update("apple"); +/// b.update("apple"); +/// +/// let result = ThetaJaccardSimilarity::default().compute(&a, &b).unwrap(); +/// assert_eq!(result.estimate(), 1.0); +/// ``` +#[derive(Clone, Copy, Debug)] +pub struct ThetaJaccardSimilarity { + op: JaccardSimilarityOperator, +} + +impl Default for ThetaJaccardSimilarity { + fn default() -> Self { + Self::with_seed(DEFAULT_UPDATE_SEED) + } +} + +impl ThetaJaccardSimilarity { + /// Creates a Jaccard similarity operator for the given `seed`. + pub fn with_seed(seed: u64) -> Self { + Self { + op: JaccardSimilarityOperator::new(seed), + } + } + + /// Computes the Jaccard similarity index for `sketch_a` and `sketch_b`. + /// + /// # Errors + /// + /// Returns an error if either non-empty sketch was built with a seed different from this + /// operator's configured seed. + pub fn compute( + &self, + sketch_a: &A, + sketch_b: &B, + ) -> Result { + self.op.compute(sketch_a, sketch_b) + } +} diff --git a/datasketches/src/theta/mod.rs b/datasketches/src/theta/mod.rs index 4b5d28b..87f21fe 100644 --- a/datasketches/src/theta/mod.rs +++ b/datasketches/src/theta/mod.rs @@ -43,6 +43,7 @@ mod a_not_b; mod bit_pack; mod hash_table; mod intersection; +mod jaccard_similarity; mod serialization; mod sketch; mod union; @@ -50,6 +51,7 @@ mod union; pub use self::a_not_b::ThetaANotB; pub use self::hash_table::ThetaEntry; pub use self::intersection::ThetaIntersection; +pub use self::jaccard_similarity::ThetaJaccardSimilarity; pub use self::sketch::CompactThetaSketch; pub use self::sketch::ThetaSketch; pub use self::sketch::ThetaSketchBuilder; diff --git a/datasketches/src/thetacommon/bounds_binomial_proportions.rs b/datasketches/src/thetacommon/bounds_binomial_proportions.rs new file mode 100644 index 0000000..b075833 --- /dev/null +++ b/datasketches/src/thetacommon/bounds_binomial_proportions.rs @@ -0,0 +1,188 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +use crate::error::Error; + +/// Computes an approximate lower bound for an unknown binomial proportion. +pub(crate) fn approximate_lower_bound_on_p( + n: u64, + k: u64, + num_std_devs: f64, +) -> Result { + check_inputs(n, k)?; + if n == 0 || k == 0 { + Ok(0.0) + } else if k == 1 { + Ok(exact_lower_bound_on_p_k_eq_1( + n, + delta_of_num_stdevs(num_std_devs), + )) + } else if k == n { + Ok(exact_lower_bound_on_p_k_eq_n( + n, + delta_of_num_stdevs(num_std_devs), + )) + } else { + let x = abramowitz_stegun_formula_26p5p22((n - k) as f64 + 1.0, k as f64, -num_std_devs); + Ok(1.0 - x) + } +} + +/// Computes an approximate upper bound for an unknown binomial proportion. +pub(crate) fn approximate_upper_bound_on_p( + n: u64, + k: u64, + num_std_devs: f64, +) -> Result { + check_inputs(n, k)?; + if n == 0 || k == n { + Ok(1.0) + } else if k == n - 1 { + Ok(exact_upper_bound_on_p_k_eq_minusone( + n, + delta_of_num_stdevs(num_std_devs), + )) + } else if k == 0 { + Ok(exact_upper_bound_on_p_k_eq_zero( + n, + delta_of_num_stdevs(num_std_devs), + )) + } else { + let x = abramowitz_stegun_formula_26p5p22((n - k) as f64, k as f64 + 1.0, num_std_devs); + Ok(1.0 - x) + } +} + +fn check_inputs(n: u64, k: u64) -> Result<(), Error> { + if k > n { + return Err(Error::invalid_argument(format!( + "k cannot exceed n: k={k}, n={n}" + ))); + } + Ok(()) +} + +fn delta_of_num_stdevs(kappa: f64) -> f64 { + normal_cdf(-kappa) +} + +fn normal_cdf(x: f64) -> f64 { + 0.5 * (1.0 + erf(x / 2.0_f64.sqrt())) +} + +fn erf(x: f64) -> f64 { + if x < 0.0 { + -erf_of_nonneg(-x) + } else { + erf_of_nonneg(x) + } +} + +fn erf_of_nonneg(x: f64) -> f64 { + let a1 = 0.0705230784; + let a2 = 0.0422820123; + let a3 = 0.0092705272; + let a4 = 0.0001520143; + let a5 = 0.0002765672; + let a6 = 0.0000430638; + let x2 = x * x; + let x3 = x2 * x; + let x4 = x2 * x2; + let x5 = x2 * x3; + let x6 = x3 * x3; + let sum = 1.0 + (a1 * x) + (a2 * x2) + (a3 * x3) + (a4 * x4) + (a5 * x5) + (a6 * x6); + let sum2 = sum * sum; + let sum4 = sum2 * sum2; + let sum8 = sum4 * sum4; + let sum16 = sum8 * sum8; + 1.0 - (1.0 / sum16) +} + +fn abramowitz_stegun_formula_26p5p22(a: f64, b: f64, yp: f64) -> f64 { + let b2m1 = (2.0 * b) - 1.0; + let a2m1 = (2.0 * a) - 1.0; + let lambda = ((yp * yp) - 3.0) / 6.0; + let reciprocal_sum = (1.0 / a2m1) + (1.0 / b2m1); + let h = 2.0 / reciprocal_sum; + let term1 = (yp * (h + lambda).sqrt()) / h; + let term2 = (1.0 / b2m1) - (1.0 / a2m1); + let term3 = (lambda + (5.0 / 6.0)) - (2.0 / (3.0 * h)); + let w = term1 - (term2 * term3); + a / (a + (b * (2.0 * w).exp())) +} + +fn exact_upper_bound_on_p_k_eq_zero(n: u64, delta: f64) -> f64 { + 1.0 - delta.powf(1.0 / n as f64) +} + +fn exact_lower_bound_on_p_k_eq_n(n: u64, delta: f64) -> f64 { + delta.powf(1.0 / n as f64) +} + +fn exact_lower_bound_on_p_k_eq_1(n: u64, delta: f64) -> f64 { + 1.0 - (1.0 - delta).powf(1.0 / n as f64) +} + +fn exact_upper_bound_on_p_k_eq_minusone(n: u64, delta: f64) -> f64 { + (1.0 - delta).powf(1.0 / n as f64) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn rejects_invalid_counts() { + assert!(approximate_lower_bound_on_p(1, 2, 2.0).is_err()); + assert!(approximate_upper_bound_on_p(1, 2, 2.0).is_err()); + } + + #[test] + fn computes_exact_edge_cases() { + assert_eq!(approximate_lower_bound_on_p(0, 0, 2.0).unwrap(), 0.0); + assert_eq!(approximate_upper_bound_on_p(0, 0, 2.0).unwrap(), 1.0); + assert_eq!(approximate_lower_bound_on_p(10, 0, 2.0).unwrap(), 0.0); + assert_eq!(approximate_upper_bound_on_p(10, 10, 2.0).unwrap(), 1.0); + } + + #[test] + fn matches_cross_language_reference_vectors() { + const LOWER: [f64; 6] = [ + 0.0, + 0.004592032688529923, + 0.04725537386564205, + 0.1396230607626959, + 0.2735831034867167, + 0.4692424353373485, + ]; + const UPPER: [f64; 6] = [ + 0.5307575646626514, + 0.7264168965132833, + 0.860376939237304, + 0.952744626134358, + 0.9954079673114701, + 1.0, + ]; + + for k in 0..=5 { + let lower = approximate_lower_bound_on_p(5, k, 2.0).unwrap(); + let upper = approximate_upper_bound_on_p(5, k, 2.0).unwrap(); + assert!((lower - LOWER[k as usize]).abs() < 1e-14); + assert!((upper - UPPER[k as usize]).abs() < 1e-14); + } + } +} diff --git a/datasketches/src/thetacommon/jaccard_similarity.rs b/datasketches/src/thetacommon/jaccard_similarity.rs new file mode 100644 index 0000000..f8c2522 --- /dev/null +++ b/datasketches/src/thetacommon/jaccard_similarity.rs @@ -0,0 +1,304 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +use crate::common::ResizeFactor; +use crate::error::Error; +use crate::hash::compute_seed_hash; +use crate::thetacommon::RetainedEntry; +use crate::thetacommon::ThetaFamilySketchView; +use crate::thetacommon::bounds_binomial_proportions; +use crate::thetacommon::constants::MAX_LG_K; +use crate::thetacommon::constants::MAX_THETA; +use crate::thetacommon::constants::MIN_LG_K; +use crate::thetacommon::intersection::IntersectionMergePolicy; +use crate::thetacommon::intersection::IntersectionState; +use crate::thetacommon::union::UnionMergePolicy; +use crate::thetacommon::union::UnionState; + +const NUM_STD_DEVS: f64 = 2.0; + +/// Jaccard similarity estimate and confidence bounds for two Theta-family sketches. +/// +/// The bounds use a 95.4% confidence interval, equivalent to +/- 2 standard deviations. +#[derive(Clone, Copy, Debug, PartialEq)] +pub struct JaccardSimilarity { + lower_bound: f64, + estimate: f64, + upper_bound: f64, +} + +impl JaccardSimilarity { + /// Returns the approximate lower bound for the Jaccard index. + pub fn lower_bound(&self) -> f64 { + self.lower_bound + } + + /// Returns the estimate of the Jaccard index. + pub fn estimate(&self) -> f64 { + self.estimate + } + + /// Returns the approximate upper bound for the Jaccard index. + pub fn upper_bound(&self) -> f64 { + self.upper_bound + } + + fn exact(value: f64) -> Self { + Self { + lower_bound: value, + estimate: value, + upper_bound: value, + } + } + + fn ratio_bounds(union_count: u64, intersection_count: u64, theta: u64) -> Result { + if intersection_count > union_count { + return Err(Error::invalid_argument(format!( + "intersection count cannot exceed union count: {intersection_count} > {union_count}" + ))); + } + if union_count == 0 { + return Ok(Self { + lower_bound: 0.0, + estimate: 0.5, + upper_bound: 1.0, + }); + } + + let sampling_probability = theta as f64 / MAX_THETA as f64; + if sampling_probability <= 0.0 || sampling_probability > 1.0 { + return Err(Error::invalid_argument(format!( + "theta must produce a probability in (0.0, 1.0], got {sampling_probability}" + ))); + } + if sampling_probability == 1.0 { + return Ok(Self::exact(intersection_count as f64 / union_count as f64)); + } + + let adjustment = NUM_STD_DEVS * sampling_adjuster(sampling_probability); + Ok(Self { + lower_bound: bounds_binomial_proportions::approximate_lower_bound_on_p( + union_count, + intersection_count, + adjustment, + )?, + estimate: intersection_count as f64 / union_count as f64, + upper_bound: bounds_binomial_proportions::approximate_upper_bound_on_p( + union_count, + intersection_count, + adjustment, + )?, + }) + } +} + +#[derive(Clone, Copy, Debug)] +struct KeyEntry { + hash: u64, +} + +impl RetainedEntry for KeyEntry { + fn hash(&self) -> u64 { + self.hash + } +} + +struct KeySketchView<'a, S> { + sketch: &'a S, +} + +impl<'a, S> KeySketchView<'a, S> { + fn new(sketch: &'a S) -> Self { + Self { sketch } + } +} + +impl ThetaFamilySketchView for KeySketchView<'_, S> { + type Entry = KeyEntry; + + fn seed_hash(&self) -> u16 { + self.sketch.seed_hash() + } + + fn theta64(&self) -> u64 { + self.sketch.theta64() + } + + fn is_empty(&self) -> bool { + self.sketch.is_empty() + } + + fn is_ordered(&self) -> bool { + self.sketch.is_ordered() + } + + fn iter(&self) -> impl Iterator + '_ { + self.sketch.iter_hashes().map(|hash| KeyEntry { hash }) + } + + fn num_retained(&self) -> usize { + self.sketch.num_retained() + } +} + +#[derive(Clone, Copy, Debug)] +struct NoopMergePolicy; + +impl UnionMergePolicy for NoopMergePolicy { + fn merge(&self, _existing: &mut E, _incoming: E) {} +} + +impl IntersectionMergePolicy for NoopMergePolicy { + fn merge(&self, _existing: &mut E, _incoming: E) {} +} + +struct CompactKeySketchView { + entries: Vec, + theta: u64, + seed_hash: u16, + ordered: bool, + empty: bool, +} + +impl ThetaFamilySketchView for CompactKeySketchView { + type Entry = KeyEntry; + + fn seed_hash(&self) -> u16 { + self.seed_hash + } + + fn theta64(&self) -> u64 { + self.theta + } + + fn is_empty(&self) -> bool { + self.empty + } + + fn is_ordered(&self) -> bool { + self.ordered + } + + fn iter(&self) -> impl Iterator + '_ { + self.entries.iter().copied() + } + + fn num_retained(&self) -> usize { + self.entries.len() + } +} + +/// Configured Jaccard operator shared by Theta and Tuple public wrappers. +#[derive(Clone, Copy, Debug)] +pub(crate) struct JaccardSimilarityOperator { + seed: u64, +} + +impl JaccardSimilarityOperator { + pub(crate) fn new(seed: u64) -> Self { + Self { seed } + } + + pub(crate) fn compute( + &self, + sketch_a: &A, + sketch_b: &B, + ) -> Result + where + A: ThetaFamilySketchView, + B: ThetaFamilySketchView, + { + if sketch_a.is_empty() && sketch_b.is_empty() { + return Ok(JaccardSimilarity::exact(1.0)); + } + if sketch_a.is_empty() || sketch_b.is_empty() { + return Ok(JaccardSimilarity::exact(0.0)); + } + + let seed_hash = compute_seed_hash(self.seed); + if seed_hash != sketch_a.seed_hash() { + return Err(Error::invalid_argument(format!( + "incompatible seed hash: expected {}, got {}", + seed_hash, + sketch_a.seed_hash(), + ))); + } + if seed_hash != sketch_b.seed_hash() { + return Err(Error::invalid_argument(format!( + "incompatible seed hash: expected {}, got {}", + seed_hash, + sketch_b.seed_hash(), + ))); + } + + let sketch_a = KeySketchView::new(sketch_a); + let sketch_b = KeySketchView::new(sketch_b); + let mut union = UnionState::new( + union_lg_k(sketch_a.num_retained(), sketch_b.num_retained()), + ResizeFactor::X8, + 1.0, + self.seed, + NoopMergePolicy, + ); + union.update(&sketch_a)?; + union.update(&sketch_b)?; + let union = union.to_compact_parts(false); + + if !union.entries.is_empty() + && union.entries.len() == sketch_a.num_retained() + && union.entries.len() == sketch_b.num_retained() + && union.theta == sketch_a.theta64() + && union.theta == sketch_b.theta64() + { + return Ok(JaccardSimilarity::exact(1.0)); + } + + let union = CompactKeySketchView { + entries: union.entries, + theta: union.theta, + seed_hash: union.seed_hash, + ordered: union.ordered, + empty: union.empty, + }; + let mut intersection = IntersectionState::new(self.seed, NoopMergePolicy); + intersection.update(&sketch_a)?; + intersection.update(&sketch_b)?; + intersection.update(&union)?; + let intersection = intersection.result(false); + + JaccardSimilarity::ratio_bounds( + union.num_retained() as u64, + intersection.entries.len() as u64, + union.theta64(), + ) + } +} + +fn sampling_adjuster(sampling_probability: f64) -> f64 { + let adjustment = (1.0 - sampling_probability).sqrt(); + if sampling_probability <= 0.5 { + adjustment + } else { + adjustment + (0.01 * (sampling_probability - 0.5)) + } +} + +fn union_lg_k(left_count: usize, right_count: usize) -> u8 { + let required_capacity = left_count.saturating_add(right_count).max(1); + let lg_k = usize::BITS - (required_capacity - 1).leading_zeros(); + (lg_k as u8).clamp(MIN_LG_K, MAX_LG_K) +} diff --git a/datasketches/src/thetacommon/mod.rs b/datasketches/src/thetacommon/mod.rs index eb8db95..6da798d 100644 --- a/datasketches/src/thetacommon/mod.rs +++ b/datasketches/src/thetacommon/mod.rs @@ -19,11 +19,15 @@ pub(crate) mod a_not_b; pub(crate) mod binomial_bounds; +pub(crate) mod bounds_binomial_proportions; pub(crate) mod constants; pub(crate) mod hash_table; pub(crate) mod intersection; +pub(crate) mod jaccard_similarity; pub(crate) mod union; +pub use self::jaccard_similarity::JaccardSimilarity; + /// An entry retained by a Theta sketch family hash table. pub trait RetainedEntry { /// Return the hash used as this entry's key. @@ -53,6 +57,13 @@ pub trait ThetaFamilySketchView { /// Return an iterator over retained entries. fn iter(&self) -> impl Iterator + '_; + /// Return an iterator over retained hash keys without requiring callers to inspect payloads. + /// + /// Tuple sketches override this method so key-only operations do not clone summary values. + fn iter_hashes(&self) -> impl Iterator + '_ { + self.iter().map(|entry| entry.hash()) + } + /// Return the number of retained entries. fn num_retained(&self) -> usize; } diff --git a/datasketches/src/tuple/jaccard_similarity.rs b/datasketches/src/tuple/jaccard_similarity.rs new file mode 100644 index 0000000..d9faa2d --- /dev/null +++ b/datasketches/src/tuple/jaccard_similarity.rs @@ -0,0 +1,83 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +//! Jaccard similarity for Tuple sketches. + +use crate::error::Error; +use crate::hash::DEFAULT_UPDATE_SEED; +use crate::thetacommon::jaccard_similarity::JaccardSimilarity; +use crate::thetacommon::jaccard_similarity::JaccardSimilarityOperator; +use crate::tuple::TupleSketchView; + +/// Jaccard similarity operator for Tuple sketches. +/// +/// Only retained hash keys participate in the similarity calculation. Summary values are ignored, +/// so the two inputs may use different summary types. The returned [`JaccardSimilarity`] contains +/// the estimate and its 95.4% confidence interval. +/// +/// # Examples +/// +/// ``` +/// # use datasketches::tuple::{DefaultUpdatePolicy, TupleJaccardSimilarity, TupleSketchBuilder}; +/// let policy = DefaultUpdatePolicy::::default(); +/// let mut a = TupleSketchBuilder::new(policy).build(); +/// let mut b = TupleSketchBuilder::new(policy).build(); +/// a.update("apple", 1); +/// b.update("apple", 2); +/// +/// let result = TupleJaccardSimilarity::default().compute(&a, &b).unwrap(); +/// assert_eq!(result.estimate(), 1.0); +/// ``` +#[derive(Clone, Copy, Debug)] +pub struct TupleJaccardSimilarity { + op: JaccardSimilarityOperator, +} + +impl Default for TupleJaccardSimilarity { + fn default() -> Self { + Self::with_seed(DEFAULT_UPDATE_SEED) + } +} + +impl TupleJaccardSimilarity { + /// Creates a Jaccard similarity operator for the given `seed`. + pub fn with_seed(seed: u64) -> Self { + Self { + op: JaccardSimilarityOperator::new(seed), + } + } + + /// Computes the Jaccard similarity index for `sketch_a` and `sketch_b`. + /// + /// Summary values do not participate in the comparison. + /// + /// # Errors + /// + /// Returns an error if either non-empty sketch was built with a seed different from this + /// operator's configured seed. + pub fn compute( + &self, + sketch_a: &A, + sketch_b: &B, + ) -> Result + where + A: TupleSketchView, + B: TupleSketchView, + { + self.op.compute(sketch_a, sketch_b) + } +} diff --git a/datasketches/src/tuple/mod.rs b/datasketches/src/tuple/mod.rs index 4c88058..1ba8b58 100644 --- a/datasketches/src/tuple/mod.rs +++ b/datasketches/src/tuple/mod.rs @@ -41,6 +41,7 @@ mod a_not_b; mod hash_table; mod intersection; +mod jaccard_similarity; mod policy; mod serialization; mod sketch; @@ -49,6 +50,7 @@ mod union; pub use self::a_not_b::TupleANotB; pub use self::hash_table::TupleEntry; pub use self::intersection::TupleIntersection; +pub use self::jaccard_similarity::TupleJaccardSimilarity; pub use self::policy::DefaultUnionPolicy; pub use self::policy::DefaultUpdatePolicy; pub use self::policy::SummaryCombinePolicy; diff --git a/datasketches/tests/theta_test/jaccard_similarity.rs b/datasketches/tests/theta_test/jaccard_similarity.rs new file mode 100644 index 0000000..428989f --- /dev/null +++ b/datasketches/tests/theta_test/jaccard_similarity.rs @@ -0,0 +1,181 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +use datasketches::theta::ThetaJaccardSimilarity; +use datasketches::theta::ThetaSketch; +use datasketches::theta::ThetaSketchBuilder; +use datasketches::thetacommon::JaccardSimilarity; + +fn assert_jaccard_exact(actual: JaccardSimilarity, expected: f64) { + assert_eq!(actual.lower_bound(), expected); + assert_eq!(actual.estimate(), expected); + assert_eq!(actual.upper_bound(), expected); +} + +fn assert_close(actual: f64, expected: f64, margin: f64) { + assert!( + (actual - expected).abs() <= margin, + "actual={actual}, expected={expected}, margin={margin}" + ); +} + +fn assert_jaccard_estimate(actual: JaccardSimilarity, expected: f64) { + assert_close(actual.lower_bound(), expected, 0.01); + assert_close(actual.estimate(), expected, 0.01); + assert_close(actual.upper_bound(), expected, 0.01); +} + +fn sketch_with_range(start: u64, count: u64) -> ThetaSketch { + let mut sketch = ThetaSketchBuilder::default().build(); + for value in start..start + count { + sketch.update(value); + } + sketch +} + +fn sketch_with_range_and_seed(start: u64, count: u64, seed: u64) -> ThetaSketch { + let mut sketch = ThetaSketchBuilder::default().seed(seed).build(); + for value in start..start + count { + sketch.update(value); + } + sketch +} + +#[test] +fn test_empty() { + let sketch_a = ThetaSketchBuilder::default().build(); + let sketch_b = ThetaSketchBuilder::default().build(); + + let jaccard = ThetaJaccardSimilarity::default() + .compute(&sketch_a, &sketch_b) + .unwrap(); + + assert_jaccard_exact(jaccard, 1.0); +} + +#[test] +fn test_same_sketch_exact_mode() { + let sketch = sketch_with_range(0, 1000); + + let operator = ThetaJaccardSimilarity::default(); + let jaccard = operator.compute(&sketch, &sketch).unwrap(); + assert_jaccard_exact(jaccard, 1.0); + + let jaccard = operator + .compute(&sketch.compact(true), &sketch.compact(true)) + .unwrap(); + assert_jaccard_exact(jaccard, 1.0); +} + +#[test] +fn test_full_overlap_exact_mode() { + let sketch_a = sketch_with_range(0, 1000); + let sketch_b = sketch_with_range(0, 1000); + + let operator = ThetaJaccardSimilarity::default(); + let jaccard = operator.compute(&sketch_a, &sketch_b).unwrap(); + assert_jaccard_exact(jaccard, 1.0); + + let jaccard = operator + .compute(&sketch_a.compact(true), &sketch_b.compact(true)) + .unwrap(); + assert_jaccard_exact(jaccard, 1.0); +} + +#[test] +fn test_disjoint_exact_mode() { + let sketch_a = sketch_with_range(0, 1000); + let sketch_b = sketch_with_range(1000, 1000); + + let operator = ThetaJaccardSimilarity::default(); + let jaccard = operator.compute(&sketch_a, &sketch_b).unwrap(); + assert_jaccard_exact(jaccard, 0.0); + + let jaccard = operator + .compute(&sketch_a.compact(true), &sketch_b.compact(true)) + .unwrap(); + assert_jaccard_exact(jaccard, 0.0); +} + +#[test] +fn test_half_overlap_estimation_mode() { + let sketch_a = sketch_with_range(0, 10000); + let sketch_b = sketch_with_range(5000, 10000); + + let operator = ThetaJaccardSimilarity::default(); + let jaccard = operator.compute(&sketch_a, &sketch_b).unwrap(); + assert_jaccard_estimate(jaccard, 0.33); + + let jaccard = operator + .compute(&sketch_a.compact(true), &sketch_b.compact(true)) + .unwrap(); + assert_jaccard_estimate(jaccard, 0.33); +} + +#[test] +fn test_half_overlap_estimation_mode_custom_seed() { + let seed = 123; + let sketch_a = sketch_with_range_and_seed(0, 10000, seed); + let sketch_b = sketch_with_range_and_seed(5000, 10000, seed); + + let operator = ThetaJaccardSimilarity::with_seed(seed); + let jaccard = operator.compute(&sketch_a, &sketch_b).unwrap(); + assert_jaccard_estimate(jaccard, 0.33); + + let jaccard = operator + .compute(&sketch_a.compact(true), &sketch_b.compact(true)) + .unwrap(); + assert_jaccard_estimate(jaccard, 0.33); +} + +#[test] +fn test_seed_mismatch() { + let mut sketch_a = ThetaSketchBuilder::default().build(); + sketch_a.update(1u64); + let mut sketch_b = ThetaSketchBuilder::default().seed(123).build(); + sketch_b.update(1u64); + + assert!( + ThetaJaccardSimilarity::default() + .compute(&sketch_a, &sketch_b) + .is_err() + ); +} + +#[test] +fn test_distinct_non_empty_sketches_with_no_retained_entries_are_uncertain() { + let mut sketch_a = ThetaSketchBuilder::default() + .sampling_probability(1e-12) + .build(); + let mut sketch_b = ThetaSketchBuilder::default() + .sampling_probability(1e-12) + .build(); + sketch_a.update("apple"); + sketch_b.update("banana"); + + assert!(!sketch_a.is_empty()); + assert!(!sketch_b.is_empty()); + assert_eq!(sketch_a.num_retained(), 0); + assert_eq!(sketch_b.num_retained(), 0); + + let jaccard = ThetaJaccardSimilarity::default() + .compute(&sketch_a, &sketch_b) + .unwrap(); + assert_eq!(jaccard.lower_bound(), 0.0); + assert_eq!(jaccard.estimate(), 0.5); + assert_eq!(jaccard.upper_bound(), 1.0); +} diff --git a/datasketches/tests/theta_test/main.rs b/datasketches/tests/theta_test/main.rs index f3d4a72..d9d0800 100644 --- a/datasketches/tests/theta_test/main.rs +++ b/datasketches/tests/theta_test/main.rs @@ -17,5 +17,6 @@ mod a_not_b; mod intersection; +mod jaccard_similarity; mod sketch; mod union; diff --git a/datasketches/tests/tuple_test/jaccard_similarity.rs b/datasketches/tests/tuple_test/jaccard_similarity.rs new file mode 100644 index 0000000..d568285 --- /dev/null +++ b/datasketches/tests/tuple_test/jaccard_similarity.rs @@ -0,0 +1,138 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +use datasketches::thetacommon::JaccardSimilarity; +use datasketches::tuple::DefaultUpdatePolicy; +use datasketches::tuple::TupleJaccardSimilarity; +use datasketches::tuple::TupleSketchBuilder; + +use crate::default_tuple_sketch_builder; +use crate::tuple_sketch_with_range; + +fn assert_jaccard_exact(actual: JaccardSimilarity, expected: f64) { + assert_eq!(actual.lower_bound(), expected); + assert_eq!(actual.estimate(), expected); + assert_eq!(actual.upper_bound(), expected); +} + +fn assert_close(actual: f64, expected: f64, margin: f64) { + assert!( + (actual - expected).abs() <= margin, + "actual={actual}, expected={expected}, margin={margin}" + ); +} + +fn assert_jaccard_estimate(actual: JaccardSimilarity, expected: f64) { + assert_close(actual.lower_bound(), expected, 0.01); + assert_close(actual.estimate(), expected, 0.01); + assert_close(actual.upper_bound(), expected, 0.01); +} + +#[test] +fn test_empty() { + let sketch_a = default_tuple_sketch_builder().build(); + let sketch_b = default_tuple_sketch_builder().build(); + + let jaccard = TupleJaccardSimilarity::default() + .compute(&sketch_a, &sketch_b) + .unwrap(); + + assert_jaccard_exact(jaccard, 1.0); +} + +#[test] +fn test_summary_values_and_types_do_not_affect_similarity() { + let mut sketch_a = TupleSketchBuilder::new(DefaultUpdatePolicy::::default()).build(); + let mut sketch_b = TupleSketchBuilder::new(DefaultUpdatePolicy::::default()).build(); + for key in 0..1000 { + sketch_a.update(key, 1u64); + sketch_b.update(key, -7i64); + } + + let operator = TupleJaccardSimilarity::default(); + let jaccard = operator.compute(&sketch_a, &sketch_b).unwrap(); + assert_jaccard_exact(jaccard, 1.0); + + let jaccard = operator + .compute(&sketch_a.compact(true), &sketch_b.compact(true)) + .unwrap(); + assert_jaccard_exact(jaccard, 1.0); +} + +#[test] +fn test_half_overlap_estimation_mode() { + let sketch_a = tuple_sketch_with_range(0, 10000); + let sketch_b = tuple_sketch_with_range(5000, 10000); + + let operator = TupleJaccardSimilarity::default(); + let jaccard = operator.compute(&sketch_a, &sketch_b).unwrap(); + assert_jaccard_estimate(jaccard, 0.33); + + let jaccard = operator + .compute(&sketch_a.compact(true), &sketch_b.compact(true)) + .unwrap(); + assert_jaccard_estimate(jaccard, 0.33); +} + +#[test] +fn test_custom_seed_and_seed_mismatch() { + let seed = 123; + let mut sketch_a = TupleSketchBuilder::new(DefaultUpdatePolicy::::default()) + .seed(seed) + .build(); + let mut sketch_b = TupleSketchBuilder::new(DefaultUpdatePolicy::::default()) + .seed(seed) + .build(); + for value in 0..1000 { + sketch_a.update(value, 1u64); + sketch_b.update(value, 2u64); + } + + let jaccard = TupleJaccardSimilarity::with_seed(seed) + .compute(&sketch_a, &sketch_b) + .unwrap(); + assert_jaccard_exact(jaccard, 1.0); + assert!( + TupleJaccardSimilarity::default() + .compute(&sketch_a, &sketch_b) + .is_err() + ); +} + +#[test] +fn test_distinct_non_empty_sketches_with_no_retained_entries_are_uncertain() { + let mut sketch_a = default_tuple_sketch_builder() + .sampling_probability(1e-12) + .build(); + let mut sketch_b = default_tuple_sketch_builder() + .sampling_probability(1e-12) + .build(); + sketch_a.update("apple", 1u64); + sketch_b.update("banana", 1u64); + + assert!(!sketch_a.is_empty()); + assert!(!sketch_b.is_empty()); + assert_eq!(sketch_a.num_retained(), 0); + assert_eq!(sketch_b.num_retained(), 0); + + let jaccard = TupleJaccardSimilarity::default() + .compute(&sketch_a, &sketch_b) + .unwrap(); + assert_eq!(jaccard.lower_bound(), 0.0); + assert_eq!(jaccard.estimate(), 0.5); + assert_eq!(jaccard.upper_bound(), 1.0); +} diff --git a/datasketches/tests/tuple_test/main.rs b/datasketches/tests/tuple_test/main.rs index 887a280..cbb6290 100644 --- a/datasketches/tests/tuple_test/main.rs +++ b/datasketches/tests/tuple_test/main.rs @@ -17,6 +17,7 @@ mod a_not_b; mod intersection; +mod jaccard_similarity; mod sketch; mod union;