r""" The goal is to recover u from the c-transform. The function computes the c-transform of a dual variable from the other dual variable: .. math:: \mathbf{u} = \mathbf{v}^{c,reg} = - \mathrm{reg} \sum_j \mathbf{b}_j \exp\left( \frac{\mathbf{v} - \mathbf{M}}{\mathr
(b, M, reg, beta)
| 223 | |
| 224 | |
| 225 | def c_transform_entropic(b, M, reg, beta): |
| 226 | r""" |
| 227 | The goal is to recover u from the c-transform. |
| 228 | |
| 229 | The function computes the c-transform of a dual variable from the other |
| 230 | dual variable: |
| 231 | |
| 232 | .. math:: |
| 233 | \mathbf{u} = \mathbf{v}^{c,reg} = - \mathrm{reg} \sum_j \mathbf{b}_j |
| 234 | \exp\left( \frac{\mathbf{v} - \mathbf{M}}{\mathrm{reg}} \right) |
| 235 | |
| 236 | Where : |
| 237 | |
| 238 | - :math:`\mathbf{M}` is the (`ns`, `nt`) metric cost matrix |
| 239 | - :math:`\mathbf{u}`, :math:`\mathbf{v}` are dual variables in :math:`\mathbb{R}^{ns} \times \mathbb{R}^{nt}` |
| 240 | - reg is the regularization term |
| 241 | |
| 242 | It is used to recover an optimal u from optimal v solving the semi dual |
| 243 | problem, see Proposition 2.1 of :ref:`[18] <references-c-transform-entropic>` |
| 244 | |
| 245 | |
| 246 | Parameters |
| 247 | ---------- |
| 248 | b : ndarray, shape (nt,) |
| 249 | Target measure |
| 250 | M : ndarray, shape (ns, nt) |
| 251 | Cost matrix |
| 252 | reg : float |
| 253 | Regularization term > 0 |
| 254 | v : ndarray, shape (nt,) |
| 255 | Dual variable. |
| 256 | |
| 257 | Returns |
| 258 | ------- |
| 259 | u : ndarray, shape (`ns`,) |
| 260 | Dual variable. |
| 261 | |
| 262 | |
| 263 | .. _references-c-transform-entropic: |
| 264 | References |
| 265 | ---------- |
| 266 | .. [18] Genevay, A., Cuturi, M., Peyré, G. & Bach, F. (2016) |
| 267 | Stochastic Optimization for Large-scale Optimal Transport. |
| 268 | Advances in Neural Information Processing Systems (2016). |
| 269 | """ |
| 270 | |
| 271 | n_source = np.shape(M)[0] |
| 272 | alpha = np.zeros(n_source) |
| 273 | for i in range(n_source): |
| 274 | r = M[i, :] - beta |
| 275 | min_r = np.min(r) |
| 276 | exp_beta = np.exp(-(r - min_r) / reg) * b |
| 277 | alpha[i] = min_r - reg * np.log(np.sum(exp_beta)) |
| 278 | return alpha |
| 279 | |
| 280 | |
| 281 | def solve_semi_dual_entropic( |