1212import ot
1313
1414
15-
1615#%% parameters
1716
18- n = 100 # nb bins
17+ n = 100 # nb bins
1918
2019# bin positions
21- x = np .arange (n ,dtype = np .float64 )
20+ x = np .arange (n , dtype = np .float64 )
2221
2322# Gaussian distributions
24- a = ot .datasets .get_1D_gauss (n ,m = 20 ,s = 5 ) # m= mean, s= std
25- b = ot .datasets .get_1D_gauss (n ,m = 60 ,s = 10 )
23+ a = ot .datasets .get_1D_gauss (n , m = 20 , s = 5 ) # m= mean, s= std
24+ b = ot .datasets .get_1D_gauss (n , m = 60 , s = 10 )
2625
2726# loss matrix
28- M = ot .dist (x .reshape ((n ,1 )),x .reshape ((n ,1 )))
29- M /= M .max ()
27+ M = ot .dist (x .reshape ((n , 1 )), x .reshape ((n , 1 )))
28+ M /= M .max ()
3029
3130#%% EMD
3231
33- G0 = ot .emd (a ,b , M )
32+ G0 = ot .emd (a , b , M )
3433
3534pl .figure (3 )
36- ot .plot .plot1D_mat (a ,b , G0 ,'OT matrix G0' )
35+ ot .plot .plot1D_mat (a , b , G0 , 'OT matrix G0' )
3736
3837#%% Example with Frobenius norm regularization
3938
40- def f (G ): return 0.5 * np .sum (G ** 2 )
41- def df (G ): return G
39+ def f (G ):
40+ return 0.5 * np .sum (G ** 2 )
41+
42+
43+ def df (G ):
44+ return G
4245
43- reg = 1e-1
46+ reg = 1e-1
4447
45- Gl2 = ot .optim .cg (a ,b , M , reg ,f , df ,verbose = True )
48+ Gl2 = ot .optim .cg (a , b , M , reg , f , df , verbose = True )
4649
4750pl .figure (3 )
48- ot .plot .plot1D_mat (a ,b , Gl2 ,'OT matrix Frob. reg' )
51+ ot .plot .plot1D_mat (a , b , Gl2 , 'OT matrix Frob. reg' )
4952
5053#%% Example with entropic regularization
5154
52- def f (G ): return np .sum (G * np .log (G ))
53- def df (G ): return np .log (G )+ 1
5455
55- reg = 1e-3
56+ def f (G ):
57+ return np .sum (G * np .log (G ))
58+
59+
60+ def df (G ):
61+ return np .log (G ) + 1.
5662
57- Ge = ot .optim .cg (a ,b ,M ,reg ,f ,df ,verbose = True )
63+ reg = 1e-3
64+
65+ Ge = ot .optim .cg (a , b , M , reg , f , df , verbose = True )
5866
5967pl .figure (4 )
60- ot .plot .plot1D_mat (a ,b , Ge ,'OT matrix Entrop. reg' )
68+ ot .plot .plot1D_mat (a , b , Ge , 'OT matrix Entrop. reg' )
6169
6270#%% Example with Frobenius norm + entropic regularization with gcg
6371
64- def f (G ): return 0.5 * np .sum (G ** 2 )
65- def df (G ): return G
6672
67- reg1 = 1e-3
68- reg2 = 1e-1
73+ def f (G ):
74+ return 0.5 * np .sum (G ** 2 )
75+
76+
77+ def df (G ):
78+ return G
79+
80+ reg1 = 1e-3
81+ reg2 = 1e-1
6982
70- Gel2 = ot .optim .gcg (a ,b , M , reg1 ,reg2 ,f , df ,verbose = True )
83+ Gel2 = ot .optim .gcg (a , b , M , reg1 , reg2 , f , df , verbose = True )
7184
7285pl .figure (5 )
73- ot .plot .plot1D_mat (a ,b , Gel2 ,'OT entropic + matrix Frob. reg' )
74- pl .show ()
86+ ot .plot .plot1D_mat (a , b , Gel2 , 'OT entropic + matrix Frob. reg' )
87+ pl .show ()
0 commit comments