{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Weak Optimal Transport VS exact Optimal Transport\n\n

Note

Example added in release: 0.8.2.

\n\nIllustration of 2D optimal transport between distributions that are weighted\nsum of Diracs. The OT matrix is plotted with the samples.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Author: Remi Flamary \n#\n# License: MIT License\n\n# sphinx_gallery_thumbnail_number = 4\n\nimport numpy as np\nimport matplotlib.pylab as pl\nimport ot\nimport ot.plot" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Generate data an plot it\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "n = 50 # nb samples\n\nmu_s = np.array([0, 0])\ncov_s = np.array([[1, 0], [0, 1]])\n\nmu_t = np.array([4, 4])\ncov_t = np.array([[1, -0.8], [-0.8, 1]])\n\nxs = ot.datasets.make_2D_samples_gauss(n, mu_s, cov_s)\nxt = ot.datasets.make_2D_samples_gauss(n, mu_t, cov_t)\n\na, b = ot.unif(n), ot.unif(n) # uniform distribution on samples\n\n# loss matrix\nM = ot.dist(xs, xt)\nM /= M.max()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "pl.figure(1)\npl.plot(xs[:, 0], xs[:, 1], \"+b\", label=\"Source samples\")\npl.plot(xt[:, 0], xt[:, 1], \"xr\", label=\"Target samples\")\npl.legend(loc=0)\npl.title(\"Source and target distributions\")\n\npl.figure(2)\npl.imshow(M, interpolation=\"nearest\")\npl.title(\"Cost matrix M\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Compute Weak OT and exact OT solutions\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "G0 = ot.emd(a, b, M)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "Gweak = ot.weak_optimal_transport(xs, xt, a, b)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Plot weak OT and exact OT solutions\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "pl.figure(3, (8, 5))\n\npl.subplot(1, 2, 1)\npl.imshow(G0, interpolation=\"nearest\")\npl.title(\"OT matrix\")\n\npl.subplot(1, 2, 2)\npl.imshow(Gweak, interpolation=\"nearest\")\npl.title(\"Weak OT matrix\")\n\npl.figure(4, (8, 5))\n\npl.subplot(1, 2, 1)\not.plot.plot2D_samples_mat(xs, xt, G0, c=[0.5, 0.5, 1])\npl.plot(xs[:, 0], xs[:, 1], \"+b\", label=\"Source samples\")\npl.plot(xt[:, 0], xt[:, 1], \"xr\", label=\"Target samples\")\npl.title(\"OT matrix with samples\")\n\npl.subplot(1, 2, 2)\not.plot.plot2D_samples_mat(xs, xt, Gweak, c=[0.5, 0.5, 1])\npl.plot(xs[:, 0], xs[:, 1], \"+b\", label=\"Source samples\")\npl.plot(xt[:, 0], xt[:, 1], \"xr\", label=\"Target samples\")\npl.title(\"Weak OT matrix with samples\")" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.18" } }, "nbformat": 4, "nbformat_minor": 0 }