Date

## # Aim

Minimize $$\sum_i \mathrm{distance}^2(\vec r_i, \mathrm{line})=\sum_i (\vec r_i\cdot \hat n-\rho)^2$$ for line $\vec r\cdot \hat n-\rho=0$. It is equivalent to

• The principle axis with least moment of inertia
• The eigenvector with largest eigenval for the covariance matrix

## # When is it useful?

Only when fitting geometrical dots, which should be rotational invariant. Ordinary linear fit does not has such an invariance.

For most common case, the $x, y$ has different dimension. As an example, the relation between weight $w$ and height $h$ is not invariant under rotation, because there is no mixture between them. In this condition, we need invariance of scaling for any axis.

## # Code

In :
def clinearfit(lx, ly):
x, y=mean(lx), mean(ly)
val, vec=eigh(cov(lx, ly))
idx = val.argsort()
vec = vec[:, idx]
return array([x, y]), vec

In :
def fitline(lx, ly, extend=0.1):
center, direct=clinearfit(lx, ly)
assert(direct)
k=direct/direct
dr=transpose(vstack([lx, ly]))-center
ds=dot(dr, direct)
def f(x):return k*(x-center)+center
x=sort(array([max(ds), min(ds)])*direct)+center
d=x-x
x-=extend*d
x+=extend*d
return f, x


## # Probabilistic

The likelihood is ?

In :
lx=[0,0.6,0.4]
ly=[0,0.4,0.6]
f, x=fitline(lx, ly)
plot(lx, ly, 'o')
axis('equal')
plot(x, f(x));