\documentclass[notitlepage]{article}
\usepackage{../../Math598}
\usepackage{listings}
\usepackage{numprint}
\usepackage{bbm}
\usepackage{amsfonts,amsmath}
\usepackage{fancyhdr}

\usepackage{lastpage}
\usepackage{enumitem}
\usepackage{bbold}


\usepackage{tikz}
\usetikzlibrary{shapes,decorations,arrows,calc,arrows.meta,fit,positioning}

\def\E{\Expect}


\lstloadlanguages{R}

\definecolor{keywordcolor}{rgb}{0,0.6,0.6}
\definecolor{delimcolor}{rgb}{0.461,0.039,0.102}
\definecolor{Rcommentcolor}{rgb}{0.101,0.043,0.432}

\lstdefinestyle{Rsettings}{
  basicstyle=\ttfamily,
  breaklines=true,
  showstringspaces=false,
  keywords={if, else, function, theFunction, tmp}, % Write as many keywords
  otherkeywords={},
  commentstyle=\itshape\color{Rcommentcolor},
  keywordstyle=\color{keywordcolor},
  moredelim=[s][\color{delimcolor}]{"}{"},
}

\lstset{basicstyle=\ttfamily, numbers=none, literate={~} {$\sim$}{2}}


\lfoot{MATH 598/782 Project \ExNumber \: Solutions} \cfoot{ } \rfoot{{\small{\em
Page \thepage \ of \pageref{LastPage}}}}


\pagestyle{empty}

\def\ExNumber{1}
\parindent 0in

\renewcommand{\headrulewidth}{0.0pt}
\renewcommand{\footrulewidth}{0.0pt}


\begin{document}
\pagestyle{fancy}

\begin{center}
{\textsclarge{MATH 598/782 -- Project \ExNumber \: Solutions }}
\end{center}

\bigskip


\tikzset{
    -Latex,auto,node distance =1 cm and 1 cm,semithick,
    state/.style ={circle, draw, minimum width = 0.7 cm},
    box/.style ={rectangle, draw, minimum width = 0.7 cm, fill=lightgray},
    point/.style = {circle, draw, inner sep=0.08cm,fill,node contents={}},
    bidirected/.style={Latex-Latex,dashed},
    el/.style = {inner sep=3pt, align=left, sloped}

}

\textit{For each of the following causal DAGs that define a joint distribution for a collection of random variables,}

\begin{enumerate}[label=(\alph*)]

\item \textit{write out the implied factorization of the joint distribution;}

\item \textit{simulate data from the joint distribution (you may make any suitable distributional assumptions);}

\item \textit{using the simulated data, demonstrate one example of unconditional d-separation, and one example of conditional d-separation; if no such examples exist, state this.  It may be sufficient to demonstrate uncorrelatedness rather than independence.}

\end{enumerate}

\bigskip

\textbf{Solution:} We will stick to Gaussian simulation throughout.

\begin{enumerate}[label=\arabic*.,itemsep=0.2in]

  \item DAG 1:
\hfill{\textsc{5 Marks}}

\medskip

\begin{figure}[h]
\centering
\begin{tikzpicture}[scale=1.5]
    % x node set with absolute coordinates
    \node[state] (x1) at (-1,0) {${X_1}$};
    \node[state] (x2) at (1,0){${X_2}$};
    \node[state] (x3) at (0,-1) {${X_3}$};
    \node[state] (x4) at (0,-2.5){${X_4}$};
    \node[state] (x5) at (-1,-3.5) {${X_5}$};
    \node[state] (x6) at (1,-3.5) {${X_6}$};

    \path (x1) edge (x3);
    \path (x2) edge (x3);
    \path (x3) edge (x4);
    \path (x4) edge (x5);
    \path (x4) edge (x6);

 \end{tikzpicture}
\end{figure}

\begin{enumerate}[label=(\alph*)]

\item Factorization:
\[
f_{X_1}(x_1) f_{X_2}(x_2) f_{X_3|X_1,X_2}(x_3|x_1,x_2) f_{X_4|X_3}(x_4|x_3) f_{X_5|X_4}(x_5|x_4) f_{X_6|X_4}(x_6|x_4)
\]

\item Simulation
<<p1,comment='+', fig.width=4.8, fig.height=4.8, fig.align='center'>>=
set.seed(238)
n<-2000
X1<-rnorm(n,1,1)
X2<-rnorm(n,2,1)
X3<-rnorm(n,1+X1-2*X2,1)
X4<-rnorm(n,X3,1)
X5<-rnorm(n,X4,1)
X6<-rnorm(n,X4,1)
Xm<-cbind(X1,X2,X3,X4,X5,X6)
round(cor(Xm),4)
par(pty='s',mar=c(3,2,1,0))
pairs(Xm,pch=19,cex=0.6)
@


\item d-separation:

\begin{enumerate}[label=(\roman*)]

