\documentclass[11pt,reqno]{amsart}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{graphicx}
%\usepackage{epstopdf}
\usepackage{hyperref}
\usepackage[left=1in,right=1in,top=0.9in,bottom=0.9in]{geometry}
\usepackage{multirow}
\usepackage{verbatim}
\usepackage{fancyhdr}
\usepackage{harvard}
\usepackage{listings}
%\usepackage[small,compact]{titlesec} 

%\usepackage{pxfonts}
%\usepackage{isomath}
\usepackage{mathpazo}
%\usepackage{arev} %     (Arev/Vera Sans)
%\usepackage{eulervm} %_   (Euler Math)
%\usepackage{fixmath} %  (Computer Modern)
%\usepackage{hvmath} %_   (HV-Math/Helvetica)
%\usepackage{tmmath} %_   (TM-Math/Times)
%\usepackage{cmbright}
%\usepackage{ccfonts} \usepackage[T1]{fontenc}
%\usepackage[garamond]{mathdesign}
\usepackage{color}
\usepackage[normalem]{ulem}

\newtheorem{theorem}{Theorem}[section]
\newtheorem{conjecture}{Conjecture}[section]
\newtheorem{corollary}{Corollary}[section]
\newtheorem{lemma}{Lemma}[section]
\newtheorem{proposition}{Proposition}[section]
\theoremstyle{definition}
\newtheorem{assumption}{}[section]
\renewcommand{\theassumption}{A\arabic{assumption}}
\newtheorem{definition}{Definition}[section]
\newtheorem{step}{Step}[section]
\newtheorem{remark}{Comment}[section]
\newtheorem{example}{Example}[section]
\newtheorem*{example*}{Example}
\newtheorem{mpart}{Part}

\linespread{1.1}

