%%%%%%%%%%%%%%%%%%%%
%I) PRELIMINARIES
%%%%%%%%%%%%%%%%%%%%
\documentclass[11pt,reqno]{amsart}
% Packages
%%%%%%%%%%%%%%%%%%
\usepackage{graphicx}%preferred package for inclusion of graphics
\usepackage{comment}
\usepackage{setspace}
\usepackage{enumerate}%for easy choice of enumerator symbol
\usepackage{tabularx}%for tables width user-defined width
\usepackage{ctable} %for toprule, midrule etc. in tables
\usepackage{multirow} %for more flexibility with tables
\usepackage{textcomp}%for cent-symbol
\usepackage[colorlinks=true, urlcolor=blue]{hyperref}%for inserting hyperlinks
\usepackage{caption} %flexibility with tables
\usepackage{subcaption} %flexibility with tables
\usepackage{url}%to get harvard to work, insert immediately before \title{...}
\usepackage[dcucite]{harvard} %bibliography style, dcu gives commas before year, semicolon between references, and "and" between authors
\usepackage{amssymb} %for the more esotheric math expressions, such as \approxeq
\usepackage{lineno}%for line numbers
\usepackage{lscape}%for inserting landscape format pages
\usepackage{float} % for more flexibility with tables
\usepackage{appendix}%allows for turning appendices on and off
\usepackage{epstopdf} %to allow import of .eps graphics
% Page formatting
%%%%%%%%%%%%%%%%%%%%
\pagestyle{plain} %puts page number center bottom
\setlength{\topmargin}{0in}
\setlength{\textheight}{8.5in}
\setlength{\oddsidemargin}{.0in}
\setlength{\evensidemargin}{.0in}
\setlength{\textwidth}{6.5in}
\setlength{\footskip}{.5in}
% Customized commands
%%%%%%%%%%%%%%%%%%%%%
% Math
\newcommand{\mlt}[1]{\mathbf{#1}} %matrix bold for Latin symbols
\newcommand{\mgr}[1]{\boldsymbol{#1}}%matrix bold for Greek symbols
\newcommand{\kl}{\left(}
\newcommand{\kr}{\right)}
\newcommand{\kll}{\left\{}
\newcommand{\krr}{\right\}}
\newcommand{\kmu}{\mgr{\mu}}
\newcommand{\kpsi}{\mgr{\psi}}
\newcommand{\kphi}{\mgr{\phi}}
\newcommand{\kgam}{\mgr{\gamma}}
\newcommand{\ktheta}{\mgr{\theta}}
\newcommand{\kbeta}{\mgr{\beta}}
\newcommand{\kdelta}{\mgr{\delta}}
\newcommand{\kt}{^{\prime}}
\newcommand{\kdel}{\partial}
\newcommand{\kdot}{\kl . \kr}
\newcommand{\keps}{\epsilon}
\newcommand{\kx}{\mlt{x}}
\newcommand{\kX}{\mlt{X}}
\newcommand{\kV}{\mlt{V}}
\newcommand{\ky}{\mlt{y}}
\newcommand{\kb}{\mlt{b}}
\newcommand{\ki}{\mlt{i}}
\newcommand{\klam}{\lambda}
\newcommand{\kp}{\mlt{p}}
\newcommand{\kprob}{\text{prob}}
\newcommand{\kz}{\mlt{z}}
\newcommand{\ksig}{\sigma^2}
\newcommand{\kSig}{\mgr{\Sigma}}
\newcommand{\klog}{\text{log}}
\newcommand{\kols}{\kl \kX\kt\kX\kr^{-1}\kX\kt\ky}
\newcommand{\kSSE}{\kl \ky-\kX\kb\kr\kt\kl\ky-\kX\kb\kr}
\newcommand{\ksp}{\vspace{0.1in}}
%Special font within regular document
\newcommand{\chp}[1]{\textbf{\textsl{#1}}}
\newcommand{\km}[1]{\textsf{\small{#1}}} %special font for my own comments
\newcommand{\mlab}{\textbf{\texttt{Matlab }}}
\newcommand{\ktt}[1]{\textbf{\texttt{#1}}}
%Tables
\newcolumntype{C}{>{\centering\arraybackslash}X} %for centered columns within tabularx,instead of justified (the default)
%Others
\newcommand{\kpm}{PM_{2.5}}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}
%%%%%%%%%%%%%%%%%%%%%%%%
\citationmode{abbr} %use only "et al" citations
% the following add space before and after equations
\abovedisplayskip=0.7cm
\abovedisplayshortskip=-0.3cm
\belowdisplayskip=0.7cm
\belowdisplayshortskip=0.4cm
%III) TOP MATTER INFORMATION
\title{Problem Set 1}
\author{AAEC 6564 \\ Instructor: Klaus Moeltner}
\maketitle %this comes at the end of the top matter to set it.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section*{General Instructions}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Please type everything in LaTeX (including all Math) and hand in a pdf file. For problems involving Matlab, answer questions in LaTeX, and attach your script, log file, and any graphs to your main pdf file.\\
If you're ambitious, you can export figures from Matlab in .eps format (as shown in Matlab script \texttt{mod1s1a}), and load them directly into your LaTeX file, using something like \\
\begin{verbatim}
\begin{figure}[!ht]
\centering
\includegraphics[height=4in]{NameofYourFigure}
\caption{Example}
\label{figure1}
\end{figure}
\end{verbatim}
\ksp
If you do this, make sure the \begin{verbatim} \usepackage{epstopdf}\end{verbatim} is called at the beginning of your LaTeX file (as it is in this example).\\
If you're extremely ambitious, you can copy your numeric output (log file) into Excel, add variable names, etc, convert them to LaTeX (via the downloadable ``Excel2LaTeX'' add-in), and import them directly into your LaTeX file. Detailed instructions for this are given at \url{http://faculty.agecon.vt.edu/LaTeX.html}.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section*{Question 1}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Consider the binomial$\kl n,\theta \kr$ likelihood and the beta$\kl \alpha, \beta\kr$ prior for $\theta$ we used in our class example. Show that the posterior distribution for $\theta$ is indeed another beta with parameters $\alpha+y, \beta+n-y$, where $y$ is the number of successes in the $n$ binomial trials.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section*{Question 2 (Matlab)}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%\subsection{Part A)
%%%%%%%%%%%%%%%%%%%
In this problem you will show that the posterior results for the normal linear regression model
with \emph{conjugate} priors can also be derived via Gibbs Sampling. Please make sure to add your full script (including all functions) to your homework - hard copy is fine. Also make sure to hand in all graphs, either as hard copy or as embedded image in your LaTeX / pdf file.\\
\ksp
\textbf{PART A}
\ksp
\begin{enumerate}
\item Re-run script \texttt{mod1s2a} and print the output log.
\item Open script \texttt{mod2s1a} and save it as \texttt{ps1q2}. Modify the script as follows:\\
%
\begin{enumerate}
\item Change the folder designations for the log file and the ``save'' command as needed.
\item Make sure r1 (burn-ins) and r2 (keepers) are set to 5000 and 10,000, respectively (else change them to these settings).
\end{enumerate}
%
\item Open the function \texttt{gs\_normal\_independent} and save it as \texttt{gsPS1Q2}. Modify the function as follows:\\
%
\begin{enumerate}
\item change the name of the function in line 2 to fit the file name.
\item Modify your Gibbs Sampler by using the correct conditional posterior moments for the normal linear regression model with \emph{conjugate} priors (see lecture notes).\\
\end{enumerate}
%
\item Run the model.
\item Open and print the log file. Verify that the posterior moments for $\kbeta$ and $\ksig$ are indeed identical to those produced by the analytical approach.
Comment.
\item Add the following task to your script: Draw 10,000 draws of $\kbeta$ from its prior, by first drawing 1000 $\ksig$'s, and for each of them 10 $\kbeta$'s
\item Plot the prior and posterior for $\beta_3$. The full code for your figure should read like this:
\begin{verbatim}
figure(1)
subplot(2,2,1)
plot(x1,f1,'-b',x2,f2,'-k','LineWidth',1);
title('prior and posterior distributions for $\beta_3$',...
'interpreter','latex','fontsize',14);
xlabel('$\beta_3$','interpreter','latex','fontsize',12);
ylabel('density');
h=legend('$p\left(\beta_3\right)$','$p\left(\beta_3|y,X\right)$');
set(h,'interpreter','latex','fontsize',12);
set(gca,'Ylim',[0 70]); % limits for your y-axis, use same for all plots
set(gca,'Xlim',[-0.2 1.2]); % limits for your x-axis, use same for all plots
set(gca,'XTick',-5:0.2:5); % tick marks on x-axis, spaced in 0.2 intervals
text(0,40,['n = ' num2str(n)],'fontsize',10);
% position of text relative to x and y axis
text(0,35,['prior variance = ' num2str(V0(end,end))],'fontsize',10);
hold on; %this will allow you to add more subplots to the figure
\end{verbatim}
\end{enumerate}
\ksp
\textbf{PART B}
\ksp
\begin{enumerate}
\item
Save your script as \texttt{ps1q2partB}. Change the log and ``save'' paths accordingly.
\item
Reduce the prior variance for $\beta_3$ ONLY to 1e-6 (0.000001). You can do this by inserting the following code after defining $\kV_0$:
\begin{verbatim}
V0(end,end)=1e-6;
\end{verbatim}
\item Begin the figure command with \begin{verbatim} subplot(2,2,2) \end{verbatim}
Drop the \begin{verbatim} hold on; \end{verbatim} at the end of the figure command.
In the figure command, change the text positioning coordinates to
\begin{verbatim}
text(0.4,40,['n = ' num2str(n)],'fontsize',10);
text(0.4,35,['prior variance = ' num2str(V0(end,end))],'fontsize',10);
\end{verbatim}
\item Re-run the script.
\end{enumerate}
\ksp
\textbf{PART C}
\ksp
\begin{enumerate}
\item
Save your script as \texttt{ps1q2partC}. Change the log and ``save'' paths accordingly.
\item
Restore the original prior for $\beta_3$ , but reduce the sample size to 1000 in the data generation section.
\item Begin the figure command with \begin{verbatim} subplot(2,2,3) \end{verbatim}
\item Use the same text positioning coordinates as for the very first subplot.
\item Re-run the script.
\end{enumerate}
\ksp
\textbf{PART D}
\ksp
\begin{enumerate}
\item
Save your script as \texttt{ps1q2partD}. Change the log and ``save'' paths accordingly.
\item
Reduce the sample size further to 100 in the data generation section.
\item Begin the figure command with \begin{verbatim} subplot(2,2,4) \end{verbatim}
\item Use the same text positioning coordinates as for the very first subplot.
\item Re-run the script. \\
\end{enumerate}
For each new version, a new subplot should be added to your original figure. Compare these plots and \textbf{\emph{comment on the effect of prior variance and sample size on the posterior}}. Print your figure and add it to your homework. HINT: Use ``File/ Print preview'' from the figure window menu to get some nice formatting options - make sure the legends and added text show nicely in each sub-plot)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\section*{Question 3 (Matlab)}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Consider data set \texttt{CTsales.raw}, which was created in STATA and saved as a delimited text file that is easy to read into Matlab. The data contain 7471 observations on residential home sales along the Connecticut coast between 2002 and 2014.\\
You will use the conjugate normal model to analyse these data. You can use your Gibbs Sampler from the previous question.
\begin{enumerate}
\item
Script \texttt{ps1q3} will get you started - just complete all the ``Your turn'' sections \& adjust the file designations to fit your environment.\\
\item
Comment on the plot of marginal effects - are these effects unambiguously positive, negative, or does the posterior reach to both sides of zero? What are the (approximate) ranges of the posteriors? Are these effects as expected?\\
\item
Look at your log file (you can open it in Word and change your page margins to ``narrow'' so everything fits nicely). Extract the four marginal effects considered in the plot for the OLS results and paste them into an Excel sheet.\\
Now extract the posterior means and the ``p$>0$'' statistics for the same four effects from the Bayesian output, and copy them to the Excel table. Make this small table look nice, print it, and add it to your homework hand-in. \\
\item Comment on these effects - how do the Bayesian posterior means compare to the OLS estimates? Give a reason why they are (or are not) similar.\\
\item Comment on the ``p$>0$'' statistics and relate them to your graphical analysis (plot).
\end{enumerate}
\end{document}