Least squares fit for Linear, Square polynomial and Circular (with deduction)

The basic approach of the Least squares fit is to minimize the function f(x) by varying the parameter p:

min ∑ (yi − fp(xi))2

Variation of p:

ddp ∑ (yi − fp(xi))2 = 0

Linear case

f(x) = kx + b

Minimization core L:

L = 1/N × ∑ [k2(x2) + 2kbx − 2k(xy) − 2by + b2 + (y2)]

Arrange and get derivative:

L(b) = 2kbx̄ − 2bȳ + b2

L'(b) = 2kx̄ − 2ȳ + 2b = 0

b = ȳ − kx̄

L(k) = k2∑(x2)/N + 2kb∑x/N − 2k∑(xy)/N

L'(k) = 2k∑(x2)/N + 2bx̄ − 2∑(xy)/N = 0

Sxy = 1/N × ∑ xy

Sxx = 1/N × ∑ x2

kSxx + bx̄ − Sxy = kSxx + (ȳ − kx̄)x̄ − Sxy = kSxx + ȳx̄ − kx̄2 − Sxy = 0

kSxx − kx̄2 = Sxy − ȳx̄

k = Sxy − x̄ȳ Sxx − x̄2

Square polynomial (Quadratic polynomial) case

f(x) = ax2 + bx + c

Minimization core Q:

Q = 1/N × ∑ [a2(x4) + 2ab(x3) − 2a(x2y) + b2(x2) + 2ac(x2) − 2b(xy) + 2bcx − 2cy + c2 + y2]

Arrange and get derivative:

Q(c) = c2 + 2ac∑x2/N + 2bc∑x/N − 2c∑y/N

Sxx = 1/N × ∑ x2

Q'(c) = 2c + 2aSxx + 2bx̄ − 2ȳ = 0

c = ȳ − aSxx − bx̄

Q(b) = b2∑x2/N + 2ab∑x3/N − 2b∑xy/N + 2bc∑x/N = b2Sxx + 2ab∑x3/N − 2b∑xy/N + 2b(ȳ − aSxx − bx̄)x̄ = b2Sxx + 2ab∑x3/N − 2b∑xy/N + 2bx̄ȳ − 2abx̄Sxx − 2b22;

Sxxx = 1/N × ∑ x3

Sxy = 1/N × ∑ xy

Q'(b) = 2bSxx + 2aSxxx − 2Sxy + 2x̄ȳ − 2ax̄Sxx − 4bx̄2 = 0

Q'(b) = bSxx − bx̄2 + aSxxx − Sxy + x̄ȳ − ax̄Sxx = 0

b(Sxx − x̄2) = − aSxxx + Sxy − x̄ȳ + ax̄Sxx

R0 = (Sxy − x̄ȳ)/(Sxx − x̄2)

R1 = (Sxxx − x̄Sxx)/(Sxx − x̄2)

b = R0 − aR1

Q(a) = a2x4 + 2abx3 - 2ax2y + 2acx2 = a2x4 + 2abx3 - 2ax2y + 2a(ȳ - aSxx - bx̄)x2 = a2x4 + 2abx3 - 2ax2y + 2aȳx2 - 2a2Sxxx2 - 2abx̄x2

Q(a) = a2x4 - 2ax2y + 2aȳx2 - 2a2Sxxx2 + 2aR0(x3 - x̄x2)-2a2R1(x3 - x̄x2)

Q'(a) = 2a∑x4/N - 2∑x2y/N + 2ȳ∑x2/N - 4aSxx∑x2/N + 2R0∑(x3 - x̄x2)/N - 4aR1∑(x3 - x̄x2)/N = 0

Sxxxx = 1/N × ∑ x4

Sxxy = 1/N × ∑ x2y

S2 = Sxxx - x̄Sxx

2aSxxxx - 2Sxxy + 2ȳSxx - 4aSxx2 + 2R0S2 - 4aR1S2 = 0

aSxxxx - 2aR1S2 - Sxxy + ȳSxx - 2aSxx2 + R0S2 = 0

a(Sxxxx - 2R1S2 - 2Sxx2) - Sxxy + ȳSxx + R0S2 = 0

a = Sxxy - ȳSxx - R0S2 Sxxxx - 2R1S2 - 2Sxx2

Circle case

f(x) = (x−x0)2 + (y−y0)2 − R2

Minimization core C all terms (doubled above diagonal, x02 means x02):

  x2 -2x0x x02 y2 -2y0y y02 -R2
x2 x4 -2x0x3 2x02x2 2x2y2 -4y0x2y 2y02x2 -2R2x2
-2x0x   4x02x2 -4x03x -4x0xy2 8x0y0xy -4x0y02x 4R2x0x
x02     x04 2x02y2 -4x02y0y 2x02y02 -2R2x02
y2       y4 -4y0y3 2y02y2 -2R2y2
-2y0y         4y02y2 -4y03y 4R2y0y
y02           y04 -2R2y02
-R2             R4

C(R) = -2R2x2 +4R2x0x -2R2x02 -2R2y2 +4R2y0y -2R2y02 +R4

