\documentclass[notitlepage]{article}
\usepackage{../Math323}
\usepackage{listings}
\usepackage{numprint}
\usepackage{enumerate}
\usepackage{multirow}
\usepackage{bbm}

\usepackage{amsfonts,amsmath}

\setlength{\parindent}{0pt}

\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}}

\begin{document}

<<setup, cache=FALSE, echo=FALSE>>=
library(knitr)
# global chunk options
opts_chunk$set(cache=TRUE, autodep=TRUE)
options(scipen=999)
options(repos=c(CRAN="https://cloud.r-project.org/"))
inline_hook <- function (x) {
  if (is.numeric(x)) {
    # ifelse does a vectorized comparison
    # If integer, print without decimal; otherwise print two places
    res <- ifelse(x == round(x),
      sprintf("%.3f", x),
      sprintf("%.3f", x)
    )
    paste(res, collapse = ", ")
  }
}
knit_hooks$set(inline = inline_hook)
@
\begin{center}
{\textsclarge{Joint Continuous Distributions}}
\end{center}

\textbf{Joint cdf:} If $Y_1$ and $Y_2$ are continuous random variables, then we may consider the probability

\[
F_{Y_1,Y_2}(y_1,y_2) = P((Y_1 \leq y_1) \cap (Y_2 \leq y_2)) = P (Y_1 \leq y_1, Y_2 \leq y_2)
\]
for any pair of real numbers $(y_1,y_2)$.  The joint cdf has the following properties:

\begin{itemize}
  \item ``starts at zero''
  \begin{align*}
  \lim_{y_1 \lra -\infty} \lim_{y_2 \lra -\infty} F_{Y_1,Y_2}(y_1,y_2) & = 0 \\[6pt]
  \lim_{y_1 \lra -\infty} F_{Y_1,Y_2}(y_1,y_2) & = 0  \quad \textrm{for all } y_2 \qquad \text{and} \qquad
  \lim_{y_2 \lra -\infty} F_{Y_1,Y_2}(y_1,y_2) = 0  \quad \textrm{for all } y_1
  \end{align*}
  \item ``ends at one''
  \[
  \lim_{y_1 \lra \infty} \lim_{y_2 \lra \infty} F_{Y_1,Y_2}(y_1,y_2) = 1
  \]
  \item ``non-decreasing in $y_1$ and $y_2$ in between''
  \begin{align*}
    F_{Y_1,Y_2}(y_1,y_2) & \leq  F_{Y_1,Y_2}(y_1+c,y_2) \\[6pt]
    F_{Y_1,Y_2}(y_1,y_2) & \leq  F_{Y_1,Y_2}(y_1,y_2+c)
  \end{align*}
    for all $y_1,y_2$, and any $c > 0$.
\end{itemize}
Furthermore, we have that
  \[
  \lim_{y_1 \lra \infty} F_{Y_1,Y_2}(y_1,y_2) = F_{Y_2}(y_2) \qquad \qquad
  \lim_{y_2 \lra \infty} F_{Y_1,Y_2}(y_1,y_2) = F_{Y_1}(y_1).
  \]

\bigskip

\textbf{EXAMPLE:} Consider the function defined

\begin{itemize}

\item for $0 \leq y_1 \leq 1, 0 \leq y_2 \leq 1$ by
\[
F_{Y_1,Y_2}(y_1,y_2) = \frac{4}{5} \left (y_1 y_2 + \frac{y_1^2 y_2^2}{4} \right)
\]

\item for $y_1 < 0$ or $y_2 < 0$ by
\[
F_{Y_1,Y_2}(y_1,y_2) = 0
\]

\item for $y_1 > 1$, but $0 \leq y_2 \leq 1$ by
\[
F_{Y_1,Y_2}(y_1,y_2) = F_{Y_1,Y_2}(1,y_2) =  \frac{4}{5} \left (y_2 + \frac{y_2^2}{4} \right)
\]
which follows as
\[
P(Y_1 \leq y_1, Y_2 \leq y_2) = P(Y_1 \leq 1, Y_2 \leq y_2).
\]

