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,
)
| 164 | |
| 165 | |
| 166 | def 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) |
no test coverage detected