1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
//! Some utilities to help choose LSH parameters.
use crate::dist::l2_norm;
use crate::prelude::*;
use fnv::FnvHashSet;
use ndarray::aview1;
use rayon::prelude::*;
use statrs::{
    consts::SQRT_2PI,
    distribution::{Normal, Univariate},
};
use std::f64::consts::PI;
use std::time::Instant;

/// Hash collision probability for L2 distance.
///
/// Assumes R normalized data points. So R = 1.
/// Compute 𝑃1 if c = 1.
/// Compute 𝑃2 if c = c
///
/// # Arguments
/// * `r` - Parameter of l2 hash function (also noted as `w`)
/// * `c` - Approximation factor. cR.
pub fn l2_ph(r: f64, c: f64) -> f64 {
    let norm = Normal::new(0., 1.).unwrap();
    1. - 2. * norm.cdf(-r / c)
        - 2. / (SQRT_2PI * r / c) * (1. - (-(r.powf(2.) / (2. * c.powf(2.)))).exp())
}

/// Hash collision probability for Sign Random Projections
/// # Arguments
/// * `cosine_sim` - Cosine similarity.
pub fn srp_ph(cosine_sim: f64) -> f64 {
    1. - cosine_sim.acos() / PI
}

///
/// Return NN w/ probability 1 - δ. Generic formula.
///
/// # Arguments
/// * `delta` - Prob. not returned NN.
/// * `p1` - P1 in literature.
/// * `k` - Number of hash projections.
pub fn estimate_l(delta: f64, p1: f64, k: usize) -> usize {
    (delta.ln() / (1. - p1.powf(k as f64)).ln()).round() as usize
}

#[derive(Debug)]
pub struct OptRes {
    pub k: usize,
    pub l: usize,
    pub search_time: f64,
    pub hash_time: f64,
    pub min_len: usize,
    pub max_len: usize,
    pub avg_len: f32,
    pub unique_hash_values: FnvHashSet<i32>,
}

fn lsh_to_result<H: 'static + VecHash<f32, i8> + Send + Sync + Clone>(
    lsh: LshMem<H, f32, i8>,
    vs: &[Vec<f32>],
    k: usize,
    l: usize,
) -> Result<OptRes> {
    let mut lsh = lsh;
    lsh.store_vecs(vs)?;
    let mut search_time = 0.;
    let mut hash_time = 0.;
    let mut bucket_lengths = Vec::with_capacity(vs.len());
    for v in vs {
        let th = Instant::now();
        let mut bucket_ids = lsh.query_bucket_ids(v)?;
        hash_time += th.elapsed().as_secs_f64();

        bucket_lengths.push(bucket_ids.len());

        let t0 = Instant::now();
        let q = aview1(&v);
        bucket_ids.par_sort_by_key(|&idx| {
            let p = &vs[idx as usize];
            let dist = &aview1(&p) - &q;
            let l2 = l2_norm(dist.as_slice().unwrap());
            (l2 * 1e5) as i32
        });
        let duration = t0.elapsed();
        search_time += duration.as_secs_f64();
    }
    let min = *bucket_lengths.iter().min().unwrap_or(&(0 as usize));
    let max = *bucket_lengths.iter().max().unwrap_or(&(0 as usize));
    let avg = bucket_lengths.iter().sum::<usize>() as f32 / bucket_lengths.len() as f32;
    let unique_hash_values = lsh.hash_tables.unwrap().get_unique_hash_int();
    Ok(OptRes {
        k,
        l,
        search_time,
        hash_time,
        min_len: min,
        max_len: max,
        avg_len: avg,
        unique_hash_values,
    })
}

/// Does a grid search over parameter *K* where *L* is determined by the `estimate_l` function.
///
/// # Arguments
/// * `delta` - Probability of not returning NN. P(NN) = 1 - δ
/// * `cosine_sim` - Cosine similarity distance within which the nearest neighbor should exist.
/// * `dim` - Dimension of the data points.
/// * `vs` - Data points.
pub fn optimize_srp_params(
    delta: f64,
    cosine_sim: f64,
    dim: usize,
    k: &[usize],
    vs: &[Vec<f32>],
) -> Result<Vec<OptRes>> {
    let mut params = vec![];
    let p1 = srp_ph(cosine_sim);
    for _k in k {
        let l = estimate_l(delta, p1, *_k);
        params.push((*_k, l))
    }
    let result = params
        .par_iter()
        .map(|&(k, l)| {
            let lsh = LshMem::new(k, l, dim).srp()?;
            lsh_to_result(lsh, vs, k, l)
        })
        .collect();
    result
}

/// Does a grid search over parameter *K* where *L* is determined by the `estimate_l` function.
/// Note that the data already should be normalized by dividing the data points
/// by the query distance *R*.
///
/// # Arguments
/// * `delta` - Probability of not returning NN. P(NN) = 1 - δ
/// * `dim` - Dimension of the data points.
/// * `vs` - Data points.
pub fn optimize_l2_params(
    delta: f64,
    dim: usize,
    k: &[usize],
    vs: &[Vec<f32>],
) -> Result<Vec<OptRes>> {
    let mut params = vec![];
    let r = 4.0;
    let p1 = l2_ph(r as f64, 1.);
    for _k in k {
        let l = estimate_l(delta, p1, *_k as usize);
        params.push((r, *_k, l))
    }
    let result = params
        .par_iter()
        .map(|&(r, k, l)| {
            let lsh = LshMem::new(k, l, dim).l2(r as f32)?;
            lsh_to_result(lsh, vs, k, l)
        })
        .collect();
    result
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn test_l2_ph() {
        // tested w/ numpy
        let r = 2.0;
        let c = 1.0;
        assert_eq!(0.609548422215397, l2_ph(r, c) as f32);
    }

    #[test]
    fn test_estimate_l() {
        let delta = 0.2;
        let p1 = 0.6;
        let k = 5;
        assert_eq!(20, estimate_l(delta, p1, k));
    }
}