Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 5 additions & 17 deletions _lectures/07-non-gaussian-likelihoods.html
Original file line number Diff line number Diff line change
Expand Up @@ -71,23 +71,11 @@ <h1 id="non-gaussian-likelihoods">Non Gaussian Likelihoods</h1>
<p>[Gaussian processes model functions. If our observation is a corrupted version of this function and the corruption process is <em>also</em> Gaussian, it is trivial to account for this. However, there are many circumstances where our observation may be non Gaussian. In these cases we need to turn to approximate inference techniques. As a simple illustration, we’ll use a dataset of binary observations of the language that is spoken in different regions of East-Timor. First we will load the data and a couple of libraries to visualize it.}</p>
<div class="sourceCode" id="cb10"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb10-1"><a href="#cb10-1" aria-hidden="true" tabindex="-1"></a><span class="im">from</span> matplotlib.patches <span class="im">import</span> Polygon</span>
<span id="cb10-2"><a href="#cb10-2" aria-hidden="true" tabindex="-1"></a><span class="im">from</span> matplotlib.collections <span class="im">import</span> PatchCollection</span>
<span id="cb10-3"><a href="#cb10-3" aria-hidden="true" tabindex="-1"></a><span class="im">from</span> pathlib <span class="im">import</span> Path</span>
<span id="cb10-4"><a href="#cb10-4" aria-hidden="true" tabindex="-1"></a><span class="im">import</span> pickle</span>
<span id="cb10-5"><a href="#cb10-5" aria-hidden="true" tabindex="-1"></a><span class="im">import</span> urllib.request</span></code></pre></div>
<div class="sourceCode" id="cb11"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb11-1"><a href="#cb11-1" aria-hidden="true" tabindex="-1"></a><span class="co"># Load East Timor language regions (local copy preferred; remote host is unreliable)</span></span>
<span id="cb11-2"><a href="#cb11-2" aria-hidden="true" tabindex="-1"></a>data_path <span class="op">=</span> Path(<span class="st">&#39;data/EastTimor2.pickle&#39;</span>)</span>
<span id="cb11-3"><a href="#cb11-3" aria-hidden="true" tabindex="-1"></a><span class="cf">if</span> <span class="kw">not</span> data_path.exists():</span>
<span id="cb11-4"><a href="#cb11-4" aria-hidden="true" tabindex="-1"></a> data_path <span class="op">=</span> Path(<span class="st">&#39;EastTimor2.pickle&#39;</span>)</span>
<span id="cb11-5"><a href="#cb11-5" aria-hidden="true" tabindex="-1"></a><span class="cf">if</span> <span class="kw">not</span> data_path.exists():</span>
<span id="cb11-6"><a href="#cb11-6" aria-hidden="true" tabindex="-1"></a> url <span class="op">=</span> <span class="st">&#39;http://staffwww.dcs.shef.ac.uk/people/M.Zwiessele/gpss/lab2/EastTimor.pickle&#39;</span></span>
<span id="cb11-7"><a href="#cb11-7" aria-hidden="true" tabindex="-1"></a> urllib.request.urlretrieve(url, <span class="st">&#39;EastTimor2.pickle&#39;</span>)</span>
<span id="cb11-8"><a href="#cb11-8" aria-hidden="true" tabindex="-1"></a> data_path <span class="op">=</span> Path(<span class="st">&#39;EastTimor2.pickle&#39;</span>)</span>
<span id="cb11-9"><a href="#cb11-9" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb11-10"><a href="#cb11-10" aria-hidden="true" tabindex="-1"></a><span class="cf">with</span> <span class="bu">open</span>(data_path, <span class="st">&#39;rb&#39;</span>) <span class="im">as</span> f:</span>
<span id="cb11-11"><a href="#cb11-11" aria-hidden="true" tabindex="-1"></a> <span class="cf">try</span>:</span>
<span id="cb11-12"><a href="#cb11-12" aria-hidden="true" tabindex="-1"></a> X, y, polygons <span class="op">=</span> pickle.load(f, encoding<span class="op">=</span><span class="st">&#39;latin1&#39;</span>)</span>
<span id="cb11-13"><a href="#cb11-13" aria-hidden="true" tabindex="-1"></a> <span class="cf">except</span> <span class="pp">TypeError</span>:</span>
<span id="cb11-14"><a href="#cb11-14" aria-hidden="true" tabindex="-1"></a> X, y, polygons <span class="op">=</span> pickle.load(f)</span></code></pre></div>
<span id="cb10-3"><a href="#cb10-3" aria-hidden="true" tabindex="-1"></a><span class="im">import</span> pods</span></code></pre></div>
<div class="sourceCode" id="cb11"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb11-1"><a href="#cb11-1" aria-hidden="true" tabindex="-1"></a>data <span class="op">=</span> pods.datasets.east_timor()</span>
<span id="cb11-2"><a href="#cb11-2" aria-hidden="true" tabindex="-1"></a>X <span class="op">=</span> data[<span class="st">&#39;X&#39;</span>]</span>
<span id="cb11-3"><a href="#cb11-3" aria-hidden="true" tabindex="-1"></a>y <span class="op">=</span> data[<span class="st">&#39;Y&#39;</span>]</span>
<span id="cb11-4"><a href="#cb11-4" aria-hidden="true" tabindex="-1"></a>polygons <span class="op">=</span> data[<span class="st">&#39;polygons&#39;</span>]</span></code></pre></div>
<p>Now we will create a map of East Timor and, using GPy, plot the data on top of it. A classification model can be defined in a similar way to the regression model, but now using <code>GPy.models.GPClassification</code>. However, once we’ve define the model, we also need to update the approximation to the likelihood. This runs the Expectation propagation updates.</p>
<div class="sourceCode" id="cb12"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb12-1"><a href="#cb12-1" aria-hidden="true" tabindex="-1"></a><span class="im">import</span> GPy</span></code></pre></div>
<div class="sourceCode" id="cb13"><pre class="sourceCode python"><code class="sourceCode python"><span id="cb13-1"><a href="#cb13-1" aria-hidden="true" tabindex="-1"></a><span class="co">#Define the model</span></span>
Expand Down
22 changes: 5 additions & 17 deletions _notebooks/07-non-gaussian-likelihoods.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -278,9 +278,7 @@
"source": [
"from matplotlib.patches import Polygon\n",
"from matplotlib.collections import PatchCollection\n",
"from pathlib import Path\n",
"import pickle\n",
"import urllib.request"
"import pods"
],
"id": "bf80c131-13db-46da-b32a-977525075286"
},
Expand All @@ -290,20 +288,10 @@
"metadata": {},
"outputs": [],
"source": [
"# Load East Timor language regions (local copy preferred; remote host is unreliable)\n",
"data_path = Path('data/EastTimor2.pickle')\n",
"if not data_path.exists():\n",
" data_path = Path('EastTimor2.pickle')\n",
"if not data_path.exists():\n",
" url = 'http://staffwww.dcs.shef.ac.uk/people/M.Zwiessele/gpss/lab2/EastTimor.pickle'\n",
" urllib.request.urlretrieve(url, 'EastTimor2.pickle')\n",
" data_path = Path('EastTimor2.pickle')\n",
"\n",
"with open(data_path, 'rb') as f:\n",
" try:\n",
" X, y, polygons = pickle.load(f, encoding='latin1')\n",
" except TypeError:\n",
" X, y, polygons = pickle.load(f)"
"data = pods.datasets.east_timor()\n",
"X = data['X']\n",
"y = data['Y']\n",
"polygons = data['polygons']"
],
"id": "03afdd84-a321-4c43-8ad7-f8a49c474a3b"
},
Expand Down
Loading