Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
E
epiLikelihoods
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Chris Jewell
epiLikelihoods
Commits
ef66ac79
Commit
ef66ac79
authored
Jan 16, 2019
by
Chris Jewell
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fixed code bugs, and some typos in the slides.
parent
227e93cf
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
29 additions
and
32 deletions
+29
-32
R/inhomPPLik.R
R/inhomPPLik.R
+5
-4
epiLikelihoods.pdf
epiLikelihoods.pdf
+0
-0
epiLikelihoods.tex
epiLikelihoods.tex
+19
-21
python/inhomPPLik.py
python/inhomPPLik.py
+5
-7
No files found.
R/inhomPPLik.R
View file @
ef66ac79
...
...
@@ -52,9 +52,10 @@ Kernel = function(c_matrix) {
#' @param B -- the full transmission matrix
#' @return scalar product part of the epidemic likelihoood
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
)
is_infected
=
t_inf_j
<
Inf
waifw
=
sapply
(
t_inf_j
,
function
(
t
)
events
[,
1
]
<
t
&
t
<
events
[,
2
])
lambdaj
=
colSums
(
B
[,
is_infected
]
*
waifw
[,
is_infected
])
I0
=
which.min
(
t_inf_j
[
is_infected
])
print
(
waifw
)
sum
(
log
(
lambdaj
[
-
I0
]))
}
...
...
@@ -79,7 +80,7 @@ interval_intersect = function(interval_i, interval_j) {
#' @param B -- the full transmission matrix
#' @return -- scalar integrated infection pressure for the epidemic
integral_part
=
function
(
t_inf_j
,
events
,
B
)
{
i_infected
=
events
[,
1
]
<
Inf
# Infection time finite
i_infected
=
events
[,
1
]
<
Inf
E
=
interval_intersect
(
events
[
i_infected
,],
cbind
(
0
,
t_inf_j
))
integral
=
E
*
B
[
i_infected
,]
sum
(
integral
)
...
...
epiLikelihoods.pdf
0 → 100644
View file @
ef66ac79
File added
epiLikelihoods.tex
View file @
ef66ac79
...
...
@@ -404,7 +404,7 @@ $$
\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
}$$
$$
\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
}}
\\
...
...
@@ -516,25 +516,23 @@ $$W:\;w_{ij} = t^{inf}_i < t^{inf}_j < t^{rem}_i$$
\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)
is
_
infected = t
_
inf
_
j < Inf
waifw = sapply(t
_
inf
_
j, function(t) events[,1] < t
&
t < events[,2])
lambdaj = colSums(B[,is
_
infected] * waifw[,is
_
infected])
I0 = which.min(t
_
inf
_
j[is
_
infected])
sum(log(lambdaj[-I0]))
}
\end{lstlisting}
}
\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)))
is
_
infected = t
_
inf
_
j < np.inf
waifw = (events[0][:, None] < t
_
inf
_
j[None, :])
&
(t
_
inf
_
j[None, :] < events[1][:, None])
lambdaj = np.sum(np.multiply(B[:, is
_
infected], waifw[:, is
_
infected]), axis=0)
I0 = np.argmin(t
_
inf
_
j[is
_
infected])
return np.sum(np.log(np.delete(lambdaj, I0)))
\end{lstlisting}
\end{frame}
...
...
@@ -594,18 +592,18 @@ All we have to do to calculate the integral is multiply relevant rows of $B$ and
\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)
i
_
infected = events[,1] < Inf
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))
i
_
infected = events[
0
] < np.inf
E = interval
_
intersect((events[
0][i
_
infected],events[1][i
_
infected]), (np.zeros
_
like(t
_
inf
_
j)
, t
_
inf
_
j))
integral = E * B[i
_
infected,:]
return np.sum(integral)
\end{lstlisting}
...
...
@@ -653,8 +651,8 @@ def log_likelihood(par, t_inf, t_rem, kernel):
\vspace
{
12pt
}
\item
Measure your code in terms of
\textcolor
{
blue
}{
lines not written
}
\begin{itemize}
\item
2
8
lines of R
\item
x
lines of Python
\item
2
9
lines of R
\item
25
lines of Python
\end{itemize}
\end{itemize}
...
...
python/inhomPPLik.py
View file @
ef66ac79
...
...
@@ -41,10 +41,10 @@ def prod_part(t_inf_j, events, B):
:param B: the full transmission matrix
:return: scalar product part of the epidemic likelihoood
"""
is_infected
=
t_inf_j
<
np
.
inf
waifw
=
(
events
[
0
][:,
None
]
<
t_inf_j
[
None
,
:])
&
(
t_inf_j
[
None
,
:]
<
events
[
1
][:,
None
])
lambdaj
=
np
.
sum
(
waifw
*
B
,
axis
=
0
)
I0
=
np
.
argmin
(
t_inf_j
)
print
(
waifw
)
lambdaj
=
np
.
sum
(
np
.
multiply
(
B
[:,
is_infected
],
waifw
[:,
is_infected
]),
axis
=
0
)
I0
=
np
.
argmin
(
t_inf_j
[
is_infected
])
return
np
.
sum
(
np
.
log
(
np
.
delete
(
lambdaj
,
I0
)))
...
...
@@ -69,8 +69,8 @@ def integral_part(t_inf_j, events, B):
:return: the integrated infection pressure for the SI epidemic"""
i_infected
=
events
[
0
]
<
np
.
inf
E
=
interval_intersect
((
events
[
0
][
i_infected
],
events
[
1
][
i_infected
]),
(
np
.
zeros_like
(
t_inf_j
),
t_inf_j
))
integral
=
np
.
multiply
(
E
,
B
[
i_infected
,:])
E
=
interval_intersect
((
events
[
0
][
i_infected
],
events
[
1
][
i_infected
]),
(
np
.
zeros_like
(
t_inf_j
),
t_inf_j
))
integral
=
np
.
multiply
(
E
,
B
[
i_infected
,
:])
return
np
.
sum
(
integral
)
...
...
@@ -85,9 +85,7 @@ def log_likelihood(par, t_inf, t_rem, kernel):
:"""
B
=
kernel
(
par
)
prod
=
prod_part
(
t_inf
,
(
t_inf
,
t_rem
),
B
)
print
(
"prod = "
,
prod
)
integral
=
integral_part
(
t_inf
,
(
t_inf
,
t_rem
),
B
)
print
(
'integral = '
,
integral
)
return
prod
-
integral
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment