{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Optimal Transport for fixed support\n\nThis example illustrates the computation of EMD and Sinkhorn transport plans\nand their visualization.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Author: Remi Flamary \n#\n# License: MIT License\n# sphinx_gallery_thumbnail_number = 3\n\nimport numpy as np\nimport matplotlib.pylab as pl\nimport ot\nimport ot.plot\nfrom ot.datasets import make_1D_gauss as gauss" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Generate data\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "n = 100 # nb bins\n\n# bin positions\nx = np.arange(n, dtype=np.float64)\n\n# Gaussian distributions\na = gauss(n, m=20, s=5) # m= mean, s= std\nb = gauss(n, m=60, s=10)\n\n# loss matrix\nM = ot.dist(x.reshape((n, 1)), x.reshape((n, 1)))\nM /= M.max()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Plot distributions and loss matrix\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "pl.figure(1, figsize=(6.4, 3))\npl.plot(x, a, \"b\", label=\"Source distribution\")\npl.plot(x, b, \"r\", label=\"Target distribution\")\npl.legend()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "pl.figure(2, figsize=(5, 5))\not.plot.plot1D_mat(a, b, M, \"Cost matrix M\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Solve Exact OT\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# use fast 1D solver\nG0 = ot.emd_1d(x, x, a, b)\n\n# Equivalent to\n# G0 = ot.emd(a, b, M)\n\npl.figure(3, figsize=(5, 5))\not.plot.plot1D_mat(a, b, G0, \"OT matrix G0\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Solve Sinkhorn\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "lambd = 1e-3\nGs = ot.sinkhorn(a, b, M, lambd, verbose=True)\n\npl.figure(4, figsize=(5, 5))\not.plot.plot1D_mat(a, b, Gs, \"OT matrix Sinkhorn\")\n\npl.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Solve Smooth OT\nWe illustrate below Smooth and Sparse (KL an L2 reg.) OT and\nsparsity-constrained OT, together with their visualizations.\n\nReference:\n\nBlondel, M., Seguy, V., & Rolet, A. (2018). Smooth and Sparse Optimal\nTransport. Proceedings of the # Twenty-First International Conference on\nArtificial Intelligence and # Statistics (AISTATS).\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "lambd = 2e-3\nGsm = ot.smooth.smooth_ot_dual(a, b, M, lambd, reg_type=\"kl\")\n\npl.figure(3, figsize=(5, 5))\not.plot.plot1D_mat(a, b, Gsm, \"OT matrix Smooth OT KL reg.\")\n\npl.show()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "lambd = 1e-1\nGsm = ot.smooth.smooth_ot_dual(a, b, M, lambd, reg_type=\"l2\")\n\npl.figure(5, figsize=(5, 5))\not.plot.plot1D_mat(a, b, Gsm, \"OT matrix Smooth OT l2 reg.\")\n\npl.show()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "lambd = 1e-1\n\nmax_nz = 2 # two non-zero entries are permitted per column of the OT plan\nGsc = ot.smooth.smooth_ot_dual(\n a, b, M, lambd, reg_type=\"sparsity_constrained\", max_nz=max_nz\n)\npl.figure(6, figsize=(5, 5))\not.plot.plot1D_mat(a, b, Gsc, \"Sparsity constrained OT matrix; k=2.\")\n\npl.show()" ] } ], "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 }