|
| 1 | +\begin{MintedVerbatim}[commandchars=\\\{\},codes={\catcode`\$=3\catcode`\^=7\catcode`\_=8\relax}] |
| 2 | +\PYG{k}{def}\PYG{+w}{ }\PYG{n+nf}{get\PYGZus{}conditional\PYGZus{}dist}\PYG{p}{(}\PYG{n}{joint\PYGZus{}mu}\PYG{p}{,} \PYG{n}{joint\PYGZus{}cov}\PYG{p}{,} \PYG{n}{var\PYGZus{}index}\PYG{p}{)}\PYG{p}{:} |
| 3 | +\PYG{+w}{ }\PYG{l+s+sd}{\PYGZsq{}\PYGZsq{}\PYGZsq{}Returns the conditional distribution given the joint distribution and which variable} |
| 4 | +\PYG{l+s+sd}{ the conditional probability should use.} |
| 5 | +\PYG{l+s+sd}{ Right now this only works for 2\PYGZhy{}variable joint distributions.} |
| 6 | + |
| 7 | +\PYG{l+s+sd}{ joint\PYGZus{}mu: joint distribution\PYGZsq{}s mu} |
| 8 | +\PYG{l+s+sd}{ joint\PYGZus{}cov: joint distribution\PYGZsq{}s covariance} |
| 9 | +\PYG{l+s+sd}{ var\PYGZus{}index: index of the variable in the joint distribution. Everything else will be} |
| 10 | +\PYG{l+s+sd}{ conditioned on. For example, if the joint distribution p(a, b, c) has mu [mu\PYGZus{}a, mu\PYGZus{}b, mu\PYGZus{}c],} |
| 11 | +\PYG{l+s+sd}{ to get p(c | a, b), use var\PYGZus{}index = 2.} |
| 12 | + |
| 13 | +\PYG{l+s+sd}{ returns:} |
| 14 | +\PYG{l+s+sd}{ a function that can sample from the univariate conditional distribution} |
| 15 | +\PYG{l+s+sd}{ \PYGZsq{}\PYGZsq{}\PYGZsq{}} |
| 16 | + \PYG{k}{assert} \PYG{n}{joint\PYGZus{}mu}\PYG{o}{.}\PYG{n}{shape}\PYG{p}{[}\PYG{l+m+mi}{0}\PYG{p}{]} \PYG{o}{==} \PYG{l+m+mi}{2}\PYG{p}{,} \PYG{l+s+s1}{\PYGZsq{}}\PYG{l+s+s1}{Sorry, this function only works for 2\PYGZhy{}dimensional joint distributions right now}\PYG{l+s+s1}{\PYGZsq{}} |
| 17 | + \PYG{n}{a} \PYG{o}{=} \PYG{n}{joint\PYGZus{}mu}\PYG{p}{[}\PYG{n}{var\PYGZus{}index}\PYG{p}{]} |
| 18 | + \PYG{n}{b} \PYG{o}{=} \PYG{n}{joint\PYGZus{}mu}\PYG{p}{[}\PYG{o}{\PYGZti{}}\PYG{n}{var\PYGZus{}index}\PYG{p}{]} |
| 19 | + |
| 20 | + \PYG{n}{A} \PYG{o}{=} \PYG{n}{joint\PYGZus{}cov}\PYG{p}{[}\PYG{n}{var\PYGZus{}index}\PYG{p}{,} \PYG{n}{var\PYGZus{}index}\PYG{p}{]} |
| 21 | + \PYG{n}{B} \PYG{o}{=} \PYG{n}{joint\PYGZus{}cov}\PYG{p}{[}\PYG{o}{\PYGZti{}}\PYG{n}{var\PYGZus{}index}\PYG{p}{,} \PYG{o}{\PYGZti{}}\PYG{n}{var\PYGZus{}index}\PYG{p}{]} |
| 22 | + \PYG{n}{C} \PYG{o}{=} \PYG{n}{joint\PYGZus{}cov}\PYG{p}{[}\PYG{n}{var\PYGZus{}index}\PYG{p}{,} \PYG{o}{\PYGZti{}}\PYG{n}{var\PYGZus{}index}\PYG{p}{]} |
| 23 | + |
| 24 | + \PYG{c+c1}{\PYGZsh{} we\PYGZsq{}re dealing with one dimension so} |
| 25 | + \PYG{n}{B\PYGZus{}inv} \PYG{o}{=} \PYG{l+m+mi}{1}\PYG{o}{/}\PYG{n}{B} |
| 26 | + |
| 27 | + \PYG{c+c1}{\PYGZsh{} Return a function that can sample given a value of g} |
| 28 | + \PYG{k}{def}\PYG{+w}{ }\PYG{n+nf}{dist}\PYG{p}{(}\PYG{n}{g}\PYG{p}{)}\PYG{p}{:} |
| 29 | + \PYG{c+c1}{\PYGZsh{} a + C*B\PYGZca{}\PYGZob{}\PYGZhy{}1\PYGZcb{}(g \PYGZhy{} b)} |
| 30 | + \PYG{n}{mu} \PYG{o}{=} \PYG{n}{a} \PYG{o}{+} \PYG{n}{C} \PYG{o}{*} \PYG{n}{B\PYGZus{}inv} \PYG{o}{*} \PYG{p}{(}\PYG{n}{g} \PYG{o}{\PYGZhy{}} \PYG{n}{b}\PYG{p}{)} |
| 31 | + \PYG{c+c1}{\PYGZsh{} A \PYGZhy{} C * B\PYGZca{}\PYGZob{}\PYGZhy{}1\PYGZcb{} * C\PYGZca{}T} |
| 32 | + \PYG{n}{cov} \PYG{o}{=} \PYG{n}{A} \PYG{o}{\PYGZhy{}} \PYG{n}{B\PYGZus{}inv} \PYG{o}{*} \PYG{n}{C} \PYG{o}{*} \PYG{n}{C} |
| 33 | + \PYG{k}{return} \PYG{n}{np}\PYG{o}{.}\PYG{n}{sqrt}\PYG{p}{(}\PYG{n}{cov}\PYG{p}{)} \PYG{o}{*} \PYG{n}{np}\PYG{o}{.}\PYG{n}{random}\PYG{o}{.}\PYG{n}{randn}\PYG{p}{(}\PYG{l+m+mi}{1}\PYG{p}{)} \PYG{o}{+} \PYG{n}{mu} |
| 34 | + |
| 35 | + \PYG{k}{return} \PYG{n}{dist} |
| 36 | + |
| 37 | + |
| 38 | +\end{MintedVerbatim} |
0 commit comments