\item for $y_2 > 1$, but $0 \leq y_1 \leq 1$ by
\[
F_{Y_1,Y_2}(y_1,y_2) = F_{Y_1,Y_2}(y_1,1) =  \frac{4}{5} \left (y_1 + \frac{y_1^2}{4} \right)
\]
which follows as in this region
\[
P(Y_1 \leq y_1, Y_2 \leq y_2) = P(Y_1 \leq y_1, Y_2 \leq 1).
\]

\item for $y_1 > 1$ and $y_2 > 1$ by $F_{Y_1,Y_2}(y_1,y_2) = 1$.

\end{itemize}
This function defines a joint cdf for two continuous random variables $Y_1$ and $Y_2$ taking values on $[0,1] \times [0,1]$.

\vspace*{-0.5 in}

<<dists01,comment='+', fig.width=5, fig.height=5, fig.align='center',echo=FALSE>>=
par(pty='s')
library(fields,quietly=TRUE)
Ffunc<-function(x,y){
     xv<-pmin(1,x)
     yv<-pmin(1,y)
     return(0.8*(xv*yv + (xv*yv/2)^2)*(xv >= 0 & yv >= 0))
}
y1<-y2<-sort(c(seq(-0.25,1.25,by=0.05),-0.01,0.01))
F12<-outer(y1,y2,Ffunc)
z<-F12
colfunc <- colorRampPalette(c("blue","lightblue","white","yellow","orange","red"))
z.facet.center <- (z[-1, -1] + z[-1, -ncol(z)] + z[-nrow(z), -1] + z[-nrow(z), -ncol(z)])/4
z.facet.range<-cut(z.facet.center, 100)

colvec<-colfunc(100)[z.facet.range]
par(mar=c(0,2,0,0))
persp(y1,y2,F12,col=colvec,phi=20,theta=-60,
   ticktype="detailed",expand=0.6,xlab="y1",ylab="y2",zlab="F(y1,y2)",cex.axis=0.6)
@

\vspace*{-0.5 in}
This surface plot displays the function $F_{Y_1,Y_2}(y_1,y_2)$ on a grid of values.  The function is zero in the dark blue region, and increases in both $y_1$ and $y_2$ for values in the range $(0,1)$.  It is also possible to plot the function as an image plot (see below), with contours linking points on the plot where the function is constant, that is, where $F_{Y_1,Y_2}(y_1,y_2) = p$, for values $p$, $0 \leq p \leq 1$.

\vspace*{-0.5 in}
<<dists02,comment='+', fig.width=4.5, fig.height=4.5, fig.align='center',echo=FALSE>>=
par(pty='s')
y1<-y2<-seq(-0.25,1.25,by=0.005)
F12<-outer(y1,y2,Ffunc)
image.plot(y1,y2,F12,col=colfunc(100),xlab=expression(y[1]),ylab=expression(y[2]),axes=F)
axis(1, at = seq(0, 1, by = 0.2),cex.axis=0.6)
axis(2, at = seq(0, 1, by = 0.2),cex.axis=0.6)
contour(y1,y2,F12,add=T)
abline(h=1,v=1,lty=2)
abline(h=0,v=0,lty=2)

@


\bigskip

\textbf{Joint pdf:} We then use a similar construction to the single variable case to define the joint pdf, that is, we write
\[
F_{Y_1,Y_2}(y_1,y_2) = \int_{-\infty}^{y_1} \int_{-\infty}^{y_2} f_{Y_1,Y_2}(t_1,t_2) \: d t_2 d t_1
\]
to define $f_{Y_1,Y_2}(y_1,y_2)$ implicitly.  We have then that
\[
f_{Y_1,Y_2}(y_1,y_2) = \frac{\partial^2}{\partial y_1 \partial y_2} \left\{F_{Y_1,Y_2}(y_1,y_2)\right\}
\]

