\input{../header}

\newcommand\eqspace{\hspace{3mm}}
\newcommand\eqvspace{\vspace{1mm}}
\newcommand\negListSpace{\hspace{-4mm}}

\newcommand\ev{u}

\mode<beamer>{\usetheme{AnnArbor}}
\mode<beamer>{\setbeamertemplate{footline}}
\mode<beamer>{\setbeamertemplate{footline}[frame number]}
\mode<beamer>{\setbeamertemplate{frametitle continuation}[from second][\insertcontinuationcountroman]}
\mode<beamer>{\setbeamertemplate{navigation symbols}{}}

\mode<handout>{\pgfpagesuselayout{2 on 1}[letterpaper,border shrink=5mm]}

\newcommand\CHAPTER{8}
% \newcommand\answer[2]{\textcolor{blue}{#2}} % to show answers
% \newcommand\answer[2]{\textcolor{red}{#2}} % to show answers
 \newcommand\answer[2]{#1} % to show blank space
\usepackage{bbm} % for blackboard bold 1

\title{\vspace{2mm} \link{https://ionides.github.io/531w22/}{Modeling and Analysis of Time Series Data}\\ \vspace{2mm}
Chapter \CHAPTER: Smoothing in the time and frequency domains}
\author{Edward L. Ionides}
\date{}

\setbeamertemplate{footline}[frame number]

\begin{document}

\maketitle

\mode<article>{\tableofcontents}

\mode<presentation>{
  \begin{frame}{Outline}
    \tableofcontents
  \end{frame}
}

\section{Smoothing a time series}
%\subsection{}

\begin{frame}{Introduction to smoothing in time series analysis}

\bi
\item   Estimating a nonparametric trend from a time series is known as smoothing. We will review some standard smoothing methods.

\item We also smooth the periodogram to estimate a spectral density.

\item Smoothers have convenient interpretation in the frequency domain. A smoother typically shrinks high frequency components and preserves low frequency components.

\ei

\end{frame}

\begin{frame}[fragile]

\frametitle{A motivating example}

\mode<presentation>{
\vspace{-2mm}
}

\bi
\item The economy fluctuates between periods of rapid expansion and periods of slower growth or contraction.

\item High unemployment is one of the most visible signs of a dysfunctional economy, in which labor is under-utilized, leading to hardships for many individuals and communities.

\item Economists, politicians, businesspeople and the general public therefore have an interest in understanding fluctuations in unemployment.

\item Economists try to distinguish between fundamental structural changes in the economy and the shorter-term cyclical booms and busts that appear to be a natural part of capitalist business activity.

\item Monthly US unemployment figures are published by the \link{https://data.bls.gov/timeseries/LNU04000000}{Bureau of Labor Statistics (BLS)}.

\item Measuring unemployment has subtleties, but these are not our immediate focus.
\ei

\end{frame}
\begin{frame}[fragile]

\vspace{-3mm}

\begin{knitrout}\small
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlkwd{system}\hlstd{(}\hlstr{"head unadjusted_unemployment.csv"}\hlstd{,}\hlkwc{intern}\hlstd{=}\hlnum{TRUE}\hlstd{)}
\end{alltt}
\end{kframe}
\end{knitrout}

\vspace{-3mm}

\begin{knitrout}\small
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{verbatim}
 [1] "# Data extracted on: February 3, 2022 (12:29:30 PM)"
 [2] "# from http://data.bls.gov/timeseries/LNU04000000"
 [3] "# Labor Force Statistics from the Current Population Survey"
 [4] "# Not Seasonally Adjusted"
 [5] "# Series title:        (Unadj) Unemployment Rate"
 [6] "# Series Id:           LNU04000000"
 [7] "# Labor force status:  Unemployment rate"
 [8] "# Type of data:        Percent or rate"
 [9] "# Age:                 16 years and over"
[10] "Year,Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec"
\end{verbatim}
\end{kframe}
\end{knitrout}

\vspace{-3mm}

\begin{knitrout}\small
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{U1} \hlkwb{<-} \hlkwd{read.table}\hlstd{(}\hlkwc{file}\hlstd{=}\hlstr{"unadjusted_unemployment.csv"}\hlstd{,}
  \hlkwc{sep}\hlstd{=}\hlstr{","}\hlstd{,}\hlkwc{header}\hlstd{=}\hlnum{TRUE}\hlstd{)}
\hlkwd{head}\hlstd{(U1,}\hlnum{3}\hlstd{)}
\end{alltt}
\end{kframe}
\end{knitrout}

\vspace{-3mm}

\begin{knitrout}\small
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{verbatim}
 Year Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
 1948 4.0 4.7 4.5 4.0 3.4 3.9 3.9 3.6 3.4 2.9 3.3 3.6
 1949 5.0 5.8 5.6 5.4 5.7 6.4 7.0 6.3 5.9 6.1 5.7 6.0
 1950 7.6 7.9 7.1 6.0 5.3 5.6 5.3 4.1 4.0 3.3 3.8 3.9
\end{verbatim}
\end{kframe}
\end{knitrout}

\end{frame}

\begin{frame}[fragile]

\vspace{-2mm}

\myquestion. A coding exercise: Explain how the tabulated data in \code{U1} are converted to a time series, below.

\answer{\vspace{15mm}}{todo}

\begin{knitrout}\small
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{u1} \hlkwb{<-} \hlkwd{t}\hlstd{(}\hlkwd{as.matrix}\hlstd{(U1[}\hlnum{2}\hlopt{:}\hlnum{13}\hlstd{]))}
\hlkwd{dim}\hlstd{(u1)} \hlkwb{<-} \hlkwa{NULL}
\hlstd{date} \hlkwb{<-} \hlkwd{seq}\hlstd{(}\hlkwc{from}\hlstd{=}\hlnum{1948}\hlstd{,}\hlkwc{length}\hlstd{=}\hlkwd{length}\hlstd{(u1),}\hlkwc{by}\hlstd{=}\hlnum{1}\hlopt{/}\hlnum{12}\hlstd{)}
\hlkwd{plot}\hlstd{(date,u1,}\hlkwc{type}\hlstd{=}\hlstr{"l"}\hlstd{,}\hlkwc{ylab}\hlstd{=}\hlstr{"Unemployment rate (unadjusted)"}\hlstd{)}
\end{alltt}
\end{kframe}
\end{knitrout}

\vspace{-3mm}

\begin{knitrout}\small
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}

{\centering \includegraphics[width=9cm]{tmp/figure/reshape-1}

}

\end{knitrout}

\end{frame}

\begin{frame}[fragile]

\vspace{-3mm}

\bi
\item We see seasonal variation and economic cycles on top of a trend.

\item The seasonal variation looks like an additive effect, say an annual fluctation with amplitude around 1 percentage point.

\item Sometimes, we may prefer to look at monthly seasonally adjusted unemployment, \link{https://data.bls.gov/timeseries/LNS14000000}{also provided by BLS}.
\ei

\vspace{-2mm}

\begin{knitrout}\small
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{U2} \hlkwb{<-} \hlkwd{read.table}\hlstd{(}\hlkwc{file}\hlstd{=}\hlstr{"adjusted_unemployment.csv"}\hlstd{,}\hlkwc{sep}\hlstd{=}\hlstr{","}\hlstd{,}
  \hlkwc{header}\hlstd{=}\hlnum{TRUE}\hlstd{)}
\hlstd{u2} \hlkwb{<-} \hlkwd{t}\hlstd{(}\hlkwd{as.matrix}\hlstd{(U2[}\hlnum{2}\hlopt{:}\hlnum{13}\hlstd{]))}
\end{alltt}
\end{kframe}
\end{knitrout}

\vspace{-4mm}

\begin{knitrout}\small
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}

{\centering \includegraphics[width=9cm]{tmp/figure/data_adj_code-1}

}

\end{knitrout}

\vspace{-2mm}

\bi
\item
We can wonder how the BLS adjusts the data, and if this introduces any artifacts that a careful statistician should be aware of.
\ei
\end{frame}

\section{Seasonal adjustment in the frequency domain}

\begin{frame}[fragile]

\vspace{-2mm}

\bi

\item To help understand the seasonal adjustment, we look at what it does to the smoothed periodogram.

\item Using the \code{ts} class we can tell R the units of time.

\ei

\vspace{-1mm}

\begin{knitrout}\small
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{u1_ts} \hlkwb{<-} \hlkwd{ts}\hlstd{(u1,}\hlkwc{start}\hlstd{=}\hlnum{1948}\hlstd{,}\hlkwc{frequency}\hlstd{=}\hlnum{12}\hlstd{)}
\hlstd{u2_ts} \hlkwb{<-} \hlkwd{ts}\hlstd{(u2,}\hlkwc{start}\hlstd{=}\hlnum{1948}\hlstd{,}\hlkwc{frequency}\hlstd{=}\hlnum{12}\hlstd{)}
\hlkwd{spectrum}\hlstd{(}\hlkwd{ts.union}\hlstd{(u1_ts,u2_ts),}\hlkwc{spans}\hlstd{=}\hlkwd{c}\hlstd{(}\hlnum{3}\hlstd{,}\hlnum{5}\hlstd{,}\hlnum{3}\hlstd{),}
  \hlkwc{main}\hlstd{=}\hlstr{"Unemployment: raw (black), seasonally adjusted (red)"}\hlstd{)}
\end{alltt}
\end{kframe}
\end{knitrout}

\vspace{-3mm}

\begin{knitrout}\small
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}

{\centering \includegraphics[width=11cm]{tmp/figure/adjustment_spectrum-1}

}

\end{knitrout}

\end{frame}

\begin{frame}{Comments on the smoothed periodogram}

\bi
\item Note: For a report, we should add units to plots. Extra details (like \code{bandwith} in the periodogram plot) should be explained or removed.
\ei

\vspace{1mm}

\myquestion. Why does the unadjusted spectrum have peaks at 2,3,4,5,6 cycles per year as well as 1 cycle per year?

\answer{\vspace{20mm}}{todo}

\myquestion. Comment on what you learn from comparing these smoothed periodograms.

\answer{\vspace{30mm}}{todo}

\end{frame}

\subsection{The frequency response function of a smoother}

\begin{frame}{The frequency response function}

\vspace{-2mm}

\bi
\item The ratio of the periodograms of the smoothed and unsmoothed time series is the \myemph{frequency response} of the smoother.

\item The frequency response function tells us how much the smoother contracts (or inflates) the sine and cosine components at each frequency $\omega$.

\item A frequency response may involve change in phase as well as magnitude, but here we consider only magnitude.

\item Linear, time invariant transformations do not move power between frequencies, so they are characterized by their frequency response function.

\item Smoothers are linear and time invariant, at least approximately. If we scale or shift the data, we expect the smoothed estimate to have the same scale or shift. We expect a smooth approximation to the sum of two time series to be approximately the sum of the two smoothed series.

\ei

\end{frame}

\begin{frame}[fragile]{Calculating a frequency response function}

\mode<presentation>{
\vspace{-2mm}
}

\bi
\item We investigate the frequency response of the smoother used by Bureau of Labor Statistics to deseasonalize the unemployment data.
\ei

\begin{knitrout}\small
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{s} \hlkwb{<-} \hlkwd{spectrum}\hlstd{(}\hlkwd{ts.union}\hlstd{(u1_ts,u2_ts),}\hlkwc{plot}\hlstd{=}\hlnum{FALSE}\hlstd{)}
\end{alltt}
\end{kframe}
\end{knitrout}

\bi
\item We find the parts of \code{s} that we need to plot the frequency response.
\ei

\begin{knitrout}\small
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlkwd{names}\hlstd{(s)}
\end{alltt}
\end{kframe}
\end{knitrout}

\mode<presentation>{
\vspace{-3mm}
}

\begin{knitrout}\small
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{verbatim}
 [1] "freq"      "spec"      "coh"       "phase"     "kernel"
 [6] "df"        "bandwidth" "n.used"    "orig.n"    "series"
[11] "snames"    "method"    "taper"     "pad"       "detrend"
[16] "demean"
\end{verbatim}
\end{kframe}
\end{knitrout}

\mode<presentation>{
\vspace{-3mm}
}

\begin{knitrout}\small
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlkwd{plot}\hlstd{(s}\hlopt{$}\hlstd{freq,s}\hlopt{$}\hlstd{spec[,}\hlnum{2}\hlstd{]}\hlopt{/}\hlstd{s}\hlopt{$}\hlstd{spec[,}\hlnum{1}\hlstd{],}\hlkwc{type}\hlstd{=}\hlstr{"l"}\hlstd{,}\hlkwc{log}\hlstd{=}\hlstr{"y"}\hlstd{,}
  \hlkwc{ylab}\hlstd{=}\hlstr{"frequency ratio"}\hlstd{,} \hlkwc{xlab}\hlstd{=}\hlstr{"frequency"}\hlstd{,}
  \hlkwc{main}\hlstd{=}\hlstr{"frequency response (red lines at 0.8 and 1.2)"}\hlstd{)}
\hlkwd{abline}\hlstd{(}\hlkwc{h}\hlstd{=}\hlkwd{c}\hlstd{(}\hlnum{0.8}\hlstd{,}\hlnum{1.2}\hlstd{),}\hlkwc{col}\hlstd{=}\hlstr{"red"}\hlstd{)}
\end{alltt}
\end{kframe}
\end{knitrout}

\end{frame}

\begin{frame}[fragile]

\begin{knitrout}\small
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}

{\centering \includegraphics[width=10cm]{tmp/figure/s_transfer-1}

}

\end{knitrout}

\vspace{-3mm}

\myquestion. What do you learn from this frequency response plot?

\answer{\vspace{20mm}}{todo}

\end{frame}

\section{Loess smoothing}

\begin{frame}[fragile]

\frametitle{Estimating trend by Loess smoothing}

\bi

\item Loess is a \myemph{Local linear regression} approach (perhaps an acronym for LOcal Estimation by Smoothing) also known as \myemph{Lowess} (perhaps LOcallyWEighted Sum of Squares).

\item At each point in time, Loess computes a linear regression (a constant, linear or quadratic trend estimate) using only neighboring times.

\item We can imagine a moving window of points included in the regression.

\item \code{loess} is an R implementation, with the fraction of points included in the moving window being scaled by the \code{span} argument.

\item We can choose a value of the span that visually separates long term trend from business cycle.

\ei

\end{frame}

\begin{frame}[fragile]{A Loess smooth of unemployment}

\begin{knitrout}\small
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{u1_loess} \hlkwb{<-} \hlkwd{loess}\hlstd{(u1}\hlopt{~}\hlstd{date,}\hlkwc{span}\hlstd{=}\hlnum{0.5}\hlstd{)}
\hlkwd{plot}\hlstd{(date,u1,}\hlkwc{type}\hlstd{=}\hlstr{"l"}\hlstd{,}\hlkwc{col}\hlstd{=}\hlstr{"red"}\hlstd{)}
\hlkwd{lines}\hlstd{(u1_loess}\hlopt{$}\hlstd{x,u1_loess}\hlopt{$}\hlstd{fitted,}\hlkwc{type}\hlstd{=}\hlstr{"l"}\hlstd{)}
\end{alltt}
\end{kframe}
\end{knitrout}

\begin{knitrout}\small
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}

{\centering \includegraphics[width=11cm]{tmp/figure/loess-1}

}

\end{knitrout}
\end{frame}

\begin{frame}[fragile]

Now, we compute the frequency response function for what we have done.

\mode<presentation>{
\vspace{-2mm}
}

\begin{knitrout}\small
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{s2} \hlkwb{<-} \hlkwd{spectrum}\hlstd{(}\hlkwd{ts.union}\hlstd{(}
  \hlstd{u1_ts,}\hlkwd{ts}\hlstd{(u1_loess}\hlopt{$}\hlstd{fitted,}\hlkwc{start}\hlstd{=}\hlnum{1948}\hlstd{,}\hlkwc{frequency}\hlstd{=}\hlnum{12}\hlstd{)),}
  \hlkwc{plot}\hlstd{=}\hlnum{FALSE}\hlstd{)}
\hlkwd{plot}\hlstd{(s2}\hlopt{$}\hlstd{freq,s2}\hlopt{$}\hlstd{spec[,}\hlnum{2}\hlstd{]}\hlopt{/}\hlstd{s}\hlopt{$}\hlstd{spec[,}\hlnum{1}\hlstd{],}\hlkwc{type}\hlstd{=}\hlstr{"l"}\hlstd{,}\hlkwc{log}\hlstd{=}\hlstr{"y"}\hlstd{,}
  \hlkwc{ylab}\hlstd{=}\hlstr{"frequency ratio"}\hlstd{,} \hlkwc{xlab}\hlstd{=}\hlstr{"frequency"}\hlstd{,} \hlkwc{xlim}\hlstd{=}\hlkwd{c}\hlstd{(}\hlnum{0}\hlstd{,}\hlnum{1.5}\hlstd{),}
  \hlkwc{main}\hlstd{=}\hlstr{"frequency response (red line at 1.0)"}\hlstd{)}
\hlkwd{abline}\hlstd{(}\hlkwc{h}\hlstd{=}\hlnum{1}\hlstd{,}\hlkwc{lty}\hlstd{=}\hlstr{"dashed"}\hlstd{,}\hlkwc{col}\hlstd{=}\hlstr{"red"}\hlstd{)}
\end{alltt}
\end{kframe}
\end{knitrout}

\end{frame}

\begin{frame}[fragile]

\vspace{-2mm}

\begin{knitrout}\small
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}

{\centering \includegraphics[width=10cm]{tmp/figure/loess_transfer-1}

}

\end{knitrout}

\myquestion. Describe the frequency domain behavior of this filter.

\answer{\vspace{50mm}}{todo}

\end{frame}

\begin{frame}[fragile]

\frametitle{Extracting business cycles: A band pass filter}

\bi

\item For the unemployment data, high frequency variation might be considered ``noise'' and low frequency variation might be considered trend.

\item A band of mid-range frequencies might be considered to correspond to the business cycle.

\item We build a smoothing operation in the time domain to extract business cycles, and then look at its frequency response function.
\ei

\begin{knitrout}\small
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{u_low} \hlkwb{<-} \hlkwd{ts}\hlstd{(}\hlkwd{loess}\hlstd{(u1}\hlopt{~}\hlstd{date,}\hlkwc{span}\hlstd{=}\hlnum{0.5}\hlstd{)}\hlopt{$}\hlstd{fitted,}
  \hlkwc{start}\hlstd{=}\hlnum{1948}\hlstd{,}\hlkwc{frequency}\hlstd{=}\hlnum{12}\hlstd{)}
\hlstd{u_hi} \hlkwb{<-} \hlkwd{ts}\hlstd{(u1} \hlopt{-} \hlkwd{loess}\hlstd{(u1}\hlopt{~}\hlstd{date,}\hlkwc{span}\hlstd{=}\hlnum{0.1}\hlstd{)}\hlopt{$}\hlstd{fitted,}
  \hlkwc{start}\hlstd{=}\hlnum{1948}\hlstd{,}\hlkwc{frequency}\hlstd{=}\hlnum{12}\hlstd{)}
\hlstd{u_cycles} \hlkwb{<-} \hlstd{u1} \hlopt{-} \hlstd{u_hi} \hlopt{-} \hlstd{u_low}
\hlkwd{plot}\hlstd{(}\hlkwd{ts.union}\hlstd{(u1, u_low,u_hi,u_cycles),}
  \hlkwc{main}\hlstd{=}\hlstr{"Decomposition of unemployment as trend + noise + cycles"}\hlstd{)}
\end{alltt}
\end{kframe}
\end{knitrout}

\vspace{10mm}

\end{frame}

\subsection{Business cycles in detrended economic data}

\begin{frame}[fragile]

\begin{knitrout}\small
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}

{\centering \includegraphics[width=11cm]{tmp/figure/cycles-1}

}

\end{knitrout}

\end{frame}

\begin{frame}[fragile]

\begin{knitrout}\small
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}
\begin{tabular}{l|r|r}
\hline
  & low & hi\\
