%!TEX root = forallxsol.tex
%\part{Interpretations}
%\label{ch.semantics}
%\addtocontents{toc}{\protect\mbox{}\protect\hrulefill\par}

\setcounter{chapter}{20}
\chapter{Truth in FOL}\setcounter{ProbPart}{0}
\problempart
\label{pr.TorF1}
Consider the following interpretation:
	\begin{ebullet}
		\item The domain comprises only Corwin and Benedict
		\item `$Ax$' is to be true of both Corwin and Benedict
		\item `$Bx$' is to be true of Benedict only
		\item `$Nx$' is to be true of no one
		\item `$c$' is to refer to Corwin
	\end{ebullet}
Determine whether each of the following sentences is true or false in that interpretation:
\begin{earg}
\item $Bc$ \hfill \myanswer{False}
\item $Ac \eiff \enot Nc$ \hfill \myanswer{True}
\item $Nc \eif (Ac \eor Bc)$ \hfill \myanswer{True}
\item $\forall x Ax$ \hfill \myanswer{True}
\item $\forall x \enot Bx$ \hfill \myanswer{False}
\item $\exists x(Ax \eand Bx)$ \hfill \myanswer{True}
\item $\exists x(Ax \eif Nx)$ \hfill \myanswer{False}
\item $\forall x(Nx \eor \enot Nx)$ \hfill \myanswer{True}
\item $\exists x Bx \eif \forall x Ax$ \hfill \myanswer{True}
\end{earg}