C(x0) = x04 - 4x03x + 6x02x2 - 2x0x3 - 4x0xy2 + 2x02y2 + 2x02y02 - 4x02y0y - 4x0y02x + 8x0y0xy - 2R2x02 + 4R2x0x

C(y0) = y04 - 4y3y + 6y02y2 - 2y0y3 - 4y0x2y + 2y02y2 + 2x02y02 - 4x02y0y - 4x0y02x + 8x0y0xy - 2R2y02 + 4R2y0y

Arrange and get derivative by R

C'(R) = 1/N ∑ [-4Rx2 +8Rx0x -4Rx02 -4Ry2 +8Ry0y -4Ry02 +4R3] = 0

As R > 0 by define, divide by 4R:

C'(R) =1/N ∑[-x2 +2x0x -x02 -y2 +2y0y -y02 +R2] = 0

R2 = ∑x2 -2x0∑x +x02 +∑y2 -2y0∑y +y02 = Sxx - 2x0x̄ + x02 + Syy - 2y0ȳ + y02

Mean optimization

Instead of using large sums of squares (and other powers), sum of squares about the mean can be used to ease the computational task and reduce rigidity. However, the result is obtained in two passes.

Example transfer formula for Sxx:

Sxx = 1/N × ∑ x2

x̄ = 1/N × ∑ x

SSxx = 1/N × ∑ (x − x̄)2 = 1/N × ∑ x2 − 1/N × 2 × x̄ ∑ x + 1/N × x̄2 ∑ 1 = 1/N × ∑ x2 − 2x̄2 + x̄2

SSxx = Sxx − x̄2

Sxx = SSxx + x̄2

Linear case

SSxx = 1/N × ∑ (x − x̄)2

SSxy = 1/N × ∑ (x − x̄)(y − ȳ)

SSxy = Sxy − x̄ȳ

k = Sxy − x̄ȳ Sxx − x̄2 = SSxySSxx

b = ȳ − k x̄

Square case

SSxxx = 1/N × ∑ (x − x̄)3

SSxxxx = 1/N × ∑ (x − x̄)4

SSxxy = 1/N × ∑ (x − x̄)2(y − ȳ)

SSxxx = Sxxx − 3x̄Sxx + 2x̄3 = Sxxx − x̄Sxx - 2x̄SSxx

SSxxxx = Sxxxx − 4x̄Sxxx + 6x̄2Sxx − 3x̄4

SSxxy = Sxxy − ȳSxx - 2x̄Sxy + 2x̄2

r1 = SSxxx SSxx
r2 = SSxy SSxx

Variables:

a = SSxxy - r1SSxy SSxxxx - r1SSxxx - (SSxx)2

Circle case

SSyy = 1/N × ∑ (y − ȳ)2

SSyyy = 1/N × ∑ (y − ȳ)3

SSxyy = 1/N × ∑ (x − x̄)(y − ȳ)2

D = SSxxSSyy - (SSxy)2

s1 = SSxxx + 2x̄SSxx + SSxyy + 2ȳSSxy

s2 = SSyyy + 2ȳSSyy + SSxxy + 2x̄SSxy

x0 = SSyy s1 - SSxy s2 2D
y0 = SSxx s2 - SSxy s1 2D

R2 = SSxx+(x̄-x0)2 + SSyy+(ȳ-y0)2

R = sqrt(R2)

Errors estimation

Summation

Summation of a large number of values over a wide range can lead to the accumulation of a significant error. Use Kahan summation algorithm wiki

Tcl Code example

# Sums $ss??? are without 1/N factor
# Linear fit  y=a1*x+a0
	set a1 [expr 1.0*$ssxy/$ssxx]
	set a0 [expr 1.0*($my-$a1*$mx)]
# Simple square fit  y=a2*x^2+a0
	set a2 [expr $ssxy/($ssxxx+2.0*$mx*$ssxx)]
	set a0 [expr $my-$a2*($ssxx/$n+$mx*$mx)]
# Full square fit y=a2*x^2+a1*x+a0
	set r1 [expr $ssxxx/$ssxx]
	set r2 [expr $ssxy/$ssxx]
	set r3 [expr $ssxx/$n]
	set a2 [expr ($r1*$ssxy-$ssxxy)/($ssxx*$r3+$ssxxx*$r1-$ssxxxx)]
	set a1 [expr $r2-$a2*($r1+2.0*$mx)]
	set a0 [expr $my-$a1*$mx-$a2*($r3+$mx*$mx)]
# Circle fit (sums 1/N factored)
	set det [expr $ssxx*$ssyy-$ssxy*$ssxy]
	set s1  [expr $ssxxx+2.0*$mx*$ssxx+$ssxyy+2.0*$my*$ssxy]
	set s2  [expr $ssyyy+2.0*$my*$ssyy+$ssxxy+2.0*$mx*$ssxy]
	set x0  [expr ($ssyy*$s1-$ssxy*$s2)/(2.0*$det)]
	set y0  [expr ($ssxx*$s2-$ssxy*$s1)/(2.0*$det)]
	set rr  [expr $ssxx+$mx*$mx-2.0*$mx*$x0+$x0*$x0+$ssyy+$my*$my-2.0*$my*$y0+$y0*$y0]
	set r0  [expr sqrt($rr)]