\item Unconditional d-separation: only one pair is unconditionally d-separated, $X_1$ and $X_2$; we can observe that they are uncorrelated with correlation \Sexpr{cor(X1,X2)}, which we can confirm empirically using the regression of one on the other

<<p2,comment='+', fig.width=4.8, fig.height=4.8, fig.align='center'>>=
summary(lm(X2~X1))$coef
@

\item Conditional d-separation: There are many examples, but for example $X_1$ is conditionally d-separated from $X_5$ by both $X_3$ and $X_4$;  we can assess that using regression
<<p3,comment='+', fig.width=4.8, fig.height=4.8, fig.align='center'>>=
summary(lm(X5~X1+X3))$coef
summary(lm(X5~X1+X4))$coef
@  

\end{enumerate}

\end{enumerate}

\pagebreak

\item DAG 2:

\hfill{\textsc{5 Marks}}

\medskip

\begin{figure}[h]
\centering
\begin{tikzpicture}[scale=1.5]
    % x node set with absolute coordinates
    \node[state] (x1) at (-1,0) {${X_1}$};
    \node[state] (x2) at (1,0){${X_2}$};
    \node[state] (x3) at (0,-1) {${X_3}$};
    \node[state] (x4) at (0,-2.5){${X_4}$};
    \node[state] (x5) at (-1.5,-3.5) {${X_5}$};
    \node[state] (x6) at (1.5,-3.5) {${X_6}$};

    \path (x1) edge (x3);
    \path (x1) edge (x2);
    \path (x2) edge (x3);
    \path (x4) edge (x3);
    \path (x4) edge (x5);
    \path (x4) edge (x6);

    \path (x5) edge (x3);
    \path (x6) edge (x3);


 \end{tikzpicture}
\end{figure}

\begin{enumerate}[label=(\alph*)]

\item Factorization:  $X_1$ and $X_4$ are founders
\[
f_{X_1}(x_1) f_{X_4}(x_4) f_{X_2|X_1}(x_2|x_1) f_{X_5|X_4}(x_5|x_4) f_{X_6|X_4}(x_6|x_4) f_{X_3|X_1,X_2,X_4,X_5,X_6}(x_3|x_1,x_2,x_4,x_5,x_6)
\]

\item Simulation:

<<p4,comment='+', fig.width=4.8, fig.height=4.8, fig.align='center'>>=
set.seed(238)
n<-2000
X1<-rnorm(n,1,1)
X4<-rnorm(n,2,1)
X2<-rnorm(n,1+X1,1)
X5<-rnorm(n,X4,1)
X6<-rnorm(n,X4,1)
X3<-rnorm(n,X1-X2+X4-2*X5+X6,1)
Xm<-cbind(X1,X2,X3,X4,X5,X6)
round(cor(Xm),4)
par(pty='s',mar=c(3,2,1,0))
pairs(Xm,pch=19,cex=0.6)
@


\item d-separation:

\begin{enumerate}[label=(\roman*)]

\item Unconditional d-separation: as $X_3$ acts as a collider on many paths, $X_1$ and $X_2$ are both unconditionally d-separated from each of $X_4$, $X_5$ and $X_6$.  We can see the zeros in the correlation matrix, and also confirm by regression, for example:

<<p5,comment='+', fig.width=4.8, fig.height=4.8, fig.align='center'>>=
summary(lm(X6~X1))$coef
summary(lm(X5~X2))$coef
@

\item Conditional d-separation: There are several examples, but for example $X_5$ is conditionally d-separated from $X_6$ by $X_4$; the other path is blocked by the collider $X_3$.
<<p6,comment='+', fig.width=4.8, fig.height=4.8, fig.align='center'>>=
summary(lm(X5~X6+X4))$coef
@
If we add the collider into the model, the dependence of $X_5$ on $X_6$ becomes non negligible.
<<p7,comment='+', fig.width=4.8, fig.height=4.8, fig.align='center'>>=
summary(lm(X5~X6+X3+X4))$coef
@


\end{enumerate}

\end{enumerate}

  \item DAG 3:
\hfill{\textsc{5 Marks}}

\medskip

\begin{figure}[h]
\centering
\begin{tikzpicture}[scale=1.5]
    % x node set with absolute coordinates
    \node[state] (x1) at (-3,0) {${X_1}$};
    \node[state] (x2) at (-1,0){${X_2}$};
    \node[state] (x3) at (1,0) {${X_3}$};
    \node[state] (x4) at (3,0){${X_4}$};
    \node[state] (x5) at (0,-1){${X_5}$};

    \path (x1) edge (x2);
    \path (x2) edge (x3);
    \path (x3) edge (x4);
    \path (x5) edge (x2);
    \path (x5) edge (x3);

    \path (x1) edge [bend left=45] (x3);
    \path (x5) edge [bend left=-45] (x4);

 \end{tikzpicture}
\end{figure}

\begin{enumerate}[label=(\alph*)]

