summaryrefslogtreecommitdiff
path: root/docs/source/auto_examples/plot_free_support_barycenter.ipynb
blob: 05a81c8d1f029a930cfb19ad3a88f07a38fe50ad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# 2D free support Wasserstein barycenters of distributions\n\n\nIllustration of 2D Wasserstein barycenters if discributions that are weighted\nsum of diracs.\n\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "# Author: Vivien Seguy <vivien.seguy@iip.ist.i.kyoto-u.ac.jp>\n#\n# License: MIT License\n\nimport numpy as np\nimport matplotlib.pylab as pl\nimport ot"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Generate data\n -------------\n%% parameters and data generation\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "N = 3\nd = 2\nmeasures_locations = []\nmeasures_weights = []\n\nfor i in range(N):\n\n    n_i = np.random.randint(low=1, high=20)  # nb samples\n\n    mu_i = np.random.normal(0., 4., (d,))  # Gaussian mean\n\n    A_i = np.random.rand(d, d)\n    cov_i = np.dot(A_i, A_i.transpose())  # Gaussian covariance matrix\n\n    x_i = ot.datasets.make_2D_samples_gauss(n_i, mu_i, cov_i)  # Dirac locations\n    b_i = np.random.uniform(0., 1., (n_i,))\n    b_i = b_i / np.sum(b_i)  # Dirac weights\n\n    measures_locations.append(x_i)\n    measures_weights.append(b_i)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Compute free support barycenter\n-------------\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "k = 10  # number of Diracs of the barycenter\nX_init = np.random.normal(0., 1., (k, d))  # initial Dirac locations\nb = np.ones((k,)) / k  # weights of the barycenter (it will not be optimized, only the locations are optimized)\n\nX = ot.lp.free_support_barycenter(measures_locations, measures_weights, X_init, b)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Plot data\n---------\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "pl.figure(1)\nfor (x_i, b_i) in zip(measures_locations, measures_weights):\n    color = np.random.randint(low=1, high=10 * N)\n    pl.scatter(x_i[:, 0], x_i[:, 1], s=b * 1000, label='input measure')\npl.scatter(X[:, 0], X[:, 1], s=b * 1000, c='black', marker='^', label='2-Wasserstein barycenter')\npl.title('Data measures and their barycenter')\npl.legend(loc=0)\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.6.5"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}