In the above example, we have that for $0 \leq y_1 \leq 1$, $0 \leq y_2 \leq 1$
\begin{align*}
f_{Y_1,Y_2}(y_1,y_2) = \frac{\partial^2}{\partial y_1 \partial y_2} \left\{ \frac{4}{5} \left (y_1 y_2 + \frac{y_1^2 y_2^2}{4} \right) \right\}
& = \frac{\partial}{\partial y_1} \left\{ \frac{\partial}{\partial y_2} \left\{ \frac{4}{5} \left (y_1 y_2 + \frac{y_1^2 y_2^2}{4} \right) \right\} \right\}\\[6pt]
& = \frac{\partial}{\partial y_1} \left\{  \frac{4}{5} \left (y_1  + \frac{y_1^2 y_2 }{2} \right) \right\} \\[6pt]
& = \frac{4}{5} \left (1  +  y_1  y_2 \right) \\[6pt]
\end{align*}
Notice that for $y_1$ and $y_2$ outside of the range $0 \leq y_1 \leq 1$, $0 \leq y_2 \leq 1$, we have that
\[
f_{Y_1,Y_2}(y_1,y_2) = 0.
\]

<<dists03,comment='+', fig.width=5, fig.height=5, fig.align='center',echo=FALSE>>=
par(pty='s')
ffunc<-function(x,y){
     xv<-x
     yv<-y
     return(0.8*(1+xv*yv)*(xv >= 0 & yv >= 0 & xv <= 1 & yv <= 1))
}
y1<-y2<-sort(c(seq(-0.25,1.25,by=0.05),-0.01,0.01))
f12<-outer(y1,y2,ffunc)
z<-f12
colfunc <- colorRampPalette(c("blue","lightblue","white","yellow","orange","red"))
z.facet.center <- (z[-1, -1] + z[-1, -ncol(z)] + z[-nrow(z), -1] + z[-nrow(z), -ncol(z)])/4
z.facet.range<-cut(z.facet.center, 100)

colvec<-colfunc(100)[z.facet.range]
par(mar=c(0,2,0,0))
persp(y1,y2,f12,col=colvec,phi=20,theta=-60,
   ticktype="detailed",expand=0.6,xlab="y1",ylab="y2",zlab="f(y1,y2)",cex.axis=0.6)
@
\vspace*{-0.5 in}

This surface plot of the joint pdf indicates that the joint pdf in this case is also increasing in $y_1$ and $y_2$ on the range $(0,1)$ for each variable.

\vspace*{-0.5 in}
<<dists04,comment='+', fig.width=5, fig.height=5, fig.align='center',echo=FALSE>>=
par(pty='s')
y1<-y2<-seq(-0.25,1.25,by=0.01)
f12<-outer(y1,y2,ffunc)
image.plot(y1,y2,f12,col=colfunc(100),xlab=expression(y[1]),ylab=expression(y[2]),axes=F)
axis(1, at = seq(0, 1, by = 0.2),cex.axis=0.8)
axis(2, at = seq(0, 1, by = 0.2),cex.axis=0.8)
contour(y1,y2,f12,add=T)
abline(h=1,v=1,lty=2)
abline(h=0,v=0,lty=2)

@

