<s>[INST] Does anyone know a reference to best-fitting lines with integral coefficients? I'm writing up a manual on how to generate "nice" Linear Algebra problems; that is, where the solutions tend to be integral. I "discovered" the following fact about the best-fitting line:
Theorem. Let $x_1,x_2,\ldots,x_n$ be integers such that (a) $\displaystyle\sum_{i=1}^n x_i=0$, and (b) $x_i\not=x_j$ for some $i,j$. Then the best-fitting line $y=ax+b$
for the data points $(x_1,y_1), (x_2,y_2), \ldots, (x_n,y_n)$
has integral coefficients iff 
$$\sum_{k=1}^n x_i y_i \equiv 0 \pmod {\sum_{i=1}^n {x_i}^2}
\quad\quad\quad\quad{\rm and}\quad\quad\quad\quad
\sum_{k=1}^n y_i \equiv 0 \pmod {n}.$$
This is the sort of result which is nice, was probably discovered before, and which I cannot find any reference to. Has anyone seen this in a paper anywhere?
 [/INST] The trial function is 
$$
 y(x) = a_{0} + a_{1} x. 
$$
The data will consist of a sequence of measurements $\left\{ x_{k}, y_{k} \right\}_{k=1}^{m}.$
The goal is to create restraints to insure that $a\in\mathbb{Z}^{2}$.

Derivation
The linear system is
$$
\begin{align}
  \mathbf{A} a  &= y \\
%
\left[ \begin{array}{cc}
 \mathbf{1} & x
\end{array} \right]
%
\left[ \begin{array}{c}
 a_{0} \\ a_{1}
\end{array} \right]
%
&=
\left[ \begin{array}{c}
 y
\end{array} \right]
%
\end{align}
$$
The normal equations are
$$
\begin{align}
  \mathbf{A}^{*} \mathbf{A} a  &= \mathbf{A}^{*} y \\
%
\left[ \begin{array}{cc}
 \mathbf{1} \cdot \mathbf{1} & \mathbf{1} \cdot x \\
 x \cdot \mathbf{1} & x \cdot x
\end{array} \right]
%
\left[ \begin{array}{c}
 a_{0} \\ a_{1}
\end{array} \right]
%
&=
\left[ \begin{array}{c}
 \mathbf{1} \cdot y \\
 x \cdot y
\end{array} \right].
%
\end{align}
$$
The solution for the normal equations is
$$
\begin{align}
  a &= \left( \mathbf{A}^{*} \mathbf{A} \right)^{-1} \mathbf{A}^{*} y \\
%
&=
%
\left(
 \det \left( \mathbf{A}^{*} \mathbf{A} \right)
\right)^{-1}
%
\left[ \begin{array}{rr}
 x \cdot x & -\mathbf{1} \cdot x \\
 -\mathbf{1} \cdot x & \mathbf{1} \cdot \mathbf{1}
\end{array} \right]
%
\left[ \begin{array}{c}
 \mathbf{1} \cdot y \\
 x \cdot y
\end{array} \right].
%
\end{align}
$$
The task is simplified with the a priori constraint that the $x$ values have zero mean:
$$
  \mathbf{1} \cdot x = 0.
$$
Now the solution is 
$$
\begin{align}
  a
%
&=
%
\left(
 \det \left( \mathbf{A}^{*} \mathbf{A} \right)
\right)^{-1}
%
\left[ \begin{array}{cc}
 x \cdot x & 0 \\
 0 & \mathbf{1} \cdot \mathbf{1}
\end{array} \right]
%
\left[ \begin{array}{c}
 \mathbf{1} \cdot y \\
 x \cdot y
\end{array} \right].
%
\end{align}
$$
Fix the denominator
Turn attention to the denominator
$$
\det 
\left( \mathbf{A}^{*} \mathbf{A} \right)
%
=
%
\left( \mathbf{1} \cdot \mathbf{1} \right) \left( x \cdot  x \right) -
\left( \mathbf{1} \cdot x \right) 
%
=
%
\left( \mathbf{1} \cdot \mathbf{1} \right) \left( x \cdot  x \right).
$$
The solution is
$$
 a_{LS} = \left( \left( \mathbf{1} \cdot \mathbf{1} \right) \left( x \cdot  x \right)\right)^{-1} 
%
\left[ \begin{array}{c}
 \left( x \cdot x \right) \left( \mathbf{1} \cdot y \right) \\
 \left( \mathbf{1} \cdot \mathbf{1} \right) \left( x \cdot y \right)
\end{array} \right].
$$
The explicit form for intercept $a_{0}$ and slope $a_{1}$ makes the constraints apparent:
$$
a_{0} = 
\frac{\mathbf{1} \cdot y} {\mathbf{1} \cdot \mathbf{1}}, \quad
%
a_{1} = 
\frac{ x \cdot y} { x \cdot x }
%
$$
</s>