<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="assets/xml/rss.xsl" media="all"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Thomas House's Personal Website</title><link>https://tah-sci.com/</link><description>Thomas House's Personal Website</description><atom:link href="https://tah-sci.com/rss.xml" rel="self" type="application/rss+xml"></atom:link><language>en</language><copyright>Contents © 2025 &lt;a href="mailto:thomas.house@manchester.ac.uk"&gt;Thomas House&lt;/a&gt; </copyright><lastBuildDate>Wed, 15 Oct 2025 11:40:22 GMT</lastBuildDate><generator>Nikola (getnikola.com)</generator><docs>http://blogs.law.harvard.edu/tech/rss</docs><item><title>Treatment of boundaries in Markov chain Monte Carlo</title><link>https://tah-sci.com/posts/treatment-of-boundaries-in-markov-chain-monte-carlo.html</link><dc:creator>Thomas House</dc:creator><description>&lt;p&gt;I have been asked by multiple students what to do with MCMC at
boundaries, in particular how to treat proposals that lie outside the
support of the target. Suppose we have a vector of parameters
&lt;span class="math"&gt;\(\theta\)&lt;/span&gt; and want to target a density (proportional to)
&lt;span class="math"&gt;\(\pi(\theta)\)&lt;/span&gt; using a proposal distribution
&lt;span class="math"&gt;\(q(\theta'|\theta)\)&lt;/span&gt;. In MCMC, we seek to define a Markov process
in discrete time, &lt;span class="math"&gt;\((\theta_0, \theta_1, \ldots, \theta_N)\)&lt;/span&gt; that
has stationary distribution with density &lt;span class="math"&gt;\(\pi\)&lt;/span&gt;. In the Metropolis
Hastings scheme, the law of such a process is that at step &lt;span class="math"&gt;\(m\)&lt;/span&gt;,
when the state of the system is &lt;span class="math"&gt;\(\theta_m\)&lt;/span&gt;, we pick a proposed new
value &lt;span class="math"&gt;\(\tilde{\theta}\)&lt;/span&gt; with density &lt;span class="math"&gt;\(q(\cdot | \theta_m)\)&lt;/span&gt;,
then&lt;/p&gt;
&lt;div class="math"&gt;
\begin{equation*}
\mathrm{Pr}(\theta_{m+1} = \tilde{\theta}) = \mathrm{min}\left( 1, \frac{\pi(\tilde{\theta}) q(\theta | \tilde{\theta})}{\pi(\theta)q(\tilde{\theta} | \theta)}\right) \, , \quad
\mathrm{Pr}(\theta_{m+1} = \theta_{m}) = 1 - \mathrm{Pr}(\theta_{m+1} = \tilde{\theta}) \, . \quad (\dagger)
\end{equation*}
&lt;/div&gt;
&lt;p&gt;The natural question is, if there is somewhere we can
propose but not move (e.g. because the likelihood is not defined) then
what should be done?&lt;/p&gt;
&lt;p&gt;Let us consider one parameter, a probability &lt;span class="math"&gt;\(p\in [0,1]\)&lt;/span&gt;, with a
density &lt;span class="math"&gt;\(\pi(p) = \mathrm{Uniform}(0,1)\)&lt;/span&gt;, and a proposal
distribution &lt;span class="math"&gt;\(q(p'|p) = \mathcal{N}(p'|p,1)\)&lt;/span&gt;. We will run chains
of length &lt;span class="math"&gt;\(N=10^7\)&lt;/span&gt;. Code is all in R.&lt;/p&gt;
&lt;div class="cell docutils container"&gt;
&lt;div class="code"&gt;&lt;pre class="code r"&gt;&lt;a id="rest_code_57761e35501f4ec1a518cde32392dd94-1" name="rest_code_57761e35501f4ec1a518cde32392dd94-1" href="https://tah-sci.com/posts/treatment-of-boundaries-in-markov-chain-monte-carlo.html#rest_code_57761e35501f4ec1a518cde32392dd94-1"&gt;&lt;/a&gt;&lt;span class="n"&gt;N&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;&amp;lt;-&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="m"&gt;10000000&lt;/span&gt;
&lt;a id="rest_code_57761e35501f4ec1a518cde32392dd94-2" name="rest_code_57761e35501f4ec1a518cde32392dd94-2" href="https://tah-sci.com/posts/treatment-of-boundaries-in-markov-chain-monte-carlo.html#rest_code_57761e35501f4ec1a518cde32392dd94-2"&gt;&lt;/a&gt;&lt;span class="n"&gt;output&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;&amp;lt;-&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nf"&gt;matrix&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;ncol&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="m"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;nrow&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;N&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;a id="rest_code_57761e35501f4ec1a518cde32392dd94-3" name="rest_code_57761e35501f4ec1a518cde32392dd94-3" href="https://tah-sci.com/posts/treatment-of-boundaries-in-markov-chain-monte-carlo.html#rest_code_57761e35501f4ec1a518cde32392dd94-3"&gt;&lt;/a&gt;&lt;span class="n"&gt;sig&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;&amp;lt;-&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;In the first chain (‘Keep picking’) we will keep proposing new values
until one is within the allowed region&lt;/p&gt;
&lt;div class="cell docutils container"&gt;
&lt;div class="code"&gt;&lt;pre class="code r"&gt;&lt;a id="rest_code_f3bd5275886347c29d24aa5153e990c7-1" name="rest_code_f3bd5275886347c29d24aa5153e990c7-1" href="https://tah-sci.com/posts/treatment-of-boundaries-in-markov-chain-monte-carlo.html#rest_code_f3bd5275886347c29d24aa5153e990c7-1"&gt;&lt;/a&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;&amp;lt;-&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="m"&gt;0.5&lt;/span&gt;
&lt;a id="rest_code_f3bd5275886347c29d24aa5153e990c7-2" name="rest_code_f3bd5275886347c29d24aa5153e990c7-2" href="https://tah-sci.com/posts/treatment-of-boundaries-in-markov-chain-monte-carlo.html#rest_code_f3bd5275886347c29d24aa5153e990c7-2"&gt;&lt;/a&gt;&lt;span class="kr"&gt;for&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kr"&gt;in&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="n"&gt;N&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
&lt;a id="rest_code_f3bd5275886347c29d24aa5153e990c7-3" name="rest_code_f3bd5275886347c29d24aa5153e990c7-3" href="https://tah-sci.com/posts/treatment-of-boundaries-in-markov-chain-monte-carlo.html#rest_code_f3bd5275886347c29d24aa5153e990c7-3"&gt;&lt;/a&gt;&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="n"&gt;pprop&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;&amp;lt;-&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="m"&gt;-1&lt;/span&gt;
&lt;a id="rest_code_f3bd5275886347c29d24aa5153e990c7-4" name="rest_code_f3bd5275886347c29d24aa5153e990c7-4" href="https://tah-sci.com/posts/treatment-of-boundaries-in-markov-chain-monte-carlo.html#rest_code_f3bd5275886347c29d24aa5153e990c7-4"&gt;&lt;/a&gt;&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="kr"&gt;while&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="n"&gt;pprop&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pprop&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)){&lt;/span&gt;
&lt;a id="rest_code_f3bd5275886347c29d24aa5153e990c7-5" name="rest_code_f3bd5275886347c29d24aa5153e990c7-5" href="https://tah-sci.com/posts/treatment-of-boundaries-in-markov-chain-monte-carlo.html#rest_code_f3bd5275886347c29d24aa5153e990c7-5"&gt;&lt;/a&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="n"&gt;pprop&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;&amp;lt;-&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nf"&gt;rnorm&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;mean&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;sd&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;sig&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;a id="rest_code_f3bd5275886347c29d24aa5153e990c7-6" name="rest_code_f3bd5275886347c29d24aa5153e990c7-6" href="https://tah-sci.com/posts/treatment-of-boundaries-in-markov-chain-monte-carlo.html#rest_code_f3bd5275886347c29d24aa5153e990c7-6"&gt;&lt;/a&gt;&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;a id="rest_code_f3bd5275886347c29d24aa5153e990c7-7" name="rest_code_f3bd5275886347c29d24aa5153e990c7-7" href="https://tah-sci.com/posts/treatment-of-boundaries-in-markov-chain-monte-carlo.html#rest_code_f3bd5275886347c29d24aa5153e990c7-7"&gt;&lt;/a&gt;&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;&amp;lt;-&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;pprop&lt;/span&gt;
&lt;a id="rest_code_f3bd5275886347c29d24aa5153e990c7-8" name="rest_code_f3bd5275886347c29d24aa5153e990c7-8" href="https://tah-sci.com/posts/treatment-of-boundaries-in-markov-chain-monte-carlo.html#rest_code_f3bd5275886347c29d24aa5153e990c7-8"&gt;&lt;/a&gt;&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="n"&gt;output&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;&amp;lt;-&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;
&lt;a id="rest_code_f3bd5275886347c29d24aa5153e990c7-9" name="rest_code_f3bd5275886347c29d24aa5153e990c7-9" href="https://tah-sci.com/posts/treatment-of-boundaries-in-markov-chain-monte-carlo.html#rest_code_f3bd5275886347c29d24aa5153e990c7-9"&gt;&lt;/a&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;In the second chain (‘Reject’) we will reject proposals outside the
region and remain at the position the proposal was made.&lt;/p&gt;
&lt;div class="cell docutils container"&gt;
&lt;div class="code"&gt;&lt;pre class="code r"&gt;&lt;a id="rest_code_e67920708b9e4fad8c5c1da9e3cc6f71-1" name="rest_code_e67920708b9e4fad8c5c1da9e3cc6f71-1" href="https://tah-sci.com/posts/treatment-of-boundaries-in-markov-chain-monte-carlo.html#rest_code_e67920708b9e4fad8c5c1da9e3cc6f71-1"&gt;&lt;/a&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;&amp;lt;-&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="m"&gt;0.5&lt;/span&gt;
&lt;a id="rest_code_e67920708b9e4fad8c5c1da9e3cc6f71-2" name="rest_code_e67920708b9e4fad8c5c1da9e3cc6f71-2" href="https://tah-sci.com/posts/treatment-of-boundaries-in-markov-chain-monte-carlo.html#rest_code_e67920708b9e4fad8c5c1da9e3cc6f71-2"&gt;&lt;/a&gt;&lt;span class="kr"&gt;for&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kr"&gt;in&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="n"&gt;N&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
&lt;a id="rest_code_e67920708b9e4fad8c5c1da9e3cc6f71-3" name="rest_code_e67920708b9e4fad8c5c1da9e3cc6f71-3" href="https://tah-sci.com/posts/treatment-of-boundaries-in-markov-chain-monte-carlo.html#rest_code_e67920708b9e4fad8c5c1da9e3cc6f71-3"&gt;&lt;/a&gt;&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="n"&gt;pprop&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;&amp;lt;-&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nf"&gt;rnorm&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;mean&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;sd&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;sig&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;a id="rest_code_e67920708b9e4fad8c5c1da9e3cc6f71-4" name="rest_code_e67920708b9e4fad8c5c1da9e3cc6f71-4" href="https://tah-sci.com/posts/treatment-of-boundaries-in-markov-chain-monte-carlo.html#rest_code_e67920708b9e4fad8c5c1da9e3cc6f71-4"&gt;&lt;/a&gt;&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="kr"&gt;if&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="n"&gt;pprop&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;=&lt;/span&gt;&lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pprop&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;=&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)){&lt;/span&gt;
&lt;a id="rest_code_e67920708b9e4fad8c5c1da9e3cc6f71-5" name="rest_code_e67920708b9e4fad8c5c1da9e3cc6f71-5" href="https://tah-sci.com/posts/treatment-of-boundaries-in-markov-chain-monte-carlo.html#rest_code_e67920708b9e4fad8c5c1da9e3cc6f71-5"&gt;&lt;/a&gt;&lt;span class="w"&gt;    &lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;&amp;lt;-&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;pprop&lt;/span&gt;
&lt;a id="rest_code_e67920708b9e4fad8c5c1da9e3cc6f71-6" name="rest_code_e67920708b9e4fad8c5c1da9e3cc6f71-6" href="https://tah-sci.com/posts/treatment-of-boundaries-in-markov-chain-monte-carlo.html#rest_code_e67920708b9e4fad8c5c1da9e3cc6f71-6"&gt;&lt;/a&gt;&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;a id="rest_code_e67920708b9e4fad8c5c1da9e3cc6f71-7" name="rest_code_e67920708b9e4fad8c5c1da9e3cc6f71-7" href="https://tah-sci.com/posts/treatment-of-boundaries-in-markov-chain-monte-carlo.html#rest_code_e67920708b9e4fad8c5c1da9e3cc6f71-7"&gt;&lt;/a&gt;&lt;span class="w"&gt;  &lt;/span&gt;&lt;span class="n"&gt;output&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="m"&gt;2&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="o"&gt;&amp;lt;-&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;p&lt;/span&gt;
&lt;a id="rest_code_e67920708b9e4fad8c5c1da9e3cc6f71-8" name="rest_code_e67920708b9e4fad8c5c1da9e3cc6f71-8" href="https://tah-sci.com/posts/treatment-of-boundaries-in-markov-chain-monte-carlo.html#rest_code_e67920708b9e4fad8c5c1da9e3cc6f71-8"&gt;&lt;/a&gt;&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;Now look at some plots&lt;/p&gt;
&lt;div class="cell docutils container"&gt;
&lt;div class="code"&gt;&lt;pre class="code r"&gt;&lt;a id="rest_code_40079b3b5cb640799e5e3645236ddeda-1" name="rest_code_40079b3b5cb640799e5e3645236ddeda-1" href="https://tah-sci.com/posts/treatment-of-boundaries-in-markov-chain-monte-carlo.html#rest_code_40079b3b5cb640799e5e3645236ddeda-1"&gt;&lt;/a&gt;&lt;span class="nf"&gt;hist&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;output&lt;/span&gt;&lt;span class="p"&gt;[,&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;breaks&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nf"&gt;seq&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="m"&gt;0.1&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;freq&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;FALSE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;ylim&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nf"&gt;c&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="m"&gt;1.2&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
&lt;a id="rest_code_40079b3b5cb640799e5e3645236ddeda-2" name="rest_code_40079b3b5cb640799e5e3645236ddeda-2" href="https://tah-sci.com/posts/treatment-of-boundaries-in-markov-chain-monte-carlo.html#rest_code_40079b3b5cb640799e5e3645236ddeda-2"&gt;&lt;/a&gt;&lt;span class="w"&gt;     &lt;/span&gt;&lt;span class="n"&gt;xlab&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;'p'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;'Keep picking'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;div class="cell-output-display docutils container"&gt;
&lt;img alt="../unnamed-chunk-4-1.png" src="https://tah-sci.com/unnamed-chunk-4-1.png" style="width: 672px;"&gt;
&lt;/div&gt;
&lt;div class="code"&gt;&lt;pre class="code r"&gt;&lt;a id="rest_code_f4fad9f952c8473cbe1e0e4ffb8eb4c0-1" name="rest_code_f4fad9f952c8473cbe1e0e4ffb8eb4c0-1" href="https://tah-sci.com/posts/treatment-of-boundaries-in-markov-chain-monte-carlo.html#rest_code_f4fad9f952c8473cbe1e0e4ffb8eb4c0-1"&gt;&lt;/a&gt;&lt;span class="nf"&gt;hist&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;output&lt;/span&gt;&lt;span class="p"&gt;[,&lt;/span&gt;&lt;span class="m"&gt;2&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;breaks&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nf"&gt;seq&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="m"&gt;0.1&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;freq&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="kc"&gt;FALSE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;ylim&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nf"&gt;c&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="m"&gt;1.2&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
&lt;a id="rest_code_f4fad9f952c8473cbe1e0e4ffb8eb4c0-2" name="rest_code_f4fad9f952c8473cbe1e0e4ffb8eb4c0-2" href="https://tah-sci.com/posts/treatment-of-boundaries-in-markov-chain-monte-carlo.html#rest_code_f4fad9f952c8473cbe1e0e4ffb8eb4c0-2"&gt;&lt;/a&gt;&lt;span class="w"&gt;     &lt;/span&gt;&lt;span class="n"&gt;xlab&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;'p'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;main&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;'Reject'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;div class="cell-output-display docutils container"&gt;
&lt;img alt="../unnamed-chunk-4-2.png" src="https://tah-sci.com/unnamed-chunk-4-2.png" style="width: 672px;"&gt;
&lt;/div&gt;
&lt;div class="code"&gt;&lt;pre class="code r"&gt;&lt;a id="rest_code_fa8fa7ddfe1a4d3e998123f1b0bad8b1-1" name="rest_code_fa8fa7ddfe1a4d3e998123f1b0bad8b1-1" href="https://tah-sci.com/posts/treatment-of-boundaries-in-markov-chain-monte-carlo.html#rest_code_fa8fa7ddfe1a4d3e998123f1b0bad8b1-1"&gt;&lt;/a&gt;&lt;span class="nf"&gt;ts.plot&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;output&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="m"&gt;500&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;&lt;span class="n"&gt;gpars&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nf"&gt;list&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;xlab&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"m"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;ylab&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"p"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;div class="cell-output-display docutils container"&gt;
&lt;img alt="../unnamed-chunk-4-3.png" src="https://tah-sci.com/unnamed-chunk-4-3.png" style="width: 672px;"&gt;
&lt;/div&gt;
&lt;div class="code"&gt;&lt;pre class="code r"&gt;&lt;a id="rest_code_12c59efc6b5847aca8501dce803ffd7d-1" name="rest_code_12c59efc6b5847aca8501dce803ffd7d-1" href="https://tah-sci.com/posts/treatment-of-boundaries-in-markov-chain-monte-carlo.html#rest_code_12c59efc6b5847aca8501dce803ffd7d-1"&gt;&lt;/a&gt;&lt;span class="nf"&gt;ts.plot&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;output&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="m"&gt;500&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="m"&gt;2&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;&lt;span class="n"&gt;gpars&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nf"&gt;list&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;xlab&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"m"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="n"&gt;ylab&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"p"&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;div class="cell-output-display docutils container"&gt;
&lt;img alt="../unnamed-chunk-4-4.png" src="https://tah-sci.com/unnamed-chunk-4-4.png" style="width: 672px;"&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;p&gt;What we see is that the first approach, the trace plot looks
superficially ‘better’, but the output is biased. From the
Metropolis-Hastings algorith as specified in &lt;span class="math"&gt;\((\dagger)\)&lt;/span&gt; this is what we
would expect; mathematically, we can interpret the ‘Reject’ approach as
taking &lt;span class="math"&gt;\(\pi(\theta&amp;lt;0) = \pi(\theta&amp;gt;1) = 0\)&lt;/span&gt;, while the ‘Keep
picking’ approach the proposal distribution effectively becomes a
truncated normal. Such a truncated normal could be evaluated as the
&lt;span class="math"&gt;\(q(\cdot|\cdot)\)&lt;/span&gt; in &lt;span class="math"&gt;\((\dagger)\)&lt;/span&gt;, and this might be a good algorithm,
but the naïve implementation introduces bias.&lt;/p&gt;</description><guid>https://tah-sci.com/posts/treatment-of-boundaries-in-markov-chain-monte-carlo.html</guid><pubDate>Mon, 14 Apr 2025 14:35:10 GMT</pubDate></item><item><title>Seasonality and Immunity</title><link>https://tah-sci.com/posts/seasonality-and-immunity.html</link><dc:creator>Thomas House</dc:creator><description>&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; This blogpost was conceptualised in mid-2021, and should be public
late in 2023. With the problems going on at 𝕏 (formerly Twitter), it's struck
me that putting any such content on a website that I control probably has its
advantages, so I hope to use this site more going forwards.&lt;/p&gt;
&lt;img alt="Spring being pushed, with displacement z from equilibrium" class="align-right" src="https://tah-sci.com/spring.jpg" style="width: 240px;"&gt;
&lt;p&gt;So let's start by considering a weight on a spring as pictured - this is going
to give us a physical 'model' of disease dynamics for an endemic infection. The
weight on the spring is a distance &lt;span class="raw-html"&gt;\(z(t)\)&lt;/span&gt; from the point at which
the system would be at rest. There are three forces acting on the weight:&lt;/p&gt;
&lt;ul class="simple"&gt;
&lt;li&gt;&lt;p&gt;The spring pulls it towards equilibrium, to a first approximation,
proportionally to its spring constant giving a force contribution
&lt;span class="raw-html"&gt;\(-k z\)&lt;/span&gt;;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;As the weight moves, friction dissipates kinetic energy giving a force
contribution &lt;span class="raw-html"&gt;\(-\nu \dot{z}\)&lt;/span&gt;;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;A human is pushing at the weight giving a time-dependent force contribution
&lt;span class="raw-html"&gt;\(\varphi(t)\)&lt;/span&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Now, dredging up our high-school physics we remember that Newton's equation is
&lt;span class="raw-html"&gt;\(F = ma\)&lt;/span&gt;. Noting that acceleration is the second derivative of
position so that &lt;span class="raw-html"&gt;\(a = \ddot{z}\)&lt;/span&gt; and combining the three force
contributions above we get &lt;span class="raw-html"&gt;\[-kz - \nu \dot{z} + \varphi(t) = m
\ddot{z}\, .\]&lt;/span&gt; Dredging still further, we might remember that momentum is
&lt;span class="raw-html"&gt;\(p = m\dot{z}\)&lt;/span&gt;, which gives us dynamics &lt;span class="raw-html"&gt;\[\dot{z} =
\frac{1}{m} p \, , \qquad \dot{p} = -kz - \frac{\nu}{m} p + \varphi(t) \, .
\qquad \mathrm{(1)} \]&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Next, let us consider an SIRS model with forcing, &lt;span class="raw-html"&gt;\[\dot{S} = \omega
R - \beta(t) S I \, , \quad\ \dot{I} = \beta(t) S I - \gamma I \, , \quad
\dot{R} = \gamma I - \omega R \, .\]&lt;/span&gt; This models a system where individuals move
from susceptible to infections upon contact with other infectious individuals,
then recover, and after that immunity wanes and they become susceptible again.
The rate &lt;span class="raw-html"&gt;\(\beta(t)\)&lt;/span&gt; depending on time is supposed to capture
different exogenous events: schools opening and closing; new vaccines; new
variants; policy changes; and all sorts of other complexity. If you want to
experiment with these equations numerically, check out &lt;a class="reference external" href="https://github.com/thomasallanhouse/covid19-incidence/blob/main/bmj.ipynb"&gt;this Jupyter notebook&lt;/a&gt;
that I make available on GitHub.&lt;/p&gt;
&lt;p&gt;This set of equations does not have a general closed form, but we can look for
an approximate one using the Ansatz &lt;span class="raw-html"&gt;\[S(t) = S_* + \epsilon x(t) \,
, \quad\ I(t) = I_* + \epsilon y(t) \, , \quad R(t) = N - S(t) - I(t) \, ,
\quad \beta(t) = \beta_* + \epsilon \phi(t) \, .\]&lt;/span&gt; We want this to correspond
to a fixed point of the dynamical system when &lt;span class="raw-html"&gt;\(\epsilon = 0\)&lt;/span&gt;,
leading to the equations &lt;span class="raw-html"&gt;\[0 = \omega (N - S_* - I_*) - \beta_* S_*
I_* \, , \qquad\ 0 = \beta_* S_* I_* - \gamma I_* \, .\]&lt;/span&gt; These have a solution
when &lt;span class="raw-html"&gt;\(S_* = N, I_* = 0\)&lt;/span&gt; that is unstable if the disease can go endemic,
i.e. when &lt;span class="raw-html"&gt;\(\mathcal{R}_0 = \beta_*/\gamma &amp;gt; 1 \)&lt;/span&gt;; the other fixed
point is when &lt;span class="raw-html"&gt;\(S_* = 1/\mathcal{R}_0\)&lt;/span&gt;, which I hope to blog
about later. The expression for the expected number of infectives
&lt;span class="raw-html"&gt;\(I_*\)&lt;/span&gt; at endemicity is more complex; feel free to derive it for
fun!  For our purposes here, we are interested in what we obtain at first order
in &lt;span class="raw-html"&gt;\(\epsilon\)&lt;/span&gt;, which gives us &lt;span class="raw-html"&gt;\[\dot{x} = - \omega x -
\omega y - \beta_* x I_* - \beta_* S_* y - \phi S_* I_* +
\mathcal{O}(\epsilon^2)  \, , \qquad\ \dot{y} = \beta_* x I_* + \beta_* S_* y +
\phi S_* I_* - \gamma y  + \mathcal{O}(\epsilon^2) \, .  \qquad \mathrm{(2)}
\]&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;OK, so take a breath. Now, with a bit of work, we can write both equations (1)
and (2) (neglecting quadratic terms in &lt;span class="raw-html"&gt;\(\epsilon\)&lt;/span&gt;) above in the
form of a two-dimensional vector differential equation
&lt;span class="raw-html"&gt;\[\dot{\mathbf{v}} = \boldsymbol{J} \mathbf{v} + \mathbf{F}(t) \, ,
\qquad \mathrm{(3)} \]&lt;/span&gt; where &lt;span class="raw-html"&gt;\(\mathbf{v}\)&lt;/span&gt; is a vector containing
the dynamical variables - &lt;span class="raw-html"&gt;\((z,p)\)&lt;/span&gt; for the spring and
&lt;span class="raw-html"&gt;\((x,y)\)&lt;/span&gt; for the SIRS model - &lt;span class="raw-html"&gt;\(\boldsymbol{J}\)&lt;/span&gt; is a
matrix depending on rate constants, and &lt;span class="raw-html"&gt;\(\mathbf{F}(t)\)&lt;/span&gt; is a
vector function of time. Some readers will note that the details of the
versions of (3) derived from (1) and (2) are not quite the same, but also that
equation (3) takes the same structure under the linear transformation
&lt;span class="raw-html"&gt;\[\tilde{\mathbf{v}} = \boldsymbol{T} \mathbf{v} \, ,\quad
\tilde{\boldsymbol{J}} = \boldsymbol{T}\boldsymbol{J}\boldsymbol{T}^{-1} \,
,\quad \tilde{\mathbf{F}} = \boldsymbol{T} \mathbf{F} \, . \]&lt;/span&gt; They might also
have fun trying to work with (3) via matrix integrating factor, Eigensystem
analysis, or maybe Fourier transform.&lt;/p&gt;
&lt;p&gt;So, that was quite fiddly (although not much beyond high-school level
mathematics apart from the last bit). But the TL;DR is this: imagine all the
complexity that you can generate by bouncing a weight up and down. If your taps
line up just right with the how the spring behaves, you can get regular
oscillations, but it's not difficult to make the weight jump irregularly all
over the place. And this is exactly what we can see with an endemic disease:
there's no guarantee that oscillatory behaviour will be regular or predictable.&lt;/p&gt;
&lt;p&gt;Now, for many diseases that are better established, we do indeed see relatively
regular annual peaks, although predicting the exact timing and height of these
even for very well studied diseases like influenza is not currently possible.
But most experts think that the relative regularity of influenza compared to
COVID arises because the exogenous forces are mainly annual events like school
holidays, people moving in and outdoors with the weather, and maybe even a
direct bio-physical impact of the weather, with evolutionary events generally
having a less dramatic impact on disease transmission than they do for COVID.&lt;/p&gt;
&lt;p&gt;Because evolution is hard to predict, it's not clear that COVID will become
more like influenza. Or even that it would be a good thing if it did. The
purpose of this post, however, was to help explain why its unpredictability is
at least somewhat predictable.&lt;/p&gt;</description><guid>https://tah-sci.com/posts/seasonality-and-immunity.html</guid><pubDate>Wed, 18 Aug 2021 18:33:04 GMT</pubDate></item><item><title>Why Zero is so Hard</title><link>https://tah-sci.com/posts/why-zero-is-so-hard.html</link><dc:creator>Thomas House</dc:creator><description>&lt;p&gt;The post on herd immunity on this blog discussed mitigation policies. Since
then, the world has moved on somewhat, and many people are talking about
long-term suppression of COVID-19, so how long might that take?&lt;/p&gt;
&lt;p&gt;Suppose we have &lt;span class="raw-html"&gt;\(N\)&lt;/span&gt; current COVID infections, the reproduction
number is &lt;span class="raw-html"&gt;\(R\)&lt;/span&gt;, the duration of infection is &lt;span class="raw-html"&gt;\(T\)&lt;/span&gt; and
we want to know the time &lt;span class="raw-html"&gt;\(t\)&lt;/span&gt; until the probability that there are
zero cases is &lt;span class="raw-html"&gt;\(p\)&lt;/span&gt;.&lt;/p&gt;
&lt;p&gt;If we model this as a Markovian birth-death chain, then the analytical result
for the time at which the given probability of extinction is reached is:
&lt;span class="raw-html"&gt;\[t(p,N,R) = \frac{T}{R-1} \ln\left( \frac{p^{1/N} - 1}{R p^{1/N} - 1}\right) \; . \]&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Results from this equation are shown in the Figure if we take 10 days from
infection to recovery; unfortunately, we need years with the reproduction number
less than one to get appreciable probabilities of of extinction. Of course, the
Markovian birth-death chain is too simple to be a realistic model of COVID. In
particular:&lt;/p&gt;
&lt;ul class="simple"&gt;
&lt;li&gt;&lt;p&gt;More infection happens several days after infection than in the initial days,
introducing additional delays;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Some population subgroups may have an effective reproduction number above 1;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;There will be imported cases from outside;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;There is variability in individual susceptibility and infectiousness;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Contact patterns are complex and heterogeneous.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;These will influence the time to zero cases in different ways; but the 'inconvenient
truth' is that it takes an extremely long time to get to zero cases even if
&lt;span class="raw-html"&gt;\(R\)&lt;/span&gt; can be kept below one indefinitely.&lt;/p&gt;
&lt;figure style="width: 600px"&gt;
&lt;a class="reference external image-reference" href="https://tah-sci.com/covext2.png"&gt;
&lt;img alt="Times until the a given probability of extinction" src="https://tah-sci.com/covext2.png"&gt;
&lt;/a&gt;
&lt;figcaption&gt;
&lt;p&gt;Times until the a given probability of extinction as a function of the
initial number of cases and the reproduction number. Contour lines
correspond to 1, 2, 3, 5 and 10 years.&lt;/p&gt;
&lt;/figcaption&gt;
&lt;/figure&gt;</description><guid>https://tah-sci.com/posts/why-zero-is-so-hard.html</guid><pubDate>Mon, 10 Aug 2020 21:18:21 GMT</pubDate></item><item><title>Modelling Herd Immunity</title><link>https://tah-sci.com/posts/modelling-herd-immunity.html</link><dc:creator>Thomas House</dc:creator><description>&lt;p&gt;The current coronavirus outbreak (COVID-19) has raised questions about herd
immunity, social distancing measures, and the relationship between these. In
particular, at time of writing the following Tweet has thousands of retweets
and likes.&lt;/p&gt;
&lt;p&gt;&lt;span class="raw-html"&gt;&lt;blockquote class="twitter-tweet"&gt;&lt;p lang="en" dir="ltr"&gt;“With the number of known &lt;a href="https://twitter.com/hashtag/coronavirus?src=hash&amp;amp;ref_src=twsrc%5Etfw"&gt;#coronavirus&lt;/a&gt; cases today in South Korea, Italy, Iran, France, Spain and the US, Wuhan was already in lockdown.”&lt;br&gt;&lt;br&gt;Read why every day we wait to make decisions is costlier that’s the previous:&lt;a href="https://t.co/6hX74wiZgF"&gt;https://t.co/6hX74wiZgF&lt;/a&gt;&lt;/p&gt;— Tomas Pueyo (@tomaspueyo) &lt;a href="https://twitter.com/tomaspueyo/status/1237421094912397314?ref_src=twsrc%5Etfw"&gt;March 10, 2020&lt;/a&gt;&lt;/blockquote&gt; &lt;script async src="https://platform.twitter.com/widgets.js" charset="utf-8"&gt;&lt;/script&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;I think that this presents an incomplete picture of the policy considerations.&lt;/p&gt;
&lt;img alt="Before herd immunity" class="align-left" src="https://tah-sci.com/Herd.png" style="width: 240px;"&gt;
&lt;img alt="After herd immunity" class="align-right" src="https://tah-sci.com/Herd2.png" style="width: 240px;"&gt;
&lt;p&gt;To see why, let us start by defining the most important concept in epidemic
modelling, the basic reproduction number &lt;span class="raw-html"&gt;\(R_0\)&lt;/span&gt;. This is equal to
the average number of infections caused by a case early in the epidemic, so that
for &lt;span class="raw-html"&gt;\(R_0 = 3\)&lt;/span&gt;, the early transmission tree might look something like
the cartoon on the left. This exhibits the exponential growth that is so worrying
at present. We have first one case, then three, then nine, then 27, then ...&lt;/p&gt;
&lt;p&gt;Now suppose that immunity has been built up, either through vaccination or by
illness followed by recovery, so that two thirds of people are immune. This
means that each case will have three contacts that would have caused infection
in the absence of immunity, but on average two of these will be immune and so
the situation looks more like the cartoon on the right. We say that the
reproduction number is reduced from its basic value to &lt;span class="raw-html"&gt;\(R_t = 1\)&lt;/span&gt;.&lt;/p&gt;
&lt;p&gt;While the individuals in the cartoon with a crossed out arrow pointing towards
them are literally immune, the people that they would have infected benefit
from &lt;strong&gt;herd immunity&lt;/strong&gt;. They can be infected if exposed, but will not be. This
is particularly beneficial to the people who are particularly vulnerable to the
disease, who might die if they catch it. The general formula for herd immunity
is that when over &lt;span class="raw-html"&gt;\((1 - (1/R_0))\times 100\%\)&lt;/span&gt; of the population
is immune, then the epidemic will start to die out.&lt;/p&gt;
&lt;p&gt;Now suppose we simulate a more realistic scenario. The Python code for this is
at the bottom of this post, and it is much simpler than the models that are
typically used to guide policy, but captures the basic phenomena of interest.
In particular, we suppose that we can reduce transmission through social
distancing measures like closing schools and workplaces, but that this is only
possible for three weeks. This represents the fact that the costs of not
educating children and not doing other activities can become quite large over
time, and in some cases can lead to levels death and illness that could exceed
those due to the unchecked epidemic. Running the epidemic model with these
interventions gives the following results for cases at a given moment and total
cases over time.&lt;/p&gt;
&lt;img alt="../output_5.png" src="https://tah-sci.com/output_5.png"&gt;
&lt;p&gt;It seems that the Tweet was right - look at the difference the early intervention
makes! But now let us zoom out of the graph; then the picture looks like the below.&lt;/p&gt;
&lt;img alt="../output_6_0.png" src="https://tah-sci.com/output_6_0.png"&gt;
&lt;p&gt;In fact, the later intervention dramatically reduces the burden on the healthcare
system, cutting in half the maximum numbers ill and potentially needing treatment
at any one time, and significantly reduces the final number infected, which the
earlier interventions fail to do.&lt;/p&gt;
&lt;p&gt;The reason that this happens is that social distancing measures do not lead to herd
immunity, so once they are lifted the epidemic starts again. In the absence of a
vaccine, it is therefore meaningless to speak about whether a policy 'aims' to get
herd immunity or not, since every country in the world will reach herd immunity
unless it is able to implement social distancing for an indefinite period of time.&lt;/p&gt;
&lt;p&gt;What mitigation policies should aim to do, therefore, is to reach herd immunity
with the minimal human cost. This will be extremely difficult, and at every
stage we will be dealing with large uncertainties. I would not want to be the
person who ultimately made the policy decisions. But to say that early
interventions are always better is incorrect.&lt;/p&gt;
&lt;div class="code"&gt;&lt;pre class="code ipython3"&gt;&lt;a id="rest_code_2e6cf89e603248aabc73eaf4f1018083-1" name="rest_code_2e6cf89e603248aabc73eaf4f1018083-1" href="https://tah-sci.com/posts/modelling-herd-immunity.html#rest_code_2e6cf89e603248aabc73eaf4f1018083-1"&gt;&lt;/a&gt;&lt;span class="c1"&gt;# Pull in libraries needed&lt;/span&gt;
&lt;a id="rest_code_2e6cf89e603248aabc73eaf4f1018083-2" name="rest_code_2e6cf89e603248aabc73eaf4f1018083-2" href="https://tah-sci.com/posts/modelling-herd-immunity.html#rest_code_2e6cf89e603248aabc73eaf4f1018083-2"&gt;&lt;/a&gt;&lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="k"&gt;matplotlib&lt;/span&gt; inline
&lt;a id="rest_code_2e6cf89e603248aabc73eaf4f1018083-3" name="rest_code_2e6cf89e603248aabc73eaf4f1018083-3" href="https://tah-sci.com/posts/modelling-herd-immunity.html#rest_code_2e6cf89e603248aabc73eaf4f1018083-3"&gt;&lt;/a&gt;&lt;span class="kn"&gt;import&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nn"&gt;numpy&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;as&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nn"&gt;np&lt;/span&gt;
&lt;a id="rest_code_2e6cf89e603248aabc73eaf4f1018083-4" name="rest_code_2e6cf89e603248aabc73eaf4f1018083-4" href="https://tah-sci.com/posts/modelling-herd-immunity.html#rest_code_2e6cf89e603248aabc73eaf4f1018083-4"&gt;&lt;/a&gt;&lt;span class="kn"&gt;from&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nn"&gt;scipy&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;integrate&lt;/span&gt;
&lt;a id="rest_code_2e6cf89e603248aabc73eaf4f1018083-5" name="rest_code_2e6cf89e603248aabc73eaf4f1018083-5" href="https://tah-sci.com/posts/modelling-herd-immunity.html#rest_code_2e6cf89e603248aabc73eaf4f1018083-5"&gt;&lt;/a&gt;&lt;span class="kn"&gt;import&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nn"&gt;matplotlib.pyplot&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;as&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nn"&gt;plt&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;div class="code"&gt;&lt;pre class="code ipython3"&gt;&lt;a id="rest_code_08e3e5019a674d28bd1edfc46d1cfb76-1" name="rest_code_08e3e5019a674d28bd1edfc46d1cfb76-1" href="https://tah-sci.com/posts/modelling-herd-immunity.html#rest_code_08e3e5019a674d28bd1edfc46d1cfb76-1"&gt;&lt;/a&gt;&lt;span class="c1"&gt;# Represent the basic dynamics&lt;/span&gt;
&lt;a id="rest_code_08e3e5019a674d28bd1edfc46d1cfb76-2" name="rest_code_08e3e5019a674d28bd1edfc46d1cfb76-2" href="https://tah-sci.com/posts/modelling-herd-immunity.html#rest_code_08e3e5019a674d28bd1edfc46d1cfb76-2"&gt;&lt;/a&gt;&lt;span class="k"&gt;def&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nf"&gt;odefun&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;beta0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;betat&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;t0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;t1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;sigma&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;gamma&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
&lt;a id="rest_code_08e3e5019a674d28bd1edfc46d1cfb76-3" name="rest_code_08e3e5019a674d28bd1edfc46d1cfb76-3" href="https://tah-sci.com/posts/modelling-herd-immunity.html#rest_code_08e3e5019a674d28bd1edfc46d1cfb76-3"&gt;&lt;/a&gt;    &lt;span class="n"&gt;dx&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;np&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;zeros&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;a id="rest_code_08e3e5019a674d28bd1edfc46d1cfb76-4" name="rest_code_08e3e5019a674d28bd1edfc46d1cfb76-4" href="https://tah-sci.com/posts/modelling-herd-immunity.html#rest_code_08e3e5019a674d28bd1edfc46d1cfb76-4"&gt;&lt;/a&gt;    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;=&lt;/span&gt;&lt;span class="n"&gt;t0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="ow"&gt;and&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;=&lt;/span&gt;&lt;span class="n"&gt;t1&lt;/span&gt;&lt;span class="p"&gt;)):&lt;/span&gt;
&lt;a id="rest_code_08e3e5019a674d28bd1edfc46d1cfb76-5" name="rest_code_08e3e5019a674d28bd1edfc46d1cfb76-5" href="https://tah-sci.com/posts/modelling-herd-immunity.html#rest_code_08e3e5019a674d28bd1edfc46d1cfb76-5"&gt;&lt;/a&gt;        &lt;span class="n"&gt;beta&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;betat&lt;/span&gt;
&lt;a id="rest_code_08e3e5019a674d28bd1edfc46d1cfb76-6" name="rest_code_08e3e5019a674d28bd1edfc46d1cfb76-6" href="https://tah-sci.com/posts/modelling-herd-immunity.html#rest_code_08e3e5019a674d28bd1edfc46d1cfb76-6"&gt;&lt;/a&gt;    &lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
&lt;a id="rest_code_08e3e5019a674d28bd1edfc46d1cfb76-7" name="rest_code_08e3e5019a674d28bd1edfc46d1cfb76-7" href="https://tah-sci.com/posts/modelling-herd-immunity.html#rest_code_08e3e5019a674d28bd1edfc46d1cfb76-7"&gt;&lt;/a&gt;        &lt;span class="n"&gt;beta&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;beta0&lt;/span&gt;
&lt;a id="rest_code_08e3e5019a674d28bd1edfc46d1cfb76-8" name="rest_code_08e3e5019a674d28bd1edfc46d1cfb76-8" href="https://tah-sci.com/posts/modelling-herd-immunity.html#rest_code_08e3e5019a674d28bd1edfc46d1cfb76-8"&gt;&lt;/a&gt;    &lt;span class="n"&gt;dx&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="n"&gt;beta&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;a id="rest_code_08e3e5019a674d28bd1edfc46d1cfb76-9" name="rest_code_08e3e5019a674d28bd1edfc46d1cfb76-9" href="https://tah-sci.com/posts/modelling-herd-immunity.html#rest_code_08e3e5019a674d28bd1edfc46d1cfb76-9"&gt;&lt;/a&gt;    &lt;span class="n"&gt;dx&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;beta&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;sigma&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;a id="rest_code_08e3e5019a674d28bd1edfc46d1cfb76-10" name="rest_code_08e3e5019a674d28bd1edfc46d1cfb76-10" href="https://tah-sci.com/posts/modelling-herd-immunity.html#rest_code_08e3e5019a674d28bd1edfc46d1cfb76-10"&gt;&lt;/a&gt;    &lt;span class="n"&gt;dx&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;sigma&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;sigma&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;a id="rest_code_08e3e5019a674d28bd1edfc46d1cfb76-11" name="rest_code_08e3e5019a674d28bd1edfc46d1cfb76-11" href="https://tah-sci.com/posts/modelling-herd-immunity.html#rest_code_08e3e5019a674d28bd1edfc46d1cfb76-11"&gt;&lt;/a&gt;    &lt;span class="n"&gt;dx&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;sigma&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;gamma&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;a id="rest_code_08e3e5019a674d28bd1edfc46d1cfb76-12" name="rest_code_08e3e5019a674d28bd1edfc46d1cfb76-12" href="https://tah-sci.com/posts/modelling-herd-immunity.html#rest_code_08e3e5019a674d28bd1edfc46d1cfb76-12"&gt;&lt;/a&gt;    &lt;span class="n"&gt;dx&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;gamma&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;gamma&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;a id="rest_code_08e3e5019a674d28bd1edfc46d1cfb76-13" name="rest_code_08e3e5019a674d28bd1edfc46d1cfb76-13" href="https://tah-sci.com/posts/modelling-herd-immunity.html#rest_code_08e3e5019a674d28bd1edfc46d1cfb76-13"&gt;&lt;/a&gt;    &lt;span class="n"&gt;dx&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;gamma&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;a id="rest_code_08e3e5019a674d28bd1edfc46d1cfb76-14" name="rest_code_08e3e5019a674d28bd1edfc46d1cfb76-14" href="https://tah-sci.com/posts/modelling-herd-immunity.html#rest_code_08e3e5019a674d28bd1edfc46d1cfb76-14"&gt;&lt;/a&gt;    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;dx&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;div class="code"&gt;&lt;pre class="code ipython3"&gt;&lt;a id="rest_code_9bf382b437f74d0fa8ac8b29866675e3-1" name="rest_code_9bf382b437f74d0fa8ac8b29866675e3-1" href="https://tah-sci.com/posts/modelling-herd-immunity.html#rest_code_9bf382b437f74d0fa8ac8b29866675e3-1"&gt;&lt;/a&gt;&lt;span class="c1"&gt;# Parameters of the model&lt;/span&gt;
&lt;a id="rest_code_9bf382b437f74d0fa8ac8b29866675e3-2" name="rest_code_9bf382b437f74d0fa8ac8b29866675e3-2" href="https://tah-sci.com/posts/modelling-herd-immunity.html#rest_code_9bf382b437f74d0fa8ac8b29866675e3-2"&gt;&lt;/a&gt;&lt;span class="n"&gt;N&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;6.7e7&lt;/span&gt; &lt;span class="c1"&gt;# Total population&lt;/span&gt;
&lt;a id="rest_code_9bf382b437f74d0fa8ac8b29866675e3-3" name="rest_code_9bf382b437f74d0fa8ac8b29866675e3-3" href="https://tah-sci.com/posts/modelling-herd-immunity.html#rest_code_9bf382b437f74d0fa8ac8b29866675e3-3"&gt;&lt;/a&gt;&lt;span class="n"&gt;i0&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;1e-4&lt;/span&gt; &lt;span class="c1"&gt;# 0.5*Proportion of the population infected on day 0&lt;/span&gt;
&lt;a id="rest_code_9bf382b437f74d0fa8ac8b29866675e3-4" name="rest_code_9bf382b437f74d0fa8ac8b29866675e3-4" href="https://tah-sci.com/posts/modelling-herd-immunity.html#rest_code_9bf382b437f74d0fa8ac8b29866675e3-4"&gt;&lt;/a&gt;&lt;span class="n"&gt;tlast&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;365.0&lt;/span&gt; &lt;span class="c1"&gt;# Consider a year&lt;/span&gt;
&lt;a id="rest_code_9bf382b437f74d0fa8ac8b29866675e3-5" name="rest_code_9bf382b437f74d0fa8ac8b29866675e3-5" href="https://tah-sci.com/posts/modelling-herd-immunity.html#rest_code_9bf382b437f74d0fa8ac8b29866675e3-5"&gt;&lt;/a&gt;&lt;span class="n"&gt;latent_period&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;5.0&lt;/span&gt; &lt;span class="c1"&gt;# Days between being infected and becoming infectious&lt;/span&gt;
&lt;a id="rest_code_9bf382b437f74d0fa8ac8b29866675e3-6" name="rest_code_9bf382b437f74d0fa8ac8b29866675e3-6" href="https://tah-sci.com/posts/modelling-herd-immunity.html#rest_code_9bf382b437f74d0fa8ac8b29866675e3-6"&gt;&lt;/a&gt;&lt;span class="n"&gt;infectious_period&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;7.0&lt;/span&gt; &lt;span class="c1"&gt;# Days infectious&lt;/span&gt;
&lt;a id="rest_code_9bf382b437f74d0fa8ac8b29866675e3-7" name="rest_code_9bf382b437f74d0fa8ac8b29866675e3-7" href="https://tah-sci.com/posts/modelling-herd-immunity.html#rest_code_9bf382b437f74d0fa8ac8b29866675e3-7"&gt;&lt;/a&gt;&lt;span class="n"&gt;R0&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;2.5&lt;/span&gt; &lt;span class="c1"&gt;# Basic reproduction number in the absence of interventions&lt;/span&gt;
&lt;a id="rest_code_9bf382b437f74d0fa8ac8b29866675e3-8" name="rest_code_9bf382b437f74d0fa8ac8b29866675e3-8" href="https://tah-sci.com/posts/modelling-herd-immunity.html#rest_code_9bf382b437f74d0fa8ac8b29866675e3-8"&gt;&lt;/a&gt;&lt;span class="n"&gt;Rt&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;0.75&lt;/span&gt; &lt;span class="c1"&gt;# Reproduction number in the presence of interventions&lt;/span&gt;
&lt;a id="rest_code_9bf382b437f74d0fa8ac8b29866675e3-9" name="rest_code_9bf382b437f74d0fa8ac8b29866675e3-9" href="https://tah-sci.com/posts/modelling-herd-immunity.html#rest_code_9bf382b437f74d0fa8ac8b29866675e3-9"&gt;&lt;/a&gt;&lt;span class="n"&gt;tend&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;21.0&lt;/span&gt; &lt;span class="c1"&gt;# Number of days of interventions&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;div class="code"&gt;&lt;pre class="code ipython3"&gt;&lt;a id="rest_code_7459b1b75bcd412cb05ec1a6de3eaa08-1" name="rest_code_7459b1b75bcd412cb05ec1a6de3eaa08-1" href="https://tah-sci.com/posts/modelling-herd-immunity.html#rest_code_7459b1b75bcd412cb05ec1a6de3eaa08-1"&gt;&lt;/a&gt;&lt;span class="n"&gt;beta0&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;R0&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="n"&gt;infectious_period&lt;/span&gt;
&lt;a id="rest_code_7459b1b75bcd412cb05ec1a6de3eaa08-2" name="rest_code_7459b1b75bcd412cb05ec1a6de3eaa08-2" href="https://tah-sci.com/posts/modelling-herd-immunity.html#rest_code_7459b1b75bcd412cb05ec1a6de3eaa08-2"&gt;&lt;/a&gt;&lt;span class="n"&gt;betat&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;Rt&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="n"&gt;infectious_period&lt;/span&gt;
&lt;a id="rest_code_7459b1b75bcd412cb05ec1a6de3eaa08-3" name="rest_code_7459b1b75bcd412cb05ec1a6de3eaa08-3" href="https://tah-sci.com/posts/modelling-herd-immunity.html#rest_code_7459b1b75bcd412cb05ec1a6de3eaa08-3"&gt;&lt;/a&gt;&lt;span class="n"&gt;sigma&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;2.0&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="n"&gt;latent_period&lt;/span&gt;
&lt;a id="rest_code_7459b1b75bcd412cb05ec1a6de3eaa08-4" name="rest_code_7459b1b75bcd412cb05ec1a6de3eaa08-4" href="https://tah-sci.com/posts/modelling-herd-immunity.html#rest_code_7459b1b75bcd412cb05ec1a6de3eaa08-4"&gt;&lt;/a&gt;&lt;span class="n"&gt;gamma&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;2.0&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="n"&gt;infectious_period&lt;/span&gt;
&lt;a id="rest_code_7459b1b75bcd412cb05ec1a6de3eaa08-5" name="rest_code_7459b1b75bcd412cb05ec1a6de3eaa08-5" href="https://tah-sci.com/posts/modelling-herd-immunity.html#rest_code_7459b1b75bcd412cb05ec1a6de3eaa08-5"&gt;&lt;/a&gt;
&lt;a id="rest_code_7459b1b75bcd412cb05ec1a6de3eaa08-6" name="rest_code_7459b1b75bcd412cb05ec1a6de3eaa08-6" href="https://tah-sci.com/posts/modelling-herd-immunity.html#rest_code_7459b1b75bcd412cb05ec1a6de3eaa08-6"&gt;&lt;/a&gt;&lt;span class="n"&gt;t0ran&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;np&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;40&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;52.5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;65&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;a id="rest_code_7459b1b75bcd412cb05ec1a6de3eaa08-7" name="rest_code_7459b1b75bcd412cb05ec1a6de3eaa08-7" href="https://tah-sci.com/posts/modelling-herd-immunity.html#rest_code_7459b1b75bcd412cb05ec1a6de3eaa08-7"&gt;&lt;/a&gt;
&lt;a id="rest_code_7459b1b75bcd412cb05ec1a6de3eaa08-8" name="rest_code_7459b1b75bcd412cb05ec1a6de3eaa08-8" href="https://tah-sci.com/posts/modelling-herd-immunity.html#rest_code_7459b1b75bcd412cb05ec1a6de3eaa08-8"&gt;&lt;/a&gt;&lt;span class="k"&gt;def&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nf"&gt;mylab&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
&lt;a id="rest_code_7459b1b75bcd412cb05ec1a6de3eaa08-9" name="rest_code_7459b1b75bcd412cb05ec1a6de3eaa08-9" href="https://tah-sci.com/posts/modelling-herd-immunity.html#rest_code_7459b1b75bcd412cb05ec1a6de3eaa08-9"&gt;&lt;/a&gt;    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
&lt;a id="rest_code_7459b1b75bcd412cb05ec1a6de3eaa08-10" name="rest_code_7459b1b75bcd412cb05ec1a6de3eaa08-10" href="https://tah-sci.com/posts/modelling-herd-immunity.html#rest_code_7459b1b75bcd412cb05ec1a6de3eaa08-10"&gt;&lt;/a&gt;        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;"Start at "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="s2"&gt;" days"&lt;/span&gt;
&lt;a id="rest_code_7459b1b75bcd412cb05ec1a6de3eaa08-11" name="rest_code_7459b1b75bcd412cb05ec1a6de3eaa08-11" href="https://tah-sci.com/posts/modelling-herd-immunity.html#rest_code_7459b1b75bcd412cb05ec1a6de3eaa08-11"&gt;&lt;/a&gt;    &lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
&lt;a id="rest_code_7459b1b75bcd412cb05ec1a6de3eaa08-12" name="rest_code_7459b1b75bcd412cb05ec1a6de3eaa08-12" href="https://tah-sci.com/posts/modelling-herd-immunity.html#rest_code_7459b1b75bcd412cb05ec1a6de3eaa08-12"&gt;&lt;/a&gt;        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s2"&gt;"Baseline"&lt;/span&gt;
&lt;a id="rest_code_7459b1b75bcd412cb05ec1a6de3eaa08-13" name="rest_code_7459b1b75bcd412cb05ec1a6de3eaa08-13" href="https://tah-sci.com/posts/modelling-herd-immunity.html#rest_code_7459b1b75bcd412cb05ec1a6de3eaa08-13"&gt;&lt;/a&gt;
&lt;a id="rest_code_7459b1b75bcd412cb05ec1a6de3eaa08-14" name="rest_code_7459b1b75bcd412cb05ec1a6de3eaa08-14" href="https://tah-sci.com/posts/modelling-herd-immunity.html#rest_code_7459b1b75bcd412cb05ec1a6de3eaa08-14"&gt;&lt;/a&gt;&lt;span class="n"&gt;sol&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt;
&lt;a id="rest_code_7459b1b75bcd412cb05ec1a6de3eaa08-15" name="rest_code_7459b1b75bcd412cb05ec1a6de3eaa08-15" href="https://tah-sci.com/posts/modelling-herd-immunity.html#rest_code_7459b1b75bcd412cb05ec1a6de3eaa08-15"&gt;&lt;/a&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;tt&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nb"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t0ran&lt;/span&gt;&lt;span class="p"&gt;)):&lt;/span&gt;
&lt;a id="rest_code_7459b1b75bcd412cb05ec1a6de3eaa08-16" name="rest_code_7459b1b75bcd412cb05ec1a6de3eaa08-16" href="https://tah-sci.com/posts/modelling-herd-immunity.html#rest_code_7459b1b75bcd412cb05ec1a6de3eaa08-16"&gt;&lt;/a&gt;    &lt;span class="n"&gt;sol&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;integrate&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;solve_ivp&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;lambda&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;odefun&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;beta0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;betat&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;t0ran&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;tt&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;&lt;span class="n"&gt;t0ran&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;tt&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="n"&gt;tend&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;sigma&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;gamma&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
&lt;a id="rest_code_7459b1b75bcd412cb05ec1a6de3eaa08-17" name="rest_code_7459b1b75bcd412cb05ec1a6de3eaa08-17" href="https://tah-sci.com/posts/modelling-herd-immunity.html#rest_code_7459b1b75bcd412cb05ec1a6de3eaa08-17"&gt;&lt;/a&gt;                              &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;0.0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;tlast&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
&lt;a id="rest_code_7459b1b75bcd412cb05ec1a6de3eaa08-18" name="rest_code_7459b1b75bcd412cb05ec1a6de3eaa08-18" href="https://tah-sci.com/posts/modelling-herd-immunity.html#rest_code_7459b1b75bcd412cb05ec1a6de3eaa08-18"&gt;&lt;/a&gt;                              &lt;span class="n"&gt;np&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mf"&gt;1.0&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mf"&gt;2.0&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;i0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;0.0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;0.0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;i0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;i0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;0.0&lt;/span&gt;&lt;span class="p"&gt;]),&lt;/span&gt;
&lt;a id="rest_code_7459b1b75bcd412cb05ec1a6de3eaa08-19" name="rest_code_7459b1b75bcd412cb05ec1a6de3eaa08-19" href="https://tah-sci.com/posts/modelling-herd-immunity.html#rest_code_7459b1b75bcd412cb05ec1a6de3eaa08-19"&gt;&lt;/a&gt;                              &lt;span class="s1"&gt;'RK45'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;a id="rest_code_7459b1b75bcd412cb05ec1a6de3eaa08-20" name="rest_code_7459b1b75bcd412cb05ec1a6de3eaa08-20" href="https://tah-sci.com/posts/modelling-herd-immunity.html#rest_code_7459b1b75bcd412cb05ec1a6de3eaa08-20"&gt;&lt;/a&gt;                              &lt;span class="n"&gt;atol&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mf"&gt;1e-8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;a id="rest_code_7459b1b75bcd412cb05ec1a6de3eaa08-21" name="rest_code_7459b1b75bcd412cb05ec1a6de3eaa08-21" href="https://tah-sci.com/posts/modelling-herd-immunity.html#rest_code_7459b1b75bcd412cb05ec1a6de3eaa08-21"&gt;&lt;/a&gt;                              &lt;span class="n"&gt;rtol&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mf"&gt;1e-9&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;div class="code"&gt;&lt;pre class="code ipython3"&gt;&lt;a id="rest_code_79060da8fb554e07823bb249f915fdf1-1" name="rest_code_79060da8fb554e07823bb249f915fdf1-1" href="https://tah-sci.com/posts/modelling-herd-immunity.html#rest_code_79060da8fb554e07823bb249f915fdf1-1"&gt;&lt;/a&gt;&lt;span class="n"&gt;plt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;figure&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;figsize&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;a id="rest_code_79060da8fb554e07823bb249f915fdf1-2" name="rest_code_79060da8fb554e07823bb249f915fdf1-2" href="https://tah-sci.com/posts/modelling-herd-immunity.html#rest_code_79060da8fb554e07823bb249f915fdf1-2"&gt;&lt;/a&gt;&lt;span class="n"&gt;plt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;subplot&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;a id="rest_code_79060da8fb554e07823bb249f915fdf1-3" name="rest_code_79060da8fb554e07823bb249f915fdf1-3" href="https://tah-sci.com/posts/modelling-herd-immunity.html#rest_code_79060da8fb554e07823bb249f915fdf1-3"&gt;&lt;/a&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;tt&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nb"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t0ran&lt;/span&gt;&lt;span class="p"&gt;)):&lt;/span&gt;
&lt;a id="rest_code_79060da8fb554e07823bb249f915fdf1-4" name="rest_code_79060da8fb554e07823bb249f915fdf1-4" href="https://tah-sci.com/posts/modelling-herd-immunity.html#rest_code_79060da8fb554e07823bb249f915fdf1-4"&gt;&lt;/a&gt;    &lt;span class="n"&gt;plt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;plot&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sol&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;tt&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;N&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sol&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;tt&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;sol&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;tt&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;label&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;mylab&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t0ran&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;tt&lt;/span&gt;&lt;span class="p"&gt;]))&lt;/span&gt;
&lt;a id="rest_code_79060da8fb554e07823bb249f915fdf1-5" name="rest_code_79060da8fb554e07823bb249f915fdf1-5" href="https://tah-sci.com/posts/modelling-herd-immunity.html#rest_code_79060da8fb554e07823bb249f915fdf1-5"&gt;&lt;/a&gt;&lt;span class="n"&gt;plt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;xlim&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;30&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;70&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;a id="rest_code_79060da8fb554e07823bb249f915fdf1-6" name="rest_code_79060da8fb554e07823bb249f915fdf1-6" href="https://tah-sci.com/posts/modelling-herd-immunity.html#rest_code_79060da8fb554e07823bb249f915fdf1-6"&gt;&lt;/a&gt;&lt;span class="n"&gt;plt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ylim&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mf"&gt;7e6&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;a id="rest_code_79060da8fb554e07823bb249f915fdf1-7" name="rest_code_79060da8fb554e07823bb249f915fdf1-7" href="https://tah-sci.com/posts/modelling-herd-immunity.html#rest_code_79060da8fb554e07823bb249f915fdf1-7"&gt;&lt;/a&gt;&lt;span class="n"&gt;plt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;xlabel&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'Time (days)'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;a id="rest_code_79060da8fb554e07823bb249f915fdf1-8" name="rest_code_79060da8fb554e07823bb249f915fdf1-8" href="https://tah-sci.com/posts/modelling-herd-immunity.html#rest_code_79060da8fb554e07823bb249f915fdf1-8"&gt;&lt;/a&gt;&lt;span class="n"&gt;plt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ylabel&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'Number of infectious cases'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;a id="rest_code_79060da8fb554e07823bb249f915fdf1-9" name="rest_code_79060da8fb554e07823bb249f915fdf1-9" href="https://tah-sci.com/posts/modelling-herd-immunity.html#rest_code_79060da8fb554e07823bb249f915fdf1-9"&gt;&lt;/a&gt;&lt;span class="n"&gt;plt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;legend&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;a id="rest_code_79060da8fb554e07823bb249f915fdf1-10" name="rest_code_79060da8fb554e07823bb249f915fdf1-10" href="https://tah-sci.com/posts/modelling-herd-immunity.html#rest_code_79060da8fb554e07823bb249f915fdf1-10"&gt;&lt;/a&gt;&lt;span class="n"&gt;plt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;subplot&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;a id="rest_code_79060da8fb554e07823bb249f915fdf1-11" name="rest_code_79060da8fb554e07823bb249f915fdf1-11" href="https://tah-sci.com/posts/modelling-herd-immunity.html#rest_code_79060da8fb554e07823bb249f915fdf1-11"&gt;&lt;/a&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;tt&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nb"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t0ran&lt;/span&gt;&lt;span class="p"&gt;)):&lt;/span&gt;
&lt;a id="rest_code_79060da8fb554e07823bb249f915fdf1-12" name="rest_code_79060da8fb554e07823bb249f915fdf1-12" href="https://tah-sci.com/posts/modelling-herd-immunity.html#rest_code_79060da8fb554e07823bb249f915fdf1-12"&gt;&lt;/a&gt;    &lt;span class="n"&gt;plt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;plot&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sol&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;tt&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;N&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;sol&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;tt&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;label&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;mylab&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t0ran&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;tt&lt;/span&gt;&lt;span class="p"&gt;]))&lt;/span&gt;
&lt;a id="rest_code_79060da8fb554e07823bb249f915fdf1-13" name="rest_code_79060da8fb554e07823bb249f915fdf1-13" href="https://tah-sci.com/posts/modelling-herd-immunity.html#rest_code_79060da8fb554e07823bb249f915fdf1-13"&gt;&lt;/a&gt;&lt;span class="n"&gt;plt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;xlabel&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'Time (days)'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;a id="rest_code_79060da8fb554e07823bb249f915fdf1-14" name="rest_code_79060da8fb554e07823bb249f915fdf1-14" href="https://tah-sci.com/posts/modelling-herd-immunity.html#rest_code_79060da8fb554e07823bb249f915fdf1-14"&gt;&lt;/a&gt;&lt;span class="n"&gt;plt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ylabel&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'Cumulative infections'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;a id="rest_code_79060da8fb554e07823bb249f915fdf1-15" name="rest_code_79060da8fb554e07823bb249f915fdf1-15" href="https://tah-sci.com/posts/modelling-herd-immunity.html#rest_code_79060da8fb554e07823bb249f915fdf1-15"&gt;&lt;/a&gt;&lt;span class="n"&gt;plt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;legend&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;a id="rest_code_79060da8fb554e07823bb249f915fdf1-16" name="rest_code_79060da8fb554e07823bb249f915fdf1-16" href="https://tah-sci.com/posts/modelling-herd-immunity.html#rest_code_79060da8fb554e07823bb249f915fdf1-16"&gt;&lt;/a&gt;&lt;span class="n"&gt;plt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;xlim&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;30&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;70&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;a id="rest_code_79060da8fb554e07823bb249f915fdf1-17" name="rest_code_79060da8fb554e07823bb249f915fdf1-17" href="https://tah-sci.com/posts/modelling-herd-immunity.html#rest_code_79060da8fb554e07823bb249f915fdf1-17"&gt;&lt;/a&gt;&lt;span class="n"&gt;plt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ylim&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mf"&gt;1e7&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;a id="rest_code_79060da8fb554e07823bb249f915fdf1-18" name="rest_code_79060da8fb554e07823bb249f915fdf1-18" href="https://tah-sci.com/posts/modelling-herd-immunity.html#rest_code_79060da8fb554e07823bb249f915fdf1-18"&gt;&lt;/a&gt;&lt;span class="n"&gt;plt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;tight_layout&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;a id="rest_code_79060da8fb554e07823bb249f915fdf1-19" name="rest_code_79060da8fb554e07823bb249f915fdf1-19" href="https://tah-sci.com/posts/modelling-herd-immunity.html#rest_code_79060da8fb554e07823bb249f915fdf1-19"&gt;&lt;/a&gt;&lt;span class="n"&gt;plt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;show&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;div class="code"&gt;&lt;pre class="code ipython3"&gt;&lt;a id="rest_code_dc1e09d7edab4b559b011d00164325da-1" name="rest_code_dc1e09d7edab4b559b011d00164325da-1" href="https://tah-sci.com/posts/modelling-herd-immunity.html#rest_code_dc1e09d7edab4b559b011d00164325da-1"&gt;&lt;/a&gt;&lt;span class="n"&gt;plt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;figure&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;figsize&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;a id="rest_code_dc1e09d7edab4b559b011d00164325da-2" name="rest_code_dc1e09d7edab4b559b011d00164325da-2" href="https://tah-sci.com/posts/modelling-herd-immunity.html#rest_code_dc1e09d7edab4b559b011d00164325da-2"&gt;&lt;/a&gt;&lt;span class="n"&gt;plt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;subplot&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;a id="rest_code_dc1e09d7edab4b559b011d00164325da-3" name="rest_code_dc1e09d7edab4b559b011d00164325da-3" href="https://tah-sci.com/posts/modelling-herd-immunity.html#rest_code_dc1e09d7edab4b559b011d00164325da-3"&gt;&lt;/a&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;tt&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nb"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t0ran&lt;/span&gt;&lt;span class="p"&gt;)):&lt;/span&gt;
&lt;a id="rest_code_dc1e09d7edab4b559b011d00164325da-4" name="rest_code_dc1e09d7edab4b559b011d00164325da-4" href="https://tah-sci.com/posts/modelling-herd-immunity.html#rest_code_dc1e09d7edab4b559b011d00164325da-4"&gt;&lt;/a&gt;    &lt;span class="n"&gt;plt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;plot&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sol&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;tt&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;N&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sol&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;tt&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;sol&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;tt&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;label&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;mylab&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t0ran&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;tt&lt;/span&gt;&lt;span class="p"&gt;]))&lt;/span&gt;
&lt;a id="rest_code_dc1e09d7edab4b559b011d00164325da-5" name="rest_code_dc1e09d7edab4b559b011d00164325da-5" href="https://tah-sci.com/posts/modelling-herd-immunity.html#rest_code_dc1e09d7edab4b559b011d00164325da-5"&gt;&lt;/a&gt;&lt;span class="n"&gt;plt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;xlim&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;tlast&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;a id="rest_code_dc1e09d7edab4b559b011d00164325da-6" name="rest_code_dc1e09d7edab4b559b011d00164325da-6" href="https://tah-sci.com/posts/modelling-herd-immunity.html#rest_code_dc1e09d7edab4b559b011d00164325da-6"&gt;&lt;/a&gt;&lt;span class="n"&gt;plt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ylim&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mf"&gt;1.2e7&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;a id="rest_code_dc1e09d7edab4b559b011d00164325da-7" name="rest_code_dc1e09d7edab4b559b011d00164325da-7" href="https://tah-sci.com/posts/modelling-herd-immunity.html#rest_code_dc1e09d7edab4b559b011d00164325da-7"&gt;&lt;/a&gt;&lt;span class="n"&gt;plt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;xlabel&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'Time (days)'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;a id="rest_code_dc1e09d7edab4b559b011d00164325da-8" name="rest_code_dc1e09d7edab4b559b011d00164325da-8" href="https://tah-sci.com/posts/modelling-herd-immunity.html#rest_code_dc1e09d7edab4b559b011d00164325da-8"&gt;&lt;/a&gt;&lt;span class="n"&gt;plt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ylabel&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'Number of infectious cases'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;a id="rest_code_dc1e09d7edab4b559b011d00164325da-9" name="rest_code_dc1e09d7edab4b559b011d00164325da-9" href="https://tah-sci.com/posts/modelling-herd-immunity.html#rest_code_dc1e09d7edab4b559b011d00164325da-9"&gt;&lt;/a&gt;&lt;span class="n"&gt;plt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;legend&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;a id="rest_code_dc1e09d7edab4b559b011d00164325da-10" name="rest_code_dc1e09d7edab4b559b011d00164325da-10" href="https://tah-sci.com/posts/modelling-herd-immunity.html#rest_code_dc1e09d7edab4b559b011d00164325da-10"&gt;&lt;/a&gt;&lt;span class="n"&gt;plt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;subplot&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;a id="rest_code_dc1e09d7edab4b559b011d00164325da-11" name="rest_code_dc1e09d7edab4b559b011d00164325da-11" href="https://tah-sci.com/posts/modelling-herd-immunity.html#rest_code_dc1e09d7edab4b559b011d00164325da-11"&gt;&lt;/a&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;tt&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nb"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t0ran&lt;/span&gt;&lt;span class="p"&gt;)):&lt;/span&gt;
&lt;a id="rest_code_dc1e09d7edab4b559b011d00164325da-12" name="rest_code_dc1e09d7edab4b559b011d00164325da-12" href="https://tah-sci.com/posts/modelling-herd-immunity.html#rest_code_dc1e09d7edab4b559b011d00164325da-12"&gt;&lt;/a&gt;    &lt;span class="n"&gt;plt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;plot&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sol&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;tt&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;N&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;sol&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;tt&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;label&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;mylab&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t0ran&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;tt&lt;/span&gt;&lt;span class="p"&gt;]))&lt;/span&gt;
&lt;a id="rest_code_dc1e09d7edab4b559b011d00164325da-13" name="rest_code_dc1e09d7edab4b559b011d00164325da-13" href="https://tah-sci.com/posts/modelling-herd-immunity.html#rest_code_dc1e09d7edab4b559b011d00164325da-13"&gt;&lt;/a&gt;&lt;span class="n"&gt;plt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;xlabel&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'Time (days)'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;a id="rest_code_dc1e09d7edab4b559b011d00164325da-14" name="rest_code_dc1e09d7edab4b559b011d00164325da-14" href="https://tah-sci.com/posts/modelling-herd-immunity.html#rest_code_dc1e09d7edab4b559b011d00164325da-14"&gt;&lt;/a&gt;&lt;span class="n"&gt;plt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ylabel&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'Cumulative infections'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;a id="rest_code_dc1e09d7edab4b559b011d00164325da-15" name="rest_code_dc1e09d7edab4b559b011d00164325da-15" href="https://tah-sci.com/posts/modelling-herd-immunity.html#rest_code_dc1e09d7edab4b559b011d00164325da-15"&gt;&lt;/a&gt;&lt;span class="n"&gt;plt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;legend&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;a id="rest_code_dc1e09d7edab4b559b011d00164325da-16" name="rest_code_dc1e09d7edab4b559b011d00164325da-16" href="https://tah-sci.com/posts/modelling-herd-immunity.html#rest_code_dc1e09d7edab4b559b011d00164325da-16"&gt;&lt;/a&gt;&lt;span class="n"&gt;plt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;xlim&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;tlast&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;a id="rest_code_dc1e09d7edab4b559b011d00164325da-17" name="rest_code_dc1e09d7edab4b559b011d00164325da-17" href="https://tah-sci.com/posts/modelling-herd-immunity.html#rest_code_dc1e09d7edab4b559b011d00164325da-17"&gt;&lt;/a&gt;&lt;span class="n"&gt;plt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ylim&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mf"&gt;6.2e7&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;a id="rest_code_dc1e09d7edab4b559b011d00164325da-18" name="rest_code_dc1e09d7edab4b559b011d00164325da-18" href="https://tah-sci.com/posts/modelling-herd-immunity.html#rest_code_dc1e09d7edab4b559b011d00164325da-18"&gt;&lt;/a&gt;&lt;span class="n"&gt;plt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;tight_layout&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;a id="rest_code_dc1e09d7edab4b559b011d00164325da-19" name="rest_code_dc1e09d7edab4b559b011d00164325da-19" href="https://tah-sci.com/posts/modelling-herd-immunity.html#rest_code_dc1e09d7edab4b559b011d00164325da-19"&gt;&lt;/a&gt;&lt;span class="n"&gt;plt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;show&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;</description><guid>https://tah-sci.com/posts/modelling-herd-immunity.html</guid><pubDate>Sat, 14 Mar 2020 09:54:38 GMT</pubDate></item><item><title>Semester 1 2018</title><link>https://tah-sci.com/posts/semester-1-2018.html</link><dc:creator>Thomas House</dc:creator><description>&lt;p&gt;This table gives a rough idea of where I was in Semester 1, 2018; I'm putting
it here mainly in case I need to re-use the HTML.&lt;/p&gt;
&lt;p&gt;&lt;span class="raw-html"&gt;&lt;div align="center"&gt;
&lt;table class="timetable"&gt;
&lt;tr&gt;
&lt;th class="timetable"&gt;&lt;/th&gt;
&lt;th class="timetable"&gt;09:00 &lt;/th&gt;
&lt;th class="timetable"&gt;10:00 &lt;/th&gt;
&lt;th class="timetable"&gt;11:00 &lt;/th&gt;
&lt;th class="timetable"&gt;12:00 &lt;/th&gt;
&lt;th class="timetable"&gt;13:00 &lt;/th&gt;
&lt;th class="timetable"&gt;14:00 &lt;/th&gt;
&lt;th class="timetable"&gt;15:00 &lt;/th&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;th class="timetable"&gt; &lt;br&gt;MON&lt;br&gt; &lt;/th&gt;
&lt;td class="timetable freehour"&gt; &lt;/td&gt; &lt;!--  9:00 --&gt;
&lt;td class="timetable" colspan="2"&gt;
Statstics and Machine Learning 1&lt;br&gt;
&lt;em&gt;Lecture&lt;/em&gt; &lt;br&gt;
Simon Building: 4.47&lt;br&gt;
&lt;/td&gt;
&lt;td class="timetable freehour"&gt; &lt;/td&gt;
&lt;td class="timetable freehour"&gt; &lt;/td&gt; &lt;!--  1:00 --&gt;
&lt;td class="timetable" colspan="2"&gt;
Statstics and Machine Learning 1&lt;br&gt;
&lt;em&gt;Computer Class&lt;/em&gt; &lt;br&gt;
Ellen Wilkinson: B3.3&lt;br&gt;
&lt;/td&gt;
&lt;/tr&gt;&lt;tr&gt;
&lt;th class="timetable"&gt; &lt;br&gt;TUES&lt;br&gt; &lt;/th&gt;
&lt;td class="timetable freehour"&gt; &lt;/td&gt;      &lt;!--  9:00 --&gt;
&lt;td class="timetable freehour"&gt; &lt;/td&gt;      &lt;!--  10:00 --&gt;
&lt;td class="timetable freehour"&gt; &lt;/td&gt;      &lt;!--  11:00 --&gt;
&lt;td class="timetable"&gt;
SQUIDS&lt;br&gt;
&lt;em&gt;Seminar&lt;/em&gt;&lt;br&gt;
Frank Adams 1&lt;br&gt;
&lt;/td&gt;
&lt;td class="timetable freehour"&gt; &lt;/td&gt;      &lt;!--  1:00 --&gt;
&lt;td class="timetable freehour"&gt; &lt;/td&gt;      &lt;!--  2:00 --&gt;
&lt;td class="timetable freehour"&gt; &lt;/td&gt;      &lt;!--  3:00 --&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;th class="timetable"&gt; &lt;br&gt;WEDS&lt;br&gt; &lt;/th&gt;
&lt;td class="timetable freehour"&gt; &lt;/td&gt;      &lt;!--  9:00 --&gt;
&lt;td class="timetable" colspan="2"&gt;
Multivariate Statistics&lt;br&gt; &lt;em&gt;Lecture&lt;/em&gt;&lt;br&gt;
University Place 3.213
&lt;/td&gt;
&lt;td class="timetable"&gt;
IM(PA):CT!&lt;br&gt;
&lt;em&gt;Seminar&lt;/em&gt;&lt;br&gt;
Frank Adams 1&lt;br&gt;
&lt;/td&gt;
&lt;td class="timetable freehour"&gt; &lt;/td&gt; &lt;!--  1:00 --&gt;
&lt;td class="timetable freehour"&gt; &lt;/td&gt; &lt;!--  2:00 --&gt;
&lt;td class="timetable freehour"&gt; &lt;/td&gt; &lt;!--  3:00 --&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;th class="timetable"&gt; &lt;br&gt;THURS&lt;br&gt; &lt;/th&gt;
&lt;td class="timetable freehour"&gt; &lt;/td&gt; &lt;!--  9:00 --&gt;
&lt;td class="timetable freehour"&gt; &lt;/td&gt; &lt;!-- 10:00 --&gt;
&lt;td class="timetable freehour"&gt; &lt;/td&gt; &lt;!-- 11:00 --&gt;
&lt;td class="timetable freehour"&gt; &lt;/td&gt; &lt;!-- 12:00 --&gt;
&lt;td class="timetable freehour"&gt; &lt;/td&gt; &lt;!--  1:00 --&gt;
&lt;td class="timetable freehour"&gt; &lt;/td&gt; &lt;!--  2:00 --&gt;
&lt;td class="timetable freehour"&gt; &lt;/td&gt; &lt;!--  3:00 --&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;th class="timetable"&gt; &lt;br&gt;FRI&lt;br&gt; &lt;/th&gt;
&lt;td class="timetable"&gt;
Multivariate Statistics&lt;br&gt; &lt;em&gt;Lecture&lt;/em&gt;&lt;br&gt;
University Place 3.214
&lt;/td&gt;
&lt;td class="timetable"&gt;
Multivariate Statistics&lt;br&gt; &lt;em&gt;Tutorial&lt;/em&gt;&lt;br&gt;
University Place 3.214
&lt;/td&gt;
&lt;td class="timetable" colspan="2"&gt;
Office Hours&lt;br&gt; &lt;em&gt;(11.30-12.30 guaranteed)&lt;/em&gt;&lt;br&gt;
Alan Turing Building 1.114
&lt;/td&gt;
&lt;td class="timetable"&gt;
Calculus and Vectors&lt;br&gt; &lt;em&gt;Group 14 Supervision&lt;/em&gt;&lt;br&gt;
Alan Turing Building G.108
&lt;/td&gt;
&lt;td class="timetable freehour"&gt; &lt;/td&gt; &lt;!--  2:00 --&gt;
&lt;td class="timetable"&gt;
Calculus and Vectors&lt;br&gt; &lt;em&gt;Group 38 Supervision&lt;/em&gt;&lt;br&gt;
Alan Turing Building G.221A
&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;/div&gt;&lt;/span&gt;&lt;/p&gt;</description><guid>https://tah-sci.com/posts/semester-1-2018.html</guid><pubDate>Fri, 21 Dec 2018 10:30:44 GMT</pubDate></item><item><title>Opportunities in Manchester</title><link>https://tah-sci.com/posts/opportunities-in-manchester.html</link><dc:creator>Thomas House</dc:creator><description>&lt;img alt="Mathematical Epidemiologists 2018" class="align-right" src="https://tah-sci.com/group_2018.jpg" style="width: 360px;"&gt;
&lt;p&gt;There are various opportunities to work in areas related to Mathematical
Epidemiology in Manchester at present - a recent meeting of our lively and
growing group is shown, please get in touch if you're interested!&lt;/p&gt;
&lt;section id="phd"&gt;
&lt;h2&gt;PhD:&lt;/h2&gt;
&lt;p&gt;&lt;a class="reference external" href="https://www.findaphd.com/search/projectdetails.aspx?PJID=80395"&gt;Understanding and Mitigating Against Organ Damage from Radiotherapy using
Statistical Learning from Large Cohort Data&lt;/a&gt; (Deadline
Friday 16 November) is a PhD programme looking at methods for cancer
radiotherapy data with the world-leading group at the Christie Hospital.&lt;/p&gt;
&lt;p&gt;&lt;a class="reference external" href="https://www.turing.ac.uk/phd-at-turing"&gt;Alan Turing Institute PhD Programme&lt;/a&gt; -
Lorenzo Pellis, Ian Hall and I are Alan Turing Institute Fellows and can support
applications to this scheme.&lt;/p&gt;
&lt;/section&gt;
&lt;section id="postdocotral"&gt;
&lt;h2&gt;Postdocotral:&lt;/h2&gt;
&lt;p&gt;&lt;a class="reference external" href="https://www.jobs.manchester.ac.uk/internal/displayjob.aspx?jobid=16284"&gt;Dame Kathleen Ollerenshaw Fellowships&lt;/a&gt;
(Deadline 23 November) are 5 year postdoctoral positions leading on to tenure.&lt;/p&gt;
&lt;/section&gt;
&lt;section id="faculty"&gt;
&lt;h2&gt;Faculty:&lt;/h2&gt;
&lt;p&gt;There are also two open Faculty positions:
&lt;a class="reference external" href="https://www.jobs.ac.uk/job/BOA591/lecturer-senior-lecturer-or-reader-in-applied-mathematics"&gt;Lecturer, Senior Lecturer or Reader in Applied Mathematics&lt;/a&gt;
and
&lt;a class="reference external" href="https://www.jobs.ac.uk/job/BOA582/lecturer-in-mathematical-finance-mathematical-statistics"&gt;Lecturer in Mathematical Finance / Mathematical Statistics&lt;/a&gt;
(Deadline 11 January).&lt;/p&gt;
&lt;/section&gt;</description><guid>https://tah-sci.com/posts/opportunities-in-manchester.html</guid><pubDate>Mon, 12 Nov 2018 12:06:21 GMT</pubDate></item><item><title>Caffeine in Pregnancy, Weak Identifiability, and Working with the Media</title><link>https://tah-sci.com/posts/caffeine-in-pregnancy-weak-identifiability-and-working-with-the-media.html</link><dc:creator>Thomas House</dc:creator><description>&lt;p&gt;A recent &lt;a class="reference external" href="http://bmjopen.bmj.com/content/8/3/e018895/"&gt;Paper in BMJ Open&lt;/a&gt;
deals with the question of maternal caffeine intake and childhood obesity.&lt;/p&gt;
&lt;p&gt;I worked with the Science Media Centre to produce a &lt;a class="reference external" href="http://www.sciencemediacentre.org/expert-reaction-to-study-looking-at-caffeine-in-pregnancy-and-weight-gain-in-children/"&gt;Press Release&lt;/a&gt; in which I made
the comment:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;"This study makes the headline claim that in a population of over 50,000
mother-child pairs, caffeine intake is positively associated with risk of
childhood obesity.  Let us make a ‘back of the envelope’ calculation on one
of the outcome measures using the cup of instant coffee per day as a measure
of the mother’s caffeine intake.&lt;/p&gt;
&lt;p&gt;If we take 100 children whose mother group had the lowest caffeine intake (1
cup or less) then at age three, around 11 will be obese; in the next group
(1-4 cups) there will be one additional obese child (12 in total); in the
next (4-6 cups) there will be an additional two (14 in total) and in the
group with highest caffeine intake (6 or more cups) there will be an
additional three (17 in total).&lt;/p&gt;
&lt;p&gt;The main problem with the study is that
maternal caffeine intake is strongly correlated with other relevant factors,
and so these should be controlled for.  In the authors’ statistical analyses,
the ‘best fit’ models they’ve used suggest around half of the additional
cases of obesity in children of age 3 are explained by caffeine rather than,
say, calorie intake of mothers.  But since the caffeine and calorie intake
are correlated, the existence of a ‘best fit’ model with equal weighting on
both means we actually can’t say whether a model looking at just calorie
intake of mothers would be an equally good explanation of child obesity.  It
also says nothing about whether including a new explanatory measure, such as
the child’s calorie intake, would change the model.&lt;/p&gt;
&lt;p&gt;As such, the evidence
provided in the study for a causal effect is extremely weak and the statement
from the authors that 'complete avoidance might actually be advisable' seems
unjustified, particularly when we consider the effects that such a
restriction might have on wellbeing of mothers."&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;This story was quite widely covered, but most papers took the same approach as
&lt;a class="reference external" href="https://inews.co.uk/news/health/pregnant-women-babies-caffeine-consumption-overweight/"&gt;iNews&lt;/a&gt;
and only quoted my main conclusion, but not the reasoning.&lt;/p&gt;
&lt;p&gt;So let's consider a simple exaple that exhibits the main problem, in particular
the following contingency table. This is much smaller than the original study,
but the same phenomenon is exhibited by larger populations with more
explanatory factors:&lt;/p&gt;
&lt;table&gt;
&lt;tbody&gt;
&lt;tr&gt;&lt;td colspan="2" rowspan="2"&gt;&lt;/td&gt;
&lt;td colspan="4"&gt;&lt;p&gt;Caffeine Intake&lt;/p&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan="2"&gt;&lt;p&gt;Low&lt;/p&gt;&lt;/td&gt;
&lt;td colspan="2"&gt;&lt;p&gt;High&lt;/p&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;&lt;td rowspan="4"&gt;&lt;p&gt;Calorie
Intake&lt;/p&gt;&lt;/td&gt;
&lt;td rowspan="2"&gt;&lt;p&gt;Low&lt;/p&gt;&lt;/td&gt;
&lt;td&gt;&lt;p&gt;Healthy&lt;/p&gt;&lt;/td&gt;
&lt;td&gt;&lt;p&gt;Overweight&lt;/p&gt;&lt;/td&gt;
&lt;td&gt;&lt;p&gt;Healthy&lt;/p&gt;&lt;/td&gt;
&lt;td&gt;&lt;p&gt;Overweight&lt;/p&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;p&gt;63&lt;/p&gt;&lt;/td&gt;
&lt;td&gt;&lt;p&gt;7&lt;/p&gt;&lt;/td&gt;
&lt;td&gt;&lt;p&gt;3&lt;/p&gt;&lt;/td&gt;
&lt;td&gt;&lt;p&gt;2&lt;/p&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;&lt;td rowspan="2"&gt;&lt;p&gt;High&lt;/p&gt;&lt;/td&gt;
&lt;td&gt;&lt;p&gt;Healthy&lt;/p&gt;&lt;/td&gt;
&lt;td&gt;&lt;p&gt;Overweight&lt;/p&gt;&lt;/td&gt;
&lt;td&gt;&lt;p&gt;Healthy&lt;/p&gt;&lt;/td&gt;
&lt;td&gt;&lt;p&gt;Overweight&lt;/p&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;&lt;p&gt;2&lt;/p&gt;&lt;/td&gt;
&lt;td&gt;&lt;p&gt;3&lt;/p&gt;&lt;/td&gt;
&lt;td&gt;&lt;p&gt;5&lt;/p&gt;&lt;/td&gt;
&lt;td&gt;&lt;p&gt;15&lt;/p&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;p&gt;Note that 68% of the high caffeine group is overweight, while only 13% of the
low caffeine group is, but we note that calorie intake may be an issue so let
us perform logistic regression as the authors did, letting
&lt;span class="raw-html"&gt;\[\mathrm{Pr}(\mathrm{Overweight}) = 1/(1+ \mathrm{exp}(-\beta_0
- \beta_1 \times \mathrm{HighCalorie} - \beta_2 \times \mathrm{HighCaffeine})) \mathrm{ ,}\]&lt;/span&gt;
where &lt;span class="raw-html"&gt;\(\beta_0\)&lt;/span&gt; is the intercept, &lt;span class="raw-html"&gt;\(\beta_1\)&lt;/span&gt; is the
log odds due to calories and &lt;span class="raw-html"&gt;\(\beta_2\)&lt;/span&gt; is the log odds due to
caffeine. Setting the intercept at its maximum likelihood value, the maximum
likelihood values for the other parameters are shown as the cross below, which
attributes roughly equal importance to the two factors:&lt;/p&gt;
&lt;img alt="Likelihood surface" src="https://tah-sci.com/likelihood.png" style="width: 400px;"&gt;
&lt;p&gt;Behind this cross is a shaded region with intensity equal to the likelihood of
different parameter combinations - and we note that there is still a reasonable
amount of shading in the area where &lt;span class="raw-html"&gt;\(\beta_2 \approx 0\)&lt;/span&gt;. This is
a special case of a general phenomenon called weak identifiability, and it is
something &lt;a class="reference external" href="http://rsif.royalsocietypublishing.org/content/13/121/20160279/"&gt;I've done a bit of work on&lt;/a&gt; - there
are various ways to deal with this, but its presence is the reason that
evidence for a causal effect is weak in the original study.&lt;/p&gt;</description><guid>https://tah-sci.com/posts/caffeine-in-pregnancy-weak-identifiability-and-working-with-the-media.html</guid><pubDate>Wed, 25 Apr 2018 12:19:27 GMT</pubDate></item><item><title>Visualising the Wishart Distribution</title><link>https://tah-sci.com/posts/visualising-the-wishart-distribution.html</link><dc:creator>Thomas House</dc:creator><description>&lt;p&gt;This semester, I have been teaching Multivariate Statistics, and one isse that comes up is how to think
about the Wishart distribution, which generalises the &lt;span class="raw-html"&gt;\(\chi^2 \)&lt;/span&gt; distribution. This has
probability density function&lt;/p&gt;
&lt;p&gt;&lt;span class="raw-html"&gt;$$ f_{\chi^2_k}(x) = \frac{1}{2\,\Gamma\!\left(\frac{k}{2}\right)} \left(\frac{x}{2}\right)^{\frac{k}{2}-1} {\rm e}^{-\frac{x}{2}} . $$&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;To visualise this at different degrees of freedom &lt;span class="raw-html"&gt;\(k \)&lt;/span&gt;, we use the following Python code.&lt;/p&gt;
&lt;div class="code"&gt;&lt;pre class="code ipython3"&gt;&lt;a id="rest_code_ea3e5051c3dd47cba418f60c4dbeaee4-1" name="rest_code_ea3e5051c3dd47cba418f60c4dbeaee4-1" href="https://tah-sci.com/posts/visualising-the-wishart-distribution.html#rest_code_ea3e5051c3dd47cba418f60c4dbeaee4-1"&gt;&lt;/a&gt;&lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="k"&gt;matplotlib&lt;/span&gt; inline
&lt;a id="rest_code_ea3e5051c3dd47cba418f60c4dbeaee4-2" name="rest_code_ea3e5051c3dd47cba418f60c4dbeaee4-2" href="https://tah-sci.com/posts/visualising-the-wishart-distribution.html#rest_code_ea3e5051c3dd47cba418f60c4dbeaee4-2"&gt;&lt;/a&gt;&lt;span class="kn"&gt;import&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nn"&gt;numpy&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;as&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nn"&gt;np&lt;/span&gt;
&lt;a id="rest_code_ea3e5051c3dd47cba418f60c4dbeaee4-3" name="rest_code_ea3e5051c3dd47cba418f60c4dbeaee4-3" href="https://tah-sci.com/posts/visualising-the-wishart-distribution.html#rest_code_ea3e5051c3dd47cba418f60c4dbeaee4-3"&gt;&lt;/a&gt;&lt;span class="kn"&gt;import&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nn"&gt;scipy.stats&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;as&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nn"&gt;st&lt;/span&gt;
&lt;a id="rest_code_ea3e5051c3dd47cba418f60c4dbeaee4-4" name="rest_code_ea3e5051c3dd47cba418f60c4dbeaee4-4" href="https://tah-sci.com/posts/visualising-the-wishart-distribution.html#rest_code_ea3e5051c3dd47cba418f60c4dbeaee4-4"&gt;&lt;/a&gt;&lt;span class="kn"&gt;import&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nn"&gt;matplotlib.pyplot&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="k"&gt;as&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nn"&gt;plt&lt;/span&gt;
&lt;a id="rest_code_ea3e5051c3dd47cba418f60c4dbeaee4-5" name="rest_code_ea3e5051c3dd47cba418f60c4dbeaee4-5" href="https://tah-sci.com/posts/visualising-the-wishart-distribution.html#rest_code_ea3e5051c3dd47cba418f60c4dbeaee4-5"&gt;&lt;/a&gt;&lt;span class="kn"&gt;from&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="nn"&gt;mpl_toolkits.mplot3d&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="n"&gt;Axes3D&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;div class="code"&gt;&lt;pre class="code ipython3"&gt;&lt;a id="rest_code_e6b233bc34704c12973f07cf93a3cc08-1" name="rest_code_e6b233bc34704c12973f07cf93a3cc08-1" href="https://tah-sci.com/posts/visualising-the-wishart-distribution.html#rest_code_e6b233bc34704c12973f07cf93a3cc08-1"&gt;&lt;/a&gt;&lt;span class="n"&gt;np&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;random&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;seed&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;42&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;a id="rest_code_e6b233bc34704c12973f07cf93a3cc08-2" name="rest_code_e6b233bc34704c12973f07cf93a3cc08-2" href="https://tah-sci.com/posts/visualising-the-wishart-distribution.html#rest_code_e6b233bc34704c12973f07cf93a3cc08-2"&gt;&lt;/a&gt;&lt;span class="n"&gt;Sig1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;np&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="p"&gt;([[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mf"&gt;0.75&lt;/span&gt;&lt;span class="p"&gt;],[&lt;/span&gt;&lt;span class="mf"&gt;0.75&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;]])&lt;/span&gt;
&lt;a id="rest_code_e6b233bc34704c12973f07cf93a3cc08-3" name="rest_code_e6b233bc34704c12973f07cf93a3cc08-3" href="https://tah-sci.com/posts/visualising-the-wishart-distribution.html#rest_code_e6b233bc34704c12973f07cf93a3cc08-3"&gt;&lt;/a&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;1000&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;div class="code"&gt;&lt;pre class="code ipython3"&gt;&lt;a id="rest_code_3bdd4158daee4e969e81233bb740543d-1" name="rest_code_3bdd4158daee4e969e81233bb740543d-1" href="https://tah-sci.com/posts/visualising-the-wishart-distribution.html#rest_code_3bdd4158daee4e969e81233bb740543d-1"&gt;&lt;/a&gt;&lt;span class="n"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;np&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;linspace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;a id="rest_code_3bdd4158daee4e969e81233bb740543d-2" name="rest_code_3bdd4158daee4e969e81233bb740543d-2" href="https://tah-sci.com/posts/visualising-the-wishart-distribution.html#rest_code_3bdd4158daee4e969e81233bb740543d-2"&gt;&lt;/a&gt;&lt;span class="n"&gt;plt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;figure&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;figsize&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;a id="rest_code_3bdd4158daee4e969e81233bb740543d-3" name="rest_code_3bdd4158daee4e969e81233bb740543d-3" href="https://tah-sci.com/posts/visualising-the-wishart-distribution.html#rest_code_3bdd4158daee4e969e81233bb740543d-3"&gt;&lt;/a&gt;&lt;span class="n"&gt;colors&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;plt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;cm&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Accent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;np&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;linspace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;a id="rest_code_3bdd4158daee4e969e81233bb740543d-4" name="rest_code_3bdd4158daee4e969e81233bb740543d-4" href="https://tah-sci.com/posts/visualising-the-wishart-distribution.html#rest_code_3bdd4158daee4e969e81233bb740543d-4"&gt;&lt;/a&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;
&lt;a id="rest_code_3bdd4158daee4e969e81233bb740543d-5" name="rest_code_3bdd4158daee4e969e81233bb740543d-5" href="https://tah-sci.com/posts/visualising-the-wishart-distribution.html#rest_code_3bdd4158daee4e969e81233bb740543d-5"&gt;&lt;/a&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
&lt;a id="rest_code_3bdd4158daee4e969e81233bb740543d-6" name="rest_code_3bdd4158daee4e969e81233bb740543d-6" href="https://tah-sci.com/posts/visualising-the-wishart-distribution.html#rest_code_3bdd4158daee4e969e81233bb740543d-6"&gt;&lt;/a&gt;    &lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;st&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;chi2&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;pdf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;a id="rest_code_3bdd4158daee4e969e81233bb740543d-7" name="rest_code_3bdd4158daee4e969e81233bb740543d-7" href="https://tah-sci.com/posts/visualising-the-wishart-distribution.html#rest_code_3bdd4158daee4e969e81233bb740543d-7"&gt;&lt;/a&gt;    &lt;span class="n"&gt;plt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;plot&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;label&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s1"&gt;'k = '&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nb"&gt;str&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;k&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="n"&gt;color&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;colors&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;&lt;span class="n"&gt;linewidth&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;a id="rest_code_3bdd4158daee4e969e81233bb740543d-8" name="rest_code_3bdd4158daee4e969e81233bb740543d-8" href="https://tah-sci.com/posts/visualising-the-wishart-distribution.html#rest_code_3bdd4158daee4e969e81233bb740543d-8"&gt;&lt;/a&gt;    &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;a id="rest_code_3bdd4158daee4e969e81233bb740543d-9" name="rest_code_3bdd4158daee4e969e81233bb740543d-9" href="https://tah-sci.com/posts/visualising-the-wishart-distribution.html#rest_code_3bdd4158daee4e969e81233bb740543d-9"&gt;&lt;/a&gt;&lt;span class="n"&gt;plt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;xlim&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;a id="rest_code_3bdd4158daee4e969e81233bb740543d-10" name="rest_code_3bdd4158daee4e969e81233bb740543d-10" href="https://tah-sci.com/posts/visualising-the-wishart-distribution.html#rest_code_3bdd4158daee4e969e81233bb740543d-10"&gt;&lt;/a&gt;&lt;span class="n"&gt;plt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ylim&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mf"&gt;0.5&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;a id="rest_code_3bdd4158daee4e969e81233bb740543d-11" name="rest_code_3bdd4158daee4e969e81233bb740543d-11" href="https://tah-sci.com/posts/visualising-the-wishart-distribution.html#rest_code_3bdd4158daee4e969e81233bb740543d-11"&gt;&lt;/a&gt;&lt;span class="n"&gt;plt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;xlabel&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'x'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;a id="rest_code_3bdd4158daee4e969e81233bb740543d-12" name="rest_code_3bdd4158daee4e969e81233bb740543d-12" href="https://tah-sci.com/posts/visualising-the-wishart-distribution.html#rest_code_3bdd4158daee4e969e81233bb740543d-12"&gt;&lt;/a&gt;&lt;span class="n"&gt;plt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ylabel&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'f(x)'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;a id="rest_code_3bdd4158daee4e969e81233bb740543d-13" name="rest_code_3bdd4158daee4e969e81233bb740543d-13" href="https://tah-sci.com/posts/visualising-the-wishart-distribution.html#rest_code_3bdd4158daee4e969e81233bb740543d-13"&gt;&lt;/a&gt;&lt;span class="n"&gt;plt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;legend&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;img alt="../output_2_1.png" src="https://tah-sci.com/output_2_1.png"&gt;
&lt;p&gt;The Wishart emerges when we make &lt;span class="raw-html"&gt;\(k \)&lt;/span&gt; observations each with &lt;span class="raw-html"&gt;\(p \)&lt;/span&gt; variables; the
sampling distribution for the covariance matrix of these data if the population is multivariate normal is
Wishart and has probability density function&lt;/p&gt;
&lt;p&gt;&lt;span class="raw-html"&gt;$$ f_{\mathcal{W}_p(k,\boldsymbol{\Sigma})}(\boldsymbol{V}) = \frac{\mathrm{det}(\boldsymbol{V})^{\frac{k - p - 1}{2}}}{2^{ \frac{k p}{2} } \mathrm{det}(\boldsymbol{\Sigma})^\frac{k}{2} \Gamma_p\!\left(\frac{k}{2} \right)} \exp\left( -\frac12 \mathrm{Tr}(\boldsymbol{\Sigma}^{-1} \boldsymbol{V}) \right) . $$&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;In general, this cannot be visualised, but for the &lt;span class="raw-html"&gt;\(p=2 \)&lt;/span&gt; case, one option is to pick random
numbers from this distribution and then produce a three-dimensional scatter plot of these. The code and results
below show that this can be seen as behaving somewhat like the chi-squared distribution that it generalises as
the degrees of freedom &lt;span class="raw-html"&gt;\(k \)&lt;/span&gt; are increased.&lt;/p&gt;
&lt;div class="code"&gt;&lt;pre class="code ipython3"&gt;&lt;a id="rest_code_019ff909007f405c951a106317160693-1" name="rest_code_019ff909007f405c951a106317160693-1" href="https://tah-sci.com/posts/visualising-the-wishart-distribution.html#rest_code_019ff909007f405c951a106317160693-1"&gt;&lt;/a&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;np&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;zeros&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;a id="rest_code_019ff909007f405c951a106317160693-2" name="rest_code_019ff909007f405c951a106317160693-2" href="https://tah-sci.com/posts/visualising-the-wishart-distribution.html#rest_code_019ff909007f405c951a106317160693-2"&gt;&lt;/a&gt;&lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;np&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;zeros&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;a id="rest_code_019ff909007f405c951a106317160693-3" name="rest_code_019ff909007f405c951a106317160693-3" href="https://tah-sci.com/posts/visualising-the-wishart-distribution.html#rest_code_019ff909007f405c951a106317160693-3"&gt;&lt;/a&gt;&lt;span class="n"&gt;z&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;np&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;zeros&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;a id="rest_code_019ff909007f405c951a106317160693-4" name="rest_code_019ff909007f405c951a106317160693-4" href="https://tah-sci.com/posts/visualising-the-wishart-distribution.html#rest_code_019ff909007f405c951a106317160693-4"&gt;&lt;/a&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nb"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
&lt;a id="rest_code_019ff909007f405c951a106317160693-5" name="rest_code_019ff909007f405c951a106317160693-5" href="https://tah-sci.com/posts/visualising-the-wishart-distribution.html#rest_code_019ff909007f405c951a106317160693-5"&gt;&lt;/a&gt;    &lt;span class="n"&gt;M&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;st&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;wishart&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;rvs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;scale&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;Sig1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;size&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;a id="rest_code_019ff909007f405c951a106317160693-6" name="rest_code_019ff909007f405c951a106317160693-6" href="https://tah-sci.com/posts/visualising-the-wishart-distribution.html#rest_code_019ff909007f405c951a106317160693-6"&gt;&lt;/a&gt;    &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;M&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;a id="rest_code_019ff909007f405c951a106317160693-7" name="rest_code_019ff909007f405c951a106317160693-7" href="https://tah-sci.com/posts/visualising-the-wishart-distribution.html#rest_code_019ff909007f405c951a106317160693-7"&gt;&lt;/a&gt;    &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;M&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;a id="rest_code_019ff909007f405c951a106317160693-8" name="rest_code_019ff909007f405c951a106317160693-8" href="https://tah-sci.com/posts/visualising-the-wishart-distribution.html#rest_code_019ff909007f405c951a106317160693-8"&gt;&lt;/a&gt;    &lt;span class="n"&gt;z&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;M&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;a id="rest_code_019ff909007f405c951a106317160693-9" name="rest_code_019ff909007f405c951a106317160693-9" href="https://tah-sci.com/posts/visualising-the-wishart-distribution.html#rest_code_019ff909007f405c951a106317160693-9"&gt;&lt;/a&gt;&lt;span class="n"&gt;fig&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;plt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;figure&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;figsize&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;a id="rest_code_019ff909007f405c951a106317160693-10" name="rest_code_019ff909007f405c951a106317160693-10" href="https://tah-sci.com/posts/visualising-the-wishart-distribution.html#rest_code_019ff909007f405c951a106317160693-10"&gt;&lt;/a&gt;&lt;span class="n"&gt;ax&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;fig&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;add_subplot&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;111&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;projection&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s1"&gt;'3d'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;a id="rest_code_019ff909007f405c951a106317160693-11" name="rest_code_019ff909007f405c951a106317160693-11" href="https://tah-sci.com/posts/visualising-the-wishart-distribution.html#rest_code_019ff909007f405c951a106317160693-11"&gt;&lt;/a&gt;&lt;span class="n"&gt;ax&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;scatter3D&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;z&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;marker&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s1"&gt;'o'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;z&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;cmap&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s1"&gt;'seismic'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;a id="rest_code_019ff909007f405c951a106317160693-12" name="rest_code_019ff909007f405c951a106317160693-12" href="https://tah-sci.com/posts/visualising-the-wishart-distribution.html#rest_code_019ff909007f405c951a106317160693-12"&gt;&lt;/a&gt;&lt;span class="n"&gt;ax&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;set_xlabel&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'V11'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;a id="rest_code_019ff909007f405c951a106317160693-13" name="rest_code_019ff909007f405c951a106317160693-13" href="https://tah-sci.com/posts/visualising-the-wishart-distribution.html#rest_code_019ff909007f405c951a106317160693-13"&gt;&lt;/a&gt;&lt;span class="n"&gt;ax&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;set_ylabel&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'V22'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;a id="rest_code_019ff909007f405c951a106317160693-14" name="rest_code_019ff909007f405c951a106317160693-14" href="https://tah-sci.com/posts/visualising-the-wishart-distribution.html#rest_code_019ff909007f405c951a106317160693-14"&gt;&lt;/a&gt;&lt;span class="n"&gt;ax&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;set_zlabel&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'V12=V21'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;a id="rest_code_019ff909007f405c951a106317160693-15" name="rest_code_019ff909007f405c951a106317160693-15" href="https://tah-sci.com/posts/visualising-the-wishart-distribution.html#rest_code_019ff909007f405c951a106317160693-15"&gt;&lt;/a&gt;&lt;span class="n"&gt;ax&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;set_xlim&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;30&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;a id="rest_code_019ff909007f405c951a106317160693-16" name="rest_code_019ff909007f405c951a106317160693-16" href="https://tah-sci.com/posts/visualising-the-wishart-distribution.html#rest_code_019ff909007f405c951a106317160693-16"&gt;&lt;/a&gt;&lt;span class="n"&gt;ax&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;set_ylim&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;70&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;a id="rest_code_019ff909007f405c951a106317160693-17" name="rest_code_019ff909007f405c951a106317160693-17" href="https://tah-sci.com/posts/visualising-the-wishart-distribution.html#rest_code_019ff909007f405c951a106317160693-17"&gt;&lt;/a&gt;&lt;span class="n"&gt;ax&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;set_zlim&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;30&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;a id="rest_code_019ff909007f405c951a106317160693-18" name="rest_code_019ff909007f405c951a106317160693-18" href="https://tah-sci.com/posts/visualising-the-wishart-distribution.html#rest_code_019ff909007f405c951a106317160693-18"&gt;&lt;/a&gt;&lt;span class="n"&gt;plt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;tight_layout&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;img alt="../output_3_0.png" src="https://tah-sci.com/output_3_0.png"&gt;
&lt;div class="code"&gt;&lt;pre class="code ipython3"&gt;&lt;a id="rest_code_08fc558b62cf4788acb46d73bdebecc9-1" name="rest_code_08fc558b62cf4788acb46d73bdebecc9-1" href="https://tah-sci.com/posts/visualising-the-wishart-distribution.html#rest_code_08fc558b62cf4788acb46d73bdebecc9-1"&gt;&lt;/a&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;np&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;zeros&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;a id="rest_code_08fc558b62cf4788acb46d73bdebecc9-2" name="rest_code_08fc558b62cf4788acb46d73bdebecc9-2" href="https://tah-sci.com/posts/visualising-the-wishart-distribution.html#rest_code_08fc558b62cf4788acb46d73bdebecc9-2"&gt;&lt;/a&gt;&lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;np&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;zeros&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;a id="rest_code_08fc558b62cf4788acb46d73bdebecc9-3" name="rest_code_08fc558b62cf4788acb46d73bdebecc9-3" href="https://tah-sci.com/posts/visualising-the-wishart-distribution.html#rest_code_08fc558b62cf4788acb46d73bdebecc9-3"&gt;&lt;/a&gt;&lt;span class="n"&gt;z&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;np&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;zeros&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;a id="rest_code_08fc558b62cf4788acb46d73bdebecc9-4" name="rest_code_08fc558b62cf4788acb46d73bdebecc9-4" href="https://tah-sci.com/posts/visualising-the-wishart-distribution.html#rest_code_08fc558b62cf4788acb46d73bdebecc9-4"&gt;&lt;/a&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nb"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
&lt;a id="rest_code_08fc558b62cf4788acb46d73bdebecc9-5" name="rest_code_08fc558b62cf4788acb46d73bdebecc9-5" href="https://tah-sci.com/posts/visualising-the-wishart-distribution.html#rest_code_08fc558b62cf4788acb46d73bdebecc9-5"&gt;&lt;/a&gt;    &lt;span class="n"&gt;M&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;st&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;wishart&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;rvs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;scale&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;Sig1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;size&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;a id="rest_code_08fc558b62cf4788acb46d73bdebecc9-6" name="rest_code_08fc558b62cf4788acb46d73bdebecc9-6" href="https://tah-sci.com/posts/visualising-the-wishart-distribution.html#rest_code_08fc558b62cf4788acb46d73bdebecc9-6"&gt;&lt;/a&gt;    &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;M&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;a id="rest_code_08fc558b62cf4788acb46d73bdebecc9-7" name="rest_code_08fc558b62cf4788acb46d73bdebecc9-7" href="https://tah-sci.com/posts/visualising-the-wishart-distribution.html#rest_code_08fc558b62cf4788acb46d73bdebecc9-7"&gt;&lt;/a&gt;    &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;M&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;a id="rest_code_08fc558b62cf4788acb46d73bdebecc9-8" name="rest_code_08fc558b62cf4788acb46d73bdebecc9-8" href="https://tah-sci.com/posts/visualising-the-wishart-distribution.html#rest_code_08fc558b62cf4788acb46d73bdebecc9-8"&gt;&lt;/a&gt;    &lt;span class="n"&gt;z&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;M&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;a id="rest_code_08fc558b62cf4788acb46d73bdebecc9-9" name="rest_code_08fc558b62cf4788acb46d73bdebecc9-9" href="https://tah-sci.com/posts/visualising-the-wishart-distribution.html#rest_code_08fc558b62cf4788acb46d73bdebecc9-9"&gt;&lt;/a&gt;&lt;span class="n"&gt;fig&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;plt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;figure&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;figsize&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;a id="rest_code_08fc558b62cf4788acb46d73bdebecc9-10" name="rest_code_08fc558b62cf4788acb46d73bdebecc9-10" href="https://tah-sci.com/posts/visualising-the-wishart-distribution.html#rest_code_08fc558b62cf4788acb46d73bdebecc9-10"&gt;&lt;/a&gt;&lt;span class="n"&gt;ax&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;fig&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;add_subplot&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;111&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;projection&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s1"&gt;'3d'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;a id="rest_code_08fc558b62cf4788acb46d73bdebecc9-11" name="rest_code_08fc558b62cf4788acb46d73bdebecc9-11" href="https://tah-sci.com/posts/visualising-the-wishart-distribution.html#rest_code_08fc558b62cf4788acb46d73bdebecc9-11"&gt;&lt;/a&gt;&lt;span class="n"&gt;ax&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;scatter3D&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;z&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;marker&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s1"&gt;'o'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;z&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;cmap&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s1"&gt;'seismic'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;a id="rest_code_08fc558b62cf4788acb46d73bdebecc9-12" name="rest_code_08fc558b62cf4788acb46d73bdebecc9-12" href="https://tah-sci.com/posts/visualising-the-wishart-distribution.html#rest_code_08fc558b62cf4788acb46d73bdebecc9-12"&gt;&lt;/a&gt;&lt;span class="n"&gt;ax&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;set_xlabel&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'V11'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;a id="rest_code_08fc558b62cf4788acb46d73bdebecc9-13" name="rest_code_08fc558b62cf4788acb46d73bdebecc9-13" href="https://tah-sci.com/posts/visualising-the-wishart-distribution.html#rest_code_08fc558b62cf4788acb46d73bdebecc9-13"&gt;&lt;/a&gt;&lt;span class="n"&gt;ax&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;set_ylabel&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'V22'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;a id="rest_code_08fc558b62cf4788acb46d73bdebecc9-14" name="rest_code_08fc558b62cf4788acb46d73bdebecc9-14" href="https://tah-sci.com/posts/visualising-the-wishart-distribution.html#rest_code_08fc558b62cf4788acb46d73bdebecc9-14"&gt;&lt;/a&gt;&lt;span class="n"&gt;ax&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;set_zlabel&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'V12=V21'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;a id="rest_code_08fc558b62cf4788acb46d73bdebecc9-15" name="rest_code_08fc558b62cf4788acb46d73bdebecc9-15" href="https://tah-sci.com/posts/visualising-the-wishart-distribution.html#rest_code_08fc558b62cf4788acb46d73bdebecc9-15"&gt;&lt;/a&gt;&lt;span class="n"&gt;ax&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;set_xlim&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;30&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;a id="rest_code_08fc558b62cf4788acb46d73bdebecc9-16" name="rest_code_08fc558b62cf4788acb46d73bdebecc9-16" href="https://tah-sci.com/posts/visualising-the-wishart-distribution.html#rest_code_08fc558b62cf4788acb46d73bdebecc9-16"&gt;&lt;/a&gt;&lt;span class="n"&gt;ax&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;set_ylim&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;70&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;a id="rest_code_08fc558b62cf4788acb46d73bdebecc9-17" name="rest_code_08fc558b62cf4788acb46d73bdebecc9-17" href="https://tah-sci.com/posts/visualising-the-wishart-distribution.html#rest_code_08fc558b62cf4788acb46d73bdebecc9-17"&gt;&lt;/a&gt;&lt;span class="n"&gt;ax&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;set_zlim&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;30&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;a id="rest_code_08fc558b62cf4788acb46d73bdebecc9-18" name="rest_code_08fc558b62cf4788acb46d73bdebecc9-18" href="https://tah-sci.com/posts/visualising-the-wishart-distribution.html#rest_code_08fc558b62cf4788acb46d73bdebecc9-18"&gt;&lt;/a&gt;&lt;span class="n"&gt;plt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;tight_layout&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;img alt="../output_4_0.png" src="https://tah-sci.com/output_4_0.png"&gt;
&lt;div class="code"&gt;&lt;pre class="code ipython3"&gt;&lt;a id="rest_code_348d71401eac4d0d8ee267141cc9b4e5-1" name="rest_code_348d71401eac4d0d8ee267141cc9b4e5-1" href="https://tah-sci.com/posts/visualising-the-wishart-distribution.html#rest_code_348d71401eac4d0d8ee267141cc9b4e5-1"&gt;&lt;/a&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;np&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;zeros&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;a id="rest_code_348d71401eac4d0d8ee267141cc9b4e5-2" name="rest_code_348d71401eac4d0d8ee267141cc9b4e5-2" href="https://tah-sci.com/posts/visualising-the-wishart-distribution.html#rest_code_348d71401eac4d0d8ee267141cc9b4e5-2"&gt;&lt;/a&gt;&lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;np&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;zeros&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;a id="rest_code_348d71401eac4d0d8ee267141cc9b4e5-3" name="rest_code_348d71401eac4d0d8ee267141cc9b4e5-3" href="https://tah-sci.com/posts/visualising-the-wishart-distribution.html#rest_code_348d71401eac4d0d8ee267141cc9b4e5-3"&gt;&lt;/a&gt;&lt;span class="n"&gt;z&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;np&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;zeros&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;a id="rest_code_348d71401eac4d0d8ee267141cc9b4e5-4" name="rest_code_348d71401eac4d0d8ee267141cc9b4e5-4" href="https://tah-sci.com/posts/visualising-the-wishart-distribution.html#rest_code_348d71401eac4d0d8ee267141cc9b4e5-4"&gt;&lt;/a&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="nb"&gt;range&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
&lt;a id="rest_code_348d71401eac4d0d8ee267141cc9b4e5-5" name="rest_code_348d71401eac4d0d8ee267141cc9b4e5-5" href="https://tah-sci.com/posts/visualising-the-wishart-distribution.html#rest_code_348d71401eac4d0d8ee267141cc9b4e5-5"&gt;&lt;/a&gt;    &lt;span class="n"&gt;M&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;st&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;wishart&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;rvs&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;scale&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;Sig1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;size&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;a id="rest_code_348d71401eac4d0d8ee267141cc9b4e5-6" name="rest_code_348d71401eac4d0d8ee267141cc9b4e5-6" href="https://tah-sci.com/posts/visualising-the-wishart-distribution.html#rest_code_348d71401eac4d0d8ee267141cc9b4e5-6"&gt;&lt;/a&gt;    &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;M&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;a id="rest_code_348d71401eac4d0d8ee267141cc9b4e5-7" name="rest_code_348d71401eac4d0d8ee267141cc9b4e5-7" href="https://tah-sci.com/posts/visualising-the-wishart-distribution.html#rest_code_348d71401eac4d0d8ee267141cc9b4e5-7"&gt;&lt;/a&gt;    &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;M&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;a id="rest_code_348d71401eac4d0d8ee267141cc9b4e5-8" name="rest_code_348d71401eac4d0d8ee267141cc9b4e5-8" href="https://tah-sci.com/posts/visualising-the-wishart-distribution.html#rest_code_348d71401eac4d0d8ee267141cc9b4e5-8"&gt;&lt;/a&gt;    &lt;span class="n"&gt;z&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;M&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;a id="rest_code_348d71401eac4d0d8ee267141cc9b4e5-9" name="rest_code_348d71401eac4d0d8ee267141cc9b4e5-9" href="https://tah-sci.com/posts/visualising-the-wishart-distribution.html#rest_code_348d71401eac4d0d8ee267141cc9b4e5-9"&gt;&lt;/a&gt;&lt;span class="n"&gt;fig&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;plt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;figure&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;figsize&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;a id="rest_code_348d71401eac4d0d8ee267141cc9b4e5-10" name="rest_code_348d71401eac4d0d8ee267141cc9b4e5-10" href="https://tah-sci.com/posts/visualising-the-wishart-distribution.html#rest_code_348d71401eac4d0d8ee267141cc9b4e5-10"&gt;&lt;/a&gt;&lt;span class="n"&gt;ax&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;fig&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;add_subplot&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;111&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;projection&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s1"&gt;'3d'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;a id="rest_code_348d71401eac4d0d8ee267141cc9b4e5-11" name="rest_code_348d71401eac4d0d8ee267141cc9b4e5-11" href="https://tah-sci.com/posts/visualising-the-wishart-distribution.html#rest_code_348d71401eac4d0d8ee267141cc9b4e5-11"&gt;&lt;/a&gt;&lt;span class="n"&gt;ax&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;scatter3D&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="n"&gt;z&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;marker&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s1"&gt;'o'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="n"&gt;z&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;cmap&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s1"&gt;'seismic'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;a id="rest_code_348d71401eac4d0d8ee267141cc9b4e5-12" name="rest_code_348d71401eac4d0d8ee267141cc9b4e5-12" href="https://tah-sci.com/posts/visualising-the-wishart-distribution.html#rest_code_348d71401eac4d0d8ee267141cc9b4e5-12"&gt;&lt;/a&gt;&lt;span class="n"&gt;ax&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;set_xlabel&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'V11'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;a id="rest_code_348d71401eac4d0d8ee267141cc9b4e5-13" name="rest_code_348d71401eac4d0d8ee267141cc9b4e5-13" href="https://tah-sci.com/posts/visualising-the-wishart-distribution.html#rest_code_348d71401eac4d0d8ee267141cc9b4e5-13"&gt;&lt;/a&gt;&lt;span class="n"&gt;ax&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;set_ylabel&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'V22'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;a id="rest_code_348d71401eac4d0d8ee267141cc9b4e5-14" name="rest_code_348d71401eac4d0d8ee267141cc9b4e5-14" href="https://tah-sci.com/posts/visualising-the-wishart-distribution.html#rest_code_348d71401eac4d0d8ee267141cc9b4e5-14"&gt;&lt;/a&gt;&lt;span class="n"&gt;ax&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;set_zlabel&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'V12=V21'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;a id="rest_code_348d71401eac4d0d8ee267141cc9b4e5-15" name="rest_code_348d71401eac4d0d8ee267141cc9b4e5-15" href="https://tah-sci.com/posts/visualising-the-wishart-distribution.html#rest_code_348d71401eac4d0d8ee267141cc9b4e5-15"&gt;&lt;/a&gt;&lt;span class="n"&gt;ax&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;set_xlim&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;30&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;a id="rest_code_348d71401eac4d0d8ee267141cc9b4e5-16" name="rest_code_348d71401eac4d0d8ee267141cc9b4e5-16" href="https://tah-sci.com/posts/visualising-the-wishart-distribution.html#rest_code_348d71401eac4d0d8ee267141cc9b4e5-16"&gt;&lt;/a&gt;&lt;span class="n"&gt;ax&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;set_ylim&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;70&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;a id="rest_code_348d71401eac4d0d8ee267141cc9b4e5-17" name="rest_code_348d71401eac4d0d8ee267141cc9b4e5-17" href="https://tah-sci.com/posts/visualising-the-wishart-distribution.html#rest_code_348d71401eac4d0d8ee267141cc9b4e5-17"&gt;&lt;/a&gt;&lt;span class="n"&gt;ax&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;set_zlim&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;30&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;a id="rest_code_348d71401eac4d0d8ee267141cc9b4e5-18" name="rest_code_348d71401eac4d0d8ee267141cc9b4e5-18" href="https://tah-sci.com/posts/visualising-the-wishart-distribution.html#rest_code_348d71401eac4d0d8ee267141cc9b4e5-18"&gt;&lt;/a&gt;&lt;span class="n"&gt;plt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;tight_layout&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/pre&gt;&lt;/div&gt;
&lt;img alt="../output_5_0.png" src="https://tah-sci.com/output_5_0.png"&gt;</description><guid>https://tah-sci.com/posts/visualising-the-wishart-distribution.html</guid><pubDate>Fri, 08 Dec 2017 12:08:22 GMT</pubDate></item><item><title>Mathematics of Memes</title><link>https://tah-sci.com/posts/mathematics-of-memes.html</link><dc:creator>Thomas House</dc:creator><description>&lt;p&gt;Models of epidemics can be adapted to consider the spread of internet Memes - for more information see
the University &lt;a class="reference external" href="http://www.manchester.ac.uk/discover/news/the-maths-behind-memes-why-numbers-could-help-us-predict-an-internet-sensation/"&gt;Press Release&lt;/a&gt;.
Below is a figure showing some of these Memes 'in the wild'! We hope that the work will be possible to extend
to more substantial health-related behaviours, but Memes provide a relatively pure signal of the kind
of social influence that must be accounted for in such contexts, and as such are a good way to develop methods.&lt;/p&gt;
&lt;figure style="width: 480px"&gt;
&lt;img alt="Memes considered in the paper" src="https://tah-sci.com/fig1.png"&gt;
&lt;/figure&gt;
&lt;p&gt;I also recently gave a talk at the local Galois Group (undergraduate mathematics) on the work:&lt;/p&gt;
&lt;p&gt;&lt;span class="raw-html"&gt;&lt;iframe src="//www.slideshare.net/slideshow/embed_code/key/oXVVGDFwrTNyQv" width="595" height="485" frameborder="0" marginwidth="0" marginheight="0" scrolling="no" style="border:1px solid #CCC; border-width:1px; margin-bottom:5px; max-width: 100%;" allowfullscreen&gt; &lt;/iframe&gt; &lt;div style="margin-bottom:5px"&gt; &lt;strong&gt; &lt;a href="//www.slideshare.net/secret/oXVVGDFwrTNyQv" title="The Mathematics of Memes" target="_blank"&gt;The Mathematics of Memes&lt;/a&gt; &lt;/strong&gt; from &lt;strong&gt;&lt;a href="https://www.slideshare.net/ThomasHouse5" target="_blank"&gt;Thomas House&lt;/a&gt;&lt;/strong&gt; &lt;/div&gt;&lt;/span&gt;&lt;/p&gt;</description><guid>https://tah-sci.com/posts/mathematics-of-memes.html</guid><pubDate>Wed, 20 Sep 2017 12:38:56 GMT</pubDate></item><item><title>Operationalising Modern Mathematical Epidemiology</title><link>https://tah-sci.com/posts/operationalising-modern-mathematical-epidemiology.html</link><dc:creator>Thomas House</dc:creator><description>&lt;p&gt;This month Liz Buckingham-Jeffery is joining me and Tim Kinyanjui to complete
the team for the EPSRC-funded project &lt;em&gt;Operationalising Modern Mathematical
Epidemiology&lt;/em&gt;, which is a Healthcare Technologies Impact Fellowship I hold
(picture of us all below). The project is about development of some recent
ideas at developed by mathematical scientists to generate benefits in the
healthcare system.  We have non-academic partners from three major Manchester
hospitals (Central Manchester, North Manchester and the Christie), Public
Health England's national Real-time Syndromic Surveillance group, and a data
analytics firm Spectra Analytics. The first papers from this project are
starting be be submitted, and we have an ambitious programme of research and
impact-related activity over the next couple of years to ensure that the
potential benefits to population health from theoretical developments are
realised.&lt;/p&gt;
&lt;img alt="Me, Liz and Tim" src="https://tah-sci.com/th_ebj_tmk.jpg" style="width: 600px;"&gt;</description><guid>https://tah-sci.com/posts/operationalising-modern-mathematical-epidemiology.html</guid><pubDate>Wed, 08 Mar 2017 10:36:43 GMT</pubDate></item></channel></rss>