diff options
| -rw-r--r-- | finale/bayes.tex | 107 | ||||
| -rw-r--r-- | finale/def.tex | 97 | ||||
| -rw-r--r-- | finale/mid_report.tex | 52 | ||||
| -rw-r--r-- | finale/mle.tex | 163 | ||||
| -rw-r--r-- | simulation/main.py | 28 |
5 files changed, 418 insertions, 29 deletions
diff --git a/finale/bayes.tex b/finale/bayes.tex new file mode 100644 index 0000000..82cb732 --- /dev/null +++ b/finale/bayes.tex @@ -0,0 +1,107 @@ +\begin{Verbatim}[commandchars=\\\{\}] +\PY{k+kn}{import} \PY{n+nn}{pymc} +\PY{k+kn}{import} \PY{n+nn}{numpy} \PY{k+kn}{as} \PY{n+nn}{np} + +\PY{k}{def} \PY{n+nf}{glm\PYZus{}node\PYZus{}setup}\PY{p}{(}\PY{n}{cascade}\PY{p}{,} \PY{n}{y\PYZus{}obs}\PY{p}{,} \PY{n}{prior}\PY{o}{=}\PY{n+nb+bp}{None}\PY{p}{,} \PY{o}{*}\PY{n}{args}\PY{p}{,} \PY{o}{*}\PY{o}{*}\PY{n}{kwargs}\PY{p}{)}\PY{p}{:} + \PY{l+s+sd}{\PYZdq{}\PYZdq{}\PYZdq{}} +\PY{l+s+sd}{ Build an IC PyMC node\PYZhy{}level model from:} +\PY{l+s+sd}{ \PYZhy{}observed cascades: cascade} +\PY{l+s+sd}{ \PYZhy{}outcome vector: y\PYZus{}obs} +\PY{l+s+sd}{ \PYZhy{}desired PyMC prior and parameters: prior, *args} +\PY{l+s+sd}{ Note: we use the glm formulation: y = Bernoulli[f(x.dot(theta))]} +\PY{l+s+sd}{ \PYZdq{}\PYZdq{}\PYZdq{}} + \PY{n}{n\PYZus{}nodes} \PY{o}{=} \PY{n+nb}{len}\PY{p}{(}\PY{n}{cascade}\PY{p}{[}\PY{l+m+mi}{0}\PY{p}{]}\PY{p}{)} + + \PY{c}{\PYZsh{} Container class for node\PYZsq{}s parents} + \PY{n}{theta} \PY{o}{=} \PY{n}{np}\PY{o}{.}\PY{n}{empty}\PY{p}{(}\PY{n}{n\PYZus{}nodes}\PY{p}{,} \PY{n}{dtype}\PY{o}{=}\PY{n+nb}{object}\PY{p}{)} + \PY{k}{for} \PY{n}{j} \PY{o+ow}{in} \PY{n+nb}{xrange}\PY{p}{(}\PY{n}{n\PYZus{}nodes}\PY{p}{)}\PY{p}{:} + \PY{k}{if} \PY{n}{prior} \PY{o+ow}{is} \PY{n+nb+bp}{None}\PY{p}{:} + \PY{n}{theta}\PY{p}{[}\PY{n}{j}\PY{p}{]} \PY{o}{=} \PY{n}{pymc}\PY{o}{.}\PY{n}{Beta}\PY{p}{(}\PY{l+s}{\PYZsq{}}\PY{l+s}{theta\PYZus{}\PYZob{}\PYZcb{}}\PY{l+s}{\PYZsq{}}\PY{o}{.}\PY{n}{format}\PY{p}{(}\PY{n}{j}\PY{p}{)}\PY{p}{,} \PY{n}{alpha}\PY{o}{=}\PY{l+m+mi}{1}\PY{p}{,} \PY{n}{beta}\PY{o}{=}\PY{l+m+mi}{1}\PY{p}{)} + \PY{k}{else}\PY{p}{:} + \PY{n}{theta}\PY{p}{[}\PY{n}{j}\PY{p}{]} \PY{o}{=} \PY{n}{prior}\PY{p}{(}\PY{l+s}{\PYZsq{}}\PY{l+s}{theta\PYZus{}\PYZob{}\PYZcb{}}\PY{l+s}{\PYZsq{}}\PY{o}{.}\PY{n}{format}\PY{p}{(}\PY{n}{j}\PY{p}{)}\PY{p}{,} \PY{o}{*}\PY{n}{args}\PY{p}{,} \PY{o}{*}\PY{o}{*}\PY{n}{kwargs}\PY{p}{)} + + \PY{c}{\PYZsh{} Observed container class for cascade realization} + \PY{n}{x} \PY{o}{=} \PY{n}{np}\PY{o}{.}\PY{n}{empty}\PY{p}{(}\PY{n}{n\PYZus{}nodes}\PY{p}{,} \PY{n}{dtype}\PY{o}{=}\PY{n+nb}{object}\PY{p}{)} + \PY{k}{for} \PY{n}{i}\PY{p}{,} \PY{n}{val} \PY{o+ow}{in} \PY{n+nb}{enumerate}\PY{p}{(}\PY{n}{cascade}\PY{o}{.}\PY{n}{T}\PY{p}{)}\PY{p}{:} + \PY{n}{x}\PY{p}{[}\PY{n}{i}\PY{p}{]} \PY{o}{=} \PY{n}{pymc}\PY{o}{.}\PY{n}{Normal}\PY{p}{(}\PY{l+s}{\PYZsq{}}\PY{l+s}{x\PYZus{}\PYZob{}\PYZcb{}}\PY{l+s}{\PYZsq{}}\PY{o}{.}\PY{n}{format}\PY{p}{(}\PY{n}{i}\PY{p}{)}\PY{p}{,} \PY{l+m+mi}{0}\PY{p}{,} \PY{l+m+mi}{1}\PY{p}{,} \PY{n}{value}\PY{o}{=}\PY{n}{val}\PY{p}{,} \PY{n}{observed}\PY{o}{=}\PY{n+nb+bp}{True}\PY{p}{)} + + \PY{n+nd}{@pymc.deterministic} + \PY{k}{def} \PY{n+nf}{glm\PYZus{}p}\PY{p}{(}\PY{n}{x}\PY{o}{=}\PY{n}{x}\PY{p}{,} \PY{n}{theta}\PY{o}{=}\PY{n}{theta}\PY{p}{)}\PY{p}{:} + \PY{k}{return} \PY{l+m+mf}{1.} \PY{o}{\PYZhy{}} \PY{n}{np}\PY{o}{.}\PY{n}{exp}\PY{p}{(}\PY{o}{\PYZhy{}}\PY{n}{x}\PY{o}{.}\PY{n}{dot}\PY{p}{(}\PY{n}{theta}\PY{p}{)}\PY{p}{)} + + \PY{n+nd}{@pymc.observed} + \PY{k}{def} \PY{n+nf}{y}\PY{p}{(}\PY{n}{glm\PYZus{}p}\PY{o}{=}\PY{n}{glm\PYZus{}p}\PY{p}{,} \PY{n}{value}\PY{o}{=}\PY{n}{y\PYZus{}obs}\PY{p}{)}\PY{p}{:} + \PY{k}{return} \PY{n}{pymc}\PY{o}{.}\PY{n}{bernoulli\PYZus{}like}\PY{p}{(}\PY{n}{value}\PY{p}{,} \PY{n}{glm\PYZus{}p}\PY{p}{)} + + \PY{k}{return} \PY{n}{pymc}\PY{o}{.}\PY{n}{Model}\PY{p}{(}\PY{p}{[}\PY{n}{y}\PY{p}{,} \PY{n}{pymc}\PY{o}{.}\PY{n}{Container}\PY{p}{(}\PY{n}{theta}\PY{p}{)}\PY{p}{,} \PY{n}{pymc}\PY{o}{.}\PY{n}{Container}\PY{p}{(}\PY{n}{x}\PY{p}{)}\PY{p}{]}\PY{p}{)} + + +\PY{k}{def} \PY{n+nf}{mc\PYZus{}graph\PYZus{}setup}\PY{p}{(}\PY{n}{infected}\PY{p}{,} \PY{n}{susceptible}\PY{p}{,} \PY{n}{prior}\PY{o}{=}\PY{n+nb+bp}{None}\PY{p}{,} \PY{o}{*}\PY{n}{args}\PY{p}{,} \PY{o}{*}\PY{o}{*}\PY{n}{kwargs}\PY{p}{)}\PY{p}{:} + \PY{l+s+sd}{\PYZdq{}\PYZdq{}\PYZdq{}} +\PY{l+s+sd}{ Build an IC PyMC graph\PYZhy{}level model from:} +\PY{l+s+sd}{ \PYZhy{}infected nodes over time: list/tuple of list/tuple of np.array} +\PY{l+s+sd}{ \PYZhy{}susceptible nodes over time: same format as above} +\PY{l+s+sd}{ Note: we use the Markov Chain formulation: X\PYZus{}\PYZob{}t+1\PYZcb{}|X\PYZus{}t,theta = f(X\PYZus{}t.theta)} +\PY{l+s+sd}{ \PYZdq{}\PYZdq{}\PYZdq{}} + \PY{c}{\PYZsh{} Container class for graph parameters} + \PY{n}{n\PYZus{}nodes} \PY{o}{=} \PY{n+nb}{len}\PY{p}{(}\PY{n}{infected}\PY{p}{[}\PY{l+m+mi}{0}\PY{p}{]}\PY{p}{[}\PY{l+m+mi}{0}\PY{p}{]}\PY{p}{)} + \PY{n}{theta} \PY{o}{=} \PY{n}{np}\PY{o}{.}\PY{n}{empty}\PY{p}{(}\PY{p}{(}\PY{n}{n\PYZus{}nodes}\PY{p}{,}\PY{n}{n\PYZus{}nodes}\PY{p}{)}\PY{p}{,} \PY{n}{dtype}\PY{o}{=}\PY{n+nb}{object}\PY{p}{)} + \PY{k}{for} \PY{n}{i} \PY{o+ow}{in} \PY{n+nb}{xrange}\PY{p}{(}\PY{n}{n\PYZus{}nodes}\PY{p}{)}\PY{p}{:} + \PY{k}{for} \PY{n}{j} \PY{o+ow}{in} \PY{n+nb}{xrange}\PY{p}{(}\PY{n}{n\PYZus{}nodes}\PY{p}{)}\PY{p}{:} + \PY{k}{if} \PY{n}{prior} \PY{o+ow}{is} \PY{n+nb+bp}{None}\PY{p}{:} + \PY{n}{theta}\PY{p}{[}\PY{n}{i}\PY{p}{,} \PY{n}{j}\PY{p}{]} \PY{o}{=} \PY{n}{pymc}\PY{o}{.}\PY{n}{Beta}\PY{p}{(}\PY{l+s}{\PYZsq{}}\PY{l+s}{theta\PYZus{}\PYZob{}\PYZcb{}\PYZob{}\PYZcb{}}\PY{l+s}{\PYZsq{}}\PY{o}{.}\PY{n}{format}\PY{p}{(}\PY{n}{i}\PY{p}{,} \PY{n}{j}\PY{p}{)}\PY{p}{,} \PY{n}{alpha}\PY{o}{=}\PY{l+m+mi}{1}\PY{p}{,} + \PY{n}{beta}\PY{o}{=}\PY{l+m+mi}{1}\PY{p}{)} + \PY{k}{else}\PY{p}{:} + \PY{n}{theta}\PY{p}{[}\PY{n}{i}\PY{p}{,} \PY{n}{j}\PY{p}{]} \PY{o}{=} \PY{n}{prior}\PY{p}{(}\PY{l+s}{\PYZsq{}}\PY{l+s}{theta\PYZus{}\PYZob{}\PYZcb{}\PYZob{}\PYZcb{}}\PY{l+s}{\PYZsq{}}\PY{o}{.}\PY{n}{format}\PY{p}{(}\PY{n}{i}\PY{p}{,} \PY{n}{j}\PY{p}{)}\PY{p}{,} \PY{o}{*}\PY{n}{args}\PY{p}{,} \PY{o}{*}\PY{o}{*}\PY{n}{kwargs}\PY{p}{)} + + \PY{c}{\PYZsh{} Container class for cascade realization} + \PY{n}{x} \PY{o}{=} \PY{p}{\PYZob{}}\PY{p}{\PYZcb{}} + \PY{k}{for} \PY{n}{i}\PY{p}{,} \PY{n}{cascade} \PY{o+ow}{in} \PY{n+nb}{enumerate}\PY{p}{(}\PY{n}{infected}\PY{p}{)}\PY{p}{:} + \PY{k}{for} \PY{n}{j}\PY{p}{,} \PY{n}{step} \PY{o+ow}{in} \PY{n+nb}{enumerate}\PY{p}{(}\PY{n}{cascade}\PY{p}{)}\PY{p}{:} + \PY{k}{for} \PY{n}{k}\PY{p}{,} \PY{n}{node} \PY{o+ow}{in} \PY{n+nb}{enumerate}\PY{p}{(}\PY{n}{step}\PY{p}{)}\PY{p}{:} + \PY{k}{if} \PY{n}{j} \PY{o+ow}{and} \PY{n}{susceptible}\PY{p}{[}\PY{n}{i}\PY{p}{]}\PY{p}{[}\PY{n}{j}\PY{p}{]}\PY{p}{[}\PY{n}{k}\PY{p}{]}\PY{p}{:} + \PY{n}{p} \PY{o}{=} \PY{l+m+mf}{1.} \PY{o}{\PYZhy{}} \PY{n}{pymc}\PY{o}{.}\PY{n}{exp}\PY{p}{(}\PY{o}{\PYZhy{}}\PY{n}{cascade}\PY{p}{[}\PY{n}{j}\PY{o}{\PYZhy{}}\PY{l+m+mi}{1}\PY{p}{]}\PY{o}{.}\PY{n}{dot}\PY{p}{(}\PY{n}{theta}\PY{p}{[}\PY{n}{k}\PY{p}{]}\PY{p}{)}\PY{p}{)} + \PY{k}{else}\PY{p}{:} + \PY{n}{p} \PY{o}{=} \PY{o}{.}\PY{l+m+mi}{5} + \PY{n}{x}\PY{p}{[}\PY{n}{i}\PY{p}{,} \PY{n}{j}\PY{p}{,} \PY{n}{k}\PY{p}{]} \PY{o}{=} \PY{n}{pymc}\PY{o}{.}\PY{n}{Bernoulli}\PY{p}{(}\PY{l+s}{\PYZsq{}}\PY{l+s}{x\PYZus{}\PYZob{}\PYZcb{}\PYZob{}\PYZcb{}\PYZob{}\PYZcb{}}\PY{l+s}{\PYZsq{}}\PY{o}{.}\PY{n}{format}\PY{p}{(}\PY{n}{i}\PY{p}{,} \PY{n}{j}\PY{p}{,} \PY{n}{k}\PY{p}{)}\PY{p}{,} \PY{n}{p}\PY{o}{=}\PY{n}{p}\PY{p}{,} + \PY{n}{value}\PY{o}{=}\PY{n}{node}\PY{p}{,} \PY{n}{observed}\PY{o}{=}\PY{n+nb+bp}{True}\PY{p}{)} + + \PY{k}{return} \PY{n}{pymc}\PY{o}{.}\PY{n}{Model}\PY{p}{(}\PY{p}{[}\PY{n}{pymc}\PY{o}{.}\PY{n}{Container}\PY{p}{(}\PY{n}{theta}\PY{p}{)}\PY{p}{,} \PY{n}{pymc}\PY{o}{.}\PY{n}{Container}\PY{p}{(}\PY{n}{x}\PY{p}{)}\PY{p}{]}\PY{p}{)} + + +\PY{k}{if} \PY{n}{\PYZus{}\PYZus{}name\PYZus{}\PYZus{}}\PY{o}{==}\PY{l+s}{\PYZdq{}}\PY{l+s}{\PYZus{}\PYZus{}main\PYZus{}\PYZus{}}\PY{l+s}{\PYZdq{}}\PY{p}{:} + \PY{k+kn}{import} \PY{n+nn}{main} + \PY{k+kn}{import} \PY{n+nn}{matplotlib.pyplot} \PY{k+kn}{as} \PY{n+nn}{plt} + \PY{k+kn}{import} \PY{n+nn}{seaborn} + \PY{n}{seaborn}\PY{o}{.}\PY{n}{set\PYZus{}style}\PY{p}{(}\PY{l+s}{\PYZsq{}}\PY{l+s}{whitegrid}\PY{l+s}{\PYZsq{}}\PY{p}{)} + \PY{n}{g} \PY{o}{=} \PY{n}{np}\PY{o}{.}\PY{n}{array}\PY{p}{(}\PY{p}{[}\PY{p}{[}\PY{l+m+mi}{0}\PY{p}{,} \PY{l+m+mi}{1}\PY{p}{,} \PY{l+m+mi}{1}\PY{p}{,} \PY{l+m+mi}{0}\PY{p}{]}\PY{p}{,} \PY{p}{[}\PY{l+m+mi}{1}\PY{p}{,} \PY{l+m+mi}{0}\PY{p}{,} \PY{l+m+mi}{0}\PY{p}{,} \PY{l+m+mi}{1}\PY{p}{]}\PY{p}{,} \PY{p}{[}\PY{l+m+mi}{1}\PY{p}{,} \PY{l+m+mi}{0}\PY{p}{,} \PY{l+m+mi}{0}\PY{p}{,} \PY{l+m+mi}{1}\PY{p}{]}\PY{p}{,} \PY{p}{[}\PY{l+m+mi}{0}\PY{p}{,} \PY{l+m+mi}{1}\PY{p}{,} \PY{l+m+mi}{1}\PY{p}{,} \PY{l+m+mi}{0}\PY{p}{]}\PY{p}{]}\PY{p}{)} + \PY{n}{p} \PY{o}{=} \PY{l+m+mf}{0.5} + \PY{n}{g} \PY{o}{=} \PY{n}{np}\PY{o}{.}\PY{n}{log}\PY{p}{(}\PY{l+m+mf}{1.} \PY{o}{/} \PY{p}{(}\PY{l+m+mi}{1} \PY{o}{\PYZhy{}} \PY{n}{p} \PY{o}{*} \PY{n}{g}\PY{p}{)}\PY{p}{)} + + \PY{k}{print}\PY{p}{(}\PY{l+s}{\PYZsq{}}\PY{l+s}{running the graph\PYZhy{}level MC set\PYZhy{}up}\PY{l+s}{\PYZsq{}}\PY{p}{)} + \PY{n}{cascades} \PY{o}{=} \PY{n}{main}\PY{o}{.}\PY{n}{simulate\PYZus{}cascades}\PY{p}{(}\PY{l+m+mi}{1000}\PY{p}{,} \PY{n}{g}\PY{p}{)} + \PY{n}{infected}\PY{p}{,} \PY{n}{susc} \PY{o}{=} \PY{n}{main}\PY{o}{.}\PY{n}{build\PYZus{}cascade\PYZus{}list}\PY{p}{(}\PY{n}{cascades}\PY{p}{)} + \PY{n}{model} \PY{o}{=} \PY{n}{mc\PYZus{}graph\PYZus{}setup}\PY{p}{(}\PY{n}{infected}\PY{p}{,} \PY{n}{susc}\PY{p}{)} + \PY{n}{mcmc} \PY{o}{=} \PY{n}{pymc}\PY{o}{.}\PY{n}{MCMC}\PY{p}{(}\PY{n}{model}\PY{p}{)} + \PY{n}{mcmc}\PY{o}{.}\PY{n}{sample}\PY{p}{(}\PY{l+m+mi}{10}\PY{o}{*}\PY{o}{*}\PY{l+m+mi}{4}\PY{p}{,} \PY{l+m+mi}{1000}\PY{p}{)} + \PY{n}{fig}\PY{p}{,} \PY{n}{ax} \PY{o}{=} \PY{n}{plt}\PY{o}{.}\PY{n}{subplots}\PY{p}{(}\PY{n+nb}{len}\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PY{p}{)} + \PY{n}{ax}\PY{p}{[}\PY{n}{i}\PY{p}{,} \PY{n}{j}\PY{p}{]}\PY{o}{.}\PY{n}{plot}\PY{p}{(}\PY{p}{[}\PY{n}{g}\PY{p}{[}\PY{n}{i}\PY{p}{,} \PY{n}{j}\PY{p}{]}\PY{p}{]}\PY{o}{*}\PY{l+m+mi}{2}\PY{p}{,} \PY{p}{[}\PY{l+m+mi}{0}\PY{p}{,} \PY{n}{ax}\PY{p}{[}\PY{n}{i}\PY{p}{,}\PY{n}{j}\PY{p}{]}\PY{o}{.}\PY{n}{get\PYZus{}ylim}\PY{p}{(}\PY{p}{)}\PY{p}{[}\PY{o}{\PYZhy{}}\PY{l+m+mi}{1}\PY{p}{]}\PY{p}{]}\PY{p}{,} \PY{n}{color}\PY{o}{=}\PY{l+s}{\PYZsq{}}\PY{l+s}{red}\PY{l+s}{\PYZsq{}}\PY{p}{)} + \PY{n}{plt}\PY{o}{.}\PY{n}{tight\PYZus{}layout}\PY{p}{(}\PY{n}{pad}\PY{o}{=}\PY{l+m+mf}{0.4}\PY{p}{,} \PY{n}{w\PYZus{}pad}\PY{o}{=}\PY{l+m+mf}{0.5}\PY{p}{,} \PY{n}{h\PYZus{}pad}\PY{o}{=}\PY{o}{.}\PY{l+m+mi}{1}\PY{p}{)} + \PY{n}{plt}\PY{o}{.}\PY{n}{show}\PY{p}{(}\PY{p}{)} + + + \PY{k}{print}\PY{p}{(}\PY{l+s}{\PYZsq{}}\PY{l+s}{running the node level set\PYZhy{}up}\PY{l+s}{\PYZsq{}}\PY{p}{)} + \PY{n}{node} \PY{o}{=} \PY{l+m+mi}{0} + \PY{n}{cascades} \PY{o}{=} \PY{n}{main}\PY{o}{.}\PY{n}{simulate\PYZus{}cascades}\PY{p}{(}\PY{l+m+mi}{100}\PY{p}{,} \PY{n}{g}\PY{p}{)} + \PY{n}{cascade}\PY{p}{,} \PY{n}{y\PYZus{}obs} \PY{o}{=} \PY{n}{main}\PY{o}{.}\PY{n}{build\PYZus{}matrix}\PY{p}{(}\PY{n}{cascades}\PY{p}{,} \PY{n}{node}\PY{p}{)} + \PY{n}{model} \PY{o}{=} \PY{n}{glm\PYZus{}node\PYZus{}setup}\PY{p}{(}\PY{n}{cascade}\PY{p}{,} \PY{n}{y\PYZus{}obs}\PY{p}{)} + \PY{n}{mcmc} \PY{o}{=} \PY{n}{pymc}\PY{o}{.}\PY{n}{MCMC}\PY{p}{(}\PY{n}{model}\PY{p}{)} + \PY{n}{mcmc}\PY{o}{.}\PY{n}{sample}\PY{p}{(}\PY{l+m+mf}{1e5}\PY{p}{,} \PY{l+m+mf}{1e4}\PY{p}{)} + \PY{n}{plt}\PY{o}{.}\PY{n}{hist}\PY{p}{(}\PY{n}{mcmc}\PY{o}{.}\PY{n}{trace}\PY{p}{(}\PY{l+s}{\PYZsq{}}\PY{l+s}{theta\PYZus{}1}\PY{l+s}{\PYZsq{}}\PY{p}{)}\PY{p}{[}\PY{p}{:}\PY{p}{]}\PY{p}{,} \PY{n}{bins}\PY{o}{=}\PY{l+m+mf}{1e2}\PY{p}{)} + \PY{n}{plt}\PY{o}{.}\PY{n}{show}\PY{p}{(}\PY{p}{)} +\end{Verbatim} diff --git a/finale/def.tex b/finale/def.tex new file mode 100644 index 0000000..7262705 --- /dev/null +++ b/finale/def.tex @@ -0,0 +1,97 @@ +\usepackage{fancyvrb} +\usepackage{color} +\usepackage[UTF-8]{inputenc} + +\makeatletter +\def\PY@reset{\let\PY@it=\relax \let\PY@bf=\relax% + \let\PY@ul=\relax \let\PY@tc=\relax% + \let\PY@bc=\relax \let\PY@ff=\relax} +\def\PY@tok#1{\csname PY@tok@#1\endcsname} +\def\PY@toks#1+{\ifx\relax#1\empty\else% + \PY@tok{#1}\expandafter\PY@toks\fi} +\def\PY@do#1{\PY@bc{\PY@tc{\PY@ul{% + \PY@it{\PY@bf{\PY@ff{#1}}}}}}} +\def\PY#1#2{\PY@reset\PY@toks#1+\relax+\PY@do{#2}} + +\expandafter\def\csname PY@tok@nt\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}} +\expandafter\def\csname PY@tok@gr\endcsname{\def\PY@tc##1{\textcolor[rgb]{1.00,0.00,0.00}{##1}}} +\expandafter\def\csname PY@tok@se\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.73,0.40,0.13}{##1}}} +\expandafter\def\csname PY@tok@gh\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.00,0.50}{##1}}} +\expandafter\def\csname PY@tok@ow\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.67,0.13,1.00}{##1}}} +\expandafter\def\csname PY@tok@kd\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}} +\expandafter\def\csname PY@tok@il\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}} +\expandafter\def\csname PY@tok@mf\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}} +\expandafter\def\csname PY@tok@sx\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}} +\expandafter\def\csname PY@tok@gp\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.00,0.50}{##1}}} +\expandafter\def\csname PY@tok@nb\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}} +\expandafter\def\csname PY@tok@go\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.53,0.53,0.53}{##1}}} +\expandafter\def\csname PY@tok@w\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.73,0.73,0.73}{##1}}} +\expandafter\def\csname PY@tok@vg\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.10,0.09,0.49}{##1}}} +\expandafter\def\csname PY@tok@gs\endcsname{\let\PY@bf=\textbf} +\expandafter\def\csname PY@tok@mo\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}} +\expandafter\def\csname PY@tok@na\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.49,0.56,0.16}{##1}}} +\expandafter\def\csname PY@tok@sr\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.73,0.40,0.53}{##1}}} +\expandafter\def\csname PY@tok@ni\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.60,0.60,0.60}{##1}}} +\expandafter\def\csname PY@tok@m\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}} +\expandafter\def\csname PY@tok@err\endcsname{\def\PY@bc##1{\setlength{\fboxsep}{0pt}\fcolorbox[rgb]{1.00,0.00,0.00}{1,1,1}{\strut ##1}}} +\expandafter\def\csname PY@tok@kc\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}} +\expandafter\def\csname PY@tok@k\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}} +\expandafter\def\csname PY@tok@gu\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.50,0.00,0.50}{##1}}} +\expandafter\def\csname PY@tok@nf\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.00,0.00,1.00}{##1}}} +\expandafter\def\csname PY@tok@si\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.73,0.40,0.53}{##1}}} +\expandafter\def\csname PY@tok@mi\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}} +\expandafter\def\csname PY@tok@mb\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}} +\expandafter\def\csname PY@tok@ss\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.10,0.09,0.49}{##1}}} +\expandafter\def\csname PY@tok@cm\endcsname{\let\PY@it=\textit\def\PY@tc##1{\textcolor[rgb]{0.25,0.50,0.50}{##1}}} +\expandafter\def\csname PY@tok@gt\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.00,0.27,0.87}{##1}}} +\expandafter\def\csname PY@tok@c1\endcsname{\let\PY@it=\textit\def\PY@tc##1{\textcolor[rgb]{0.25,0.50,0.50}{##1}}} +\expandafter\def\csname PY@tok@no\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.53,0.00,0.00}{##1}}} +\expandafter\def\csname PY@tok@bp\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}} +\expandafter\def\csname PY@tok@ne\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.82,0.25,0.23}{##1}}} +\expandafter\def\csname PY@tok@nc\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.00,1.00}{##1}}} +\expandafter\def\csname PY@tok@kp\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}} +\expandafter\def\csname PY@tok@gd\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.63,0.00,0.00}{##1}}} +\expandafter\def\csname PY@tok@sc\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}} +\expandafter\def\csname PY@tok@ge\endcsname{\let\PY@it=\textit} +\expandafter\def\csname PY@tok@c\endcsname{\let\PY@it=\textit\def\PY@tc##1{\textcolor[rgb]{0.25,0.50,0.50}{##1}}} +\expandafter\def\csname PY@tok@o\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}} +\expandafter\def\csname PY@tok@vc\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.10,0.09,0.49}{##1}}} +\expandafter\def\csname PY@tok@gi\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.00,0.63,0.00}{##1}}} +\expandafter\def\csname PY@tok@nn\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.00,1.00}{##1}}} +\expandafter\def\csname PY@tok@sh\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}} +\expandafter\def\csname PY@tok@cp\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.74,0.48,0.00}{##1}}} +\expandafter\def\csname PY@tok@cs\endcsname{\let\PY@it=\textit\def\PY@tc##1{\textcolor[rgb]{0.25,0.50,0.50}{##1}}} +\expandafter\def\csname PY@tok@nd\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.67,0.13,1.00}{##1}}} +\expandafter\def\csname PY@tok@kr\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}} +\expandafter\def\csname PY@tok@sd\endcsname{\let\PY@it=\textit\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}} +\expandafter\def\csname PY@tok@kt\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.69,0.00,0.25}{##1}}} +\expandafter\def\csname PY@tok@nl\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.63,0.63,0.00}{##1}}} +\expandafter\def\csname PY@tok@vi\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.10,0.09,0.49}{##1}}} +\expandafter\def\csname PY@tok@mh\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.40,0.40,0.40}{##1}}} +\expandafter\def\csname PY@tok@kn\endcsname{\let\PY@bf=\textbf\def\PY@tc##1{\textcolor[rgb]{0.00,0.50,0.00}{##1}}} +\expandafter\def\csname PY@tok@s\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}} +\expandafter\def\csname PY@tok@sb\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}} +\expandafter\def\csname PY@tok@nv\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.10,0.09,0.49}{##1}}} +\expandafter\def\csname PY@tok@s2\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}} +\expandafter\def\csname PY@tok@s1\endcsname{\def\PY@tc##1{\textcolor[rgb]{0.73,0.13,0.13}{##1}}} + +\def\PYZbs{\char`\\} +\def\PYZus{\char`\_} +\def\PYZob{\char`\{} +\def\PYZcb{\char`\}} +\def\PYZca{\char`\^} +\def\PYZam{\char`\&} +\def\PYZlt{\char`\<} +\def\PYZgt{\char`\>} +\def\PYZsh{\char`\#} +\def\PYZpc{\char`\%} +\def\PYZdl{\char`\$} +\def\PYZhy{\char`\-} +\def\PYZsq{\char`\'} +\def\PYZdq{\char`\"} +\def\PYZti{\char`\~} +% for compatibility with earlier versions +\def\PYZat{@} +\def\PYZlb{[} +\def\PYZrb{]} +\makeatother diff --git a/finale/mid_report.tex b/finale/mid_report.tex index bd7f475..4abcd47 100644 --- a/finale/mid_report.tex +++ b/finale/mid_report.tex @@ -8,8 +8,9 @@ \usepackage{graphicx, subfig} \usepackage{bbm} \usepackage{fullpage} - +\input{def} \DeclareMathOperator*{\argmax}{arg\,max} +\DeclareMathOperator*{\argmin}{arg\,min} \DeclareMathOperator{\E}{\mathbb{E}} \let\P\relax \DeclareMathOperator{\P}{\mathbb{P}} @@ -457,42 +458,47 @@ with $\binom{4}{2}=6$ parameters to learn with the MCMC package PyMC\@. We plot in Figure~\ref{betapriorbayeslearning} the progressive learning of $\Theta$ for increasing numbers of observations. Of note, since the GLMC model does not include self-loops, the diagonal terms are never properly learned, which is -expected but not undesirable. We notice that the existence or not of an edge is -(relatively) quickly learned, with the posterior on edges with no weight -converging to $0$ after $100$ cascades. To get a concentrated posterior around -the true non-zero edge weigth requires $1000$ cascades, which is unreasonably -high considering the small number of parameters that we are learning in this -experiment. +expected but not undesirable. We notice that the absence of an edge is +(relatively) quickly learned: the posterior on edges with no weight converges +to $0$ after $100$ cascades. For existing edges though, the posterior visually +concentrates only after $1000$ cascades, which is unreasonably high considering +the small number of parameters that we are learning in this experiment. \subsection{Active Learning} -Finally, we propose an Active Learning formulation of Network Inference +Finally, we propose an Active Learning formulation of the Network Inference problem. In this setup, the source $x^0$ is no longer drawn from a random distribution $p_s$ but a single source $i$ is chosen by the designer of the experiment. Once a source is drawn, a cascade is drawn from the Markovian -process, $\mathcal{D'}$. We wish to choose the source which maximises the -information gain on the parameter $\Theta$, so as to speed up learning. We -suggest to choose the source which maximises the mutual information between -$\theta$ and $X$ at each time step. The mutual information can be computed -node-by-node by sampling: +process $\mathcal{D'}$ described in \eqref{eq:markov}. + +Our suggested selection stratgey is to select at each time step the source is +to select the node which maximises the information gain (mutual information) on +the parameter $\Theta$ resulting from observing the cascade $\bx$. The mutual +information can be computed node-by-node by sampling: \begin{equation*} -I((x_t)_{t\geq 1} ,\Theta | x_0 = i) = \mathbb{E}_{\substack{\Theta \sim D_t \\ +I(\bx ,\Theta | x^0 = i) = \mathbb{E}_{\substack{\Theta \sim D_t \\ x | \Theta, i \sim D'}}\log p(x|\Theta) - \mathbb{E}_{\substack{\Theta \sim D_t \\ x' | \Theta, i \sim D'}} \log p(x') \end{equation*} -The second term involves marginalizing over $\Theta$: $p(x') = -\mathbb{E}_{\Theta \sim D_t} p(x'| \Theta)$. +where $D_t$ denotes the posterior distribution on $\Theta$ after the first $t$ +cascades. Not that the second term involves marginalizing over $\Theta$: +$p(x') = \mathbb{E}_{\Theta \sim D_t} p(x'| \Theta)$. The full specification of +our Active Learning procedure is described in Algorithm 1. For the final +version of this project, we plan to test experimentally the learning speedup +induced by using Active Learning compared to independent observations from +a random source. \begin{algorithm} \caption{Active Bayesian Learning through Cascades (ABC)} \begin{algorithmic}[1] -\State $\theta \sim \mathcal{D}_0 = \mathcal{D}$ \Comment{Initial prior on -$\theta$} +\State $\Theta \sim \mathcal{D}_0 = \mathcal{D}$ \Comment{Initial prior on +$\Theta$} \While{True} -\State $i \leftarrow \arg\min_{i} I(\theta, (x_t)_{t \geq 1} | x_0 = i)$ +\State $i \leftarrow \argmin_{i} I(\Theta, \bx | x_0 = i)$ \Comment{Maximize mutual information} \State Observe $(x_t)_{t\geq1} |x_0 = i$ \Comment{Observe cascade from source} -\State $\mathcal{D}_{t+1} \gets \text{posterior of $\theta \sim \mathcal{D}_t$ +\State $\mathcal{D}_{t+1} \gets \text{posterior of $\Theta \sim \mathcal{D}_t$ given $(x_t)_{t\geq1}$}$ \Comment{Update posterior on $\theta$} \EndWhile \end{algorithmic} @@ -500,5 +506,9 @@ given $(x_t)_{t\geq1}$}$ \Comment{Update posterior on $\theta$} \bibliography{sparse}{} \bibliographystyle{abbrv} - +\appendix +\section{\textsf{mle.py}} +\input{mle} +\section{\textsf{bayes.py}} +\input{bayes} \end{document} diff --git a/finale/mle.tex b/finale/mle.tex new file mode 100644 index 0000000..70caa3e --- /dev/null +++ b/finale/mle.tex @@ -0,0 +1,163 @@ +\begin{Verbatim}[commandchars=\\\{\}] +\PY{k+kn}{import} \PY{n+nn}{numpy} \PY{k+kn}{as} \PY{n+nn}{np} +\PY{k+kn}{from} \PY{n+nn}{numpy.linalg} \PY{k+kn}{import} \PY{n}{norm} +\PY{k+kn}{import} \PY{n+nn}{numpy.random} \PY{k+kn}{as} \PY{n+nn}{nr} +\PY{k+kn}{from} \PY{n+nn}{scipy.optimize} \PY{k+kn}{import} \PY{n}{minimize} +\PY{k+kn}{import} \PY{n+nn}{matplotlib.pyplot} \PY{k+kn}{as} \PY{n+nn}{plt} +\PY{k+kn}{import} \PY{n+nn}{seaborn} +\PY{k+kn}{from} \PY{n+nn}{random} \PY{k+kn}{import} \PY{n}{random}\PY{p}{,} \PY{n}{randint} + +\PY{n}{seaborn}\PY{o}{.}\PY{n}{set\PYZus{}style}\PY{p}{(}\PY{l+s}{\PYZdq{}}\PY{l+s}{white}\PY{l+s}{\PYZdq{}}\PY{p}{)} + + +\PY{k}{def} \PY{n+nf}{likelihood}\PY{p}{(}\PY{n}{p}\PY{p}{,} \PY{n}{x}\PY{p}{,} \PY{n}{y}\PY{p}{)}\PY{p}{:} + \PY{n}{a} \PY{o}{=} \PY{n}{np}\PY{o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def} \PY{n+nf}{f}\PY{p}{(}\PY{n}{p}\PY{p}{)}\PY{p}{:} + \PY{n}{l}\PY{p}{,} \PY{n}{g} \PY{o}{=} \PY{n}{likelihood\PYZus{}gradient}\PY{p}{(}\PY{n}{p}\PY{p}{,} \PY{n}{x}\PY{p}{,} \PY{n}{y}\PY{p}{)} + \PY{k}{return} \PY{o}{\PYZhy{}}\PY{n}{l}\PY{p}{,} \PY{o}{\PYZhy{}}\PY{n}{g} + \PY{n}{x0} \PY{o}{=} \PY{n}{np}\PY{o}{.}\PY{n}{ones}\PY{p}{(}\PY{n}{x}\PY{o}{.}\PY{n}{shape}\PY{p}{[}\PY{l+m+mi}{1}\PY{p}{]}\PY{p}{)} + \PY{n}{bounds} \PY{o}{=} \PY{p}{[}\PY{p}{(}\PY{l+m+mf}{1e\PYZhy{}10}\PY{p}{,} \PY{n+nb+bp}{None}\PY{p}{)}\PY{p}{]} \PY{o}{*} \PY{n}{x}\PY{o}{.}\PY{n}{shape}\PY{p}{[}\PY{l+m+mi}{1}\PY{p}{]} + \PY{k}{return} \PY{n}{minimize}\PY{p}{(}\PY{n}{f}\PY{p}{,} \PY{n}{x0}\PY{p}{,} \PY{n}{jac}\PY{o}{=}\PY{n+nb+bp}{True}\PY{p}{,} \PY{n}{bounds}\PY{o}{=}\PY{n}{bounds}\PY{p}{,} \PY{n}{method}\PY{o}{=}\PY{l+s}{\PYZdq{}}\PY{l+s}{L\PYZhy{}BFGS\PYZhy{}B}\PY{l+s}{\PYZdq{}}\PY{p}{)}\PY{o}{.}\PY{n}{x} + + +\PY{k}{def} \PY{n+nf}{bootstrap}\PY{p}{(}\PY{n}{x}\PY{p}{,} \PY{n}{y}\PY{p}{,} \PY{n}{n\PYZus{}iter}\PY{o}{=}\PY{l+m+mi}{100}\PY{p}{)}\PY{p}{:} + \PY{n}{rval} \PY{o}{=} \PY{n}{np}\PY{o}{.}\PY{n}{zeros}\PY{p}{(}\PY{p}{(}\PY{n}{n\PYZus{}iter}\PY{p}{,} \PY{n}{x}\PY{o}{.}\PY{n}{shape}\PY{p}{[}\PY{l+m+mi}{1}\PY{p}{]}\PY{p}{)}\PY{p}{)} + \PY{k}{for} \PY{n}{i} \PY{o+ow}{in} \PY{n+nb}{xrange}\PY{p}{(}\PY{n}{n\PYZus{}iter}\PY{p}{)}\PY{p}{:} + \PY{n}{indices} \PY{o}{=} \PY{n}{np}\PY{o}{.}\PY{n}{random}\PY{o}{.}\PY{n}{choice}\PY{p}{(}\PY{n+nb}{len}\PY{p}{(}\PY{n}{y}\PY{p}{)}\PY{p}{,} \PY{n}{replace}\PY{o}{=}\PY{n+nb+bp}{False}\PY{p}{,} \PY{n}{size}\PY{o}{=}\PY{n+nb}{int}\PY{p}{(}\PY{n+nb}{len}\PY{p}{(}\PY{n}{y}\PY{p}{)}\PY{o}{*}\PY{o}{.}\PY{l+m+mi}{9}\PY{p}{)}\PY{p}{)} + \PY{n}{rval}\PY{p}{[}\PY{n}{i}\PY{p}{]} \PY{o}{=} \PY{n}{infer}\PY{p}{(}\PY{n}{x}\PY{p}{[}\PY{n}{indices}\PY{p}{]}\PY{p}{,} \PY{n}{y}\PY{p}{[}\PY{n}{indices}\PY{p}{]}\PY{p}{)} + \PY{k}{return} \PY{n}{rval} + + +\PY{k}{def} \PY{n+nf}{confidence\PYZus{}interval}\PY{p}{(}\PY{n}{counts}\PY{p}{,} \PY{n}{bins}\PY{p}{)}\PY{p}{:} + \PY{n}{k} \PY{o}{=} \PY{l+m+mi}{0} + \PY{k}{while} \PY{n}{np}\PY{o}{.}\PY{n}{sum}\PY{p}{(}\PY{n}{counts}\PY{p}{[}\PY{n+nb}{len}\PY{p}{(}\PY{n}{counts}\PY{p}{)}\PY{o}{/}\PY{l+m+mi}{2}\PY{o}{\PYZhy{}}\PY{n}{k}\PY{p}{:}\PY{n+nb}{len}\PY{p}{(}\PY{n}{counts}\PY{p}{)}\PY{o}{/}\PY{l+m+mi}{2}\PY{o}{+}\PY{n}{k}\PY{p}{]}\PY{p}{)} \PY{o}{\PYZlt{}}\PY{o}{=} \PY{o}{.}\PY{l+m+mi}{95}\PY{o}{*}\PY{n}{np}\PY{o}{.}\PY{n}{sum}\PY{p}{(}\PY{n}{counts}\PY{p}{)}\PY{p}{:} + \PY{n}{k} \PY{o}{+}\PY{o}{=} \PY{l+m+mi}{1} + \PY{k}{return} \PY{n}{bins}\PY{p}{[}\PY{n+nb}{len}\PY{p}{(}\PY{n}{bins}\PY{p}{)}\PY{o}{/}\PY{l+m+mi}{2}\PY{o}{\PYZhy{}}\PY{n}{k}\PY{p}{]}\PY{p}{,} \PY{n}{bins}\PY{p}{[}\PY{n+nb}{len}\PY{p}{(}\PY{n}{bins}\PY{p}{)}\PY{o}{/}\PY{l+m+mi}{2}\PY{o}{+}\PY{n}{k}\PY{p}{]} + + +\PY{k}{def} \PY{n+nf}{build\PYZus{}matrix}\PY{p}{(}\PY{n}{cascades}\PY{p}{,} \PY{n}{node}\PY{p}{)}\PY{p}{:} + + \PY{k}{def} \PY{n+nf}{aux}\PY{p}{(}\PY{n}{cascade}\PY{p}{,} \PY{n}{node}\PY{p}{)}\PY{p}{:} + \PY{n}{xlist}\PY{p}{,} \PY{n}{slist} \PY{o}{=} \PY{n+nb}{zip}\PY{p}{(}\PY{o}{*}\PY{n}{cascade}\PY{p}{)} + \PY{n}{indices} \PY{o}{=} \PY{p}{[}\PY{n}{i} \PY{k}{for} \PY{n}{i}\PY{p}{,} \PY{n}{s} \PY{o+ow}{in} \PY{n+nb}{enumerate}\PY{p}{(}\PY{n}{slist}\PY{p}{)} \PY{k}{if} \PY{n}{s}\PY{p}{[}\PY{n}{node}\PY{p}{]} \PY{o+ow}{and} \PY{n}{i} \PY{o}{\PYZgt{}}\PY{o}{=} \PY{l+m+mi}{1}\PY{p}{]} + \PY{k}{if} \PY{n}{indices}\PY{p}{:} + \PY{n}{x} \PY{o}{=} \PY{n}{np}\PY{o}{.}\PY{n}{vstack}\PY{p}{(}\PY{n}{xlist}\PY{p}{[}\PY{n}{i}\PY{o}{\PYZhy{}}\PY{l+m+mi}{1}\PY{p}{]} \PY{k}{for} \PY{n}{i} \PY{o+ow}{in} \PY{n}{indices}\PY{p}{)} + \PY{n}{y} \PY{o}{=} \PY{n}{np}\PY{o}{.}\PY{n}{array}\PY{p}{(}\PY{p}{[}\PY{n}{xlist}\PY{p}{[}\PY{n}{i}\PY{p}{]}\PY{p}{[}\PY{n}{node}\PY{p}{]} \PY{k}{for} \PY{n}{i} \PY{o+ow}{in} \PY{n}{indices}\PY{p}{]}\PY{p}{)} + \PY{k}{return} \PY{n}{x}\PY{p}{,} \PY{n}{y} + \PY{k}{else}\PY{p}{:} + \PY{k}{return} \PY{n+nb+bp}{None} + + \PY{n}{pairs} \PY{o}{=} \PY{p}{(}\PY{n}{aux}\PY{p}{(}\PY{n}{cascade}\PY{p}{,} \PY{n}{node}\PY{p}{)} \PY{k}{for} \PY{n}{cascade} \PY{o+ow}{in} \PY{n}{cascades}\PY{p}{)} + \PY{n}{xs}\PY{p}{,} \PY{n}{ys} \PY{o}{=} \PY{n+nb}{zip}\PY{p}{(}\PY{o}{*}\PY{p}{(}\PY{n}{pair} \PY{k}{for} \PY{n}{pair} \PY{o+ow}{in} \PY{n}{pairs} \PY{k}{if} \PY{n}{pair}\PY{p}{)}\PY{p}{)} + \PY{n}{x} \PY{o}{=} \PY{n}{np}\PY{o}{.}\PY{n}{vstack}\PY{p}{(}\PY{n}{xs}\PY{p}{)} + \PY{n}{y} \PY{o}{=} \PY{n}{np}\PY{o}{.}\PY{n}{concatenate}\PY{p}{(}\PY{n}{ys}\PY{p}{)} + \PY{k}{return} \PY{n}{x}\PY{p}{,} \PY{n}{y} + + +\PY{k}{def} \PY{n+nf}{build\PYZus{}cascade\PYZus{}list}\PY{p}{(}\PY{n}{cascades}\PY{p}{)}\PY{p}{:} + \PY{n}{x}\PY{p}{,} \PY{n}{s} \PY{o}{=} \PY{p}{[}\PY{p}{]}\PY{p}{,} \PY{p}{[}\PY{p}{]} + \PY{k}{for} \PY{n}{cascade} \PY{o+ow}{in} \PY{n}{cascades}\PY{p}{:} + \PY{n}{xlist}\PY{p}{,} \PY{n}{slist} \PY{o}{=} \PY{n+nb}{zip}\PY{p}{(}\PY{o}{*}\PY{n}{cascade}\PY{p}{)} + \PY{n}{x}\PY{o}{.}\PY{n}{append}\PY{p}{(}\PY{n}{xlist}\PY{p}{)} + \PY{n}{s}\PY{o}{.}\PY{n}{append}\PY{p}{(}\PY{n}{slist}\PY{p}{)} + \PY{k}{return} \PY{n}{x}\PY{p}{,} \PY{n}{s} + + +\PY{k}{def} \PY{n+nf}{simulate\PYZus{}cascade}\PY{p}{(}\PY{n}{x}\PY{p}{,} \PY{n}{graph}\PY{p}{)}\PY{p}{:} + \PY{l+s+sd}{\PYZdq{}\PYZdq{}\PYZdq{}} +\PY{l+s+sd}{ Simulate an IC cascade given a graph and initial state.} + +\PY{l+s+sd}{ For each time step we yield:} +\PY{l+s+sd}{ \PYZhy{} susc: the nodes susceptible at the beginning of this time step} +\PY{l+s+sd}{ \PYZhy{} x: the subset of susc who became infected} +\PY{l+s+sd}{ \PYZdq{}\PYZdq{}\PYZdq{}} + \PY{n}{susc} \PY{o}{=} \PY{n}{np}\PY{o}{.}\PY{n}{ones}\PY{p}{(}\PY{n}{graph}\PY{o}{.}\PY{n}{shape}\PY{p}{[}\PY{l+m+mi}{0}\PY{p}{]}\PY{p}{,} \PY{n}{dtype}\PY{o}{=}\PY{n+nb}{bool}\PY{p}{)} \PY{c}{\PYZsh{} t=0, everyone is susceptible} + \PY{k}{yield} \PY{n}{x}\PY{p}{,} \PY{n}{susc} + \PY{k}{while} \PY{n}{np}\PY{o}{.}\PY{n}{any}\PY{p}{(}\PY{n}{x}\PY{p}{)}\PY{p}{:} + \PY{n}{susc} \PY{o}{=} \PY{n}{susc} \PY{o}{\PYZca{}} \PY{n}{x} \PY{c}{\PYZsh{} nodes infected at previous step are now inactive} + \PY{k}{if} \PY{o+ow}{not} \PY{n}{np}\PY{o}{.}\PY{n}{any}\PY{p}{(}\PY{n}{susc}\PY{p}{)}\PY{p}{:} + \PY{k}{break} + \PY{n}{x} \PY{o}{=} \PY{l+m+mi}{1} \PY{o}{\PYZhy{}} \PY{n}{np}\PY{o}{.}\PY{n}{exp}\PY{p}{(}\PY{o}{\PYZhy{}}\PY{n}{np}\PY{o}{.}\PY{n}{dot}\PY{p}{(}\PY{n}{graph}\PY{o}{.}\PY{n}{T}\PY{p}{,} \PY{n}{x}\PY{p}{)}\PY{p}{)} + \PY{n}{y} \PY{o}{=} \PY{n}{nr}\PY{o}{.}\PY{n}{random}\PY{p}{(}\PY{n}{x}\PY{o}{.}\PY{n}{shape}\PY{p}{[}\PY{l+m+mi}{0}\PY{p}{]}\PY{p}{)} + \PY{n}{x} \PY{o}{=} \PY{p}{(}\PY{n}{x} \PY{o}{\PYZgt{}}\PY{o}{=} \PY{n}{y}\PY{p}{)} \PY{o}{\PYZam{}} \PY{n}{susc} + \PY{k}{yield} \PY{n}{x}\PY{p}{,} \PY{n}{susc} + + +\PY{k}{def} \PY{n+nf}{uniform\PYZus{}source}\PY{p}{(}\PY{n}{graph}\PY{p}{)}\PY{p}{:} + \PY{n}{x0} \PY{o}{=} \PY{n}{np}\PY{o}{.}\PY{n}{zeros}\PY{p}{(}\PY{n}{graph}\PY{o}{.}\PY{n}{shape}\PY{p}{[}\PY{l+m+mi}{0}\PY{p}{]}\PY{p}{,} \PY{n}{dtype}\PY{o}{=}\PY{n+nb}{bool}\PY{p}{)} + \PY{n}{x0}\PY{p}{[}\PY{n}{nr}\PY{o}{.}\PY{n}{randint}\PY{p}{(}\PY{l+m+mi}{0}\PY{p}{,} \PY{n}{graph}\PY{o}{.}\PY{n}{shape}\PY{p}{[}\PY{l+m+mi}{0}\PY{p}{]}\PY{p}{)}\PY{p}{]} \PY{o}{=} \PY{n+nb+bp}{True} + \PY{k}{return} \PY{n}{x0} + + +\PY{k}{def} \PY{n+nf}{simulate\PYZus{}cascades}\PY{p}{(}\PY{n}{n}\PY{p}{,} \PY{n}{graph}\PY{p}{,} \PY{n}{source}\PY{o}{=}\PY{n}{uniform\PYZus{}source}\PY{p}{)}\PY{p}{:} + \PY{k}{for} \PY{n}{\PYZus{}} \PY{o+ow}{in} \PY{n+nb}{xrange}\PY{p}{(}\PY{n}{n}\PY{p}{)}\PY{p}{:} + \PY{n}{x0} \PY{o}{=} \PY{n}{source}\PY{p}{(}\PY{n}{graph}\PY{p}{)} + \PY{k}{yield} \PY{n}{simulate\PYZus{}cascade}\PY{p}{(}\PY{n}{x0}\PY{p}{,} \PY{n}{graph}\PY{p}{)} + + +\PY{k}{if} \PY{n}{\PYZus{}\PYZus{}name\PYZus{}\PYZus{}} \PY{o}{==} \PY{l+s}{\PYZdq{}}\PY{l+s}{\PYZus{}\PYZus{}main\PYZus{}\PYZus{}}\PY{l+s}{\PYZdq{}}\PY{p}{:} + \PY{c}{\PYZsh{} g = np.array([[0, 1, 1, 0], [1, 0, 0, 1], [1, 0, 0, 1], [0, 1, 1, 0]])} + \PY{n}{g} \PY{o}{=} \PY{n}{np}\PY{o}{.}\PY{n}{array}\PY{p}{(}\PY{p}{[}\PY{p}{[}\PY{l+m+mi}{0}\PY{p}{,} \PY{l+m+mi}{0}\PY{p}{,} \PY{l+m+mi}{1}\PY{p}{]}\PY{p}{,} \PY{p}{[}\PY{l+m+mi}{0}\PY{p}{,} \PY{l+m+mi}{0}\PY{p}{,} \PY{l+m+mf}{0.5}\PY{p}{]}\PY{p}{,} \PY{p}{[}\PY{l+m+mi}{0}\PY{p}{,} \PY{l+m+mi}{0}\PY{p}{,} \PY{l+m+mi}{0}\PY{p}{]}\PY{p}{]}\PY{p}{)} + \PY{n}{p} \PY{o}{=} \PY{l+m+mf}{0.5} + \PY{n}{g} \PY{o}{=} \PY{n}{np}\PY{o}{.}\PY{n}{log}\PY{p}{(}\PY{l+m+mf}{1.} \PY{o}{/} \PY{p}{(}\PY{l+m+mi}{1} \PY{o}{\PYZhy{}} \PY{n}{p} \PY{o}{*} \PY{n}{g}\PY{p}{)}\PY{p}{)} + \PY{c}{\PYZsh{} error = []} + + \PY{k}{def} \PY{n+nf}{source}\PY{p}{(}\PY{n}{graph}\PY{p}{,} \PY{n}{t}\PY{p}{)}\PY{p}{:} + \PY{n}{x0} \PY{o}{=} \PY{n}{np}\PY{o}{.}\PY{n}{zeros}\PY{p}{(}\PY{n}{graph}\PY{o}{.}\PY{n}{shape}\PY{p}{[}\PY{l+m+mi}{0}\PY{p}{]}\PY{p}{,} \PY{n}{dtype}\PY{o}{=}\PY{n+nb}{bool}\PY{p}{)} + \PY{n}{a} \PY{o}{=} \PY{n}{randint}\PY{p}{(}\PY{l+m+mi}{0}\PY{p}{,} \PY{l+m+mi}{1}\PY{p}{)} + \PY{n}{x0}\PY{p}{[}\PY{n}{a}\PY{p}{]} \PY{o}{=} \PY{n+nb+bp}{True} + \PY{k}{if} \PY{n}{random}\PY{p}{(}\PY{p}{)} \PY{o}{\PYZgt{}} \PY{n}{t}\PY{p}{:} + \PY{n}{x0}\PY{p}{[}\PY{l+m+mi}{1}\PY{o}{\PYZhy{}}\PY{n}{a}\PY{p}{]} \PY{o}{=} \PY{n+nb+bp}{True} + \PY{k}{return} \PY{n}{x0} + + \PY{n}{thresh} \PY{o}{=} \PY{n}{np}\PY{o}{.}\PY{n}{arange}\PY{p}{(}\PY{l+m+mf}{0.}\PY{p}{,} \PY{l+m+mf}{1.1}\PY{p}{,} \PY{n}{step}\PY{o}{=}\PY{l+m+mf}{0.2}\PY{p}{)} + \PY{n}{sizes} \PY{o}{=} \PY{n}{np}\PY{o}{.}\PY{n}{arange}\PY{p}{(}\PY{l+m+mi}{10}\PY{p}{,} \PY{l+m+mi}{100}\PY{p}{,} \PY{n}{step}\PY{o}{=}\PY{l+m+mi}{10}\PY{p}{)} + \PY{n}{nsimul} \PY{o}{=} \PY{l+m+mi}{10} + \PY{n}{r} \PY{o}{=} \PY{n}{np}\PY{o}{.}\PY{n}{zeros}\PY{p}{(}\PY{n+nb}{len}\PY{p}{(}\PY{n}{sizes}\PY{p}{)}\PY{p}{,} \PY{n+nb}{len}\PY{p}{(}\PY{n}{thresh}\PY{p}{)}\PY{p}{)} + \PY{k}{for} \PY{n}{t} \PY{o+ow}{in} \PY{n}{thresh}\PY{p}{:} + \PY{k}{for} \PY{n}{i} \PY{o+ow}{in} \PY{n}{nsimul}\PY{p}{:} + \PY{n}{cascades} \PY{o}{=} \PY{n}{simulate\PYZus{}cascades}\PY{p}{(}\PY{n}{np}\PY{o}{.}\PY{n}{max}\PY{p}{(}\PY{n}{sizes}\PY{p}{)}\PY{p}{,} \PY{n}{g}\PY{p}{,} + \PY{n}{source}\PY{o}{=}\PY{k}{lambda} \PY{n}{graph}\PY{p}{:} \PY{n}{source}\PY{p}{(}\PY{n}{graph}\PY{p}{,} \PY{n}{t}\PY{p}{)}\PY{p}{)} + \PY{n}{e} \PY{o}{=} \PY{n}{np}\PY{o}{.}\PY{n}{zeros}\PY{p}{(}\PY{n}{g}\PY{o}{.}\PY{n}{shape}\PY{p}{[}\PY{l+m+mi}{0}\PY{p}{]}\PY{p}{)} + \PY{k}{for} \PY{n}{j}\PY{p}{,} \PY{n}{s} \PY{o+ow}{in} \PY{n+nb}{enumerate}\PY{p}{(}\PY{n}{sizes}\PY{p}{)}\PY{p}{:} + \PY{n}{x}\PY{p}{,} \PY{n}{y} \PY{o}{=} \PY{n}{build\PYZus{}matrix}\PY{p}{(}\PY{n}{cascades}\PY{p}{,} \PY{l+m+mi}{2}\PY{p}{)} + \PY{n}{e} \PY{o}{+}\PY{o}{=} \PY{n}{infer}\PY{p}{(}\PY{n}{x}\PY{p}{[}\PY{p}{:}\PY{n}{s}\PY{p}{]}\PY{p}{,} \PY{n}{y}\PY{p}{[}\PY{p}{:}\PY{n}{s}\PY{p}{]}\PY{p}{)} + + \PY{k}{for} \PY{n}{i}\PY{p}{,} \PY{n}{t} \PY{o+ow}{in} \PY{n+nb}{enumerate}\PY{p}{(}\PY{n}{thresh}\PY{p}{)}\PY{p}{:} + \PY{n}{plt}\PY{o}{.}\PY{n}{plot}\PY{p}{(}\PY{n}{sizes}\PY{p}{,} \PY{n}{m}\PY{p}{[}\PY{p}{:}\PY{p}{,} \PY{n}{i}\PY{p}{]}\PY{p}{,} \PY{n}{label}\PY{o}{=}\PY{n+nb}{str}\PY{p}{(}\PY{n}{t}\PY{p}{)}\PY{p}{)} + \PY{n}{plt}\PY{o}{.}\PY{n}{legend}\PY{p}{(}\PY{p}{)} + \PY{n}{plt}\PY{o}{.}\PY{n}{show}\PY{p}{(}\PY{p}{)} + + + \PY{c}{\PYZsh{} conf = bootstrap(x, y, n\PYZus{}iter=100)} + \PY{c}{\PYZsh{} estimand = np.linalg.norm(np.delete(conf \PYZhy{} g[0], 0, axis=1), axis=1)} + \PY{c}{\PYZsh{} error.append(confidence\PYZus{}interval(*np.histogram(estimand, bins=50)))} + \PY{c}{\PYZsh{} plt.semilogx(sizes, error)} + \PY{c}{\PYZsh{} plt.show()} +\end{Verbatim} diff --git a/simulation/main.py b/simulation/main.py index bdc5c97..e4b30f2 100644 --- a/simulation/main.py +++ b/simulation/main.py @@ -125,22 +125,34 @@ if __name__ == "__main__": g = np.array([[0, 0, 1], [0, 0, 0.5], [0, 0, 0]]) p = 0.5 g = np.log(1. / (1 - p * g)) - sizes = [100, 500, 1000, 5000, 10000] # error = [] - def source(graph): + def source(graph, t): x0 = np.zeros(graph.shape[0], dtype=bool) a = randint(0, 1) x0[a] = True - if random() > 0.01: + if random() > t: x0[1-a] = True return x0 - for i in sizes: - cascades = simulate_cascades(i, g, source=source) - x, y = build_matrix(cascades, 2) - e = infer(x, y) - print e + thresh = np.arange(0., 1.1, step=0.2) + sizes = np.arange(10, 100, step=10) + nsimul = 10 + r = np.zeros(len(sizes), len(thresh)) + for t in thresh: + for i in nsimul: + cascades = simulate_cascades(np.max(sizes), g, + source=lambda graph: source(graph, t)) + e = np.zeros(g.shape[0]) + for j, s in enumerate(sizes): + x, y = build_matrix(cascades, 2) + e += infer(x[:s], y[:s]) + + for i, t in enumerate(thresh): + plt.plot(sizes, m[:, i], label=str(t)) + plt.legend() + plt.show() + # conf = bootstrap(x, y, n_iter=100) # estimand = np.linalg.norm(np.delete(conf - g[0], 0, axis=1), axis=1) |