\hline
frequency range, region for ratio greater than 0.5 & 0.048 & 0.211\\
\hline
\end{tabular}

\end{knitrout}

\begin{knitrout}\small
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}

{\centering \includegraphics[width=10cm]{tmp/figure/show_range-1}

}

\end{knitrout}

\myquestion. Describe the frequencies (and corresponding periods) that this decomposition identifies as business cycles. Note: units of frequency are omitted to give you an exercise!

\answer{\vspace{15mm}}{todo}

\end{frame}

\begin{frame}[fragile]

\mode<presentation>{
\vspace{-2mm}
}

Below is a smoothed periodogram for the raw unemployment data, plotted up to 0.7 cycles per year to focus on relevant frequencies.

\begin{knitrout}\small
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}

{\centering \includegraphics[width=9cm]{tmp/figure/zoomed_spectrum-1}

}

\end{knitrout}

\vspace{-1mm}

\myquestion. Comment on the evidence for and against the concept of a business cycle in the above figure.

\answer{\vspace{30mm}}{todo}

\end{frame}

\begin{frame}[fragile]

\frametitle{Common smoothers in R}

\bi

\item Above, we have used the \myemph{local regression smoother} \code{loess} but there are other similar options.

\item \code{ksmooth} is a \myemph{kernel smoother}. The default periodogram smoother in \code{spectrum} is also a kernel smoother. See \url{https://en.wikipedia.org/wiki/Kernel_smoother}

\item \code{smooth.spline} is a \myemph{spline smoother}.
\url{https://en.wikipedia.org/wiki/Smoothing_spline}

\item You can learn about alternative smoothers, and try them out if you like, but \code{loess} is a good practical choice for many smoothing applications.

\ei
\end{frame}

\begin{frame}[fragile]
\frametitle{Bandwidth for a smoother}
\bi
\item All these smoothers have some concept of a \myemph{bandwidth}, which is a measure of the size of the neighborhood of time points in which data affect the smoothed value at a particular time point.

\item The concept of bandwidth is most obvious for kernel smoothers, but exists for other smoothers.

\item We usually only interpret bandwidth up to a constant. For a particular smoothing algorithm and software implementation, you learn by experience to interpret the comparative value. Smaller bandwidth means less smoothing.

\item Typically, when writing reports, it makes sense to focus on the tuning parameter for the smoother in question, which is not the bandwidth unless you are doing kernel smoothing.

\ei
\end{frame}

\begin{frame}{Further reading}

\bi

\item Section~2.3 of \citet{shumway17} discusses smoothing of time series, in the time domain.

\item Section~4.2 of \citet{shumway17} presents a frequency response function for linear filters, related to this chapter but in a different context.

\ei

\end{frame}

\newcommand\acknowledgments{
\begin{itemize}
\item   Compiled on {\today} using \Rlanguage version 4.1.2.
\item   \parbox[t]{0.75\textwidth}{Licensed under the \link{http://creativecommons.org/licenses/by-nc/4.0/}{Creative Commons Attribution-NonCommercial license}.
    Please share and remix non-commercially, mentioning its origin.}
    \parbox[c]{1.5cm}{\includegraphics[height=12pt]{../cc-by-nc}}
\item We acknowledge \link{https://ionides.github.io/531w22/acknowledge.html}{previous versions of this course}.
\end{itemize}
}

\mode<presentation>{
\begin{frame}[allowframebreaks=0.8]{References and Acknowledgements}

\bibliography{../bib531}

\vspace{3mm}

\acknowledgments

\end{frame}
}

\mode<article>{

{\bf \Large \noindent Acknowledgments}

\acknowledgments

  \bibliography{../bib531}

}

\end{document}


\section{Discussion}\label{sec:4}
This paper presents an \oos\ test statistic similar to \poscw\ that is
asymptotically normal when comparing nested or non-nested models.
Normality is achieved by estimating the alternative model using a
fixed-length rolling window --- as do Clark and West --- but
estimating the benchmark model with a recursive window.  Simulations
indicate that the new statistic behaves similarly to Clark and West's
original test when the \dgp\ is stable but can have much higher power
when the \dgp\ has structural breaks. We also have presented an
empirical study of the equity premium that demonstrates how to use
these results with several alternative models.

%%% Local Variables:
%%% mode: latex
%%% TeX-master: "mixedwindow"
%%% TeX-command-extra-options: "-shell-escape"
%%% End:


In this article we explore the connection between epistemic logic and game theory. In games of imperfect information, this connection arises naturally: The players need to reason about the knowledge of their teammates and opponents and epistemic logic seems like a natural choice.

However, even in games of perfect information, there is also a connection to epistemic logic: In game theory, the standard assumption is that all players are rational and -- more crucially -- that this is \emph{common knowledge} among all players. Is this assumption valid and can it be formalized? What are the implications of this assumption? Specifically, does backward induction actually follow? These are questions of epistemic nature and thus, they should be discussed using the terminology of epistemic logic.

\subsubsection*{Overview of this article}

The structure of this article is as follows: In \secref{sec:imperfect-information}, we discuss games of imperfect information and in \secref{sec:rationality} we consider the questions regarding common knowledge of rationality. Also, we define possible worlds models and epistemic logic as in \cite{fagin1995a}.


\documentclass[conference]{IEEEtran}

\usepackage{amsmath,amssymb}
\usepackage[all]{xy}

\newcommand{\op}[1]{{\textsc{#1}}}
\newcommand{\DB}{\mathbf{DB}}
\newcommand{\DOC}{\mathbf{DOC}}
\newcommand{\attr}{\mathrm{attr}}
\newcommand{\textattr}{\mathrm{attr}_\mathrm{text}}
\newcommand{\Val}{\mathcal{V}}
\newcommand{\dom}{\mathrm{dom}}
\newcommand{\Tables}{\mathrm{tables}}
\newcommand{\Words}{\textsc{Words}}
\newcommand{\todo}[1]{{\em TODO: #1}}

\begin{document}
\title{Using Document Space For Relational Search}
\author{
    \IEEEauthorblockN{Richard Drake}
    \IEEEauthorblockA{
        Faculty of Science\\
        University of Ontario Inst of Technology
    }
    \and
    \IEEEauthorblockN{Ken Pu}
    \IEEEauthorblockA{
        Faculty of Science\\
        University of Ontario Inst of Technology
    }
}
\maketitle

\begin{abstract}
In this paper, we present a family of methods and algorithms to efficiently integrate text indexing and keyword search from information retrieval to support search in relational databases.  We propose a bi-directional transformation that maps relational database instances to document collections.  The transformation is shown to be a homomorphism of keyword search.  Thus, any search of tuple networks by a keyword query can be efficiently executed as a search for documents, and vice versa.  By this construction, we demonstrate that indexing and search technologies developed for documents can naturally be reduced and integrated into relational database systems.
\end{abstract}

\section{Motivation}

Information retrieval has been an active and fruitful field of research since 1960's.  With
seminal work by \cite{jones72} and \cite{salton75}, the
IR community has laid the foundation of automatic text indexing and
keyword query processing of text documents.  The technology for document
indexing continues to gain momentum with the growing presence of text data found
on the Web and in social media.  For instance, new techniques by
\cite{janu12} and \cite{goyal13} improve on the traditional
similarity measures by incorporating further (NLP) on the context of phrases and words.

In the last decade, there has been a tremendous interest from the database community to support keyword search queries for structured relational databases.  Systems such as {\em Discover} \cite{hris02}, {\em DBxplorer} \cite{agrawal2002dbxplorer} and {\em BANKS} \cite{bhalotia2002keyword} and many others \cite{hristidis2003efficient,liu2006effective} model relational tuples as documents, and foreign key joins as links.  Thus, it's possible to derive IR-style scoring function for document similarity.  More recently, semantic information \cite{zeng2013semantic}, schema and meta data \cite{bergamaschi2011keyword} have been incorporated into the search algorithm.

We are motivated to explore the possibility of a unified framework to integrate and reuse indexing and search algorithms from IR system and databases.  In particular, our interest is to construct a pair of transformations:

\begin{eqnarray*}
h &:& \mathbf{DB} \to \mathbf{DOC} \\
g &:& \mathbf{DOC} \to \mathbf{DB}
\end{eqnarray*}
where $\mathbf{DB}$ is the domain of databases and $\mathbf{DOC}$ is the domain of collections of documents.

We want to design $h$ and $g$ satisfying the following:

\begin{displaymath}
\xymatrix {
    \mathbf{DB} \ar[r]^h\ar[d]_{\op{search}_\mathbf{DB}}
        & \mathbf{DOC}\ar[d]^{\op{search}_\mathbf{DOC}} \\
    \mathbf{DB} & \mathbf{DOC}\ar[l]^g
}
\end{displaymath}

where $\op{search}_\DB$ and $\op{search}_\DOC$ are the search functions for relational databases and documents respectively.

Practical experiences have demonstrated that the state-of-the-art $\op{search}_\DOC$ has more performant implementations (\cite{xapian,lucene}) compared to its relational database counter part.  Our interest is to construct efficient transformations $h$ and $g$ such that relational search $\op{search}_\DB$ can be efficiently implemented as a composition of $g\circ \op{search}_\DOC\circ h$, effectively taking advantage of the document search technology.

\section{Problem Definition}

In this section, we present the formal definition of relational databases and collections of documents.  We also formalize the notion of keyword search entity graphs and join networks of entity graphs in relational databases.

\subsection{Relational entities}

A relational database consists of a collection of tables which are interconnected via joinable linkages.

A table, $T$, has a number of attributes:

$$ \attr(T) $$

A tuple in a table $r\in T$ is defined as a mapping from attributes to values:

$$ r:\attr(T)\to\Val\quad \mbox{such that } r(\alpha)\in\dom(\alpha) $$

Given two tables $T$ and $T'$, a joinable linkage $L=\left<A,B\right>$, is defined as two lists of attributes of equal length.

$$A = \left<\alpha_1, \alpha_2, \dots, \alpha_n\right>$$
$$B = \left<\beta_1, \beta_2, \dots, \beta_n\right>$$

such that $\alpha_i\in\attr(T)$ and $\beta_j\in\attr(T')$.  Furthermore,

$$ \forall\ i\leq n,\quad \dom(\alpha_i) = \dom(\beta_i)$$

The set of all linkages are denoted as $\mathcal{L}$.

Two tables $T$ and $T'$ are joinable by some link $L=\left<A,B\right>$, written $T\sim_L T'$ if

$$A\subseteq\attr(T)\mbox{ and } B\subseteq\attr(T')$$

Two tuples $r\in T$ and $r'\in T'$ are joinable by some link $L=\left<A,B\right>$,
written $r\sim_L r'$ if

$$ T\sim_L T'\quad\mbox{and}\quad r[A] = r[B] $$

\textbf{Entity groups: schema and instances}

An {entity group schema}, $G$, is characterized by a {\em forest} of tables connected by joinable links.

$$G = (V_G, E_G)$$

where $V_G \subseteq \Tables(\DB)$, and $E_G\subseteq\mathcal{L}$.

A $G$-entity instance, $g$, is a forest of tuples

$$\{r_T : T\in V_G\} \mbox{ where } r_T\in T$$

connected by edges:

$$\left<r_T, r_{T'}\right> \mbox{ where } T\sim T' \mbox{ wrt } E_G$$

{\bf Networks of entity group instances}

Two entity group instances, $g$ and $g'$, are {\em connected} if
$$\Tables(g)\cap\Tables(g')\not=\emptyset$$
and for some $T\in\Tables(g)\cap\Tables(g')$, we have:

$$\exists\alpha\in\attr(T), g_T(\alpha) = g'_T(\alpha)$$

\subsection{Keyword search queries}

A {\em keyword query} is a bag of words.

$$ Q = \{w_1, w_2, \dots, w_n\} $$

A tuple $r\in T$ in some table $T$ satisfies the keyword query if

$$\exists \alpha\in\textattr(T),\ \Words(r(\alpha))\cap Q\not=\empty$$

\bibliographystyle{IEEEtran}
\bibliography{references}
\end{document}


%
% $Id$
%
\label{sec:rel}
All methods which include treatment of relativistic effects are ultimately
based on the Dirac equation, which has a four component wave function. The
solutions to the Dirac equation describe both positrons (the ``negative
energy'' states) and electrons (the ``positive energy'' states), as well as
both spin orientations, hence the four components. The wave function may be
broken down into two-component functions traditionally known as the large
and small components; these may further be broken down into the spin
components.

The implementation of approximate all-electron relativistic methods in
quantum chemical codes requires the removal of the negative energy states
and the factoring out of the spin-free terms. Both of these may be achieved
using a transformation of the Dirac Hamiltonian known in general as a
Foldy-Wouthuysen transformation. Unfortunately this transformation cannot be
represented in closed form for a general potential, and must be
approximated.  One popular approach is that originally formulated by Douglas
and Kroll\footnote{M.~Douglas and N.~M.~Kroll, Ann. Phys. (N.Y.)  {\bf 82},
89 (1974)} and developed by Hess\footnote{B.A.~Hess, Phys.~Rev.~A~{\bf 32},
756 (1985); {\bf 33}, 3742 (1986)}. This approach decouples the positive and
negative energy parts to second order in the external potential (and also
fourth order in the fine structure constant, $\alpha$). Other approaches include
the Zeroth Order Regular Approximation (ZORA)\footnote{C.~Chang, M.~Pelissier,
M.~Durand, Physica Scripta ~{\bf 34}, 294 (1986); E.~van Lenthe, ~{\it The ZORA Equation},
doctoral thesis, Vrije Universiteit, Amsterdam (1996); S.~Faas, J.G.~Snijders,
J.H.~van Lenthe, E.~van Lenthe, and E.J.~Baerends, Chem.~Phys.~ Lett.~{\bf 246}, 632 (1995).}
and modification of the Dirac equation by Dyall\footnote{K.~G.~Dyall,
J.~Chem.~Phys.~{\bf 100}, 2118 (1994)}, and involves an exact FW
transformation on the atomic basis set level\footnote{K.~G.~Dyall,
J.~Chem.~Phys.~{\bf 106}, 9618 (1997); K.~G.~Dyall and T.~Enevoldsen,
J.~Chem.~Phys.~{\bf 111}, 10000 (1999).}.

Since these approximations only modify the integrals, they can in principle
be used at all levels of theory. At present the Douglas-Kroll and ZORA
implementations can be used at all levels of theory whereas
Dyall's approach is currently available at the Hartree-Fock level.
The derivatives have been implemented, allowing both methods to be used in
geometry optimizations and frequency calculations.

The \verb+RELATIVISTIC+ directive provides input for the implemented relativistic
approximations and is a compound directive that encloses additional directives
specific to the approximations:
\begin{verbatim}
  RELATIVISTIC
   [DOUGLAS-KROLL [<string (ON||OFF) default ON> \
                 <string (FPP||DKH||DKFULL||DK3||DK3FULL) default DKH>]  ||
    ZORA [ (ON || OFF) default ON ] ||
    DYALL-MOD-DIRAC [ (ON || OFF) default ON ]
                  [ (NESC1E || NESC2E) default NESC1E ] ]
   [CLIGHT <real clight default 137.0359895>]
  END
\end{verbatim}

Only one of the methods may be chosen at a time.  If both methods are found
to be on in the input block, NWChem will stop and print an error message.
There is one general option for both methods, the definition of the speed
of light in atomic units:

\begin{verbatim}
  CLIGHT <real clight default 137.0359895>
\end{verbatim}

The following sections describe the optional sub-directives that
can be specified within the \verb+RELATIVISTIC+ block.

\section{Douglas-Kroll approximation}
\label{sec:douglas-kroll}

The spin-free and spin-orbit one-electron Douglas-Kroll
approximation have been implemented. The use of relativistic effects
from this Douglas-Kroll approximation can be invoked by specifying:

\begin{verbatim}
  DOUGLAS-KROLL [<string (ON||OFF) default ON> \
                 <string (FPP||DKH||DKFULL|DK3|DK3FULL) default DKH>]
\end{verbatim}

The \verb+ON|OFF+ string is used to turn on or off the
Douglas-Kroll approximation.  By default, if the \verb+DOUGLAS-KROLL+
keyword is found, the approximation will be used in the calculation.
If the user wishes to calculate a non-relativistic quantity after turning
on Douglas-Kroll, the user will need to define a new \verb+RELATIVISTIC+
block and turn the approximation \verb+OFF+.  The user could also simply
put a blank \verb+RELATIVISTIC+ block in the input file and all options
will be turned off.

The \verb+FPP+ is the approximation based on free-particle projection
operators\footnote{B.A.~Hess, Phys.~Rev.~A~{\bf 32}, 756 (1985)} whereas the
\verb+DKH+ and \verb+DKFULL+ approximations are based on external-field
projection operators\footnote{B.A.~Hess, Phys.~Rev.~A~{\bf 33}, 3742 (1986)}.
The latter two are considerably better approximations than the former. \verb+DKH+
is the Douglas-Kroll-Hess approach and is the approach that is generally
implemented in quantum chemistry codes. \verb+DKFULL+ includes certain
cross-product integral terms ignored in the \verb+DKH+ approach (see for example
H\"{a}berlen and R\"{o}sch\footnote{O.D.~H\"{a}berlen, N.~R\"{o}sch,
Chem.~Phys.~Lett.~{\bf 199}, 491 (1992)}). The third-order Douglas-Kroll
approximation has been implemented by T. Nakajima and K. Hirao\footnote{T. Nakajima
and K. Hirao, Chem.~Phys.~Lett.~{\bf 329}, 5111 (2000); T. Nakajima and K. Hirao,
J.~Chem.~Phys.~{\bf 113}, 7786 (2000)}. This approximation can be called using
\verb+DK3+ (DK3 without cross-product integral terms) or \verb+DK3FULL+ (DK3 with
cross-product integral terms).

The contracted basis sets used in the calculations should reflect the relativistic
effects, i.e. one should use contracted basis sets which were generated using the
Douglas-Kroll Hamiltonian. Basis sets that were contracted using the
non-relativistic (Sch\"{o}dinger) Hamiltonian WILL PRODUCE ERRONEOUS RESULTS for
elements beyond the first row. See appendix \ref{sec:knownbasis} for available
basis sets and their naming convention.

NOTE: we suggest that spherical basis sets are used in the calculation. The use of
high quality cartesian basis sets can lead to numerical inaccuracies.

In order to compute the integrals needed for the Douglas-Kroll approximation
the implementation makes use of a fitting basis set (see literature given
above for details). The current code will create this fitting basis set
based on the given {\tt "ao basis"} by simply uncontracting that basis. This
again is what is commonly implemented in quantum chemistry codes that
include the Douglas-Kroll method.  Additional flexibility is available to
the user by explicitly specifying a Douglas-Kroll fitting basis
set. This basis set must be named {\tt "D-K basis"} (see Chapter
\ref{sec:basis}).

\section{Zeroth Order regular approximation (ZORA)}
\label{sec:zora}

The spin-free and spin-orbit one-electron zeroth-order regular approximation (ZORA)
have been implemented. The use of relativistic effects with ZORA
can be invoked by specifying:

\begin{verbatim}
  ZORA [<string (ON||OFF) default ON>
\end{verbatim}

The \verb+ON|OFF+ string is used to turn on or off ZORA.
By default, if the \verb+ZORA+ keyword is found, the approximation
will be used in the calculation. If the user wishes to calculate
a non-relativistic quantity after turning on ZORA, the user
will need to define a new \verb+RELATIVISTIC+ block and turn
the approximation \verb+OFF+.  The user can also simply put
a blank \verb+RELATIVISTIC+ block in the input file and all options
will be turned off.

\section{Dyall's Modified Dirac Hamitonian approximation}
\label{sec:dyall-mod-dir}

The approximate methods described in this section are all based on Dyall's
modified Dirac Hamiltonian. This Hamiltonian is entirely equivalent to the
original Dirac Hamiltonian, and its solutions have the same properties.
The modification is achieved by a transformation on the small component,
extracting out \hbox{$\sigma\cdot{\bf p}/2mc$}. This gives the modified small
component the same symmetry as the large component, and in fact it differs
from the large component only at order $\alpha^2$.  The advantage of the
modification is that the operators now resemble the operators of the
Breit-Pauli Hamiltonian, and can be classified in a similar fashion into
spin-free, spin-orbit and spin-spin terms. It is the spin-free terms which
have been implemented in NWChem, with a number of further approximations.

The first is that the negative energy states are removed by a normalized
elimination of the small component (NESC), which is equivalent to an exact
Foldy-Wouthuysen (EFW) transformation. The number of components in the wave
function is thereby effectively reduced from 4 to 2. NESC on its own does
not provide any advantages, and in fact complicates things because the
transformation is energy-dependent. The second approximation therefore
performs the elimination on an atom-by-atom basis, which is equivalent to
neglecting blocks which couple different atoms in the EFW transformation.
The advantage of this approximation is that all the energy dependence can be
included in the contraction coefficients of the basis set.  The tests which
have been done show that this approximation gives results well within
chemical accuracy. The third approximation neglects the commutator of the
EFW transformation with the two-electron Coulomb interaction, so that the
only corrections that need to be made are in the one-electron integrals.
This is the equivalent of the Douglas-Kroll(-Hess) approximation as it is
usually applied.

The use of these approximations can be invoked with the use of the
\verb+DYALL-MOD-DIRAC+ directive in the \verb+RELATIVISTIC+ directive block.
The syntax is as follows.

\begin{verbatim}
  DYALL-MOD-DIRAC [ (ON || OFF) default ON ]
                  [ (NESC1E || NESC2E) default NESC1E ]
\end{verbatim}

The \verb+ON|OFF+ string is used to turn on or off the
Dyall's modified Dirac approximation. By default, if the \verb+DYALL-MOD-DIRAC+
keyword is found, the approximation will be used in the calculation.
If the user wishes to calculate a non-relativistic quantity after turning
on Dyall's modified Dirac, the user will need to define a new
\verb+RELATIVISTIC+
block and turn the approximation \verb+OFF+.  The user could also simply
put a blank \verb+RELATIVISTIC+ block in the input file and all options
will be turned off.

Both one- and two-electron approximations are available
\verb+NESC1E || NESC2E+, and both have
analytic gradients. The one-electron approximation is the default.
The two-electron approximation specified by \verb+NESC2E+ has some sub
options which are placed on the same logical line as the
\verb+DYALL-MOD-DIRAC+ directive, with the following syntax:

\begin{verbatim}
  NESC2E [ (SS1CENT [ (ON || OFF) default ON ] || SSALL) default SSALL ]
         [ (SSSS [ (ON || OFF) default ON ] || NOSSSS) default SSSS ]
\end{verbatim}

The first sub-option gives the capability to limit the two-electron
corrections to those in which the small components in any density must be on
the same center.  This reduces the $(LL|SS)$ contributions to at most
three-center integrals and the $(SS|SS)$ contributions to two centers. For a
case with only one relativistic atom this option is redundant. The second
controls the inclusion of the $(SS|SS)$ integrals which are of order
$\alpha^4$. For light atoms they may safely be neglected, but for heavy
atoms they should be included.

In addition to the selection of this keyword in the \verb+RELATIVISTIC+
directive block, it is necessary to supply basis sets in addition to the
\verb+ao basis+. For the one-electron approximation, three basis sets are
needed: the atomic FW basis set, the large component basis set and the small
component basis set. The atomic FW basis set should be included in the
\verb+ao basis+.
The large and small components should similarly be incorporated
in basis sets named \verb+large component+ and \verb+small component+,
respectively. For the two-electron approximation, only two basis sets are
needed. These are the large component and the small component. The large component
should be included in the \verb+ao basis+ and the small component
is specified separately as \verb+small component+, as for the one-electron
approximation. This means that the two approximations can {\it not} be run
correctly without changing the \verb+ao basis+, and it is up to the user to
ensure that the basis sets are correctly specified.

There is one further requirement in the specification of the basis sets. In
the \verb+ao basis+, it is necessary to add the \verb+rel+ keyword either to the
\verb+basis+ directive or the library tag line (See below for examples).
The former marks the basis
functions specified by the tag as relativistic, the latter marks the whole
basis as relativistic. The marking is actually done at the unique shell
level, so that it is possible not only to have relativistic and
nonrelativistic atoms, it is also possible to have relativistic and
nonrelativistic shells on a given atom. This would be useful, for example,
for diffuse functions or for high angular momentum correlating functions,
where the influence of relativity was small. The marking of shells as
relativistic is necessary to set up a mapping between the ao basis and the
large and/or small component basis sets. For the one-electron approximation
the large and small component basis sets MUST be of the same size and
construction, i.e. differing only in the contraction coefficients.

It should also be noted that the relativistic code will NOT work with basis
sets that contain sp shells, nor will it work with ECPs. Both of these are
tested and flagged as an error.

Some examples follow. The first example sets up the data for relativistic
calculations on water with the one-electron approximation and the
two-electron approximation, using the library basis sets.

\begin{verbatim}
  start h2o-dmd

  geometry units bohr
  symmetry c2v
    O       0.000000000    0.000000000   -0.009000000
    H       1.515260000    0.000000000   -1.058900000
    H      -1.515260000    0.000000000   -1.058900000
  end

  basis "fw" rel
    oxygen library cc-pvdz_pt_sf_fw
    hydrogen library cc-pvdz_pt_sf_fw
  end

  basis "large"
    oxygen library cc-pvdz_pt_sf_lc
    hydrogen library cc-pvdz_pt_sf_lc
  end

  basis "large2" rel
    oxygen library cc-pvdz_pt_sf_lc
    hydrogen library cc-pvdz_pt_sf_lc
  end

  basis "small"
    oxygen library cc-pvdz_pt_sf_sc
    hydrogen library cc-pvdz_pt_sf_sc
  end

  set "ao basis" fw
  set "large component" large
  set "small component" small

  relativistic
    dyall-mod-dirac
  end

  task scf

  set "ao basis" large2
  unset "large component"
  set "small component" small

  relativistic
    dyall-mod-dirac nesc2e
  end

  task scf
\end{verbatim}

The second example has oxygen as a relativistic atom and hydrogen nonrelativistic.

\begin{verbatim}
  start h2o-dmd2

  geometry units bohr
  symmetry c2v
    O       0.000000000    0.000000000   -0.009000000
    H       1.515260000    0.000000000   -1.058900000
    H      -1.515260000    0.000000000   -1.058900000
  end

  basis "ao basis"
    oxygen library cc-pvdz_pt_sf_fw rel
    hydrogen library cc-pvdz
  end

  basis "large component"
    oxygen library cc-pvdz_pt_sf_lc
  end

  basis "small component"
    oxygen library cc-pvdz_pt_sf_sc
  end

  relativistic
    dyall-mod-dirac
  end

  task scf
\end{verbatim}


\documentclass[12pt]{article}

\usepackage[utf8]{inputenc}

\usepackage{amsmath, amssymb, amsfonts, amsthm, mathrsfs, bm}
%\usepackage{array, multirow}
\usepackage{color, float, graphicx, caption, subcaption}
\usepackage{comment}

\usepackage{mathtools}%for paired delimiters

\usepackage{hyperref}

\renewcommand{\arraystretch}{1.0}

\captionsetup{font=small}
\usepackage{setspace}
\setstretch{1.0}
%\setlength\topmargin{0in}
%\setlength\textheight{8in}
%\setlength\textwidth{5.5in}
%\setlength\evensidemargin{.5in}
%\setlength\oddsidemargin{.5in}

\setlength\topmargin{-.45in}
\setlength\textheight{8.5in}
\setlength\textwidth{6in}
\setlength\evensidemargin{.25in}
\setlength\oddsidemargin{.25in}

\graphicspath{{InkscapePics/}}

\newcommand{\executeiffilenewer}[3]{%
 \ifnum\pdfstrcmp{\pdffilemoddate{#1}}%
 {\pdffilemoddate{#2}}>0%
 {\immediate\write18{#3}}\fi%
}
\newcommand{\includesvg}[1]{%
 \executeiffilenewer{#1.svg}{#1.pdf}%
 {inkscape -z -D --file=#1.svg %
 --export-pdf=#1.pdf --export-latex}%
 \input{#1.pdf_tex}%
}

\DeclarePairedDelimiter\ceil{\lceil}{\rceil}

\renewcommand{\a}{\alpha}
\renewcommand{\b}{\beta}
\renewcommand{\d}{\delta}
\newcommand{\e}{\varepsilon}
\newcommand{\g}{\gamma}
\renewcommand{\k}{\kappa}
\renewcommand{\l}{\lambda}
\newcommand{\m}{\mu}
\renewcommand{\o}{\omega}
\newcommand{\p}{\rho}
\newcommand{\s}{\sigma}
\renewcommand{\t}{\tau}
\renewcommand{\th}{\theta}
\newcommand{\z}{\zeta}

\newcommand{\ba}{\bm{a}}
\newcommand{\bg}{\bm{\gamma}}
\newcommand{\bp}{\bm{p}}
\newcommand{\bq}{\bm{q}}
\newcommand{\bmm}{\bm{m}}
\newcommand{\bmu}{\bm{\mu}}
\newcommand{\bnu}{\bm{\nu}}
\newcommand{\bw}{\bm{w}}
\newcommand{\bx}{\bm{x}}
\newcommand{\bpi}{\bm{\pi}}
\newcommand{\bs}{\bm{\sigma}}
\newcommand{\bS}{\bm{\Sigma}}
\newcommand{\bth}{\bm{\theta}}
\newcommand{\bu}{\bm{u}}
\newcommand{\bv}{\bm{v}}

\newcommand{\bA}{\bm{A}}
\newcommand{\bH}{\bm{H}}
\newcommand{\bM}{\bm{M}}
\newcommand{\bW}{\bm{W}}
\newcommand{\bX}{\bm{X}}

\newcommand{\rp}{{_{r}p}}
\newcommand{\rD}{{_{r}\D}}
\newcommand{\sD}{{_{s}\D}}

\newcommand{\A}{\mathcal{A}}
\newcommand{\C}{\mathcal{C}}
\newcommand{\D}{\Delta}
\newcommand{\mD}{\mathcal{D}}
\newcommand{\E}{\mathcal{E}}
\newcommand{\F}{\mathcal{F}}
\newcommand{\G}{\mathscr{G}}
\renewcommand{\H}{\mathcal{H}}
\newcommand{\I}{\mathcal{I}}
\newcommand{\M}{\mathcal{M}}
\renewcommand{\O}{\Omega}
\newcommand{\Pt}{\mathscr{P}}
\renewcommand{\P}{\mathcal{P}}
\newcommand{\Q}{\mathcal{Q}}
\newcommand{\R}{\mathbb{R}}
\renewcommand{\S}{\mathcal{S}}
\newcommand{\T}{\mathcal{T}}
\newcommand{\Th}{\Theta}
\newcommand{\Y}{\mathcal{Y}}
\newcommand{\V}{\mathscr{V}}
\newcommand{\Z}{\mathbb{Z}}

\newcommand{\ol}{\overline}
\newcommand{\oll}[1]{\overline{\overline{ #1}}}
\newcommand{\ul}{\underline}
\newcommand{\Ex}{\mathbf{E}}
\renewcommand{\Pr}{\mathbf{P}}
\newcommand{\td}{\tilde}
\newcommand{\tr}{\triangleleft}

\newtheorem{lemma}{Lemma}
\newtheorem*{example}{Example}
\newtheorem{theorem}{Theorem}
\newtheorem{proposition}{Proposition}
\newtheorem*{definition}{Definition}
\newtheorem*{maxminrefinement}{Max-Min Threshold Refinement}
\newtheorem*{multimaxminrefinement}{Multi-Worker Max-Min Threshold Refinement}
\newtheorem*{refinement}{An Intuitive Credible Threats Refinement}
\newtheorem*{minrefinement}{Minimal Refinement}
\newtheorem{corollary}{Corollary}
\newtheorem{observation}{Observation}
\newtheorem{remark}{Remark}
\newtheorem{assumption}{Assumption}
\newtheorem*{efficiency}{Efficiency Wage Contract}

\DeclareMathOperator*{\argmax}{arg\,max}
\DeclareMathOperator*{\argmin}{arg\,min}
\DeclareMathOperator{\sgn}{sgn}

\begin{document}

\begin{center}
{\Large Econ 780 \hspace{0.5cm} HW Chapter 3}\\
\textbf{Minh Cao, Grant Smith, Ella Barnes, Alexander Erwin and Mark Coomes}\\ %You should put your name here
Due:  %You should write the date here.
\end{center}

\vspace{0.2 cm}

\subsection*{Exercises for chapter 3}

\begin{enumerate}
    \item Recall the matrix $D$ from chapter 3. Introduce the matrix $\ol{D}$: The first column is the date 0 price of the assets while the rest of the matrix is $D$. Assume as in the book $P(\o_j) > 0$ for all $j$. Consider the following market:
    \begin{align*}
    \ol{D}=
    \left[\begin{array}{llll}
    1 & 2 & 2 & 3\\
    1 & 0 & 3 & 3
    \end{array}
    \right],
    \end{align*}
    As in the book, denote the two assets by $S^1$ and $S^2$.
      \begin{itemize}
        \item Is there a risk-free rate for this market?
        \begin{itemize}
            \item We found that the initial prices could be generated by $$(1/6-3/6 z_3, 1/3 - 3/3 z_3,z_3)$$
            We require that all three be positive, which happens when $z_3$ is between 0 and $1/3$. This means the sum of the numbers is between $1/2$ and $1/3$ and $R$ must be between 1 and 2.
        \end{itemize}
        \item Fixing $S^1$ as numeraire, what are all the martingale measures?
        \begin{itemize}
            \item Not sure on this one.
        \end{itemize}
        \item Describe another asset $S^3$ such that the market $(S^1, S^2, S^3)$ is complete
        \emph{but not arbitrage free}.
        \begin{itemize}
            \item         An additional asset could be added to make the following matrix:
            \begin{align*}
    \ol{D}=
    \left[\begin{array}{llll}
    1 & 2 & 2 & 3\\
    1 & 0 & 3 & 3 \\
    1 & 0 & 0 & 1
    \end{array}
    \right]
    \end{align*}
        \end{itemize}

        \item Introduce the asset $X$ where $X_0 = 1$ and $X_1 = [3\ 0\ \frac{8}{3}]$ and consider the market $(S^1, S^2, X)$. Fixing $S^1$ as numeraire, is there a martingale measure? If so what is it? If not, find an arbitrage portfolio.
        \begin{itemize}
            \item The new matrix is
                        \begin{align*}
    D=
    \left[\begin{array}{lll}
    2 & 2 & 3\\
     0 & 3 & 3 \\
     3 & 0 & 8/3
    \end{array}
    \right]
    \end{align*}
    Which is non-singular. Thus, any way to get $S_0$ from linear combinations of these columns is the only linear combination that works. We calculated that linear combination, and we got negative values for the first two vectors, which means there is arbitrage. We just need to find an arbitrage portfolio. Any holding vector whose components add to zero is free because the initial prices are all 1. So we need to find a holding vector whose components add to 1 and give positive or zero dot products with the columns of $D$ (with at least one being positive). We could do that with $h = (1,-.5,-.5)$
        \end{itemize}

      \end{itemize}
      \item Consider the following market
\begin{align*}
\ol{D} = \begin{bmatrix}
1	& 2	& 2	& 2\\
2	& 8	& 4	& 2
\end{bmatrix}
\end{align*}
As in the book, denote the two assets by $S^1$ and $S^2$.

\begin{itemize}
  \item[a.] Create the normalized market by choosing $S^1$ as numeraire. Find all martingale measures.

  Express you answer in the following way: Let $q_1$ denote the probability of $\o_1$ under an arbitrary martingale measure. Characterize the set of martingale measures by stating what values $q_1$ can take, and, for each such $q_1$, what are the corresponding probabilities for $\o_2$ and $\o_3$.

  For example, you could write: ``\textit{the set of martingale measures is} $\{(q_1, \frac{1}{2} + q_1, \frac{1}{2} - 2 q_1)\ \vert\ q_1 \in (0, \frac{1}{4})\}$." (This is the wrong answer of course).

\begin{itemize}
    \item the set of martingale measures is $\{(q_1, 1-  3q_1, 2q_1)\ \vert\ q_1 \in (0, \frac{1}{3})\}$
\end{itemize}

\begin{comment}
  \textit{Solution}: The normalized market is
  \begin{align*}
  \ol{Z} = \begin{bmatrix}
  1	& 1	& 1	& 1\\
  2	& 4	& 2	& 1
  \end{bmatrix}
  \end{align*}
  Let $(q_1, q_2, q_3)$ be a martingale measure. The conditions on the $q$'s are: $q_i \in (0, 1)$ for all $i$, $1 = q_1 + q_2 + q_3$, and $2 = 4 q_1 + 2 q_2 + q_3$.

  This yields the condition $1 = 3 q_1 + q_2$ or, equivalently, $q_2 = 1 - 3 q_1$. Thus, the set of martingale measures is $\{(q_1, 1 - 3 q_1, 2 q_1)\ \vert\ q_1 \in (0, \frac{1}{3})\}$.
\end{comment}

  \item[b.] True of False. If we choose $S^2$ instead of $S^1$ as numeraire, we still generate the same set of martingale measures. (No need to show work.)
  \begin{itemize}
      \item True. It's just scaling the vectors, so our linear combination numbers would be different, but we scale those anyway to make the probability measure. This means the answer to this problem would be the same if we didn't scale at all or choose a numeraire.
  \end{itemize}

\begin{comment}
  \textit{Solution}: False. The normalized market with $S^2$ as numeraire is
  \begin{align*}
  \ol{Z} = \begin{bmatrix}
  \frac{1}{2}	& \frac{1}{4}	& \frac{1}{2}	& 1\\
  1			& 1			& 1			& 1
  \end{bmatrix}
  \end{align*}
  The probability $(\frac{1}{4}, \frac{1}{4}, \frac{1}{2})$ is a martingale measure when $S^1$ is numeraire, but not when $S^2$ is numeraire.
\end{comment}

  \item[c.] A put option $PutK$ on $S^2$ with strike price $K$ maturing at date 1 is a contingent claim with date 1 payoff $PutK_1 = \max\{K - S^2, 0\}$. What is $Put5_1(\o_i)$ for $i = 1, 2, 3$?
  \begin{itemize}
      \item $Put5_1(\o_i)=(0,1,3)$
  \end{itemize}

\begin{comment}
  \textit{Solution}: $Put5_1 = \begin{bmatrix} 0 & 1 & 3 \end{bmatrix}$.
\end{comment}

  \item[d.] Suppose the date 0 price of $Put5$ is $Put5_0 = \frac{7}{8}$. Is the market $(S^1, S^2, Put5)$ arbitrage free?
  \begin{itemize}
      \item i THINK THERE IS SOMETHING WRONG WITH THIS ANSWER!!!
      \item No it is not. To prove this, we try to create $7/8$ with the dot product of our martingale measures above and $(0,1,3)$.  This gives us $7/8 = 1+3q_1$, but $q_1$ must be positive, so we have a contradiction.
  \end{itemize}

\begin{comment}
  \textit{Solution}: To show $(S^1, S^2, Put5)$ is arbitrage free amounts to finding a martingale measure $Q$ of the normalized market $(1, S^2/S^1)$ with $S^1$ as numeraire such that, in addition, $\frac{7}{8} = \frac{1}{2} \Ex^Q Put5_1$.

  The above equation plus the characterization of martingale measures for the normalized market $(1, S^2/S^1)$ from part (a) implies $\frac{7}{4} = 1 \cdot (1 - 3q_1) + 3 \cdot (2 q_1)$, which yields $q_1 = \frac{1}{4}$ and a probability $(\frac{1}{4}, \frac{1}{4}, \frac{1}{2})$. Thus, $(S^1, S^2, Put5)$ is arbitrage free because $(\frac{1}{4}, \frac{1}{4}, \frac{1}{2})$ is the unique martingale measure of the corresponding normalized market $(1, S^2/S^1, Put5/S^1)$ with $S^1$ as numeraire.
\end{comment}

  \item[e.] Continuing to assume the date 0 price of $Put5$ is $\frac{7}{8}$. What is the arbitrage free date 0 price of $Put4$?
  \begin{itemize}
      \item The replicating holding vector is $(-4/3,1/3,4/3)$, and $hS_0$ is 0.5.
  \end{itemize}

\begin{comment}
  \textit{Solution}: The arbitrage free date 0 price of $Put4$ is $\frac{1}{2} \Ex^{(\frac{1}{4}, \frac{1}{4}, \frac{1}{2})} Put4_1 = \frac{1}{2} (\frac{1}{2} \cdot 2) = \frac{1}{2}$.
\end{comment}
\end{itemize}
  \item Exercise 3.2.
  \begin{itemize}
      \item If $X$ is replicable, then:
      $$\exists h; hS_1 = X$$
      \item Now, using the definition of the value of a portfolio:
      $$V_0^h = hS_0$$
      \item If there is no arbitrage, then there is a column vector, $z$ such that $S_0$ can be formed by $z$'s scaling of $S_1$'s columns. Thus,
      $$\exists z; S_0 = S_1z$$
      \item We can use this equation with the second bullet to get:
      $$V_0^h = hS_0 = hS_1z$$
      \item If we want to turn z into a vector that scales to 1 (to make it a probability measure), we would want to divide its components by the sum of all the components, i.e. to normalize it. So to do this, we can multiply and divide by the sum, and we can call the new normalized z, q:
      $$V_0^h = hS_0 = hS_1z \frac{\Sigma z_i}{\Sigma z_i} = \Sigma z_i * hS_1q $$
      \item But we know that $\Sigma z_i$ has a name, and it is $\beta$, which is also $1/1+R$. Thus,
      $$V_0^h = hS_0 = hS_1z \frac{\Sigma z_i}{\Sigma z_i} = \Sigma z_i * hS_1q =  \frac{1}{1+R}hS_1q$$
      \item Now we use our existence in the very first step, which is our replicating portfolio.  Thus, we get (and removing the intermediate steps):
      $$V_0^h =  \frac{1}{1+R}hS_1q = \frac{1}{1+R}Xq $$
      \item And lastly, we say that $Xq$ is the expectation of $X$ under $S$:
      $$V_0^h = \frac{1}{1+R}Xq = \frac{1}{1+R}\mathbb{E}^Q[X]$$
  \end{itemize}
  \item Exercise 3.3.
  \begin{itemize}
      \item We want to prove that the arbitrage free price of an claim $X$ is given by:
      $$\Pi (0,X) = \mathbb{E}^P[\Lambda (\omega) X(\omega)]$$
      \item We start with the definition of $\Lambda (\omega)$:
      $$\Lambda (\omega) = \frac{1}{1+R} L(\omega) = \frac{1}{1+R} \frac{q_i}{p_i}$$
      \item We now substitute this into the first equation:
            $$\Pi (0,X) = \mathbb{E}^P[\frac{1}{1+R} \frac{q_i}{p_i} X(\omega)] = \frac{1}{1+R}\mathbb{E}^P[ \frac{q_i}{p_i} X(\omega)]$$
        \item We now expand the expectation:
        $$\Pi (0,X)  = \mathbb{E}^P[\Lambda (\omega) X(\omega)] = \frac{1}{1+R}\mathbb{E}^Q [X] $$
        Which was our goal.
  \end{itemize}

\end{enumerate}

\end{document}


\subsection{Related Work}
We now review several related works that use secure computation techniques. With respect to functionality the closest related work is that of Blanton and Aguiar\cite{ASIACCS:BlaAgu12} which describes a relatively complete set of protocols for performing intersections, unions, set difference, etc. and the corresponding SQL-like operations. Moreover, these operations are composable in that the inputs and outputs are secret shared between the parties. At the core of their technique is the use of a generic MPC protocol and an oblivious sorting algorithm that merges the two sets. This is followed by a linear pass over the sorted data where a relation is performed on adjacent items. Their technique has the advantage of being very general and flexible. However, the proposed sorting algorithm has complexity $O(n \log^2 n)$ and is not constant round\footnote{It is not constant round when the underlying MPC protocol is not constant round which is typically required for high throughput.}.
\iffullversion
As a result, the implementation from 2011 performed poorly by current standards, intersecting $2^{10}$ items in 12 seconds. The modern protocol \cite{CCS:KKRT16} which is \emph{not composable} can perform intersections of $2^{20}$ items in 4 seconds. While this difference of three orders of magnitude would narrow if reimplemented using modern techniques, the gap would remain large.
\else
This results in poor concrete performance as shown in \sectionref{sec:eval}.
\fi

\iffullversion
% Private Set Intersection:Are Garbled Circuits Better than Custom Protocols?
%     https://ssltest.cs.umd.edu/~jkatz/papers/psi.pdf
%\item
Huang, Evans and Katz\cite{HEK12} also described a set intersection protocol based on sorting. Unlike \cite{ASIACCS:BlaAgu12}, this work considers the two party setting where each party holds a set in the clear.
This requirement prevents the protocol from being composable but allows the complexity to be reduced to $O(n\log n)$. The key idea is that each party locally sorts their set followed by merging the sets within MPC. The protocol can then perform a single pass over the sorted data to construct the intersection. While this results in performance improvements the overall protocol requires  $O(n\log n)$ operations and is not composable.
\fi

\iffullversion
% (KS06) Privacy-Preserving Set Operations
%     https://www.cs.cmu.edu/~leak/papers/set-tech-full.pdf
% (MF06) Efficient Polynomial Operations in the Shared-Coefficients Setting
%     https://pdfs.semanticscholar.org/80ca/9f56cffce534e047d049884736ff16204958.pdf
%\item
Another line of work was begun by Kissner and Song\cite{KS06} and improved on by \cite{MF06}. Their approach is based on the observation that set intersection and multi-set union have a correspondence to operations on polynomials. A set $S$ can be encoded as the polynomial $\hat S(x)= \prod_{s\in S}(x-s)\in \mathbb{F}[x]$. That is, the polynomial $\hat S(x)$ has a root at all $s\in S$. Given two such polynomials, $\hat S(x), \hat T(x)$, the polynomial encoding the intersection is $\hat S(x)+\hat T(x)$ with overwhelming probability given a sufficiently large field $\mathbb{F}$.

Multi-set union can similarly be performed by multiplying the two polynomials together. Unlike with normal union, if an item $y$ is contained in $S$ and $T$ then $\hat S(x)\hat T(x)$ will contain two roots at $y$ which is often not the desired functionality. This general idea can be transformed into a secure multi-party protocol using oblivious polynomial evaluation\cite{NP99} along with randomizing the result polynomial. The original computational overhead was $O(n^2)$ which can be reduced to the cost of polynomial interpolation $O(n\log n)$ using techniques from \cite{MF06}. The communication complexity is linear. In addition, this scheme assumes an ideal functionality to generate a shared Paillier key pair. We are unaware of any efficient protocol to realize this functionality except for \cite{RSA:HMRT12} in the two party setting.

This general approach is also composable. However, due to randomization that is performed the degree of the polynomial after each operation doubles. This limits the practical ability of the protocol to compose more than a few operations. Moreover, it is not clear how this protocol can be extended to support SQL-like queries where elements are key-value tuples. This general approach is also composable but incurs a 2 times overhead for each successive operations and cannot be extended to SQL-like queries.
\fi

\iffullversion
% C. Hazay and K. Nissim. Efficient set operations in the presence of malicious adversaries. In PKC, 2010.
%   http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.454.1521&rep=rep1&type=pdf
%\item
Hazay and Nissim\cite{HN12} introduce a pair of protocols computing set intersection and union which are also based on oblivious polynomial evaluation where the roots of the polynomial encode a set. However, these protocols are restricted to the two party case and are not composable. The non-composability comes from the fact that only one party constructs a polynomial $\hat S(x)$ encoding their set $S$ while the other party obliviously evaluates it on each element in their set. The result of these evaluations are compared with zero\footnote{The real protocol is slightly more complicated than this.}. These protocols have linear overhead and can achieve security in the malicious setting.
\fi

%\item
Pinkas, Schneider and Zohner \cite{usenix:PSZ14} introduced a paradigm for set intersection that combines a hash table technique known as cuckoo hashing with a randomized encoding technique using oblivious transfer. Due to the hashing technique, the problem is reduced to comparing a single item $x$ to a small set $\{y_1,...,y_m\}$. Oblivious transfer is then used to interactively compute the randomized encoding $\llbracket x\rrbracket$ while the other party locally computes the encodings $\{\llbracket y_1\rrbracket,...,\llbracket y_m\rrbracket \}$. A plaintext intersection can then be perform directly on these encodings. With the use of several optimization\cite{USENIX:PSSZ15,PSZ16,CCS:KKRT16,OOS17} this paradigm is extremely efficient and can perform a set intersection using $O(n)$ calls to a random oracle and $O(n)$ communication. These protocols are not composable.
\iffullversion
More recently this approach has also been extended to the malicious setting \cite{CCS:RinRos17} and separately to have sublinear communication when one set is much larger than the other\cite{CLR17} .
\fi

% https://eprint.iacr.org/2011/429.pdf
%\item Laur, Willemson and Zhang\cite{LWZ11} \todo{....}

% https://eprint.iacr.org/2013/203.pdf
%\item
Laur, Talvista and Willemson\cite{LTW13} present techniques in the honest majority setting for composable joins, unions and  many other operations at the expense of information leakage. Consider two parties each with a sets $X,Y$. The parties first generate secret shares of the sets and then use a generic MPC protocol to apply a pseudorandom function (PRF) $F$ to the shared sets to compute $X' = \{F_k(x) \mid x\in X\}, Y'=\{F_k(y) \mid y\in Y\}$ where the key $k$ is uniformly sampled by the MPC protocol (i.e. neither party knows $k$). $X'$ and $Y'$ are then revealed to both parties who use this information to infer the intersection, union and many other SQL-like operations. This basic approach dates back to the first PSI protocols \cite{Mea86,HFH99} where the (oblivious) PRF was implemented using a special purpose Diffie-Hellman protocol. \cite{LTW13} extended this paradigm to allow the input sets to be secret shared as opposed to being known in the clear.

The primary limitation of this approach is that all operations require all parties to know $X'$ and $Y'$. This prevents the protocol from being composable without significant information leakage. In particular, the cardinality of $X'\cap Y'$ and the result of the \texttt{where} clause for each row is revealed. This is of particular concern when several dataset are being combined. Learning the size of the intersection or the union can represent significant information. For instance, in the threat log application the union of many sets are taken. Each of these unions would reveal how many unique logs the new set has. Alternatively, taking the join between a set of hospital patients and a set of HIV positive patients would reveal how many have HIV. When combined with other information it could lead to the ability to identify some or all of these patients. Beyond this, the provided three party implementation achieved relatively poor performance. A join between two tables of a million records is estimated to require one hour on their three benchmark machines\cite{LTW13}. Looking forward, our protocol can perform a similar join operation in 4 seconds while preventing all information leakage.

%\end{itemize}


Implements the \emph{virtual substitution} method for a conjunction of constraints as described in \cite{Article_Corzilius_FCT2011}. This module supports incremental calls, efficient backtracking and infeasible subset generation. Note, that the infeasible subsets are often very small but not necessarily minimal. The implemented approach is not complete, as it maybe cannot decide the satisfiability of a conjunction containing a constraint, which involves a variable with degree $3$ or more. Note, that even if no constraint of such form occurs in the received formula, this module might not be able to determine the consistency of its received formula, as it could create constraints of this form in its solving process. Nevertheless, the implemented approach is efficient compared to other approaches for non-linear real arithmetic conjunctions, and therefore well-suited to be used for solving conjunctions of non-linear real arithmetic constraints before complete approaches have their try. In combination with a backend, this module tries to solve the given problem and calls the backend on problems with less variables.

\paragraph{Efficiency} The worst case complexity of this approach is exponential in the number of real arithmetic variables occurring in the conjunction to solve. It performs especially good on almost linear instances and slightly prefers problems only containing constraints with the relation symbols $\leq$, $\geq$ and $=$. It is often the case, that even if the conjunction to solve contains many not suited constraints, this module can determine the consistency on the basis of a well suited subset of the constraints in this conjunction.


\documentclass[acmtog]{acmart}
\usepackage{graphicx}
\usepackage{subfigure}
\usepackage{natbib}
\usepackage{listings}
\usepackage{bm}

\definecolor{blve}{rgb}{0.3372549 , 0.61176471, 0.83921569}
\definecolor{gr33n}{rgb}{0.29019608, 0.7372549, 0.64705882}
\makeatletter
\lst@InstallKeywords k{class}{classstyle}\slshape{classstyle}{}ld
\makeatother
\lstset{language=C++,
	basicstyle=\ttfamily,
	keywordstyle=\color{blve}\ttfamily,
	stringstyle=\color{red}\ttfamily,
	commentstyle=\color{magenta}\ttfamily,
	morecomment=[l][\color{magenta}]{\#},
	classstyle = \bfseries\color{gr33n},
	breaklines=true,
	tabsize=2
}
\lstset{basicstyle=\ttfamily}

% Title portion
\title{Assignment 4:\\ {Global Illumination}}

\author{Name:\quad Yang Hongdi  \\ student number:\ 2019533234
\\email:\quad yanghd@shanghaitech.edu.cn}

% Document starts
\begin{document}
\maketitle

\vspace*{2 ex}

\section{Introduction}
In this project, ray tracing with direct and indirect lighting is performed using Monte-Carlo integration. BVH-tree is used for acceleration.
\section{Implementation Details}
\subsection{Acceleration : BVH}
	In this project, to accelerate ray-triangle intersection, BVH is used. When building BVH, SAH is used for best space division.\\
	First, we sort the triangles by the coordinate of its center, then we save the result of all possible space division by the triangles. Then we compute the time cost of each space division
	by using the formula $$c(A,B) = t_{trav} + p_A\sum_{i=1}^{N_A}t_{isect}(ai) + p_B\sum_{i=1}^{N_B}t_{isect}(b_i)$$
	where we set $p_A,p_B$ equal to the surface area of the AABB, $t_isect = 1, t_{trav} = 0$, and we choose the space division with the minimum time cost as the
	best space division. We repeat this for all three axis and then choose the best axis to split the space.\\
	Now we get the best spcae division, we store the left AABB and the right AABB with their triangles, then we continue to split the space recursively.
	Until we just have only a few triangles or enough recursion depth, the we store the node as leaf node and store the triangles.
	\begin{lstlisting}
		// recursively build left and right
    leftChild = new KdTreeNode(leftTriangles, leftSpace, depth + 1);
    rightChild = new KdTreeNode(rightTriangles, rightSpace, depth + 1);
	\end{lstlisting}
	For intersection test, we just first check whether the ray hit the bouding box, if it does not, end checking directly and return false.
	\begin{lstlisting}
		if (!this->box.rayIntersection(ray, tIn, tOut)) return false; // not intersect with bounding box, return false directly.
	\end{lstlisting}
	If it hits the bouding box, we then recursively check whether it hits the left and right child bouding box. When the hit the light hit the leaf node, we just
	violently check if it hits the triangles inside the bouding box. Finally, we return true with the nearest triangle that has been hit.
\subsection{Path tracing with Monte-Carlo integration}
In this project, path tracing with Monte-Carlo integration is performed.
\subsubsection{Ideal Diffusion BRDF}
	\quad For ideal diffusion BRDF, we use cos-weighted sampling. We first sample uniformly on a disk.
	\begin{lstlisting}
		auto random_samples = unif(0.0, 1.0, 2);

	//sampling on a disk
	Float x1 = random_samples[0];
	Float y1 = random_samples[1];
	\end{lstlisting}
	Then we transform (x,y) to $(r,\theta_1)$, $$r = \sqrt{x_1}, \theta_1 = 2\pi y_1 $$
	then we project it onto the unit hemisphere. we need to transform $(r, \theta_1)$ to $(\theta, \phi)$(Note: $\theta$ is different from $\theta_1$)
	$$\sin\theta = r = \sqrt{x_1}, \phi = \theta_1 = 2\pi y_1 $$
	then we can compute $$ x = \sin\theta\cos\phi $$$$ y = \sin\theta\sin\phi $$$$ z = \cos\theta$$
	Last, we convert it from local coordinate to world coordinate using the normal.
	\begin{lstlisting}
		vec3 rotation_axis = vec3(0,0,1).cross(interact.normal);
	Eigen::AngleAxisf rotationVector(theta, rotation_axis);
	Eigen::Matrix3f rotation_matrix = rotationVector.toRotationMatrix();
	interact.wi = -(rotation_matrix * tmp_wi).normalized(); // from (0,0,1) system to world coordinate
	\end{lstlisting}
	Then we get the sampled incoming ray. And the pdf equal to $\frac{cos\theta}{\pi}$
\subsubsection{Ideal Specular BRDF}
	\quad For Ideal Specular BRDF, it is quite easy. We simply sample the reflect light, which should have a pdf of 1.
	The reflect direction can be computed by $$w_i = 2(w_o \cdot n)n - w_o$$
\subsubsection{Ideal Transmission BRDF}
	\quad For Ideal Transmission BRDF, similar as Ideal Specular, we sample the refraction light, which also have a pdf of 1.
	We use $$w_i = \frac{\eta_1}{\eta_2}w_o + (\frac{\eta_1}{\eta_2}\cos \theta_i - \sqrt{1 - \sin^2 \theta_t})n$$
	If we find that $\cos \theta_i < 0$, that means the ray is inside the object, we will then let $\cos \theta_{i_1} = -\cos \theta_{i}, n_1 = -n, \frac{\eta_{1_1}}{\eta_{2_1}} = \frac{\eta_2}{\eta_1}$ to compute the next ray.
\subsubsection{Glossy Specular}
	\quad For Glossy Specular, Disney principle BRDF is choosed to simulate Glossy surface BRDF. Multiple importance sampling is used.
	First, we calculate all angles as the graph shows.
	\begin{figure}[H]
		\centering

		\subfigure[Scene0]{
		\begin{minipage}[t]{0.45\linewidth}
		\centering
		\includegraphics[width=1.5in]{images/angles.png}
		%\caption{original image}
		\end{minipage}%
		}
	\end{figure}
	We let $$spec = D(\theta_h)F(\theta_d)G(\theta_l,\theta_v)$$
	where $$D_{GTR}(\theta_h) = \frac{\alpha}{\pi}\frac{1}{(1 + (\alpha^2-1)\cos^2\theta_h)^2}$$ $$\alpha = roughness^2$$
	$$F(\theta_d) = 0.08 + (1- 0.08)(1-\cos \theta_d)^5$$
	$$G(v) = \frac{n \cdot v}{(n \cdot b) + \sqrt{\alpha^2 + (1-\alpha^2)(n \cdot v)^2}}$$
	And we use BRDF weighted sampling to sample the reflect ray. i.e
	$$pdf_h = D(\theta_h)\cos\theta_h, \ pdf_l = \frac{pdf_h}{4(l \cdot h)}$$
	we sample both the light and brdf, then combine them with multiple importance sampling.
	$$dirlight = \frac{pdf_{light}^2}{pdf_{light}^2 + pdf_{brdf}^2}L_{light} + \frac{pdf_{brdf}^2}{pdf_{light}^2 + pdf_{brdf}^2}L_{brdf}$$
	\subsubsection{Area light sampling}
	To sample on the rectangle area light is quit easy. we just uniformly sample a point on the rectangle area light.
	\begin{lstlisting}
		auto random_sample = unif(0.0, 1.0, 2);
    Float x1 = random_sample[0];
    Float z1 = random_sample[1];
    vec3 sample_position = position + ((x1 - 0.5) * areaSize[0] * vec3(1, 0, 0)) + ((z1 - 0.5) * areaSize[1] * vec3(0, 0, 1));//uniformly ramdom choose a point on arealight
	\end{lstlisting}
	and the pdf will be $\frac{1}{A}$.
	However, as $$L_o(x,w_o) = \int_{\Omega}L_i(p,w_i)f_r(p,w_i,w_o)(n\cdot w_i)dw_i$$
	we need to transform $dw_i$ to $d_A$. By the definition of solid angle, we have $dw = \frac{dA \cos\theta^{'}}{||x^{'} - x||^2}$, where
	$\cos\theta^{'} = n_{light} \cdot w_i$, $x^{'}$ is our sampled position on light, $x$ is the intersection point. Then we have
	$$L_o(x,w_o) = \int_{\Omega}L_i(p,w_i)f_r(p,w_i,w_o)\frac{\cos\theta\cos\theta^{'}}{||x^{'} - x||^2}dA$$
	here is the code for computing solid angle.
	\begin{lstlisting}
	auto geom = geoms[0];
    Float cos_theta1 = abs(geom.get()->getNormal().dot(ref_it.wi));
    Float distance = (pos - ref_it.entryPoint).norm();
    return cos_theta1 / (distance * distance); // solid angle of the tiny surface
	\end{lstlisting}
	\subsubsection{Integrator based on Monte-Carlo Path Tracing}
	For each ray, we perform Monte-Carlo Path Tracing with following recursive procedure.
	\begin{lstlisting}
	shade(p, wo, depth)
		Check if depth > MAX_DEPTH return vec3(0,0,0)
		If hit light, check (n * wo) return light emission
		If hit object, sample the light at x1
		L_dir = L_i * f_r * cos_theta *cos_theta1/|x1-p|^2/pdf_light
		shoot another ray to compute indirect lighting
		Ray r(p, wi)
		If r hit anything at position q
		L_indir = shade(q, wi, depth + 1) * f_r * cos_theta/pdf
		return L_dir + L_indir
	\end{lstlisting}
	\section{Results}
	\begin{figure}[H]
		\centering

		\subfigure[Scene0]{
		\begin{minipage}[t]{0.45\linewidth}
		\centering
		\includegraphics[width=1.5in]{images/output_256.png}
		%\caption{original image}
		\end{minipage}%
		}
		\subfigure[Scene1]{
		\begin{minipage}[t]{0.45\linewidth}
		\centering
		\includegraphics[width=1.5in]{images/output_bunny.png}
		%\caption{ground truth}
		\end{minipage}%
		}%

		\subfigure[Scene2]{
		\begin{minipage}[t]{0.45\linewidth}
		\centering
		\includegraphics[width=1.5in]{images/output_dragon1.png}
		%\caption{depth prediction result}
		\end{minipage}
		}
		\subfigure[Scene3]{
		\begin{minipage}[t]{0.45\linewidth}
		\centering
		\includegraphics[width=1.5in]{images/output_dragon_bunny.png}
		%\caption{depth prediction result}
		\end{minipage}
		}
		\subfigure[Scene4]{
		\begin{minipage}[t]{0.45\linewidth}
		\centering
		\includegraphics[width=1.5in]{images/output_all_dragon.png}
		%\caption{depth prediction result}
		\end{minipage}
		}
		\subfigure[Scene0spp512]{
		\begin{minipage}[t]{0.45\linewidth}
		\centering
		\includegraphics[width=1.5in]{images/output_birghter_512.png}
		%\caption{depth prediction result}
		\end{minipage}
		}
		\subfigure[Scene1spp512]{
		\begin{minipage}[t]{0.45\linewidth}
		\centering
		\includegraphics[width=1.5in]{images/output_bunny_512.png}
		%\caption{depth prediction result}
		\end{minipage}
		}
		\subfigure[Scene1specular]{
		\begin{minipage}[t]{0.45\linewidth}
		\centering
		\includegraphics[width=1.5in]{images/output_spec.png}
		%\caption{depth prediction result}
		\end{minipage}
		}
		\subfigure[Scene2Transmission]{
		\begin{minipage}[t]{0.45\linewidth}
		\centering
		\includegraphics[width=1.5in]{images/output_dragon_transmission_depth6.png}
		%\caption{depth prediction result}
		\end{minipage}
		}
		\subfigure[Scene1Glossy]{
		\begin{minipage}[t]{0.45\linewidth}
		\centering
		\includegraphics[width=1.5in]{images/output_glossy_box0.2_bunny0.1.png}
		%\caption{depth prediction result}
		\end{minipage}
		}
		\end{figure}
\end{document}