Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 29 additions & 29 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
fail_fast: true

# See https://pre-commit.com for more information
# See https://pre-commit.com/hooks.html for more hooks
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
hooks:
- id: debug-statements
- id: trailing-whitespace
exclude: '.bumpversion.cfg'
- id: end-of-file-fixer
- id: check-yaml
- id: check-toml
- id: check-added-large-files
- id: check-merge-conflict
- repo: local
hooks:
- id: cargo-fmt
name: cargo fmt
entry: cargo fmt --all -- --check
language: rust
types: [rust]
- id: cargo-clippy
name: cargo clippy
entry: cargo clippy --all -- -D warnings -W clippy::pedantic -W clippy::nursery
language: rust
pass_filenames: false
types: [rust]
fail_fast: true
# See https://pre-commit.com for more information
# See https://pre-commit.com/hooks.html for more hooks
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
hooks:
- id: debug-statements
- id: trailing-whitespace
exclude: '.bumpversion.cfg'
- id: end-of-file-fixer
- id: check-yaml
- id: check-toml
- id: check-added-large-files
- id: check-merge-conflict
- repo: local
hooks:
- id: cargo-fmt
name: cargo fmt
entry: cargo fmt --all -- --check
language: rust
types: [rust]
- id: cargo-clippy
name: cargo clippy
entry: cargo clippy --all -- -D warnings -W clippy::pedantic -W clippy::nursery
language: rust
pass_filenames: false
types: [rust]
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ assert!((distance - (27.0_f32).sqrt()).abs() < 1e-6);
- [ ] Normalized versions of the above.
- [ ] Sets:
- [x] `jaccard`
- [x] `kulsinski`
- [ ] `hausdorff`
- [Hausdorff Distance](https://en.wikipedia.org/wiki/Hausdorff_distance)
- [ ] Graphs:
Expand Down
43 changes: 43 additions & 0 deletions src/sets/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,46 @@ pub fn jaccard<T: Int, U: Float>(x: &[T], y: &[T]) -> U {
U::one() - intersection / union
}
}

/// Kulsinski distance.
///
/// Similar to the Jaccard distance, the Kulsinski distance is a measure of the dissimilarity
/// between two sets. It is defined as the cardinality of the intersection divided by the
/// sum of the number of not equal dimensions and the total number of dimensions.
///
/// # Arguments
///
/// * `x`: A set represented as a slice of `Int`s, i.e. a type generic over integers.
/// * `y`: A set represented as a slice of `Int`s, i.e. a type generic over integers.
///
/// # Examples
///
/// ```
/// use distances::sets::kulsinski;
///
/// let x: Vec<u32> = vec![1, 2, 3];
/// let y: Vec<u32> = vec![2, 3, 4];
///
/// let distance: f32 = kulsinski(&x, &y);
/// let real_distance: f32 = 2_f32 / 3_f32;
///
/// assert!((distance - real_distance).abs() < f32::EPSILON);
/// ```
pub fn kulsinski<T: Int, U: Float>(x: &[T], y: &[T]) -> U {
if x.is_empty() || y.is_empty() {
return U::one();
}

let x = x.iter().copied().collect::<BTreeSet<_>>();
let y = y.iter().copied().collect::<BTreeSet<_>>();

let intersection = x.intersection(&y).count();
let union = x.union(&y).count();
let not_equal = union - intersection;

if intersection == x.len() && intersection == y.len() {
U::zero()
} else {
U::one() - (U::from(intersection) / U::from(union + not_equal))
}
}
74 changes: 74 additions & 0 deletions tests/test_sets.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
use distances::sets::jaccard;
use distances::sets::kulsinski;

/// Generates a length 1000 set with each bit flipped randomly on or off
fn gen_set() -> Vec<u16> {
let mut vec = Vec::new();
for i in 0..1000 {
if rand::random() {
vec.push(i);
}
}
vec
}

/// Random exhaustive testing of set distances, manually creating union and intersection values
#[test]
fn sets_test() {
for _ in 0..10000 {
let x: Vec<u16> = gen_set();
let y: Vec<u16> = gen_set();
let mut union: f32 = 0.0;
let mut intersection: f32 = 0.0;
for i in 0_u16..1000 {
if x.contains(&i) || y.contains(&i) {
union += 1.0;
}
if x.contains(&i) && y.contains(&i) {
intersection += 1.0;
}
}
let mut distance: f32;
let mut real_distance: f32;

distance = jaccard(&x, &y);
if union == 0.0 {
real_distance = 0.0;
} else {
real_distance = 1_f32 - intersection / union;
}
assert!((distance - real_distance).abs() < f32::EPSILON);

distance = kulsinski(&x, &y);
if union == 0.0 {
real_distance = 0.0;
} else {
real_distance = 1_f32 - intersection / (union + union - intersection);
}
assert!((distance - real_distance).abs() < f32::EPSILON);
}
}

/// Boundary testing for set distances, equal sets or one zero set
#[test]
fn bounds_test() {
let x: Vec<u16> = gen_set();
let y: Vec<u16> = Vec::new();

let mut distance: f32;
let mut real_distance: f32;

distance = jaccard(&x, &x);
assert!(distance < f32::EPSILON);
distance = jaccard(&x, &y);
assert!(distance - 1.0 < f32::EPSILON);
distance = jaccard(&y, &y);
assert!(distance - 1.0 < f32::EPSILON);

distance = kulsinski(&x, &x);
assert!(distance < f32::EPSILON);
distance = kulsinski(&x, &y);
assert!(distance - 1.0 < f32::EPSILON);
distance = kulsinski(&y, &y);
assert!(distance - 1.0 < f32::EPSILON);
}