\textbf{Marginal distributions:} we may compute the marginal pdfs by noting that
\[
F_{Y_1}(y_1) = F_{Y_1,Y_2}(y_1,1) = \frac{4}{5} \left (y_1 + \frac{y_1^2}{4} \right) \qquad 0 \leq y_1 \leq 1
\]
with $F_{Y_1}(y_1) = 0$ if $y_1 < 0$ and $F_{Y_1}(y_1) = 1$ if $y_1 > 0$.  Similarly
\[
F_{Y_2}(y_2) = F_{Y_1,Y_2}(1,y_2) = \frac{4}{5} \left (y_2 + \frac{y_2^2}{4} \right) \qquad 0 \leq y_2 \leq 1
\]
with $F_{Y_2}(y_2) = 0$ if $y_2 < 0$ and $F_{Y_2}(y_2) = 1$ if $y_2 > 0$.   By differentiation, we have that
\[
f_{Y_1}(y_1) = \left \{
\begin{array}{ll}
\dfrac{4}{5} \left (1 + \dfrac{y_1}{2} \right) & 0 \leq y_1 \leq 1\\[12pt]
0 & \text{otherwise}
\end{array}
\right. .
\]
Notice that
\[
\int_{-\infty}^\infty f_{Y_1}(y_1) \: d y_1 =   \int_{0}^1 \frac{4}{5} \left (1 + \frac{y_1}{2} \right) \: d y_1 = \frac{4}{5} \left[y_1 + \frac{y_1^2}{4} \right]_0^1 = \frac{4}{5} \left[1 + \frac{1}{4} \right] = 1.
\]

By an identical calculation
\[
f_{Y_2}(y_2) = \left \{
\begin{array}{ll}
\dfrac{4}{5} \left (1 + \dfrac{y_2}{2} \right) & 0 \leq y_2 \leq 1\\[12pt]
0 & \text{otherwise}
\end{array}
\right. .
\]

Here we have that for general $(y_1,y_2)$, $f_{Y_1,Y_2}(y_1,y_2) \neq f_{Y_1}(y_1) f_{Y_2}(y_2)$, so therefore the random variables $Y_1$ and $Y_2$ are \textbf{not independent} (that is, they are \textbf{dependent}).

\bigskip

\textbf{Conditional pdfs:}  we can compute the conditional pdfs using the definition.  For example, for $0 \leq y_2 \leq 1$
\[
f_{Y_1|Y_2}(y_1|y_2) = \frac{f_{Y_1,Y_2}(y_1,y_2)}{f_{Y_2}(y_2)} = \frac{\left (1  +  y_1  y_2 \right)}{\left (1 + \dfrac{y_2}{2} \right)} \qquad  0 \leq y_1 \leq 1, \text{zero otherwise}.
\]

\bigskip

\textbf{EXAMPLE:} Consider the function defined for $y_1 \in \R$ and $y_2 \in \R$ by
\[
F_{Y_1,Y_2}(y_1,y_2) = \frac{e^{y_1+y_2}}{(1+e^{y_1})(1+e^{y_2})}
\]

This function defines a joint cdf for two continuous random variables $Y_1$ and $Y_2$.

\vspace*{-0.5 in}

<<dists11,comment='+', fig.width=5, fig.height=5, fig.align='center',echo=FALSE>>=
par(pty='s')
library(fields,quietly=TRUE)
Ffunc<-function(x,y){
     return((exp(x)/(1+exp(x))*(exp(y)/(1+exp(y)))))
}
y1<-y2<-seq(-3,3,by=0.1)
F12<-outer(y1,y2,Ffunc)
z<-F12
colfunc <- colorRampPalette(c("blue","lightblue","white","yellow","orange","red"))
z.facet.center <- (z[-1, -1] + z[-1, -ncol(z)] + z[-nrow(z), -1] + z[-nrow(z), -ncol(z)])/4
z.facet.range<-cut(z.facet.center, 100)

colvec<-colfunc(100)[z.facet.range]
par(mar=c(2,4,1,0))
persp(y1,y2,F12,col=colvec,phi=20,theta=-60,
   ticktype="detailed",expand=0.6,xlab="y1",ylab="y2",zlab="F(y1,y2)",cex.axis=0.6,zlim=range(0,1))
@

\vspace*{-1.0 in}

<<dists12,comment='+', fig.width=4.5, fig.height=4.5, fig.align='center',echo=FALSE>>=
par(pty='s')
y1<-y2<-seq(-3,3,by=0.01)
F12<-outer(y1,y2,Ffunc)
image.plot(y1,y2,F12,col=colfunc(100),xlab=expression(y[1]),ylab=expression(y[2]),
            axes=F)
axis(1, at = seq(-3, 3, by = 1),cex.axis=0.8)
axis(2, at = seq(-3, 3, by = 1),cex.axis=0.8)
contour(y1,y2,F12,add=T)
@

\textbf{Joint pdf:} We have by direct calculation, differentiating the joint cdf, that
\[
f_{Y_1,Y_2}(y_1,y_2) = \frac{e^{y_1+y_2}}{(1+e^{y_1})^2 (1+e^{y_2})^2} = \frac{e^{y_1}}{(1+e^{y_1})^2} \frac{e^{y_2}}{(1+e^{y_2})^2}
\]
\vspace*{-0.5 in}

<<dists13,comment='+', fig.width=5, fig.height=5, fig.align='center',echo=FALSE>>=
par(pty='s')
ffunc<-function(x,y){
     xv<-x
     yv<-y
     return((exp(x)/(1+exp(x))^2*(exp(y)/(1+exp(y))^2)))
}
y1<-y2<-seq(-3,3,by=0.05)
f12<-outer(y1,y2,ffunc)
z<-f12
colfunc <- colorRampPalette(c("blue","lightblue","white","yellow","orange","red"))
z.facet.center <- (z[-1, -1] + z[-1, -ncol(z)] + z[-nrow(z), -1] + z[-nrow(z), -ncol(z)])/4
z.facet.range<-cut(z.facet.center, 100)

colvec<-colfunc(100)[z.facet.range]
par(mar=c(2,4,1,0))
persp(y1,y2,f12,col=colvec,phi=20,theta=-60,
   ticktype="detailed",expand=0.6,xlab="y1",ylab="y2",zlab="f(y1,y2)",cex.axis=0.6)
@

\vspace*{-1.0 in}

<<dists14,comment='+', fig.width=5, fig.height=5, fig.align='center',echo=FALSE>>=
par(pty='s')
y1<-y2<-seq(-3,3,by=0.01)
f12<-outer(y1,y2,ffunc)
image.plot(y1,y2,f12,col=colfunc(100),xlab=expression(y[1]),ylab=expression(y[2]),axes=F)
axis(1, at = seq(-3, 3, by = 1),cex.axis=0.8)
axis(2, at = seq(-3, 3, by = 1),cex.axis=0.8)
contour(y1,y2,f12,add=T)
@

\textbf{Marginal distributions:} we may compute the marginal pdfs by noting that
\[
F_{Y_1}(y_1) = F_{Y_1,Y_2}(y_1,\infty) = \frac{e^{y_1}}{(1+e^{y_1})}  \qquad y_1 \in \R
\]
and
\[
F_{Y_2}(y_2) = F_{Y_1,Y_2}(\infty,y_2) = \frac{e^{y_2}}{(1+e^{y_2})}  \qquad y_2 \in \R
\]
 By differentiation, we have that
\[
f_{Y_1}(y_1) =\frac{e^{y_1}}{(1+e^{y_1})^2} \qquad y_1 \in \R
\]
and
\[
f_{Y_2}(y_2) =\frac{e^{y_2}}{(1+e^{y_2})^2} \qquad y_2 \in \R
\]

Here we have that for all $(y_1,y_2)$
\[
f_{Y_1,Y_2}(y_1,y_2) = f_{Y_1}(y_1) f_{Y_2}(y_2)
\]
so therefore the random variables $Y_1$ and $Y_2$ are \textbf{independent}.

\bigskip

\textbf{Conditional pdfs:}  Due to independence, we have that
\[
f_{Y_1|Y_2}(y_1|y_2) = f_{Y_1}(y_1)
\]
for all $y_1$ and
\[
f_{Y_2|Y_1}(y_2|y_1) = f_{Y_2}(y_2)
\]
for all $y_2$.

<<dists15,comment='+', fig.width=7, fig.height=4, fig.align='center',echo=FALSE>>=
par(mar=c(4,4,2,0))
y<-seq(-3,3,by=0.001)
Fy<-exp(y)/(1+exp(y))
plot(y,Fy,type='l',xlab=expression(y[1]),ylab=expression(F[Y[1]](y[1])),ylim=range(0,1),cex.axis=0.8)
title(expression(F[Y[1]](y[1])))
fy<-exp(y)/(1+exp(y))^2
plot(y,fy,type='l',xlab=expression(y[1]),ylab=expression(f[Y[1]](y[1])),ylim=range(0,0.5),cex.axis=0.8)
title(expression(f[Y[1]](y[1])))
@

\end{document}