\pagestyle{fancy}
%\renewcommand{\sectionmark}[1]{\markright{#1}{}}
\fancyhead{}
\fancyfoot{} 
%\fancyhead[LE,LO]{\tiny{\thepage}}
\fancyhead[CE,CO]{\tiny{\rightmark}}
\fancyfoot[C]{\small{\thepage}}
\renewcommand{\headrulewidth}{0pt}
\renewcommand{\footrulewidth}{0pt}

\fancypagestyle{plain}{%
\fancyhf{} % clear all header and footer fields
\fancyfoot[C]{\small{\thepage}} % except the center
\renewcommand{\headrulewidth}{0pt}
\renewcommand{\footrulewidth}{0pt}}

\makeatletter
\renewcommand{\@maketitle}{
  \null 
  \begin{center}%
    \rule{\linewidth}{1pt} 
    {\Large \textbf{\textsc{\@title}}} \par
    {\small \textsc{Paul Schrimpf}} \par
    {\small \textsc{\@date}} \par
    {\small \textsc{University of British Columbia}} \par
    {\small \textsc{Economics 628: Topics in econometrics}} \par
    \rule{\linewidth}{1pt} 
  \end{center}%
  \par \vskip 0.9em
}
\makeatother

\newcommand{\argmax}{\operatornamewithlimits{arg\,max}}
\newcommand{\argmin}{\operatornamewithlimits{arg\,min}}
\def\inprobLOW{\rightarrow_p}
\def\inprobHIGH{\,{\buildrel p \over \rightarrow}\,} 
\def\inprob{\,{\inprobHIGH}\,} 
\def\indist{\,{\buildrel d \over \rightarrow}\,} 
\def\F{\mathbb{F}}
\def\R{\mathbb{R}}
\newcommand{\gmatrix}[1]{\begin{pmatrix} {#1}_{11} & \cdots &
    {#1}_{1n} \\ \vdots & \ddots & \vdots \\ {#1}_{m1} & \cdots &
    {#1}_{mn} \end{pmatrix}}
\newcommand{\iprod}[2]{\left\langle {#1} , {#2} \right\rangle}
\newcommand{\norm}[1]{\left\Vert {#1} \right\Vert}
\newcommand{\abs}[1]{\left\vert {#1} \right\vert}
\renewcommand{\det}{\mathrm{det}}
\newcommand{\rank}{\mathrm{rank}}
\newcommand{\spn}{\mathrm{span}}
\newcommand{\row}{\mathrm{Row}}
\newcommand{\col}{\mathrm{Col}}
\renewcommand{\dim}{\mathrm{dim}}
\newcommand{\prefeq}{\succeq}
\newcommand{\pref}{\succ}
\newcommand{\seq}[1]{\{{#1}_n \}_{n=1}^\infty }
\renewcommand{\to}{{\rightarrow}}

\providecommand{\En}{\mathbb{E}_n}
\providecommand{\Er}{{\mathrm{E}}}
\renewcommand{\Pr}{{\mathrm{P}}}
\providecommand{\set}[1]{\left\{#1\right\}}
\providecommand{\plim}{\operatornamewithlimits{plim}}
\newcommand\indep{\protect\mathpalette{\protect\independenT}{\perp}}
\def\independenT#1#2{\mathrel{\setbox0\hbox{$#1#2$}%
    \copy0\kern-\wd0\mkern4mu\box0}} 

\renewcommand{\cite}{\citeasnoun}

\lstset{language=Matlab,                % the language of the code
  basicstyle=\ttfamily,
  keywordstyle=\textcolor{blue},
  commentstyle=\textcolor{red}
}
\title{Homework 5: Quantile Regression}
\date{Due: Monday, November 21st}

\begin{document}

\maketitle

In this assignment, you will analyze Graddy's data on the Fulton fish
market using quantile regression. 
\begin{mpart}\label{ls}
  Download the daily Fulton fish market data in worksheet format from
  \url{http://people.brandeis.edu/~kgraddy/data.html}. 
  You can load the data into Matlab/Octave with 
  \begin{lstlisting}
    data=dlmread('fish.out.txt','\t',2,0);
    dayOfWeek = data(:,1:4); % monday, tuesday, wednesday, thursday
    stormy = data(:,6); % stormy weather
    mixed = data(:,7);  % rough weather
    logp = data(:,8); % log price
    logq = data(:,9); % log quantity sold
    rainy = data(:,10); % rainy weather
    cold = data(:,11); % cold weather
    windspd = data(:,12); % wind speed
    windspd2 = data(:,13); % wind speed squared
    price = data(:,14); % price
    qRecieved = data(:,15); % quantity of fish received from fishermen
    qSold = data(:,16); % quantity of fish sold
  \end{lstlisting}
  Even though this is time series data, you can assume that the
  observations are independent for this assignment. 
  \begin{enumerate}
  \item Estimate a least squares regression of log quantity on log
    price and a constant. Report the coefficient on log price and its
    standard error.  
  \item Estimate a two stage least squares regression of log quantity
    on log price and a constant. Report the coefficient on log price
    and its standard error.
  \end{enumerate}
\end{mpart}

\begin{mpart}\label{qr}
  The problem of minimizing a linear function subject to linear
  constraints,  
  \begin{align*} & \min_{x \in \R^n} c^T x \\
    & \text{ subject to  } \\
    & Ax \leq b 
  \end{align*}
  is called a linear program. Note that the constraints can include
  equality constraints or upper and lower bound constraints on $x$ by
  defining $A$ appropriately. Linear programs have many applications,
  so there are efficient algorithms to compute their
  solutions. Chapter 6 of Koenker (2005) is a good introduction to
  linear programming and how it relates to quantile regression. 

  Recall that quantile regression minimizes the average of the check
  function,
  \begin{align*}
    \min_{\beta \in \R^d} \En \left[\rho_\tau(y_i - x_i \beta )\right].
  \end{align*}
  This can be written in the form of a linear program as follows. Let
  $X$ be the $n \times d$ matrix of regressors, and $Y$ be the $n
  \times 1$ vectors of outcomes. 
  \begin{align*}
    & \min_{(u,v,\beta) \in \R^{2n+d}}  \tau u + (1-\tau) v \\
    & \text{\;\;\; subject to } \\
    & Y - X\beta = u - v \\
    & u \geq 0 \\
    & v \geq 0
  \end{align*}
  or
  \begin{align*}
    & \min_{(u,v,\beta) \in \R^{2n+d}} 
    \begin{pmatrix} 
      \tau \imath_n \\
      (1-\tau)\imath_n \\
      0 \imath_d
    \end{pmatrix}^T 
    \begin{pmatrix} 
      u \\
      v \\
      \beta
    \end{pmatrix} \\
    & \text{\;\;\; subject to } \\
    & \begin{pmatrix} I_n & -I_n & X\\
       -I_n & I_n & -X \\
       -I_n & 0 & 0\\
       0 & -I_n & 0 
    \end{pmatrix}
    \begin{pmatrix} u \\ v\\ \beta \end{pmatrix} 
    \leq 
    \begin{pmatrix} Y \\ Y \\ 0 \imath_n \\ 0 \imath_n 
    \end{pmatrix}    
  \end{align*}
  where $\imath_n$ is a $n \times 1$ vector of ones. The Matlab
  command for solving linear programs is \lstinline!linprog!, and the
  Octave command is \lstinline!glpk!. 
  \begin{enumerate}
  \item Estimate a quantile regression of log quantity on log
    price. Do so for each decile, $\tau = 0.1, 0.2, ... 0.9$. 
  \item Estimate the standard error for your quantile regression
    coefficients. There are many ways to do this, one way is the following:
    \begin{lstlisting}
    % estimate variance
    e = y - x*b;
    % bandwidth set similar to Koenker 2005 p81
    c = numel(y)^(-1/3)*min(tau,1-tau); 
    h = std(e)*(norminv(tau+c)-norminv(tau-c))
    % epanechnikov kernel
    K = @(u) 0.75*(abs(u)<1).*(u.^2-1);
    J = inv(((K(e/h)/h*ones(1,size(x,2))).*x)' *x/n);
    xte = x.*((tau-1*(e>0))*ones(1,size(x,2)));
    Om = xte'*xte/n;
    V = J*Om*J/n;
    \end{lstlisting}
    Another option would be to use bootstrap. However you estimate the
    standard errors, say whether your method is robust to
    misspecification of the conditional quantile function. 
  \item Graph your estimates of the coefficient of log price as a
    function of the quantile.  Include 90\% confidence bands. State
    whether your confidence bands are pointwise or uniform in $\tau$.     
  \end{enumerate}  
\end{mpart}

\begin{mpart}\label{model}
  Suppose that market demand is 
  \[ y^d = q(p,u) \]
  where $q$ is increasing in $u$, and $u \sim U(0,1)$ is an unobserved
  variable capturing the state of demand. Suppose supply is  
  \[ y^s = s(p,z,\epsilon) \]
  for some observed variable $z$ and unobserved variable $\epsilon$.
  Then the equilibrium price solves
  \[ q(p,u) = s(p,z,\epsilon), \]
  so 
  \[ p = \delta(z,v) \]
  for some function $\delta$ with $v = (u,\epsilon)$. 
  
  Assume that $q(p,u) = A(u) p^{\beta(u)}$
  \begin{enumerate}
  \item Does the quantile regression in part \ref{qr} estimate
    $\beta(u)$ consistently?
  \item What does 2SLS from part \ref{ls} estimate in terms of $A(u)$
    and $\beta(u)$?
  \item Under what assumptions can control function quantile
    regression be used to $\log A(u)$ and $\beta(u)$?
  \item Under what assumptions can IV quantile regression be used to
    estimate $\log A(u)$ and $\beta(u)$?
  \end{enumerate}  
\end{mpart}

\begin{mpart}\label{cf}
  In this part, you will estimate $\beta(u)$ and $\log A(u)$ for $u =
  0.1, 0.2, ..., 0.9$ using the control function approach.
  \begin{enumerate}
  \item Regress log price on instruments wind speed, stormy, and
    mixed. Form the estimated residuals $\hat{v}$. 
  \item Perform a quantile regression of log quantity on log price, a
    constant, and a series of $\hat{v}$. For example, 
    \begin{lstlisting}
      Pi = regress(logp,z);
      vhat = logp - z*Pi;
      v = vhat-min(vhat);
      v = v/max(v)*pi;
      xcf = [logp ones(n,1) cos(v*(1:k))];
      b = qregress(logq,xcf,tau(j))
    \end{lstlisting}
    where \lstinline!regress! and \lstinline!qregress! are functions
    that perform regression and quantile regression (you have to
    create these).
  \item Plot the estimated demand curves.     
  \end{enumerate}
\end{mpart}

\begin{mpart}\label{ivqr}
  In this part, you will estimate $\beta(u)$ and $\log A(u)$ for $u =
  0.1, 0.2, ..., 0.9$ using IV quantile regression.
  \begin{enumerate}
  \item For a fine grid of possible values for $\beta(u)$, quantile
    regress log quantity minus $\beta(u)$ times log price on a
    constant and instruments, stormy and mixed. Calculate and save the
    norm of the coefficients of the instruments. Plot the norm of the
    coefficients of the instruments as a function of $\beta(u)$. Set
    $\hat{\beta}(u)$ to the value that minimizes the norm of the
    coefficients of the instruments.
  \item Repeat the previous steps using stormy, mixed, and wind speed
    as instruments. Comment on the results.
  \item Plot $\hat{\beta}(u)$ as function of $u$, along with the
    control function estimates from part \ref{cf} and the plain
    quantile regression estimates from part \ref{qr}
  \end{enumerate}
\end{mpart}

\end{document}

