sourcelibs/Matrix/demos/line-fit.xtl

1⍝!/usr/bin/env xetal 2⍝# A straight line through noisy points by least squares: the normal 3⍝# equations (X' X) b = X' y, solved. Run with "just demo Matrix line-fit". 4 5ᵐˣ⁼u̲se< "Matrix" 6x ← f̲loat r̲ange 8 7y ← 2.9 5.1 7.2 8.8 11.1 13.0 14.8 17.2 ⍝ about 1 + 2x 8 9⍝ The design matrix: a column of 1s (the intercept) and the x values. 10X ← 1 c̲at₂ (8 1 r̲eshape x) 11Xt ← o̲\ X 12b ← (Xt ᵐˣm̲ul y) ᵐˣs̲olve Xt ᵐˣm̲ul X 13b ⍝ intercept and slope 14 15⍝ Residuals, and their sum of squares. 16r ← y − X ᵐˣm̲ul b 17'+ r̲/ r × r