summaryrefslogtreecommitdiff
path: root/test/test_emd.py
blob: 4757cd174ac844e2bd2ceb9eeffdc9c047a638fb (plain)
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
#!/usr/bin/env python2
# -*- coding: utf-8 -*-

import numpy as np
import pylab as pl
import ot

from ot.datasets import get_1D_gauss as gauss
reload(ot.lp)

#%% parameters

n=5000 # nb bins
m=6000 # nb bins

mean1 = 1000
mean2 = 1100

tol = 1e-6

# bin positions
x=np.arange(n,dtype=np.float64)
y=np.arange(m,dtype=np.float64)

# Gaussian distributions
a=gauss(n,m=mean1,s=5) # m= mean, s= std

b=gauss(m,m=mean2,s=10)

# loss matrix
M=ot.dist(x.reshape((-1,1)), y.reshape((-1,1))) ** (1./2)
print M[0,:]
#M/=M.max()

#%%

print('Computing {} EMD '.format(1))

# emd loss 1 proc
ot.tic()
G = ot.emd(a,b,M)
ot.toc('1 proc : {} s')

cost1 = (G * M).sum()

# emd loss 1 proc
ot.tic()
cost_emd2 = ot.emd2(a,b,M)
ot.toc('1 proc : {} s')

ot.tic()
G = ot.emd(b, a, np.ascontiguousarray(M.T))
ot.toc('1 proc : {} s')

cost2 = (G * M.T).sum()

assert np.abs(cost1-cost_emd2)/np.abs(cost1) < tol
assert np.abs(cost1-cost2)/np.abs(cost1) < tol
assert np.abs(cost1-np.abs(mean1-mean2))/np.abs(cost1) < tol