r""" Computes optimal values and gradients at X for a strongly convex potential :math:`\varphi` with Lipschitz gradients on the partitions defined by `X_classes`, where :math:`\varphi` is optimal such that :math:`\nabla \varphi \#\mu \approx \nu`, given samples :math:`X = x_1, \cdots, x_
(
X,
V,
X_classes=None,
a=None,
b=None,
strongly_convex_constant=0.6,
gradient_lipschitz_constant=1.4,
its=100,
log=False,
init_method="barycentric",
solver=None,
)
| 21 | |
| 22 | |
| 23 | def nearest_brenier_potential_fit( |
| 24 | X, |
| 25 | V, |
| 26 | X_classes=None, |
| 27 | a=None, |
| 28 | b=None, |
| 29 | strongly_convex_constant=0.6, |
| 30 | gradient_lipschitz_constant=1.4, |
| 31 | its=100, |
| 32 | log=False, |
| 33 | init_method="barycentric", |
| 34 | solver=None, |
| 35 | ): |
| 36 | r""" |
| 37 | Computes optimal values and gradients at X for a strongly convex potential :math:`\varphi` with Lipschitz gradients |
| 38 | on the partitions defined by `X_classes`, where :math:`\varphi` is optimal such that |
| 39 | :math:`\nabla \varphi \#\mu \approx \nu`, given samples :math:`X = x_1, \cdots, x_n \sim \mu` and |
| 40 | :math:`V = v_1, \cdots, v_n \sim \nu`. Finding such a potential that has the desired regularity on the |
| 41 | partition :math:`(E_k)_{k \in [K]}` (given by the classes `X_classes`) is equivalent to finding optimal values |
| 42 | `phi` for the :math:`\varphi(x_i)` and its gradients :math:`\nabla \varphi(x_i)` (variable`G`). |
| 43 | In practice, these optimal values are found by solving the following problem |
| 44 | |
| 45 | .. math:: |
| 46 | :nowrap: |
| 47 | |
| 48 | \begin{gather*} |
| 49 | \text{min} \sum_{i,j}\pi_{i,j}\|g_i - v_j\|_2^2 \\ |
| 50 | g_1,\cdots, g_n \in \mathbb{R}^d,\; \varphi_1, \cdots, \varphi_n \in \mathbb{R},\; \pi \in \Pi(a, b) \\ |
| 51 | \text{s.t.}\ \forall k \in [K],\; \forall i,j \in I_k: \\ |
| 52 | \varphi_i-\varphi_j-\langle g_j, x_i-x_j\rangle \geq c_1\|g_i - g_j\|_2^2 |
| 53 | + c_2\|x_i-x_j\|_2^2 - c_3\langle g_j-g_i, x_j -x_i \rangle. |
| 54 | \end{gather*} |
| 55 | |
| 56 | The constants :math:`c_1, c_2, c_3` only depend on `strongly_convex_constant` and `gradient_lipschitz_constant`. |
| 57 | The constraint :math:`\pi \in \Pi(a, b)` denotes the fact that the matrix :math:`\pi` belong to the OT polytope |
| 58 | of marginals a and b. :math:`I_k` is the subset of :math:`[n]` of the i such that :math:`x_i` is in the |
| 59 | partition (or class) :math:`E_k`, i.e. `X_classes[i] == k`. |
| 60 | |
| 61 | This problem is solved by alternating over the variable :math:`\pi` and the variables :math:`\varphi_i, g_i`. |
| 62 | For :math:`\pi`, the problem is the standard discrete OT problem, and for :math:`\varphi_i, g_i`, the |
| 63 | problem is a convex QCQP solved using :code:`cvxpy` (ECOS solver). |
| 64 | |
| 65 | Accepts any compatible backend, but will perform the QCQP optimisation on Numpy arrays, and convert back at the end. |
| 66 | |
| 67 | .. warning:: This function requires the CVXPY library |
| 68 | .. warning:: Accepts any backend but will convert to Numpy then back to the backend. |
| 69 | |
| 70 | Parameters |
| 71 | ---------- |
| 72 | X : array-like (n, d) |
| 73 | reference points used to compute the optimal values phi and G |
| 74 | V : array-like (n, d) |
| 75 | values of the gradients at the reference points X |
| 76 | X_classes : array-like (n,), optional |
| 77 | classes of the reference points, defaults to a single class |
| 78 | a : array-like (n,), optional |
| 79 | weights for the reference points X, defaults to uniform |
| 80 | b : array-like (n,), optional |
no test coverage detected