epiLikelihoods.tex 20 KB
Newer Older
Chris Jewell's avatar
Chris Jewell committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
\documentclass[xcolor=dvipsnames]{beamer}
\usepackage{graphicx}
\usepackage{pgf}
\usepackage{multicol}
\usepackage{bm}
\usepackage{amsmath,amssymb}
\usepackage{float, fancybox}
\usepackage{listings}
\usepackage{tikz}
\usetikzlibrary{positioning,calc,fit,arrows,backgrounds}
\usepackage[english]{babel}
\usepackage{blkarray}
\usepackage{listings}

\definecolor{LancsRed}{RGB}{139,14,29}
\mode<presentation>{
%   \usecolortheme{orchid}
   \usetheme{Darmstadt}
   \setbeamercolor*{block title}{use=structure,fg=white,bg=LancsRed}
   \setbeamercolor*{palette primary}{use=structure,fg=white,bg=LancsRed}
   \setbeamercolor*{palette secondary}{use=structure,fg=white,bg=gray}
   \setbeamercovered{transparent}
}

\setbeamerfont{parent A}{size=\huge}
\setbeamerfont{mathsmall}{size=\small}
\setbeamerfont{tiny}{size=\tiny}
\setbeamerfont{normaltext}{size=\normalsize}
\pgfdeclareimage[height=0.6cm]{clogo}{clogo.png}
\pgfdeclareimage[height=2.3cm]{fmd}{fmd1}
\logo{\pgfuseimage{clogo}}


\definecolor{mygreen}{rgb}{0,0.6,0}
\definecolor{mygray}{rgb}{0.5,0.5,0.5}
\definecolor{mylightgray}{rgb}{0.9,0.9,0.9}
\definecolor{mymauve}{rgb}{0.58,0,0.82}

\lstset{ 
  backgroundcolor=\color{mylightgray},   % choose the background color; you must add \usepackage{color} or \usepackage{xcolor}; should come as last argument
  basicstyle=\tiny,        % the size of the fonts that are used for the code
  breakatwhitespace=false,         % sets if automatic breaks should only happen at whitespace
  breaklines=true,                 % sets automatic line breaking
  captionpos=b,                    % sets the caption-position to bottom
  commentstyle=\color{mygreen},    % comment style
  deletekeywords={...},            % if you want to delete keywords from the given language
  escapeinside={\%*}{*)},          % if you want to add LaTeX within your code
  extendedchars=true,              % lets you use non-ASCII characters; for 8-bits encodings only, does not work with UTF-8
  keepspaces=true,                 % keeps spaces in text, useful for keeping indentation of code (possibly needs columns=flexible)
  keywordstyle=\color{blue},       % keyword style
  language=Octave,                 % the language of the code
  morekeywords={*,...},            % if you want to add more keywords to the set
  numbers=left,                    % where to put the line-numbers; possible values are (none, left, right)
  numbersep=5pt,                   % how far the line-numbers are from the code
  numberstyle=\tiny\color{mygray}, % the style that is used for the line-numbers
  rulecolor=\color{black},         % if not set, the frame-color may be changed on line-breaks within not-black text (e.g. comments (green here))
  showspaces=false,                % show spaces everywhere adding particular underscores; it overrides 'showstringspaces'
  showstringspaces=false,          % underline spaces within strings only
  showtabs=false,                  % show tabs within strings adding particular underscores
  stepnumber=2,                    % the step between two line-numbers. If it's 1, each line will be numbered
  stringstyle=\color{mymauve},     % string literal style
  tabsize=2,	                   % sets default tabsize to 2 spaces
  title=\lstname                   % show the filename of files included with \lstinputlisting; also try caption instead of title
}