\problempart
\label{pr.TorF3}
Consider the following interpretation:
	\begin{ebullet}
		\item The domain comprises only Lemmy, Courtney and Eddy
		\item `$Gx$' is to be true of Lemmy, Courtney and Eddy.
		\item `$Hx$' is to be true of and only of Courtney
		\item `$Mx$' is to be true of and only of Lemmy and Eddy
		\item `$c$' is to refer to Courtney
		\item `$e$' is to refer to Eddy
	\end{ebullet}
Determine whether each of the following sentences is true or false in that interpretation:
\begin{earg}
\item $Hc$ \hfill \myanswer{True}
\item $He$\hfill \myanswer{False}
\item $Mc \eor Me$ \hfill \myanswer{True}
\item $Gc \eor \enot Gc$ \hfill \myanswer{True}
\item $Mc \eif Gc$ \hfill \myanswer{True}
\item $\exists x Hx$ \hfill \myanswer{True}
\item $\forall x Hx$ \hfill \myanswer{False}
\item $\exists x \enot Mx$ \hfill \myanswer{True}
\item $\exists x(Hx \eand Gx)$ \hfill \myanswer{True}
\item $\exists x(Mx \eand Gx)$ \hfill \myanswer{True}
\item $\forall x(Hx \eor Mx)$ \hfill \myanswer{True}
\item $\exists x Hx \eand \exists x Mx$ \hfill \myanswer{True}
\item $\forall x(Hx \eiff \enot Mx)$ \hfill \myanswer{True}
\item $\exists x Gx \eand \exists x \enot Gx$ \hfill \myanswer{False}
\item $\forall x\exists y(Gx \eand Hy)$ \hfill \myanswer{True}
\end{earg}

\problempart
\label{pr.TorF3}
Following the diagram conventions introduced at the end of \S23, consider the following interpretation:
\begin{center}
\begin{tikzpicture}
\node (atom1) at (0,2) {1};
\node (atom2) at (2,2) {2};
\node (atom4) at (0,0) {3};
\node (atom5) at (2,0) {4};
\node (atom6) at (4,0) {5};
\draw[->, thick] (atom1)+(-0.15,0.15) arc (-330:-30:.3);
\draw[->, thick] (atom6)+(0.15,-0.15) arc (-150:150:.3);
\draw[->, thick] (atom1) -- (atom2);
\draw[->, thick] (atom1) -- (atom4);
\draw[<->, thick] (atom1) -- (atom5);
\draw[->, thick] (atom1) -- (atom6);
\draw[->, thick] (atom2) -- (atom6);
\end{tikzpicture}
\end{center}
Determine whether each of the following sentences is true or false in that interpretation:
\begin{earg}
\item $\exists x Rxx$ \hfill \myanswer{True}
\item $\forall x Rxx$ \hfill \myanswer{False}
\item $\exists x \forall y Rxy$ \hfill \myanswer{True}
\item $\exists x \forall y Ryx$ \hfill \myanswer{False}
\item $\forall x \forall y \forall z ((Rxy \eand Ryz) \eif Rxz)$ \hfill \myanswer{False}
\item $\forall x \forall y \forall z ((Rxy \eand Rxz) \eif Ryz)$ \hfill \myanswer{False}
\item $\exists x \forall y \enot Rxy$ \hfill \myanswer{True}
\item $\forall x(\exists y Rxy \eif \exists y Ryx)$ \hfill \myanswer{True}
\item $\exists x \exists y (\enot x = y \eand Rxy \eand Ryx)$ \hfill \myanswer{True}
\item $\exists x \forall y(Rxy \eiff x = y)$ \hfill \myanswer{True}
\item $\exists x \forall y(Ryx \eiff x = y)$ \hfill \myanswer{False}
\item $\exists x \exists y(\enot x = y \eand Rxy \eand \forall z(Rzx \eiff y = z))$ \hfill \myanswer{True}
\end{earg}


\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[a4paper, left=1in, right=1in, top=1in, bottom=1in]{geometry}
\usepackage{amsmath}
\title{Report(pruning)}
\author{Qi Ji}
\date{May 2020}

\begin{document}

\maketitle

\section{Background knowledge}
We can do pruning to optimize the tree size and reduce overfitting. Generally, there are two kinds of pruning: Pre-pruning and Post-pruning.
\subsection{Pre-pruning}
\begin{itemize}
    \item Pre-pruning is also called early stoping.
    \item Pre-pruning stops the tree before it grows perfectly.
    \item People can set maximum depth of the tree to restrict the tree.
\end{itemize}

\subsection{Post-pruning}
\subsubsection{REP(Reduced-Error Pruning)}
REP is one of the simplest forms of Post-pruning.\\
First, calculate the error of a subtree $E_{r}(t)$.
Then, calculate the error of each leaf node of this subtree $E_{t}(Tt)$.
If $$\ E_{r}(t) < \sum E_{r}(T_{t})$$
Then, replace the subtree with a leaf node, whose label is determined by the majority label of the subtree. Repeat this process from the bottom of the tree to the top.
\subsubsection{PEP(Pessimistic-Error Pruning)}
For a leaf node with N samples and E errors, its error rate $e$ is $\frac{E+0.5}{N}$. "0.5" is called penalty factor. For a subtree with L leaf nodes, its error rate is
$$ \ p = \frac{\sum_{i=1}^{L}\ E_{i}+0.5L}{\sum_{i=1}^{L}\ N_{i}}$$
Suppose that all the samples in a subtree is of binomial distribution $B(N,P)$, then the expectation and standard deviation of error before pruning are:
$$\ E_{T} = N*p = N*\frac{\sum_{i=1}^{L}\ E_{i}+0.5L}{\sum_{i=1}^{L}\ N_{i}} = \sum_{i=1}^{L}\ E_{i}+0.5L $$
$$\sigma = \sqrt{N*p*(1-p)}$$
Expectation of error after pruning is:
$$\ E_{t} = N*e = N*\frac{E+0.5}{N} = E+0.5$$
If $$E_{t} - E_{T} < \sigma$$
then prune the subtree. Repeat this process from the top of the tree to the bottom.
\subsubsection{CCP(Cost-Complexity Pruning)}
$\alpha$ is a real number called the complexity parameter, it's defined as follows:
$$\alpha = \frac{R(t)- R(T_{t})}{|T_{t}|-1},$$ where $R(t)$ is the error after pruning, $R(T_{t})$ is the error of the original subtree and $|T_{t}|$ is the number of samples in the subtree.\\
Step1: Calculate the value of $\alpha$ for all the subtrees from the bottom to the top, each time prune the subtree with minimal $\alpha$. Get a set $\{T_{0}, T_{1},..., T_{M}\}$, where $T_{0}$ is a complete tree and $T_{M}$ is a root node.\\
Step2: Pick the best tree from $\{T_{0}, T_{1},..., T_{M}\}$, according to its performance on the testing sets.\\
*Note that if we use CCP to prune the tree, we should separate a testing set from the given training set before training begins.
\section{Approaches to try}
We adopted Pre-pruning by setting the maximum depth of the tree when building it. We considered three methods of Post-pruning: REP(Reduced-Error Pruning), PEP(Pessimistic-Error Pruning) and CCP(Cost-Complexity Pruning). Eventually we tried PEP and CCP, and CCP worked better. Therefore, we chose CCP to do the pruning.
\end{document}


\documentclass{article}

\title{Advanced Computer Graphics\\ Exercise 2}
\author{Phil\'emon Favrod \and Solal Pirelli \and J\'er\'emy Rabasco}
\date{Group 6}

\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{decorations.pathreplacing}
\usepackage{enumerate}

\setlength{\parindent}{0em}
\setlength{\parskip}{1em}

\begin{document}

\maketitle

\paragraph*{Time needed} 17 hours
\paragraph*{Main problems encountered}
\begin{itemize}
\item Understanding the intricacies of the theory.
\item Figuring out what is $\omega_i$ and $\omega_o$ without confusing them.
\item Projecting from the perfect specular reflective reference frame to the local reference frame before figuring out that the \textit{Frame} class could help us.
\end{itemize}

\section*{Question 2.2.3}
In this answer, we will assume the following:
\begin{itemize}
\item Floating point numbers are actually considered as real numbers; and
\item \textit{drand48()} is a perfect uniform random generator in the interval $[0, 1]$.
\end{itemize}
The probability that the loop ends at a given iteration is the probability that a randomly selected point inside a cube with edges of length two is contained inside the upper hemisphere of the inscribed unit sphere. Therefore, it can be computed as
$$
\frac{V_{\text{Hemisphere}}}{V_{\text{Cube}}} = \frac{V_{\text{Sphere}}}{2V_{\text{Cube}}} = \frac{\frac{4}{3}\pi}{2^4} = \frac{\pi}{12} \approx 0.2618.
$$

We are now wondering the expected value of a random variable following a Geometric distribution with probability of success being $p = \frac{\pi}{12}$. Therefore,
the expected number of iterations is $\frac 1p = \frac{12}{\pi} \approx 3.8197$.

\section*{Question 2.3.1}
The challenge here is to find a uniformly distributed point of the unit hemisphere given two random rel numbers uniformly distributed between 0 and 1, say $\xi_1$ and $\xi_2$. The reader might refer to Figure \ref{fig:hemisphere} to understand our solution. It basically proceeds as follows:
\begin{enumerate}[1.]
\item A random point $r$ uniformly distributed over the circular surface which is the base of our hemisphere is generated. The method presented in class is used here, i.e. $(\sqrt{\xi_1}, 2\pi\xi_2)$ is used instead of the naive but biased approach that takes $(\xi_1, 2\pi\xi_2)$ as a random point\footnote{The points of this sentence are represented in polar coordinates in the XY plane.}. As a reminder, the problem with the later is that points near the center of the circular surface are more likely to be picked.
\item Then, this random point is projected onto the hemisphere surface by a simple application of the Pythagorean theorem resulting, in a Cartesian coordinate system, in the point
$$
p = (\sqrt{\xi_1}\cos{\left( 2\pi\xi_2\right)}, \sqrt{\xi_1}\sin{\left( 2\pi\xi_2\right)}, \sqrt{1 - \xi_1}).
$$
\end{enumerate}

\begin{figure}[h]
\centering
\begin{tikzpicture}[thick, scale=1.4]
% base circle
\draw (0,0) arc (180:360:4 and 1);
\draw [dashed] (0,0) arc (180:0:4 and 1);

% hemisphere
\draw (0,0) arc (180:0:4 and 4);
\draw (2,-.85) arc (180:90:2 and 4.85);

% axes
\draw [->, gray] (4,0,0) -- ++(-5,0,0)node[anchor=east]{$x$};
\draw [->, gray] (4,0,0) -- ++(0,-2.5,-2.5)node[anchor=north]{$y$};
\draw [<-, gray] (4,5)node[anchor=south]{$z$} -- (4, 0);

% schema
\draw [dashed] (4,0) -- (2.9,-0.5)node[anchor=north] {$r$};
\draw [dashed] (2.9, -0.5) -- (2.9, 3.19)node[anchor=south] {$p$} -- (4,0);
\draw [decorate,decoration={brace,amplitude=5pt},xshift=4pt,yshift=1pt]
(2.9, 3.19) -- (4,0) node [black,midway,xshift=0.65cm, yshift=0.15cm]{$= 1$};
\end{tikzpicture}
\caption{Generating a uniformly distributed random point of a unit hemisphere given two uniformly distributed random real numbers between 0 and 1.}
\label{fig:hemisphere}
\end{figure}

\section*{Question 3.3.2}
In this part, we want to estimate
$$
L_o(x, \omega_o) = \int_{H^2}f(x, \omega_i, \omega_o)L_i(x, \omega_i)(n \cdot \omega_i)d\omega_i
$$
where $L_o(x, \omega_o)$ represents the radiance reflected when an incoming ray $\omega_i$ with radiance $L_i(x, \omega_i)$ hits a point $x$ of a surface with normal $n$. $\omega_i$, $\omega_o$ and $n$ are unit vectors in the above expression.

The Phong model provides us with the following BRDF:
$$
f(x, \omega_i, \omega_o) = f_d(x, \omega_i, \omega_o) + f_s(x, \omega_i, \omega_o) = \frac{k_d}{\pi} + k_s\frac{n + 2}{2\pi}(\omega_i \cdot s)^n
$$
where $s$ is a unit vector pointing in the perfect specular reflective direction with respect to $\omega_o$. Note that we use the dot product notation here while we could also use $\cos{(\alpha)} := \omega_i \cdot s$ and $\cos{(\theta_i)} := n \cdot \omega_i$ since all our vectors are unit vectors.

We are trying to perform a backward ray-tracing with importance sampling, i.e. trying to guess the most important $\omega_i$ given $\omega_o$ and $x$ to determine $L_o(x, \omega_o)$. We therefore want to find the probability distribution function $p(\omega_i)$ such that the Monte Carlo estimator
$$F_N = \frac{1}{N}\sum\limits_{i=1}^N\frac{f(x, \omega_i, \omega_o)L_i(x, \omega_i)(n \cdot \omega_i)}{p(\omega_i)}
$$
is unbiased and has a small variance. The ideal case -- in which the estimator would actually be equal to the integral -- would be to choose a PDF proportional $f(x, \omega_i, \omega_o)L_i(x, \omega_i)(n \cdot \omega_i)$ but we don't know $\omega_i$ and therefore do not know $L_i(x, \omega_i)$. We therefore take the trade-off to choose $p(\omega_i)$ such that it is proportional to only $f(x, \omega_i, \omega_o)(n \cdot \omega_i)$.

\paragraph{Question 2.3.2a}
We chose $p(\omega_i)$ according to the provided paper \cite{lawrence}. In this paragraph, we will try to capture the intuition out of these formulas. For simplicity reasons, it is easier to decompose $p(\omega_i)$ in its specular and diffuse component and choose $p_s(\omega_i)$ proportional to the specular component and $p_d(\omega_i)$ proportional to the diffuse component.

Regarding the diffuse component, the paper advises use to take $$p_s(\omega_i) = \frac{n+1}{2\pi}\cos^n(\alpha)$$ to get a ray distributed mainly near the specular direction.
One could be surprised that $\frac{n+2}{2\pi}(\omega_i \cdot s)^n(n \cdot \omega_i)$ was not simply chosen.
According to \cite{lafortune}, the main reason is that it lacks integrability and so fails to be a PDF.

For the diffuse case, $p_d(\omega_i) = \frac{(n \cdot \omega_i)}{\pi}$ is the obvious choice. It favors rays that are evenly distributed ray around the surface normal at the point of intersection while limiting the numbers of sample with a 90 degree angle with the surface normal (those rays have little irradiance).

Then with probability $\frac{k_s}{k_s + k_d}$ we consider specular reflection and with probability $\frac{k_d}{k_s + k_d}$ we consider a diffuse reflection. Thus, we perform multiple importance sampling with a scheme that resembles the balance heuristic presented in the book (14.4.1) to obtain

$$p(\omega_i) = \frac{k_s \cdot p_s(\omega_i) + k_d \cdot p_d(\omega_i)}{k_s + k_d}.$$

It is easy to see that this gives an unbiased estimator as the weights in the weighed average used to compute $p(\omega_i)$ are equal to the probabilities to choose a specular sample or a diffuse sample.

\paragraph{Question 2.3.2b}Once we have picked $\omega_i$, one can easily compute\linebreak
$\cos\alpha := \omega_i \cdot s$ where $\alpha$ is the angle between $s$ and $\omega_i$. First note that $$\cos \left (\angle(\omega_o, n)\right ) = \cos \left ( \angle(n,s) \right ) = \omega_o\cdot n.$$ Similarly, we have that
$$\cos\left(\angle (n, \omega_i)\right) = \cos\left(\angle(n,s) + \alpha\right) = \omega_i \cdot n.$$
It follows that $\cos^{-1}{\left(\omega_i \cdot n\right)} - \cos^{-1}{\left(\omega_o\cdot n\right)} = \alpha.$

\section*{Results}

\subsection*{Naive/Lambertian Model}
Figure \ref{fig:lamb} shows the RGB standard deviation as the number of samples increases (from 64 to 4096 samples) under both the Naive and Lambertian model. As one could expect, the standard deviation is slightly less in the Lambertian case than in the naive one for a small sample size. However, as the number of samples approaches infinity, both approach resemble each other in terms of standard deviation. Finally, the difference  is negligible to the human eye as suggests Figure \ref{fig:naive_rendering} and \ref{fig:lamb_rendering}.

\begin{figure}[p]
\centering
\includegraphics[width=\textwidth]{assets/naive_stdev}
\\
\includegraphics[width=\textwidth]{assets/lamb_stdev}

\caption{Comparison between the Naive and the Lambertian Model convergence.}
\label{fig:lamb}
\end{figure}

\begin{figure}[p]
\centering
\includegraphics[width=.5\textwidth]{assets/diffuse-naive_plot_6_red} \\
\includegraphics[width=.5\textwidth]{assets/diffuse-naive_plot_6_green} \\
\includegraphics[width=.5\textwidth]{assets/diffuse-naive_plot_6_blue}
\caption{Deviation graph for the Naive Model (256 samples).}
\label{fig:naive_stdev_details}
\end{figure}

\begin{figure}[p]
\centering
\includegraphics[width=.5\textwidth]{assets/diffuse-brdf_plot_6_red} \\
\includegraphics[width=.5\textwidth]{assets/diffuse-brdf_plot_6_green} \\
\includegraphics[width=.5\textwidth]{assets/diffuse-brdf_plot_6_blue}
\caption{Deviation graph for the Lambertian Model (256 samples).}
\label{fig:lamb_stdev_details}
\end{figure}

\begin{figure}[p]
\centering
\includegraphics[width=.75\textwidth]{assets/naive64}
\\
\includegraphics[width=.75\textwidth]{assets/naive1024}

\caption{A scene rendered using the Naive Model using 64~(top) and 4096~samples~(bottom).}
\label{fig:naive_rendering}
\end{figure}

\begin{figure}[p]
\centering
\includegraphics[width=.75\textwidth]{assets/lamb64}
\\
\includegraphics[width=.75\textwidth]{assets/lamb1024}

\caption{A scene rendered using the Lambertian Model using 64~(top) and 4096~samples~(bottom).}
\label{fig:lamb_rendering}
\end{figure}

\subsection*{Phong Model}
Figure \ref{fig:phong_stdev} shows the RGB standard deviation as the number of sampels increases (again from 64 to 4096 samples) under the Phong model. Figure \ref{fig:phong_stdev_details} shows the details of the deviation while sampling with 64 samples. One can see how the absence of red in the scene is illustrated by its low deviation value and absence of variation.

It is hard to draw general conclusions by comparing the absolute values of such results with the ones of the Lambertian model since this is a different scene even though the deviation of the Phong Model is much smaller. Furthermore, one can observe a quicker convergence as the curves flatten sooner in the Phong Model.
\begin{figure}[h]
\centering
\includegraphics[width=\textwidth]{assets/phong_stdev}
\label{fig:phong_stdev}
\end{figure}

\begin{figure}[p]
\centering
\includegraphics[width=.5\textwidth]{assets/phong-brdf_plot_6_red} \\
\includegraphics[width=.5\textwidth]{assets/phong-brdf_plot_6_green} \\
\includegraphics[width=.5\textwidth]{assets/phong-brdf_plot_6_blue}
\caption{Deviation graph for the Phong Model (64 samples).}
\label{fig:phong_stdev_details}
\end{figure}

\begin{figure}[p]
\centering
\includegraphics[width=\textwidth]{assets/phong64}\\
\includegraphics[width=\textwidth]{assets/phong4096}
\caption{A scene rendered using the Phong Model using 64~(top) and 4096~samples~(bottom).}
\end{figure}

\clearpage
\bibliography{bibliography}{}
\bibliographystyle{plain}
\end{document}


\section{Aperiodic Scheduling}
\begin{center}
	{\renewcommand{\arraystretch}{2}
	\begin{tabular}{|c|c|c|}
		\hline
							& Equal arrival, non-preemptive	& Arbitrary arrival, preemptive \tabularnewline\hline
		Independent Tasks	& EDD							& EDF							\tabularnewline\hline
		Dependent Tasks		& LDF							& EDF*							\tabularnewline\hline
	\end{tabular}}
\end{center}

\subsection{Definitions}
\begin{itemize}
	\item $J = \set{j_1, j_2, \dots, j_n}$ is a set of tasks.
	\item $a_i$ or $r_i$ is the arrival / release time of task $i$.
	\item $d_i$ is the deadline of task $i$.
	\item $C_i$ is the total computation time of task $i$.
	\item $c_i(t)$ is the the remaining execution time of task $i$ at time $t$.
	\item $s_i$ is the start time of task $i$.
	\item $f_i$ is the finish time of task $i$.
	\item $L_i = f_i - d_i$ is the lateness of task $i$.
	\item $E_i = \max\left(0, L_i\right)$ is the exceeding time or tardyness of task $i$.
	\item $X_i = d_i - a_i - C_i$ is the laxity or slack of task $i$.
	\item $w_i = s_i - a_i$ is the waiting time of task $i$.
	\item $R_i = f_i - a_i$ is the response time of task $i$.
\end{itemize}

\subsection{EDD}
Schedule the tasks in order of non-decreasing deadlines. This minimizes the
maximal lateness.

\subsection{EDF}
Always execute the task with the earliest absolute deadline.
Schedulability test:
\begin{equation*}
	\forall i \in [n] . t + \sum_{k = 1}^{i} c_k(t) \leq d_i
\end{equation*}

\subsection{LDF}
Among all tasks without successors select the task with the latest deadline. Put
it in a stack. Repeat until no more tasks. Now execute tasks as they are on the
stack.

\subsection{EDF*}
Modify arrival and deadline of each task and use EDF on modified tasks.
\begin{align*}
	r_j^* &= \max_{j}\left(r_j, \max_{i}\left(r_i^* + C_i | J_i \to J_j\right)\right) \\
	d_i^* &= \min_{i}\left(d_i, \min_{j}\left(d_j^* - C_j | J_i \to J_j\right)\right)
\end{align*}


\section{Sparse matrix orderings}
\label{section:ordering}
\par
The past few years have seen a resurgence of interest and
accompanying improvement in algorithms and software to order sparse
matrices.
The minimum degree algorithm, specifically the multiple external
minimum degree algorithm \cite{liu85-mmd},
was the preferred algorithm of choice for
the better part of a decade.
Alternative minimum priority codes have recently pushed multiple
minimum degree aside,
including approximate minimum degree \cite{ame96-amd} and
approximate deficiency \cite{ng96-mindefIdaho},
\cite{roth98-minfill}.
They offer improved quality or improved run time, and on occasion,
both.
\par
Nested dissection for regular grids \cite{geo73-nested} is within a
factor of optimal with respect to factor entries and operation counts.
One of the earliest attempts, automatic nested dissection
\cite{geo81-book} used a simple profile algorithm to find separators.
It rarely performed as well as minimum degree.
Better heuristics to find separators were brought in from the
electrical device simulation area \cite{lei89-fidmat} and while
these algorithms produced better orderings, the run times kept
them from practical application.
Nested dissection came on its own with two developments.
The first was the application of spectral analysis of graphs to
find separators \cite{pot90-partition}.
The eigenvector associated with the smallest nonzero eigenvalue of
the Laplacian of a graph generates a spectrum of separators.
While the ordering times for spectral nested dissection were still
on the order of ten or more times the cost of a minimum degree ordering,
the ordering quality sometimes made the cost worthwhile.
\par
The key that made nested dissection a competitive and practical
alternative to minimum degree was the introduction of multilevel
ideas ---
to find a separator on a graph, first find a separator on a coarse
graph and project back to the original.
Early implementations include \cite{bar93-partition}
and \cite{bui93-partition}.
Multilevel algorithms are very popular in current software
including
{\bf CHACO}
\cite{hen93-chaco},
\cite{hen92-partition},
{\bf METIS}
\cite{kar95-multilevel},
\cite{karypis98metis},
{\bf BEND}
\cite{hr98-msnd},
{\bf WGGP}
\cite{gup96-WGPP}
and
{\bf PCO}
\cite{rag95-PCO}.
\par
{\bf SPOOLES} also includes a hybrid ordering approach called
multi-section
\cite{ash97-partition},
\cite{ash98-maxflow},
\cite{ash98-multisection} and
\cite{ro95-hybrid}.
For some types of graphs, nested dissection does much better than
minimum degree, for others much worse.
Multisection is an ordering that uses both nested dissection and
minimum degree to create an ordering that is almost always as good
or better than the better of nested dissection or minimum degree
and rarely much worse.
\par
\subsection{The {\tt Graph} object}
\label{subsection:graph}
\par
Our goal is to find a permutation matrix $P$ such that the
factorization of $PAP^T$ has low-fill.
This is a symbolic step, we do not need to know the numerical
entries in $A$, but we do need to know the structure of $A$.
More specifically, since
when computing $LDU$, $U^TDU$ or $U^HDU$ factorizations
we consider $A$ to have symmetric structure.
We need to know the structure of $A + A^T$,
and so we need to construct the graph of $A+A^T$.
The way that {\bf SPOOLES} deals with the graph of $A + A^T$ is
via a {\tt Graph} object.
There are several ways to construct a {\tt Graph} object,
some are high level, some are low level.
\par
Inside each {\tt Graph} object is an {\tt IVL} object.
{\tt IVL} stands for {\tt I}nteger {\tt V}ector {\tt L}ist,
and stores the adjacency lists for the vertices.
For example, consider a $3 \times 4$ grid with a nine point operator.
The adjacency lists for this graph are stored in the {\tt IVL}
object, displayed in Figure~\ref{fig:3x4-grid}.
(The listing comes from the {\tt IVL\_writeForHumanEye()} method.)
\par
\input fig3x4grid.tex
\par
One can construct the {\tt IVL} object directly.
There are methods to set the number of lists,
to set the size of a list,
to copy entries in a list into the object.
It resizes itself as necessary.
However, if one already has the matrix entries of $A$ stored in an
{\tt InpMtx} object (which is the way that {\bf SPOOLES} deals with
sparse matrices), there is an easier way.
One can create an {\tt IVL} object from the {\tt InpMtx} object,
as follows.
\begin{verbatim}
InpMtx   *A ;
IVL      *adjIVL ;

adjIVL = InpMtx_fullAdjacency(A) ;
\end{verbatim}
During a block shifted Lanczos eigenanalysis, one needs the graph
of $A + \sigma B$ for a pair of matrices.
There is a method to construct the {\tt IVL} object for this case.
\begin{verbatim}
InpMtx   *A, *B ;
IVL      *adjIVL ;

adjIVL = InpMtx_fullAdjacency2(A, B) ;
\end{verbatim}
Recall, we actually construct the adjacency structure of $A + A^T$
(or $A + A^T + B + B^T$), because the graph object is undirected,
and so needs a symmetric structure.
\par
Once we have an {\tt IVL} object, we can construct a {\tt Graph}
object as follows.
\begin{verbatim}
Graph   *graph ;
IVL     *adjIVL ;
int     nedges, neqns ;

nedges = IVL_tsize(adjIVL) ;
graph = Graph_new() ;
Graph_init2(graph, 0, neqns, 0, nedges, neqns, nedges, adjIVL, NULL, NULL) ;
\end{verbatim}
This is an initializer for the {\tt Graph} object, one that
takes as input a complete {\tt IVL} adjacency object.
The {\tt 0} and {\tt NULL} fields are not applicable here.
(The {\tt Graph} object is sophisticated --- it can have weighted or
unweighted vertices, weighted or unweighted edges, or both, and it
can have boundary vertices. Neither is relevant now.)
\par
\subsection{Constructing an ordering}
\label{subsection:order}
\par
Once we have a {\tt Graph} object, we can construct an ordering.
There are four choices:
\begin{itemize}
\item
minimum degree, (actually multiple external minimum degree,
from \cite{liu85-mmd}),
\item
generalized nested dissection,
\item
multisection, and
\item
the better of generalized nested dissection and multisection.
\end{itemize}
Minimum degree takes the least amount of CPU time.
Generalized nested dissection and multisection both require the
a partition of the graph, which can be much more expensive to compute
than a minimum degree ordering.
By and large, for larger graphs nested dissection generates
better orderings than minimum degree, and the difference in quality
increases as the graph size increases.
Multisection is an ordering which almost all the time does about as
well as the better of nested dissection and minimum degree.
The user should know their problem and choose the ordering.
Here are some rules of thumb.
\begin{itemize}
\item
If the matrix size is small to moderate in size, say up to 10,000
rows and columns, use minimum degree.
The extra ordering time for nested dissection or multisection may
not make up for any decrease in factor or solve time.
\item
If the matrix size comes from a partial differential equation that
has several degrees of freedom at a grid point, use multisection or
nested dissection, no matter the size.
\item
If the target is a parallel factorization, use nested dissection.
\item
For 2-D problems, minimum degree is usually good enough, for 3-D
problems, use nested dissection or multisection.
\item
To be safe, use the better of the nested dissection and
multisection methods.
The additional ordering time is not much more than using either of
them alone.
\end{itemize}
Before we discuss the different ordering methods found in {\tt
SPOOLES}, what is the output of the ordering?
\par
One normally thinks of a permutation matrix $P$ as represented by a
permutation vector, a map from old vertices to new vertices, or
vice versa.
That is sufficient if one is just concerned with permuting a
matrix, but there is much more information needed for the factor
and solves.
The {\tt SPOOLES} ordering methods construct and return an {\tt
ETree} object that holds the {\it front tree}.
We will discuss this object in the next section.
Let us now look at the four different wrapper methods for the
orderings.
\begin{verbatim}
ETree   *etree ;
Graph   *graph ;
int     maxdomainsize, maxsize, maxzeros, msglvl, seed ;
FILE    *msgFile ;

etree = orderViaMMD(graph, seed, msglvl, msgFile) ;
etree = orderViaND(graph, maxdomainsize, seed, msglvl, msgFile) ;
etree = orderViaMS(graph, maxdomainsize, seed, msglvl, msgFile) ;
etree = orderViaBestOfNDandMS(graph, maxdomainsize, maxzeros,
                              maxsize, seed, msglvl, msgFile) ;
\end{verbatim}
Now let us describe the different parameters.
\begin{itemize}
\item
The {\tt msglvl} and {\tt msgFile} parameters are used to control
output.
When {\tt msglvl = 0}, there is no output.
When {\tt msglvl > 0}, output goes to the {\tt msgFile} file.
The {\bf SPOOLES} library is a research code, we have left a great
deal of monitoring and debug code in the software.
Large values of {\tt msglvl} may result in large message files.
To see the statistics generated during the ordering, use
{\tt msglvl = 1}.
\item
The {\tt seed} parameter is used as a random number seed.
(There are many places in the graph partitioning and minimum degree
algorithms where randomness plays a part.
Using a random number seed ensures repeatability.)
\item
{\tt maxdomainsize} is used for the nested dissection and
multisection orderings.
This parameter is used during the graph partition.
Any subgraph that is larger than {\tt maxdomainsize} is split.
We recommend using a value of {\tt neqns/16} or {\tt neqns/32}.
Note: {\tt maxdomainsize} must be greater than zero.
\item
{\tt maxzeros} and {\tt maxsize} are used to transform the front tree.
In effect, we have placed the ordering functionality as well as the
transformation of the front tree into this method.
So let's wait until the next section to learn about the
{\tt maxzeros} and {\tt maxsize} parameters.
\end{itemize}
There is also the capability to create a front tree from a graph
using any permutation vectors, e.g., a permutation that came from
another graph partitioning or ordering library.
\begin{verbatim}
ETree   *etree ;
Graph   *graph ;
int     newToOld[], oldToNew[] ;

etree = ETree_new() ;
ETree_initFromGraphWithPerms(etree, graph, newToOld, oldToNew) ;
\end{verbatim}
The output from this method is a {\it vertex} elimination tree,
there has been no merging of rows and columns into fronts.
But we are getting ahead of ourselves.
\par
\subsection{Results}
\label{subsection:results}
\par
Let us look at the four different orderings and compare the
ordering time, the number of factor entries, and number of
operations in the factorization.
(The latter two are for a real, symmetric matrix.)
\par
The {\tt R2D10000} matrix is a randomly triangulated grid on the
unit square. There are 10000 grid points, 100 points are equally
spaced along each boundary.
The remainder of the vertices are placed in the interior using
quasi-random points, and the Delauney triangulation is computed.
\begin{center}
\begin{tabular}{rrrrrrr}
& \multicolumn{3}{c}{minimum degree}
& \multicolumn{3}{c}{nested dissection} \\
seed & CPU & \# entries & \# ops & CPU & \# entries & \# ops \\
10101 & 1.4 & 212811 & 11600517 & 4.8 & 213235 & 11092015 \\
10102 & 1.5 & 211928 & 11654848 & 4.6 & 216555 & 11665187 \\
10103 & 1.5 & 222119 & 13492499 & 4.9 & 217141 & 11606103 \\
10104 & 1.5 & 214151 & 11849181 & 5.0 & 217486 & 11896366 \\
10105 & 1.5  & 216176 & 12063326 & 4.8 & 216404 & 11638612 \\
& \multicolumn{3}{c}{multisection}
& \multicolumn{3}{c}{better of ND and MS} \\
seed & CPU & \# entries & \# ops & CPU & \# entries & \# ops \\
10101 & 4.6 & 207927 & 10407553 & 6.2 & 208724 & 10612824 \\
10102 & 4.6 & 210364 & 10651916 & 6.2 & 211089 & 10722231 \\
10103 & 4.6 & 215795 & 11760095 & 6.4 & 217141 & 11606103 \\
10104 & 4.6 & 210989 & 10842091 & 6.1 & 212828 & 11168728 \\
10105 & 4.8 & 209201 & 10335761 & 6.1 & 210468 & 10582750
\end{tabular}
\end{center}
For the nested dissection and multisection orderings, we used
{\tt maxdomainsize = 100}.
We see that there is really little difference in ordering quality,
while the minimum degree ordering takes much less time than the
other orderings.
\par
Let us now look at a random triangulation of a unit cube.
This matrix has 13824 rows and columns.
Each face of the cube has a $22 \times 22$ regular grid of points.
The remainder of the vertices are placed in the interior using
quasi-random points, and the Delauney triangulation is computed.
\begin{center}
\begin{tabular}{rrrrrrr}
& \multicolumn{3}{c}{minimum degree}
& \multicolumn{3}{c}{nested dissection} \\
seed & CPU & \# entries & \# ops & CPU & \# entries & \# ops \\
10101 & 9.2 & 5783892 & 6119141542 & 27.8 & 3410222 & 1921402246 \\
10102 & 8.8 & 5651678 & 5959584620 & 31.4 & 3470063 & 1998795621 \\
10103 & 8.8 & 6002897 & 6724035555 & 25.8 & 3456887 & 1986837981 \\
10104 & 8.6 & 5888698 & 6652391434 & 29.6 & 3459432 & 1977133474 \\
10105 & 8.5 & 5749469 & 6074040475 & 30.1 & 3567956 & 2223250836 \\
& \multicolumn{3}{c}{multisection}
& \multicolumn{3}{c}{better of ND and MS} \\
seed & CPU & \# entries & \# ops & CPU & \# entries & \# ops \\
10101 & 26.9 & 3984032 & 2807531148 & 34.3 & 3410222 & 1921402246 \\
10102 & 29.7 & 4209860 & 3266381908 & 37.0 & 3470063 & 1998795621 \\
10103 & 23.5 & 4044399 & 2963782415 & 31.7 & 3456887 & 1986837981 \\
10104 & 25.9 & 4239568 & 3325299298 & 34.8 & 3459432 & 1977133474 \\
10105 & 27.2 & 4039078 & 2945539836 & 35.9 & 3567956 & 2223250836
\end{tabular}
\end{center}
Again there is about a factor of three in CPU time comparing
minimum degree against the others,
but unlike R2D10000, the minimum degree requires far
fewer operations than the others.
Note how the multisection ordering requires about 50\% more
operations than nested dissection.
The situation can be reversed in other cases.
That is the reason that we recommend using the wrapper that
generates the better of the nested dissection and multisection
orderings.


%!TEX root = dsp_2nd_program_hw.tex
\subsection{DTMF Detection --- find\_dtmf\_symbol.h}
For convenience, we wrote a function to obtain the symbol of a DTMF signal.
By inputing the max two frequency, we can get the symbol $1,2,3,4,5,6,7,8,9,A,B,C,D,0,*,\#$

The code is shown in section~\ref{code:finddtmf}

\subsection{Recognition of Dataset 1 --- 10 signals}

\subsubsection{FFT --- DTMF\_1.cpp}
The result is
$$5,1,6,9,8,7,3,4,0,2$$
for data
$$1081,1107,1140,1219,1234,1489,1507,1611,1942,1944$$
respectively, as shown in Table~\ref{tab:result}.
The code is shown in section~\ref{dtmf1}.

\begin{table}[htp]
\centering
{\small
\begin{tabular}{c|ccc}
    \hline
    \textbf{Signal length} & \textbf{1st freq} & \textbf{2nd freq} & \textbf{symbol} \\
    \hline
	   1081 & 769 & 1337 & 5 \\
	   1107 & 696 & 1212 & 1 \\
	   1140 & 770 & 1477 & 6 \\
	   1219 & 852 & 1477 & 9 \\
	   1234 & 852 & 1337 & 8 \\
	   1489 & 852 & 1212 & 7 \\
	   1507 & 696 & 1477 & 3 \\
	   1611 & 770 & 1212 & 4 \\
	   1942 & 942 & 1337 & 0 \\
	   1944 & 696 & 1337 & 2 \\
	\hline
\end{tabular}
}
\vspace{-0.1in}
\caption{DTMF result for 10 respective signals}
\label{tab:result}
\end{table}

\subsubsection{Goertzel --- DTMF\_2.cpp}
Goertzel has shown exactly same result with FFT. The code is shown in section~\ref{dtmf2}.

\newpage

\subsection{Recognition of Dataset 2 --- A long signal, DTMF\_3.cpp}

\subsubsection{Judge the start-end time}
By Matlab, we can obtain the time-zone signals as shown in Figure~\ref{fig:timezone3}. We then manually get the start, end time of each of signal as shown in Table~\ref{tab:startendtime}.
\begin{figure}[htp]
	\centering
	\includegraphics[width=10cm]{../fig/data2.eps}
	\vspace{-0.1in}
	\caption{Timezone signal of Data 2}
	\label{fig:timezone3}

\end{figure}
\vspace{-0.2in}

\begin{table}[htp]
\centering
{\small
\begin{tabular}{c|cc|c|cc}
    \hline
    \textbf{\#} & \textbf{Start time} & \textbf{End time} & \textbf{\#} & \textbf{Start time} & \textbf{End time} \\
    \hline
	   1 & 0.45 & 0.66	& 9   & 3.78 & 4.00	\\
	   2 & 0.86 & 1.07	& 10  & 4.18 & 4.40	\\
	   3 & 1.27 & 1.47	& 11  & 4.58 & 4.79	\\
	   4 & 1.70 & 1.89	& 12  & 5.01 & 5.22	\\
	   5 & 2.09 & 2.29	& 13  & 5.43 & 5.64	\\
	   6 & 2.50 & 2.72	& 14  & 5.87 & 6.06	\\
	   7 & 2.92 & 3.10	& 15  & 6.26 & 6.45	\\
	   8 & 3.34 & 3.55	& & &\\
	\hline
\end{tabular}
}
\vspace{-0.1in}
\caption{Start-End time of each signal}
\label{tab:startendtime}
\end{table}
\vspace{-0.3in}

\subsubsection{Result}
By the Goertzel method, we can obtain the symbols for the long signal are:
$$2,0,5,8,9,1,1,3,2,0,u,4,6,4,9$$
$u$ represents unknown, i.e. we cannot detect the 11th symbol, its amplitude response is shown in Figure~\ref{fig:freq11}.From the figure, we can see that we don't have two target frequencies which have a significant amplitude, s.t. we cannot decipher this symbol. The code is shown in section~\ref{dtmf3}.
\begin{figure}[htp]
	\centering
	\includegraphics[width=16cm]{../fig/freq_11.png}
	\caption{Amplitude response of the 11th symbol}
	\label{fig:freq11}
\end{figure}


% !TeX root = ../main.tex
\chapter{\abstractname}

In this thesis, we use the interactive theorem prover Isabelle/HOL \parencite{DBLP:books/sp/NipkowPW02}
to verify an imperative implementation of the classical B-tree data structure \parencite{DBLP:journals/acta/BayerM72}.
The implementation supports set membership and insertion queries
with efficient binary search for intra-node navigation.
This is accomplished by first specifying the structure abstractly
in the functional modeling language HOL and proving functional correctness.
Using manual refinement, we derive an imperative implementation
in Imperative/HOL \parencite{DBLP:conf/tphol/BulwahnKHEM08}.
We show the validity of this refinement using
the separation logic utilities from the
Isabelle Refinement Framework \parencite{DBLP:journals/jar/Lammich19}.
The code can be exported to the programming languages SML and Scala.
We examine the runtime of all operations indirectly by reproducing results
of the logarithmic relationship between height and the number of nodes.
Considering development time and lines of code,
our approach compares well to other approaches at mechanized
verifications of the B-tree data structure.

%TODO: Abstract


\documentclass[11pt]{scrartcl}
\usepackage[utf8]{inputenc}
\usepackage[margins=1in]{geometry} % margins
\usepackage{amsmath}
\usepackage{graphicx}
\usepackage{natbib}
\usepackage{url}
\title{Title}
\subtitle{Empirical Behavioral Economics, Boston College}
\author{Your name}
\date{Fall 2019}
\begin{document}
\maketitle
\begin{abstract}
Here is the abstract.
\end{abstract}

\section{Introduction}

\section{Related Literature}

\section{Design}

\section{Conclusion}

\section{LaTeX stuff}

Here is inline math: $y = \beta x + \epsilon$.

Here is a numbered equation:

\begin{equation}
    y = \beta x + \epsilon
\end{equation}

Here is an unnumbered equation:

\begin{equation*}
y = \beta x + \epsilon
\end{equation*}

Here is a citation with the author names as part of the sentence \citet{hamilton2015zoology}.
Here is an end-of-sentence citation \citep{hamilton2015zoology}.

Learn \LaTeX in 30 minutes: \url{https://www.overleaf.com/learn/latex/Learn_LaTeX_in_30_minutes}

\newpage
\bibliographystyle{chicago}
\bibliography{references.bib}

\end{document}


The \code{lm} operator finds model coefficients.
\index{P}{lm@\texttt{lm}}
\index{P}{Modeling!lm@\texttt{lm}}
\index{C}{coefficients!computing best fitting}
To illustrate,
here's a pair of statements that read in a data frame and fit a model
to it:
\begin{Schunk}
\begin{Sinput}
> swim = fetchData("swim100m.csv")
> mod = lm(time ~ year + sex, data=swim)
\end{Sinput}
\end{Schunk}
The first argument to \code{lm} is a model design, the second is the data frame.

\datasetSwimming

The object  created by \code{lm} --- here given the name \code{mod} --- contains a variety of
information about the model.
To access the coefficients themselves, use the \code{coef}
\index{P}{coef@\texttt{coef}}
\index{P}{Modeling!coef@\texttt{coef}}
operator applied to the model:
\begin{Schunk}
\begin{Sinput}
> coef(mod)
\end{Sinput}
\begin{Soutput}
(Intercept)        year        sexM
    555.717      -0.251      -9.798
\end{Soutput}
\end{Schunk}

As shorthand to display the coefficients, just type the
name of the object that is storing the model:
\begin{Schunk}
\begin{Sinput}
> mod
\end{Sinput}
\begin{Soutput}
...
(Intercept)         year         sexM
    555.717       -0.251       -9.798
\end{Soutput}
\end{Schunk}

A more detailed report can be gotten with the \code{summary}
operator.
\index{P}{summary@\texttt{summary}!for \texttt{lm}}
\index{P}{Modeling@regression report with \texttt{summary}}
\index{P}{Regression report with \texttt{summary}}
\index{C}{regression report!computing}
 This gives additional statistical information
that will be used in later chapters:
\begin{Schunk}
\begin{Sinput}
> summary(mod)
\end{Sinput}
\end{Schunk}
% latex table generated in R 2.13.0 by xtable 1.5-6 package
% Tue Aug 23 18:13:08 2011
\begin{tabular}{rrrrr}
  \hline
 & Estimate & Std. Error & t value & Pr($>$$|$t$|$) \\
  \hline
(Intercept) & 555.7168 & 33.7999 & 16.44 & 0.0000 \\
  year & -0.2515 & 0.0173 & -14.52 & 0.0000 \\
  sexM & -9.7980 & 1.0129 & -9.67 & 0.0000 \\
   \hline
\end{tabular}
\index{C}{model value!calculating}

From time to time in the exercises, you will be asked to calculate
model values ``by hand.''  This is accomplished by multiplying
the coefficients by the appropriate values and adding them up.  For
example, the model value for a male swimmer in 2010 would be:
\begin{Schunk}
\begin{Sinput}
> 555.7 - 0.2515*2010 - 9.798
\end{Sinput}
\begin{Soutput}
[1] 40.4
\end{Soutput}
\end{Schunk}
Notice that the ``value'' used to multiply the intercept is always 1,
and the ``value'' used for a categorical level is either 0 or 1
depending on whether there is a match with the level.  In this example,
since the swimmer in question was male, the value of \indicatorVar{sex}{M} is 1.
If the swimmer had been female, the value for
\indicatorVar{sex}{M} would have been 0.

When a model includes interaction terms, the interaction coefficients
need to be multiplied by all the values involved in the interaction.
For example, here is a model with an interaction between \VN{year} and
\VN{sex}:
\index{C}{interaction term!computing model values}
\begin{Schunk}
\begin{Sinput}
> mod2 = lm(time ~ year*sex, data=swim)
> coef(mod2)
\end{Sinput}
\begin{Soutput}
(Intercept)        year        sexM   year:sexM
    697.301      -0.324    -302.464       0.150
\end{Soutput}
\begin{Sinput}
> 697.3 - 0.3240*2010 - 302.5 + 0.1499*2010
\end{Sinput}
\begin{Soutput}
[1] 44.9
\end{Soutput}
\end{Schunk}
The \code{year:sexM} coefficient is being multiplied by the year
(2010) and the value of \indicatorVar{sex}{M}, which is 1 for this
male swimmer.

\subsubsection*{Other Useful Operators}

\begin{description}

\index{P}{cross@\texttt{cross}*}
\index{P}{Data!cross@\texttt{cross}*}
\index{C}{crossing categorical variables}
\index{C}{categorical variable!crossing}
\item[\function{cross}] will combine two categorical variables into a single
  variable.  For example, in the Current Population Survey data, the
  variable \VN{sex} has levels F and M, while the variable \VN{race}
  has levels W and NW.  Crossing the two variables combines them; the
  new variable has four levels: F.NW, M.NW, F.W, M.W:
\begin{Schunk}
\begin{Sinput}
> cps = fetchData("cps.csv")
> cross(cps$sex, cps$race)
\end{Sinput}
\end{Schunk}
\begin{Schunk}
\begin{Soutput}
 [1] M:W  M:W  F:W  F:W  M:W  F:W  F:W  M:W  M:W  F:W  M:W  M:W  M:W  M:W
[15] M:W  M:W  M:W  M:NW M:NW M:W  F:NW M:NW
Levels: F:NW F:W M:NW M:W
... for 534 cases altogether ...
\end{Soutput}
\end{Schunk}

\index{P}{as.factor@\texttt{as.factor}}
\index{P}{Data!as.factor@\texttt{as.factor}}
\index{C}{categorical variable!from quantitative}
\index{C}{quantitative variable!to categorical}
\item[\function{as.factor}] will convert a quantitative variable to a categorical
  variable.  This is useful when a quantity like \VN{month} has been
  coded as a number, say 1 for January and 2 for February, etc.
  but you do not want models to treat it as such.

  To illustrate, consider two different models of the usage
  temperature versus month:
\begin{Schunk}
\begin{Sinput}
> utils = fetchData("utilities.csv")
> mod1 = lm(temp ~ month, data=utils)
> mod2 = lm(temp ~ as.factor(month), data=utils)
\end{Sinput}
\end{Schunk}

\datasetUtilities
Here are the graphs of those models:

\begin{Schunk}
\begin{Sinput}
> xyplot(temp + fitted(mod1) ~ month, data=utils)
\end{Sinput}
\end{Schunk}
\includegraphics{Figures/formulas-util-mod1}

\begin{Schunk}
\begin{Sinput}
> xyplot( temp + fitted(mod2) ~ month, data=utils )
\end{Sinput}
\end{Schunk}
\includegraphics{Figures/formulas-util-mod2}

\begin{comment}
utils = read.csv("/users/kaplan/kaplanfiles/sbook/datasets/utilities.csv")
mod1 = lm( temp ~ month, data=utils)
xyplot( temp ~ month, data=utils, main="Model 1")
trellis.focus()
x = seq(1,12)
y = 39.04 + 1.63*x
llines(x,y,col='red',lwd=3)
trellis.unfocus()
\end{comment}

\begin{comment}
mod2 = lm( temp ~ as.factor(month)-1, data=utils)
x = 1:12
y = as.numeric( coefficients(mod2))
xyplot( temp ~ month, data=utils, main="Model 2")
trellis.focus()
llines(  x,y, lwd=3, col='red')
trellis.unfocus()
\end{comment}

In the first model, month is treated quantitatively, so the model term
\VN{month} produces a straight-line relationship that does not
correspond well to the data.

In the second model, month is treated categorically, allowing a more
complicated model relationship.

% \item[abbreviate] shortens the names of factors

\end{description}