MCPcopy Create free account
hub / github.com/PythonOT/POT / emd

Function emd

ot/lp/_network_simplex.py:166–349  ·  view source on GitHub ↗

r"""Solves the Earth Movers distance problem and returns the OT matrix .. math:: \gamma = \mathop{\arg \min}_\gamma \quad \langle \gamma, \mathbf{M} \rangle_F s.t. \ \gamma \mathbf{1} = \mathbf{a} \gamma^T \mathbf{1} = \mathbf{b} \gamma \geq 0

(
    a,
    b,
    M,
    numItermax=100000,
    log=False,
    center_dual=True,
    numThreads=1,
    check_marginals=True,
)

Source from the content-addressed store, hash-verified

164
165
166def emd(
167 a,
168 b,
169 M,
170 numItermax=100000,
171 log=False,
172 center_dual=True,
173 numThreads=1,
174 check_marginals=True,
175):
176 r"""Solves the Earth Movers distance problem and returns the OT matrix
177
178
179 .. math::
180 \gamma = \mathop{\arg \min}_\gamma \quad \langle \gamma, \mathbf{M} \rangle_F
181
182 s.t. \ \gamma \mathbf{1} = \mathbf{a}
183
184 \gamma^T \mathbf{1} = \mathbf{b}
185
186 \gamma \geq 0
187
188 where :
189
190 - :math:`\mathbf{M}` is the metric cost matrix
191 - :math:`\mathbf{a}` and :math:`\mathbf{b}` are the sample weights
192
193 .. warning:: Note that the :math:`\mathbf{M}` matrix in numpy needs to be a C-order
194 numpy.array in float64 format. It will be converted if not in this
195 format
196
197 .. note:: This function is backend-compatible and will work on arrays
198 from all compatible backends. But the algorithm uses the C++ CPU backend
199 which can lead to copy overhead on GPU arrays.
200
201 .. note:: This function will cast the computed transport plan to the data type
202 of the provided input with the following priority: :math:`\mathbf{a}`,
203 then :math:`\mathbf{b}`, then :math:`\mathbf{M}` if marginals are not provided.
204 Casting to an integer tensor might result in a loss of precision.
205 If this behaviour is unwanted, please make sure to provide a
206 floating point input.
207
208 .. note:: An error will be raised if the vectors :math:`\mathbf{a}` and :math:`\mathbf{b}` do not sum to the same value.
209
210 Uses the algorithm proposed in :ref:`[1] <references-emd>`.
211
212 Parameters
213 ----------
214 a : (ns,) array-like, float
215 Source histogram (uniform weight if empty list)
216 b : (nt,) array-like, float
217 Target histogram (uniform weight if empty list)
218 M : (ns,nt) array-like, float
219 Loss matrix (c-order array in numpy with type float64)
220 numItermax : int, optional (default=100000)
221 The maximum number of iterations before stopping the optimization
222 algorithm if it has not converged.
223 log: bool, optional (default=False)

Callers 15

lp_solverFunction · 0.85
solve_otFunction · 0.85
co_optimal_transportFunction · 0.85
joint_OT_mapping_linearFunction · 0.85
joint_OT_mapping_kernelFunction · 0.85
gmm_ot_planFunction · 0.85
fitMethod · 0.85
partial_wassersteinFunction · 0.85
free_support_barycenterFunction · 0.85

Calls 11

list_to_arrayFunction · 0.85
get_backendFunction · 0.85
check_number_threadsFunction · 0.85
center_ot_dualFunction · 0.85
to_numpyMethod · 0.80
from_numpyMethod · 0.80
onesMethod · 0.45
sumMethod · 0.45
anyMethod · 0.45
is_floating_pointMethod · 0.45

Tested by

no test coverage detected