\item Factorization:  $X_1$ and $X_5$ are founders
\[
f_{X_1}(x_1) f_{X_5}(x_5) f_{X_2|X_1,X_5}(x_2|x_1,x_5) f_{X_3|X_1,X_2,X_5}(x_3|x_1,x_2,x_5) f_{X_4|X_3,X_5}(x_4|x_3,x_5)
\]

\item Simulation:

<<p8,comment='+', fig.width=4.8, fig.height=4.8, fig.align='center'>>=
set.seed(238)
n<-2000
X1<-rnorm(n,1,1)
X5<-rnorm(n,2,1)
X2<-rnorm(n,1+X1+2*X5,1)
X3<-rnorm(n,X1+X2-X5,1)
X4<-rnorm(n,2*X3+X5,1)
Xm<-cbind(X1,X2,X3,X4,X5)
round(cor(Xm),4)
par(pty='s',mar=c(3,2,1,0))
pairs(Xm,pch=19,cex=0.6)
@


\item d-separation:

\begin{enumerate}[label=(\roman*)]

\item Unconditional d-separation: only $X_1$ and $X_5$ are unconditionally d-separated.

<<p9,comment='+', fig.width=4.8, fig.height=4.8, fig.align='center'>>=
summary(lm(X5~X1))$coef
@

\item Conditional d-separation: There are several examples, for instance $X_2$ is conditionally d-separated from $X_4$ by $(X_3,X_5)$. 

<<p10,comment='+', fig.width=4.8, fig.height=4.8, fig.align='center'>>=
summary(lm(X4~X2+X3+X5))$coef
@
Conditioning on $X_3$ alone, or $X_5$ alone, is not sufficient to yield d-separation.
<<p11,comment='+', fig.width=4.8, fig.height=4.8, fig.align='center'>>=
summary(lm(X4~X2+X3))$coef
@

<<p12,comment='+', fig.width=4.8, fig.height=4.8, fig.align='center'>>=
summary(lm(X4~X2+X5))$coef
@
\end{enumerate}

\end{enumerate}


  \item DAG 4:
\hfill{\textsc{5 Marks}}

\medskip

\begin{figure}[h]
\centering
\begin{tikzpicture}[scale=1.5]
    % x node set with absolute coordinates
    \node[state] (x1) at (-2,2) {${X_1}$};
    \node[state] (x2) at (2,2) {${X_2}$};
    \node[state] (x3) at (-2,-2){${X_3}$};
    \node[state] (x4) at (2,-2){${X_4}$};
    \node[state] (x5) at (0,0){${X_5}$};

    \path (x1) edge (x2);
    \path (x1) edge (x3);
    \path (x5) edge (x4);
    \path (x5) edge (x3);
    \path (x2) edge (x4);
    \path (x5) edge (x2);


 \end{tikzpicture}
\end{figure}

\begin{enumerate}[label=(\alph*)]

\item Factorization:  $X_1$ and $X_5$ are founders
\[
f_{X_1}(x_1) f_{X_5}(x_5) f_{X_2|X_1,X_5}(x_2|x_1,x_5) f_{X_3|X_1,X_5}(x_3|x_1,x_5) f_{X_4|X_2,X_5}(x_4|x_2,x_5)
\]

\item Simulation:

<<p13,comment='+', fig.width=4.8, fig.height=4.8, fig.align='center'>>=
set.seed(238)
n<-2000
X1<-rnorm(n,1,1)
X5<-rnorm(n,2,1)
X2<-rnorm(n,X1-X5,1)
X3<-rnorm(n,X1-2*X5,1)
X4<-rnorm(n,X2-X5)
Xm<-cbind(X1,X2,X3,X4,X5)
round(cor(Xm),4)
par(pty='s',mar=c(3,2,1,0))
pairs(Xm,pch=19,cex=0.6)
@

\item d-separation:

\begin{enumerate}[label=(\roman*)]

\item Unconditional d-separation: only $X_1$ and $X_5$ are unconditionally d-separated.

<<p14,comment='+', fig.width=4.8, fig.height=4.8, fig.align='center'>>=
summary(lm(X5~X1))$coef
@

\item Conditional d-separation: $X_2$ and $X_3$ are conditionally d-separated given $(X_1,X_5)$

<<p15,comment='+', fig.width=4.8, fig.height=4.8, fig.align='center'>>=
summary(lm(X3~X2+X1+X5))$coef
@
Conditioning on $X_1$ alone, or $X_5$ alone, is not sufficient to yield d-separation.
<<p16,comment='+', fig.width=4.8, fig.height=4.8, fig.align='center'>>=
summary(lm(X3~X2+X1))$coef
@

<<p17,comment='+', fig.width=4.8, fig.height=4.8, fig.align='center'>>=
summary(lm(X3~X2+X5))$coef
@
\end{enumerate}

\end{enumerate}

\end{enumerate}

\end{document}
