-
Notifications
You must be signed in to change notification settings - Fork 5
/
WeightedMinHash.cpp
222 lines (185 loc) · 5.07 KB
/
WeightedMinHash.cpp
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#include "WeightedMinHash.h"
#include <vector>
#include <iostream>
#include <algorithm>
#include <time.h>
#include <math.h>
#define PI 3.14159265
//#pragma once
using namespace std;
WeightedMinHash::WeightedMinHash()
{
}
void WeightedMinHash::Initialize(int dimention,int numOfHashes)
{
_dim = dimention;
_numhashes = numOfHashes;
std::random_device rd;
std::mt19937 gen(rd());
// values near the mean are the most likely
// standard deviation affects the dispersion of generated values from the mean
std::normal_distribution<> d(0,1);
_rand_vec = new double*[_numhashes];
for (size_t i = 0; i < _numhashes; i++ )
{
_rand_vec[i] = new double[_dim];
double sum = 0.0;
for (size_t j = 0; j < _dim; j++)
{
_rand_vec[i][j] = d(gen);
sum += _rand_vec[i][j];
}
// Normalize
for (size_t j = 0; j < _dim; j++)
{
_rand_vec[i][j] /= sum;
//printf("%f \n", _rand_vec[i][j]);
}
}
}
WeightedMinHash::WeightedMinHash(int dimention,int numOfHashes)
{
_dim = dimention;
_numhashes = numOfHashes;
std::random_device rd;
std::mt19937 gen(rd());
// values near the mean are the most likely
// standard deviation affects the dispersion of generated values from the mean
std::normal_distribution<> d(0,1);
_rand_vec = new double*[_numhashes];
for (size_t i = 0; i < _numhashes; i++ )
{
_rand_vec[i] = new double[_dim];
double sum = 0.0;
for (size_t j = 0; j < _dim; j++)
{
_rand_vec[i][j] = d(gen);
sum += _rand_vec[i][j];
}
// Normalize
for (size_t j = 0; j < _dim; j++)
{
_rand_vec[i][j] /= sum;
}
}
}
/*
* Calculate the collision probability under sign random projection
*/
double WeightedMinHash::getProb(double * q, double * vector, int length)
{
return 0.0;
}
int * WeightedMinHash::getHash(double * vector, int length)
{
int * hashes = new int[_numhashes];
for (size_t i = 0; i < _numhashes; i++)
{
double inner_product = 0.0;
hashes[i] = 0;
double _b = (double) (rand() % int(_w));
for (size_t j=0;j<length;j++)
{
inner_product += vector[j]* _rand_vec[i][j];
}
//printf("%f ", inner_product);
hashes[i] = floor((inner_product+_b) / _w);
//printf ("%d ", hashes[i]);
}
//printf ("\n");
return hashes;
}
/*
The function CWS is Consistent weighted sampling, for the details of this function, please refer the following paper:
Ioffe, Sergey. "Improved consistent sampling, weighted minhash and l1 sketching." Data Mining (ICDM), 2010 IEEE 10th International Conference on. IEEE, 2010.
*/
pair<int, double> WeightedMinHash::CWS (double * vector, int length)
{
double *r = new double[length];
double *c = new double[length];
double *b = new double[length];
double *t = new double[length];
double *y = new double[length];
// Using vector here for the argmin function.
std::vector<double> a(length);
for (int i=0;i<length;i++)
{
r[i] = rgamma(2.0,1.0);
c[i] = rgamma(2.0,1.0);
b[i] = uniform(0.0,1);
}
int i = 0;
while(vector[i] > 0 && i<length)
{
double s = vector[i];
t[i] = (log(s)/r[i]) + b[i] ;
y[i] = exp(r[i]*(t[i]-b[i]));
a[i] = c[i]/(y[i]*exp(r[i])) ;
i++;
}
std::pair <int,double> sample;
sample.second = *std::min_element(a.begin(), a.end());
sample.first = std::distance(a.begin(), std::min_element(a.begin(), a.end()));
return sample;
}
// Generate a random number from a gamma distribution
double WeightedMinHash::rgamma(double a,double b)
{
double d,c,x,v,u;
if(a>=1)
{
d = a-1./3.;
c = 1./sqrt(9.*d);
while(1)
{
do
{
x=gauss(0,1.0);
v=1.+c*x;
}
while(v<=0.);
v = v * v * v;
u = uniform(0,1);
if( u < 1.0-0.0331*(x*x)*(x*x) )
{
return d*v*b;
}
if( log(u) < 0.5*x*x+d*(1.0-v+log(v)) )
{
return d*v*b;
}
}
} else
{
x = rgamma(a+1,b);
x = x * pow(uniform(0,1), 1.0/a);
return x;
}
}
// generate a random number from a uniform distribution
double WeightedMinHash::uniform(double a, double b)
{
return rand() / (RAND_MAX + 1.0) * (b - a) + a;
}
// Generate a random number from a gaussion distribution
double WeightedMinHash::gauss(double mu,double sigma)
{
double x1, x2, w, y1, y2;
do {
x1 = 2.0 * uniform(0,1) - 1.0;
x2 = 2.0 * uniform(0,1) - 1.0;
w = x1 * x1 + x2 * x2;
} while ( w >= 1.0 );
w = sqrt( (-2.0 * log( w ) ) / w );
y1 = x1 * w;
y2 = x2 * w;
return mu+sigma*y1;
}
WeightedMinHash::~WeightedMinHash()
{
for (size_t i = 0; i < _numhashes; i++)
{
delete[] _rand_vec[i];
}
delete[] _rand_vec;
}