# How to find a “least squares” line with a known slope?

I have gps trackings that I know they fall into a certain pattern - a line with a known angle. How do I find the line that minimizes the distances of the points from it but is in the correct angle?

Unfortunately, I can't post an image for example.

• Welcome to Mathematics Stack Exchange! I would suggest you to explain a little bit how you would try to solve it, at least your thoughts about it, so other people could help you better. Good luck! – iadvd Jul 29 '15 at 8:01

If I well understand, you have a set of $n$ points $(x_1,y_1),(x_2,y_2),...,(x_k,y_k),..., (x_n,y_n)$ and you want to fit a straight line $y=ax+b$ which coefficient $a$ is known.

This is a regression problem of the kind $Y(x)=b$ where $Y(x)=y(x)-ax$

Hense $$b\simeq \frac{1}{n}\sum_{k=1}^n (y_k-ax_k)$$

In this particular case ($a$ known), the adjusted value of $b$ is the same if we consider the orthogonal distances.

• Why $\simeq$ and not $=$ ? Cheers – Claude Leibovici Jul 29 '15 at 9:06
• OK for $=$ if you prefer. Cheers ! – JJacquelin Jul 29 '15 at 9:18
• Hi JJacquelin, thank you for your answer, this is exactly what I was looking for. Do you know how can I also get the estimated points on the line? – user3316066 Jul 29 '15 at 11:59
• What do you mean exactly as "estimated points on the line" ? – JJacquelin Jul 29 '15 at 12:17
• Sorry, I mean, is there a way, after I have the equation, to match a point on the line for every point from the input? – user3316066 Jul 29 '15 at 15:35

Consider your data as points $$(x_i,y_i)$$ and you have $$n$$ data points, then you want to find $$\beta_1 ,\beta_2$$ such that $$y\simeq\beta_1+\beta_2x$$.
$$X\beta= \begin{bmatrix} 1 & x_1 \\ 1 & x_2 \\ 1 & x_3 \\ \vdots & \vdots\\ 1 & x_n\\ \end{bmatrix} \begin{bmatrix} \beta_1\\ \beta_2\\ \end{bmatrix}=\begin{bmatrix} y_1\\ y_2\\ \vdots\\ y_n \end{bmatrix}=y$$ If you have received lots of data from gps your $$X$$ matrix is tall and a pseudoinverse can be found as:

$$X^+=(X^TX)^{-1}X^T$$

And then you can find the solution:

$$\beta=X^+y$$

It's MATLAB code is

beta=X\y;


If $$\beta_2$$ is known and you want to find $$\beta_1$$ such that, $$y-\beta_2x\simeq\beta_1$$ then
$$X\beta= \begin{bmatrix} 1 \\ 1 \\ 1 \\ \vdots\\ 1 \\ \end{bmatrix} \begin{bmatrix} \beta_1\\ \end{bmatrix}=\begin{bmatrix} y_1-\beta_2x_1\\ y_2-\beta_2x_2\\ \vdots\\ y_n-\beta_2x_n \end{bmatrix}$$
$$X^+=(X^TX)^{-1}X^T=(\begin{bmatrix} 1 & 1 & 1 & \cdots& 1 \end{bmatrix} \begin{bmatrix} 1 \\ 1 \\ 1 \\ \vdots\\ 1 \\ \end{bmatrix})^{-1} \begin{bmatrix} 1 & 1 & 1 & \cdots& 1 \end{bmatrix}$$ And the answer will be:
$$\beta_1=X^+y=\frac{1}{n}\begin{bmatrix} 1 & 1 & 1 & \cdots& 1 \end{bmatrix}\begin{bmatrix} y_1-\beta_2x_1\\ y_2-\beta_2x_2\\ \vdots\\ y_n-\beta_2x_n \end{bmatrix}$$ $$\beta_1=\frac{1}{n}\sum_{k=1}^n (y_i-\beta_2x_i)$$
• If I well understand the question of user3316066, the parameter $\beta_2$ is known and given as an initial input. So, the regression is of lower case (no need to optimise and compute another value for $\beta_2$). – JJacquelin Jul 29 '15 at 8:28