You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Add Wasserstein distance (aka Earth Mover's distance) to linfa-nn (#440)
* feat(linfa-nn): Added Earth Mover's Distance
* Changed EarthMoverDist as WassersteinDist and use tests from SciPy
* Added SciPy version that was used for comparison to Wasserstein distance documentation
/// Computes a similarity matrix with gaussian kernel and scaling parameter `eps`
122
148
///
123
149
/// The generated matrix is a upper triangular matrix with dimension NxN (number of observations) and contains the similarity between all permutations of observations
@@ -157,6 +183,7 @@ mod test {
157
183
has_autotraits::<L2Dist>();
158
184
has_autotraits::<LInfDist>();
159
185
has_autotraits::<LpDist<f64>>();
186
+
has_autotraits::<WassersteinDist>();
160
187
}
161
188
162
189
fndist_test<D:Distance<f64>>(dist:D,result:f64){
@@ -204,4 +231,98 @@ mod test {
204
231
fnlp_dist(){
205
232
dist_test(LpDist(3.3),4.635);
206
233
}
234
+
235
+
#[test]
236
+
fnwasserstein_dist(){
237
+
dist_test(WassersteinDist,4.2);
238
+
}
239
+
240
+
// The following Wasserstein tests are from SciPy (v1.17.0).
241
+
// However, since SciPy Wasserstein distance has different API as ours,
242
+
// we need to first transform the SciPy parameters into histograms that our API accepts.
243
+
//
244
+
// For example, SciPy values `[1, 3]` with weights `[1, 9]`:
245
+
// At index 1 we have weight 1 out of total weight 10 => 0.1.
246
+
// At index 3 we have weight 9 out of total weight 10 => 0.9.
247
+
// Thus we get a histogram [0.0, 0.1, 0.0, 0.9]
248
+
249
+
#[test]
250
+
/// For basic distributions, the value of the Wasserstein distance is straightforward.
0 commit comments