\title{Approaches to calculating epidemic likelihoods}
\author{Chris Jewell \\ \texttt{c.jewell@lancaster.ac.uk}}
\institute{ \includegraphics[height=1.2cm]{clogo}}
\date{\small \url{http://fhm-chicas-code.lancs.ac.uk/jewell/epilikelihoods}}
\begin{document}

\begin{frame}
  \titlepage
\end{frame}

\begin{frame}
\frametitle{Overview}

\begin{enumerate}
\item Approach to epidemic likelihoods in \textcolor{blue}{heterogeneous} populations
\vspace{12pt}
\item How best to represent a likelihood
\vspace{12pt}
\item Practical approach to software implementation
\end{enumerate}

\end{frame}

\section{Motivation}

\begin{frame}
  \frametitle{Example: Foot and mouth disease 2001}
  
  \begin{columns}
  \begin{column}{2.5in}
  Data:
  \begin{itemize}
	\item Population: 188361 farms
	  \begin{itemize}
	  \item Coordinates (point)
	  \item Numbers of cattle, pigs, sheep
	  \end{itemize}
	\item Total infected: 2026 farms
	  \begin{itemize}
	   \item \textcolor{purple}{Notification} and \textcolor{ForestGreen}{Cull} dates
	  \end{itemize}
	\item Total culled: 10611 farms
  \end{itemize}
  \vspace{24pt}
  Task:
  \begin{itemize}
    \item DA-MCMC
	\item Real-time analysis \textcolor{red}{during} outbreak
	\item Overnight timeframe (12h max)
  \end{itemize}
  \end{column}
  \begin{column}{2in}
  \includegraphics[height=7cm]{fmd2001map.pdf}
  \end{column}
  \end{columns}
\end{frame}


\section[Model]{Model preliminaries}

\begin{frame}
  \frametitle{Conceptual model}

  \begin{itemize}
  \item A model is a \textcolor{red}{conceptualisation} of reality
  \item For infectious diseases, we \textcolor{red}{assume} individuals exist in different \textcolor{red}{epidemiological states}
  \end{itemize}
  
  \begin{block}{State Transition Model}
  \begin{figure}[H]
  \centering
  \begin{tikzpicture}[
  auto,
  node distance=36pt,
  box/.style={
  draw=black,
  align=center}]
  \node[box] (A) {A};
  \node[box, right=of A] (B) {B};
  \node[box, right=of B] (C) {C};
  \node[box, right=of C] (D) {D};
  
  \draw[->, very thick] (A) to node (AB) {$\alpha$} (B);
  \draw[->, very thick] (B) to node (BC) {$\epsilon$} (C);
  \draw[->, very thick] (C) to node (CD) {$\rho$} (D);
\end{tikzpicture}
\end{figure}
\end{block}

\begin{itemize}
\item Individuals \textcolor{blue}{transition} between states at rates $\alpha$, $\epsilon$, and $\rho$
\item Rates are determined by the system
  \begin{itemize}
  \item e.g. Michaelis-Menten dynamics: $$E + S \overset{k_f}{\underset{k_r}{\rightleftharpoons}} ES \overset{k_{cat}}{\longrightarrow} E + P$$
  \end{itemize}
\end{itemize}
  
\end{frame}


\begin{frame}
\frametitle{The SIR model}
\framesubtitle{Kermack and McKendrick (1927) \emph{Proc. Roy. Soc. Lond. A} \textbf{115}:700-721.}

  \begin{itemize}
  \item Consider a \textcolor{red}{closed} population
  \item Divide population into:
    \begin{itemize}
    \item \textcolor{Green}{Susceptible}
      \begin{itemize}
      \item Can be infected
      \end{itemize}
    \item \textcolor{Red}{Infected}
      \begin{itemize}
      \item Is infected
	  \end{itemize}
    \item \textcolor{Gray}{Removed}
      \begin{itemize}
      \item Recovered with immunity or dead
      \end{itemize}
    \end{itemize}
  \end{itemize}
  
\end{frame}


\begin{frame}
\frametitle{SIR transitions}

\begin{itemize}
  \item \textbf{Assume} individuals travel between states:
    \begin{itemize}
      \item \textcolor{ForestGreen}{Susceptible} $\rightarrow$ \textcolor{Red}{Infected}
      \item \textcolor{Red}{Infected} $\rightarrow$ \textcolor{Gray}{Removed}
    \end{itemize}
\end{itemize}

\begin{figure}[H]
\centering
\input{figSIRModel0}
\end{figure}

\begin{itemize}
\item $S, I, R$ numbers of susceptible, infected, removed individuals
\item N.B. \textcolor{blue}{Closed} population: no migration, birth, or (natural) death.
\end{itemize}

\end{frame}

\begin{frame}
  \frametitle{SIR force of infection}
  \framesubtitle{Homogeneous mixing}

  \begin{itemize}
  \item Modelling depends on knowing \textcolor{blue}{transition rates} along the arrows
  \item \textbf{Assume} each \textcolor{Red}{infected} infects \textcolor{ForestGreen}{susceptible} with rate $\beta$ (homogeneous mixing)
  \end{itemize}

\begin{columns}
\begin{column}{1.75in}
  \begin{figure}[H]
  \input{figPressure}
  \end{figure}
\end{column}
\begin{column}{3in}
\begin{itemize}
\item S$\rightarrow$I at rate $\beta I$
  \begin{itemize}
  \item ``\textcolor{red}{Force of infection}'' on \emph{single} individual
  \end{itemize}
\vspace{12pt}
\item $S$ and $I$ evolve with time
  \begin{itemize}
  \item $S(t) \rightarrow I(t+\delta)$ at rate $\beta I(t)$
  \end{itemize}
\vspace{12pt}
\item \textcolor{blue}{Total} force of infection $\beta I(t) S(t)$
\end{itemize}
\end{column}
\end{columns}
\end{frame}

\begin{frame}
\frametitle{SIR transitions}
\framesubtitle{Force of infection}

\begin{itemize}
\item Infection process is a \textcolor{blue}{feedback} loop:
\end{itemize}

\begin{figure}[H]
\centering
\input{figSIRModel1}
\end{figure}

\begin{itemize}
\item ``Removal'' process is \textcolor{blue}{independent} of the state of the system
\item Once infected, individuals are \textcolor{gray}{removed} at rate $\gamma$
\end{itemize}

\end{frame}


\begin{frame}
\frametitle{Assumptions}

So far, we have made a few \textbf{assumptions}:
\begin{itemize}
\item \textcolor{ForestGreen}{S} $\rightarrow$ \textcolor{Red}{I} $\rightarrow$ \textcolor{Gray}{R}
\item \textcolor{blue}{Homogeneous} mixing
\item Infection rates are \textcolor{blue}{additive}
\item Infection rate \textcolor{blue}{scales with population size}
\end{itemize}

\begin{itemize}
\item Assumptions help to simplify
  \begin{itemize}
  \item \textcolor{Red}{Be sure to state all assumptions}!
  \end{itemize}
\item Always \textcolor{blue}{query} assumptions
  \begin{itemize}
  \item Are they appropriate?
  \item Strong vs. weak?
  \item Supported by evidence?
  \end{itemize}
\end{itemize}

\end{frame}




\begin{frame}
\frametitle{Epidemic models vs. survival analysis}
\framesubtitle{Assume Exponential \textcolor{blue}{survival times}}

Consider a transition from $S \rightarrow I$:
\begin{itemize}
\item At each time $t$, individuals $j=1,\dots,N$ experience \textcolor{blue}{hazard rate}
$$\lambda_j(t) = \sum_{i \in \mathcal{I}(t)} \beta = \beta |\mathcal{I}(t)|.$$
\item Let $t^{SI}_j$ be the time that $j$ moves from $\mathcal{S}$ to $\mathcal{I}$
\begin{block}{}
\begin{eqnarray*}
Pr(t^{SI}_j = s | \mathcal{H}_s) & = & \lambda_j(s^-) \exp^{-\int_{I_0}^{s} \lambda_j(t) \mathrm{d}t} \\
Pr(t^{SI}_j > s | \mathcal{H}_s) & = & \exp^{-\int_{I_0}^{s} \lambda_j(t) \mathrm{d}t}
\end{eqnarray*}
\end{block}
\end{itemize}

\end{frame}
  

\section[Heterogeneity]{Heterogeneous mixing matrices}

\begin{frame}
\frametitle{Adding population heterogeneity}
\framesubtitle{Contact networks}

\begin{itemize}
\item In reality, individuals are connected by \textcolor{blue}{network}s.
\item<2-> Consider a population of 5 people, \textcolor{red}{binary contact network}
\item<2-> Represent connectivity between individuals $i$ (rows) and $j$ (cols):
$$
C = \left( \begin{array}{ccccc}
0 & 1 & 0 & 1 & 1\\
1 & 0 & 1 & 0 & 1\\
0 & 1 & 0 & 1 & 0\\
1 & 0 & 1 & 0 & 0 \\
1 & 1 & 0 & 0 & 0\end{array} \right)
$$
\vspace{12pt}
\item<3-> \textbf{Assumption}: $j$ can infect $i$ \emph{if and only if} $c_{ij}=1$
\end{itemize}
\end{frame}



\begin{frame}
\frametitle{Heterogeneous force of infection}
\framesubtitle{Network model}

\begin{columns}
\begin{column}{2in}
\begin{equation*}
C = 
\begin{blockarray}{cccccc}
\textcolor{Red}{a} & \textcolor{ForestGreen}{b} & \textcolor{Red}{c} & \textcolor{Red}{d} & \textcolor{ForestGreen}{e} \\
\begin{block}{(ccccc)c}
0 & 1 & 0 & 1 & 1 & \textcolor{Red}{a} \\
\textcolor{blue}{1} & 0 & \textcolor{blue}{1} & \textcolor{blue}{0} & 1 & \textcolor{ForestGreen}{b}\\
0 & 1 & 0 & 1 & 0 & \textcolor{Red}{c}\\
1 & 0 & 1 & 0 & 0 & \textcolor{Red}{d}\\
1 & 1 & 0 & 0 & 0 & \textcolor{ForestGreen}{e}\\
\end{block}
\end{blockarray}
\end{equation*}
\end{column}

\begin{column}{2in}
\begin{figure}
\centering
\begin{tikzpicture}[
auto,
node distance=24pt,
circ/.style={
circle,
draw=black,
align=center,
minimum height=24pt,
minimum width=24pt}]
\node[circ, fill=ForestGreen] (S) {$b$};
\node[circ, fill=Red, above right=of S] (I1) {$a$};
\node[circ,fill=Red, above left=of S] (I2) {$c$};
\node[circ,fill=Red, below left=of S] (I3) {$d$};
\node[circ,fill=ForestGreen, below right=of S] (I4) {$e$};
\draw[->,very thick] (I1) to node (I1S) {$\beta_{ba}$} (S);
\draw[->,very thick] (I2) to node (I2S) {$\beta_{bc}$} (S);
\draw[->,very thick,draw=Gray] (I4) to node (I4S) {\textcolor{Gray}{$\beta_{be}$}} (S);
\end{tikzpicture}
\end{figure}
\end{column}
\end{columns}
\vspace{12pt}
\begin{itemize}
\item $j$ infects $i$ \emph{if and only if} $c_{ij}=1$ \textcolor{red}{and} $j$ is infected
\vspace{12pt}
\item<2-> At time $t$, \textcolor{blue}{force of infection} on individual \textcolor{ForestGreen}{$b$}:
$$
\lambda_{b}(t) = \beta \left( c_{ba} + c_{bc} \right)
$$
\end{itemize}

\end{frame}


\begin{frame}
\frametitle{Individual-level model}
\framesubtitle{Network model}

\begin{itemize}
\item We can now calculate $\lambda_i(t)$ for each member $i$ of $\mathcal{S}$:
$$\lambda_j(t) = \beta \sum{i \in \mathcal{I}(t)} c_{ij}$$
\item Likelihood takes a product over \emph{all} individuals in the population $\mathcal{P}$:
\begin{eqnarray*}
L(\bm{t}^{SI} | \beta) & = & \prod_{j \in \mathcal{P}}  \left[ \lambda_j(s^-) e^{-\int_{I_0}^{s} \lambda_j(t) \mathrm{d}t} \right]^{t^{SI}_j < T_{max}} \left[ e^{-\int_{I_0}^{s} \lambda_j(t) \mathrm{d}t} \right]^{t^{SI}_j \geq T_{max}} \\
& = & \prod_{j: t^{SI}_j < T_{max}} \left[ \lambda_j(t^{SI-}_j) \right] e^{-\sum_{j \in \mathcal{P}} \int_{I_0}^{s} \lambda_j(t) \mathrm{d}t}
\end{eqnarray*}
$$\ell(\bm{t}^{SI} | \beta) = \sum_{j: t^{SI}_j < T_{max}} \left[ \log \lambda_j(t^{SI-}_j) \right] - \sum_{j \in \mathcal{P}} \int_{I_0}^{s} \lambda_j(t) \mathrm{d}t$$
\end{itemize}
\end{frame}



\section[Computation]{A practical approach to computing likelihoods}

\begin{frame}
\frametitle{Computing likelihoods}

\begin{itemize}
\item Ingredients
  \begin{itemize}
  \item Transition rate equation $\lambda_j(t) = \dots$
  \item Time-inhomogenous Poisson process likelihood function $L(\bm{t}^{SI} | \beta)$
  \end{itemize}
\vspace{12pt}
\item \textcolor{blue}{Inference} requires efficient computation!
\vspace{12pt}
\item Efficient development:
  \begin{itemize}
  \item \textcolor{blue}{DRY}: ``Do not repeat yourself''
  \item \textcolor{blue}{Declarative}: \textcolor{red}{Describe what you want}, not how to get it
  \item \textcolor{blue}{Clean code}: avoid bugs
  \end{itemize}
\item Runtime speed:
  \begin{itemize}
  \item Optimise \textcolor{blue}{only} when you need to!
  \end{itemize}
\end{itemize}
\end{frame}



\begin{frame}[fragile]
\frametitle{A recipe for calculating epidemic (log) likelihoods}
\framesubtitle{Infectious pressure matrix}

Let $B$ be an infectious pressure ``\textcolor{blue}{kernel}'' matrix with $ij$th element:
$$b_{ij} = \beta c_{ij}$$

Encapsulate data in a closure (R/Python) or class (Python):
\begin{columns}
\begin{column}{0.5\textwidth}
\begin{lstlisting}[language=R]
# R
Kernel = function(c_matrix) {
      function(beta) {
          beta * c_matrix
      }
}


kernel = Kernel(c)
B = kernel(beta)
\end{lstlisting}
\end{column}

\begin{column}{0.5\textwidth}
\begin{lstlisting}[language=Python]
# Python
class Kernel:
    def __init__(self, c_matrix):
        self.c_matrix = c_matrix
        
    def __call__(self, beta):
        return beta * c
        
        
kernel = Kernel(c)
B = kernel(beta)
\end{lstlisting}
\end{column}
\end{columns}
\textbf{Exercise}
write a closure/class to represent a distance kernel, where $B_{ij} = \beta e^{-||x_i - x_j|| / \phi}$
\end{frame}


\begin{frame}
\frametitle{A recipe for calculating epidemic (log) likelihoods}
\framesubtitle{Product part}

\begin{itemize}
\item Let $A = \sum_{j: t^{inf}_j < T_{max}} \left[ \log \lambda_j(t^{inf-}_j) \right]$
\end{itemize}

\begin{enumerate}
\item Calculate ``who acquired infection from whom'' (WAIFW) matrix
$$W:\;w_{ij} = t^{inf}_i < t^{inf}_j < t^{rem}_i$$
\item Multiply by pressure matrix: $\bm{\lambda}(\bm{t}) = (B \odot W) \cdot \mathbf{1}$
\item Log and sum: $A = \log(\bm{\lambda}(\bm{t}))^T \cdot \mathbf{1}$ (omit I0!)
\end{enumerate}

\textbf{Exercise}: the above algorithm is inefficient.  How can we improve it?

\end{frame}

\begin{frame}[fragile]
\frametitle{A recipe for calculating epidemic (log) likelihoods}
\framesubtitle{Product part -- code}

\begin{lstlisting}[language=R]
# R
prod_part = function(t_inf_j, events, B) {
    waifw = sapply(t_inf_j, function(t) events[,1] < t & events[,2])
    lambdaj = colSums(B * waifw)
    I0 = which.min(t_inf_j)
    sum(log(lambdaj[-I0]))
}
 \end{lstlisting}


 \begin{lstlisting}[language=Python]
 # Python
 import numpy as np
 def prod_part(t_inf_j, events, B):
    infec, remove = events[:,0], events[:,1]
    waifw = (infec[:,None] < t_inf_j[None,:]) & (t_inf_j[None,:] < remove[:,None])
    lambdaj = np.sum(waifw * B, axis=0)
    I0 = np.argmin(t_inf_j)
    return np.sum(np.log(np.delete(lambdaj,I0)))
\end{lstlisting}

\end{frame}



\begin{frame}
\frametitle{A recipe for calculating epidemic (log) likelihoods}
\framesubtitle{Integral part}

\begin{itemize}
\item NB. \textcolor{blue}{Pair-wise} infection pressure constant between $\mathcal{S}\mathcal{I}$ dyads
$$ \sum_{j \in \mathcal{P}} \int_{I_0}^{s} \lambda_j(t) \mathrm{d}t = \sum_{j \in \mathcal{P}} \sum_{i \in \mathcal{I}(\cdot)} b_{ij}[R_i \wedge I_j - I_j \wedge I_i]$$
\item Let $e_{ij} = R_i \wedge I_j - I_j \wedge I_i$
    \begin{itemize}
    \item $e_{ij}$ represents $\mbox{sojourn}_i \cap \mbox{sojourn}_j$
    \item i.e. the interval $j \in \mathcal{S}$ is exposed to $i \in \mathcal{I}$ for all $i,j$ pairs
    \end{itemize}
\vspace{12pt}
\item \textcolor{blue}{In general}, for intervals $(a_0, a_1)$ and $(b_0, b_1)$
$$ (a_0, a_1) \cap (b_0, b_1) = 0 \vee (a_1 \wedge b_1 - a_0 \vee b_0)$$
\item for the $\mathcal{S}$ class, there is no lower bound.
\end{itemize}

\end{frame}


\begin{frame}[fragile]
\frametitle{A recipe for calculating epidemic (log) likelihoods}
\framesubtitle{Exposure matrix}

Let $E: e_{ij}\; \forall\; i,j$ be the \textcolor{blue}{exposure} matrix.

\begin{lstlisting}[language=R]
# R
interval_intersect = function(interval_i, interval_j) {
    int_start = sapply(interval_j[,1], function(x) pmax(x, interval_i[,1]))
    int_end = sapply(interval_j[,2], function(x) pmin(x, interval_i[,2]))
    pmax(int_end - int_start, 0)
}
\end{lstlisting}

\begin{lstlisting}[language=Python]
# Python
def interval_intersect(interval_i, interval_j):
    int_start = np.maximum(interval_i[0][:,None], interval_j[0][None,:])
    int_end = np.minimum(interval_i[0][:,None], interval_j[0][None,:])
    return np.maximum(0., int_end - int_start)
\end{lstlisting}
\end{frame}


\begin{frame}[fragile]
\frametitle{A recipe for calculating epidemic (log) likelihoods}
\framesubtitle{Integrated infection pressure}

All we have to do to calculate the integral is multiply relevant rows of $B$ and $E$, and sum up the remaining matrix

\begin{lstlisting}[language=R]
# R
integral_part = function(t_inf_j, events, B) {
	i_infected = events[,1] < Inf # Infection time finite
	E = interval_intersect(events[i_infected,], cbind(0, t_inf_j))
	integral = E * B[i_infected,]
	sum(integral)
}
\end{lstlisting}

\begin{lstlisting}[language=Python]
# Python
def integral_part(t_inf_j, events, B):
    i_infected = events[:,1] < np.inf
    E = interval_intersect((events[:,0],events[:,1]), (0., t_inf_j))
    integral = E * B[i_infected,:]
    return np.sum(integral)
\end{lstlisting}
\end{frame}


\begin{frame}[fragile]
\frametitle{A recipe for calculating epidemic (log) likelihoods}
\framesubtitle{Full calculation}

...and compose our \texttt{Kernel}, \texttt{prod\_part} and \texttt{integral\_part} functions

\begin{lstlisting}[language=R]
# R
log_likelihood = function(par, t_inf, t_rem, kernel)
    B = kernel(par)
    prod = prod_part (t_inf, cbind(t_inf, t_rem), B)
    integral = integral_part(t_inf, cbind(t_inf, t_rem), B)
    prod - integral
}
\end{lstlisting}

\begin{lstlisting}[language=Python]
# Python
def log_likelihood(par, t_inf, t_rem, kernel):
    B = kernel(par)
    prod = prod_part(t_inf, (t_inf, t_rem), B)
    integral = integral_part(t_inf, (t_inf, t_rem), B)
    return prod - integral
\end{lstlisting}
\end{frame}


\begin{frame}
\frametitle{A recipe for calculating epidemic (log) likelihoods}
\framesubtitle{Summary}

\begin{itemize}
\item A clear, concise recipe for calculating an SI epidemic likelihood!
\item Not optimal -- build for conciseness and clarity
  \begin{itemize}
  \item Exercise to optimise runtime further
  \item \textcolor{red}{But only when you need it}!
  \end{itemize}
\vspace{12pt}
\item Measure your code in terms of \textcolor{blue}{lines not written}
  \begin{itemize}
  \item 28 lines of R
  \item x lines of Python
  \end{itemize}
\end{itemize}

\end{frame}


\end{document}