{ "nbformat_minor": 0, "nbformat": 4, "cells": [ { "execution_count": null, "cell_type": "code", "source": [ "%matplotlib inline" ], "outputs": [], "metadata": { "collapsed": false } }, { "source": [ "\n# 2D Optimal transport for different metrics\n\n\nStole the figure idea from Fig. 1 and 2 in\nhttps://arxiv.org/pdf/1706.07650.pdf\n\n\n\n" ], "cell_type": "markdown", "metadata": {} }, { "execution_count": null, "cell_type": "code", "source": [ "# Author: Remi Flamary \n#\n# License: MIT License\n\nimport numpy as np\nimport matplotlib.pylab as pl\nimport ot\n\n#%% parameters and data generation\n\nfor data in range(2):\n\n if data:\n n = 20 # nb samples\n xs = np.zeros((n, 2))\n xs[:, 0] = np.arange(n) + 1\n xs[:, 1] = (np.arange(n) + 1) * -0.001 # to make it strictly convex...\n\n xt = np.zeros((n, 2))\n xt[:, 1] = np.arange(n) + 1\n else:\n\n n = 50 # nb samples\n xtot = np.zeros((n + 1, 2))\n xtot[:, 0] = np.cos(\n (np.arange(n + 1) + 1.0) * 0.9 / (n + 2) * 2 * np.pi)\n xtot[:, 1] = np.sin(\n (np.arange(n + 1) + 1.0) * 0.9 / (n + 2) * 2 * np.pi)\n\n xs = xtot[:n, :]\n xt = xtot[1:, :]\n\n a, b = ot.unif(n), ot.unif(n) # uniform distribution on samples\n\n # loss matrix\n M1 = ot.dist(xs, xt, metric='euclidean')\n M1 /= M1.max()\n\n # loss matrix\n M2 = ot.dist(xs, xt, metric='sqeuclidean')\n M2 /= M2.max()\n\n # loss matrix\n Mp = np.sqrt(ot.dist(xs, xt, metric='euclidean'))\n Mp /= Mp.max()\n\n #%% plot samples\n\n pl.figure(1 + 3 * data, figsize=(7, 3))\n pl.clf()\n pl.plot(xs[:, 0], xs[:, 1], '+b', label='Source samples')\n pl.plot(xt[:, 0], xt[:, 1], 'xr', label='Target samples')\n pl.axis('equal')\n pl.title('Source and traget distributions')\n\n pl.figure(2 + 3 * data, figsize=(7, 3))\n\n pl.subplot(1, 3, 1)\n pl.imshow(M1, interpolation='nearest')\n pl.title('Euclidean cost')\n\n pl.subplot(1, 3, 2)\n pl.imshow(M2, interpolation='nearest')\n pl.title('Squared Euclidean cost')\n\n pl.subplot(1, 3, 3)\n pl.imshow(Mp, interpolation='nearest')\n pl.title('Sqrt Euclidean cost')\n pl.tight_layout()\n\n #%% EMD\n G1 = ot.emd(a, b, M1)\n G2 = ot.emd(a, b, M2)\n Gp = ot.emd(a, b, Mp)\n\n pl.figure(3 + 3 * data, figsize=(7, 3))\n\n pl.subplot(1, 3, 1)\n ot.plot.plot2D_samples_mat(xs, xt, G1, c=[.5, .5, 1])\n pl.plot(xs[:, 0], xs[:, 1], '+b', label='Source samples')\n pl.plot(xt[:, 0], xt[:, 1], 'xr', label='Target samples')\n pl.axis('equal')\n # pl.legend(loc=0)\n pl.title('OT Euclidean')\n\n pl.subplot(1, 3, 2)\n ot.plot.plot2D_samples_mat(xs, xt, G2, c=[.5, .5, 1])\n pl.plot(xs[:, 0], xs[:, 1], '+b', label='Source samples')\n pl.plot(xt[:, 0], xt[:, 1], 'xr', label='Target samples')\n pl.axis('equal')\n # pl.legend(loc=0)\n pl.title('OT squared Euclidean')\n\n pl.subplot(1, 3, 3)\n ot.plot.plot2D_samples_mat(xs, xt, Gp, c=[.5, .5, 1])\n pl.plot(xs[:, 0], xs[:, 1], '+b', label='Source samples')\n pl.plot(xt[:, 0], xt[:, 1], 'xr', label='Target samples')\n pl.axis('equal')\n # pl.legend(loc=0)\n pl.title('OT sqrt Euclidean')\n pl.tight_layout()\n\npl.show()" ], "outputs": [], "metadata": { "collapsed": false } } ], "metadata": { "kernelspec": { "display_name": "Python 2", "name": "python2", "language": "python" }, "language_info": { "mimetype": "text/x-python", "nbconvert_exporter": "python", "name": "python", "file_extension": ".py", "version": "2.7.12", "pygments_lexer": "ipython2", "codemirror_mode": { "version": 2, "name": "ipython" } } } }