{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Computing 1-dimensional Barycenters via d-MMOT\n\n

Note

Example added in release: 0.9.1.

\n\nWhen the cost is discretized (Monge), the d-MMOT solver can more quickly\ncompute and minimize the distance between many distributions without the need\nfor intermediate barycenter computations. This example compares the time to\nidentify, and the quality of, solutions for the d-MMOT problem using a\nprimal/dual algorithm and classical LP barycenter approaches.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Author: Ronak Mehta \n# Xizheng Yu \n#\n# License: MIT License\n\n# sphinx_gallery_thumbnail_number = 2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Generating 2 distributions\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import numpy as np\nimport matplotlib.pyplot as pl\nimport ot\n\nnp.random.seed(0)\n\nn = 100\nd = 2\n# Gaussian distributions\na1 = ot.datasets.make_1D_gauss(n, m=20, s=5) # m=mean, s=std\na2 = ot.datasets.make_1D_gauss(n, m=60, s=8)\nA = np.vstack((a1, a2)).T\nx = np.arange(n, dtype=np.float64)\nM = ot.utils.dist(x.reshape((n, 1)), metric=\"minkowski\")\n\npl.figure(1, figsize=(6.4, 3))\npl.plot(x, a1, \"b\", label=\"Source distribution\")\npl.plot(x, a2, \"r\", label=\"Target distribution\")\npl.legend()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Minimize the distances among distributions, identify the Barycenter\nThe objective being minimized is different for both methods, so the objective\nvalues cannot be compared.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# L2 Iteration\nweights = np.ones(d) / d\nl2_bary = A.dot(weights)\n\nprint(\"LP Iterations:\")\nweights = np.ones(d) / d\nlp_bary, lp_log = ot.lp.barycenter(\n A, M, weights, solver=\"interior-point\", verbose=False, log=True\n)\nprint(\"Time\\t: \", ot.toc(\"\"))\nprint(\"Obj\\t: \", lp_log[\"fun\"])\n\nprint(\"\")\nprint(\"Discrete MMOT Algorithm:\")\not.tic()\nbarys, log = ot.lp.dmmot_monge_1dgrid_optimize(\n A, niters=4000, lr_init=1e-5, lr_decay=0.997, log=True\n)\ndmmot_obj = log[\"primal objective\"]\nprint(\"Time\\t: \", ot.toc(\"\"))\nprint(\"Obj\\t: \", dmmot_obj)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Compare Barycenters in both methods\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "pl.figure(1, figsize=(6.4, 3))\nfor i in range(len(barys)):\n if i == 0:\n pl.plot(x, barys[i], \"g-*\", label=\"Discrete MMOT\")\n else:\n continue\n # pl.plot(x, barys[i], 'g-*')\npl.plot(x, lp_bary, label=\"LP Barycenter\")\npl.plot(x, l2_bary, label=\"L2 Barycenter\")\npl.plot(x, a1, \"b\", label=\"Source distribution\")\npl.plot(x, a2, \"r\", label=\"Target distribution\")\npl.title(\"Monge Cost: Barycenters from LP Solver and dmmot solver\")\npl.legend()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## More than 2 distributions\nGenerate 7 pseudorandom gaussian distributions with 50 bins.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "n = 50 # nb bins\nd = 7\nvecsize = n * d\n\ndata = []\nfor i in range(d):\n m = n * (0.5 * np.random.rand(1)) * float(np.random.randint(2) + 1)\n a = ot.datasets.make_1D_gauss(n, m=m, s=5)\n data.append(a)\n\nx = np.arange(n, dtype=np.float64)\nM = ot.utils.dist(x.reshape((n, 1)), metric=\"minkowski\")\nA = np.vstack(data).T\n\npl.figure(1, figsize=(6.4, 3))\nfor i in range(len(data)):\n pl.plot(x, data[i])\n\npl.title(\"Distributions\")\npl.legend()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Minimizing Distances Among Many Distributions\nThe objective being minimized is different for both methods, so the objective\nvalues cannot be compared.\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# Perform gradient descent optimization using the d-MMOT method.\nbarys = ot.lp.dmmot_monge_1dgrid_optimize(A, niters=3000, lr_init=1e-4, lr_decay=0.997)\n\n# after minimization, any distribution can be used as a estimate of barycenter.\nbary = barys[0]\n\n# Compute 1D Wasserstein barycenter using the L2/LP method\nweights = ot.unif(d)\nl2_bary = A.dot(weights)\nlp_bary, bary_log = ot.lp.barycenter(\n A, M, weights, solver=\"interior-point\", verbose=False, log=True\n)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Compare Barycenters in both methods\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "pl.figure(1, figsize=(6.4, 3))\npl.plot(x, bary, \"g-*\", label=\"Discrete MMOT\")\npl.plot(x, l2_bary, \"k\", label=\"L2 Barycenter\")\npl.plot(x, lp_bary, \"k-\", label=\"LP Wasserstein\")\npl.title(\"Barycenters\")\npl.legend()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Compare with original distributions\n\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "pl.figure(1, figsize=(6.4, 3))\nfor i in range(len(data)):\n pl.plot(x, data[i])\nfor i in range(len(barys)):\n if i == 0:\n pl.plot(x, barys[i], \"g-*\", label=\"Discrete MMOT\")\n else:\n continue\n # pl.plot(x, barys[i], 'g')\npl.plot(x, l2_bary, \"k^\", label=\"L2\")\npl.plot(x, lp_bary, \"o\", color=\"grey\", label=\"LP\")\npl.title(\"Barycenters\")\npl.legend()\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 }