\documentclass[twocolumn]{article}
% \documentclass{article}

\setlength{\oddsidemargin}{0.5in}
\setlength{\evensidemargin}{0.5in}
\setlength{\topmargin}{0.0in}
\setlength{\headheight}{0in}
\setlength{\headsep}{0in}
\setlength{\textwidth}{6.0in}
\setlength{\textheight}{9.0in}
\setlength{\parindent}{0in}
\setlength{\parskip}{0.05in}
\setlength{\columnseprule}{0.3pt}

\setlength\columnsep{25pt}

\usepackage{fancyvrb}
\usepackage{relsize}
\usepackage{hyperref}

\usepackage{listings}
\usepackage{amsmath}

\begin{document}

\lstset{xleftmargin=5mm,framexleftmargin=10mm,basicstyle=\footnotesize}

% Name: \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_

{\bf DIRECTIONS:} Write your solutions in a single {\bf .tex} file,
\underline{including} R code. Your {\bf .tar} package will consist of
that file, its output {\bf .pdf} file, and a separate file for each
problem requiring R code, each file named in the form {\bf x.R}, where x
is the problem number.  Name your {\bf .tar} file as you did in the
homework, but with your own address only. Your submission must be in the
256quiz2 directory in handin, {\bf timestamped on or before 3:00
p.m.  NO LATE SUBMISSIONS; keep submitting the work you have, as you go 
along, so that you at least have something turned in.}  You are not
necessarily expected to solve all the problems.

{\bf 1.} (35) Consider the ``Wharton experiment,'' p.427 of our book.
Write a function with ``declaration'' 

\begin{lstlisting}
wharton <- function(indata,sg,nnoise)
\end{lstlisting}

that performs this experiment on any data frame {\bf indata}. Here {\bf
nnoise} is the number of $N(0,\sigma^2)$ noise variables to be added,
where $\sigma$ is {\bf sg}.

It is assumed that the response variable is the one in the final, i.e.\
rightmost, column of {\bf indata}.  

There will be no return value.  Instead, your function will make a call
{\bf print(summary(lm()))} to run the regression and print out
the results, replete with asterisks.  (You may not get a lot of them.)

You may wish to convert {\bf indata} to a matrix within your code.

Place your code in {\bf 1.R}, and a listing in your {\bf .tex} file.

{\bf 2.} (35) Suppose 

\begin{equation}
m_{Y;X}(t) = 2t ~~ (\textrm{as in } (22.1))
\end{equation}

\begin{equation}
Var(Y ~|~ X = t) = t
\end{equation}

and that $X$ has a uniform distribution on (0,1).  Find $Var(Y)$.

For full credit, have no explicit integrals.

{\bf 3.} (30) Suppose $X$ has support (0,1), with density

\begin{equation}
f_X(t) = 
\begin{cases}  
    c, & \textrm{ if } t \in (0,q) \\
    2c, & \textrm{ if } t \in (q,1)\\
\end{cases}  
\end{equation}

This is a 1-parameter density family, with the parameter $q$. Note that
$c$ is a constant to make the function integrate to 1; it is not a
second parameter.  

Write a function with ``declaration'' 

\begin{lstlisting}
gmmq <- function(x,initq)
\end{lstlisting}

which uses the {\bf gmm} package (this is required) to estimate $q$ from
the data vector {\bf x} and an initial guess {\bf initq}.  The return
value of the function will be the object that {\bf gmm()} returns.
You'll need to use the recipe given to Nick by the author of {\bf
gmm()}.

{\bf Note:}  You must include your mathematical derivation in your {\bf
.tex} file, and your R code in both that file and {\bf 3.R}.

\onecolumn

\newpage

{\bf Solutions:}

{\bf 1.}

\begin{lstlisting}
wh <- function(xy,sg,nnoise) {
   n <- nrow(xy)
   p <- ncol(xy)
   xy <- cbind(xy,matrix(sg*rnorm(n*nnoise),ncol=nnoise))
   xy <- as.matrix(xy)
   print(summary(lm(xy[,p] ~ xy[,-p])))
}
\end{lstlisting}

{\bf 2.}

\begin{eqnarray}
Var(Y) &=& E[Var(Y|X)] + Var[E(Y|X)] \\
       &=& EX + Var(2X) \\
       &=& 0.5 + 4 (1/12)
\end{eqnarray}

{\bf 3.}

\begin{lstlisting}
gmmq <- function(x,initq) {
   if (is.vector(x)) x <- matrix(x,ncol=1)
   g <- function(th,x) {
      q <- th[1]
      c <- 1/(2-q)
      c*q^2/2 + c * (1-q^2) - x
   }
   gmm(g,x,c(q = initq),lower=0,upper=1, method='Brent')
}
\end{lstlisting}

\end{document}

library(freqparcoord) 
data(mlb) 
xy <- mlb[,c(4,6,5)] 




sim <- function(n) {
   tmp <- runif(n,0,0.5)
   ifelse(sample(0:1,n,prob=c(0.3333,0.6667),replace=T),
      tmp+0.5,tmp)
}

gmmq <- function(x,initq) {
   if (is.vector(x)) x <- matrix(x,ncol=1)
   g <- function(th,x) {
      q <- th[1]
      c <- 1/(2-q)
      c*q^2/2 + c * (1-q^2) - x
   }
   gmm(g,x,c(q = initq),lower=0,upper=1, method='Brent')
}

