Skip to content

Commit

Permalink
Add line fitting example.
Browse files Browse the repository at this point in the history
  • Loading branch information
athas committed Jul 11, 2024
1 parent 031dc51 commit 2c83ede
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
1 change: 1 addition & 0 deletions examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ for plotting or rendering graphics.
- [Variance](examples/variance.html)
- [Matching parentheses](examples/parens.html)
- [Evaluating polynomials](examples/polynomials.html)
- [Line fitting](examples/line-fitting.html)

# Automatic differentiation

Expand Down
20 changes: 20 additions & 0 deletions examples/line-fitting.fut
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
-- # Line fitting
--
-- The following function uses [linear least
-- squares](https://en.wikipedia.org/wiki/Linear_least_squares) to
-- determine constants `a`, `b` for a function `f(x)=b*x+a` given a
-- collection of observations.

let line_fit [n] (x: [n]f64) (y: [n]f64) =
let n = f64.i64 n
let xa = f64.sum x / n
let ya = f64.sum y / n
let Stt = f64.sum (map (**2) (map (\x' -> x' - xa) x))
let b = f64.sum (map2 (*) (map (\x' -> x' - xa) x) y) / Stt
let a = ya - xa*b
let chi2 = f64.sum(map2 (\x' y' -> (y'-a-b*x')**2) x y)
let siga = f64.sqrt((1/n + xa**2/Stt)*chi2/n)
let sigb = f64.sqrt((1/Stt)*(chi2/n))
in (a, b, siga, sigb)

-- > line_fit [0.0, 1.0, 2.0, 3.0, 4.0] [2.75, 5.0, 7.0, 9.0, 11.0]

0 comments on commit 2c83ede

Please sign in to comment.