Bestfit: Lines of Best Fit
(require bestfit) | package: plot-bestfit |
Bestfit is a library for calculating lines of best fit using Least Squares Fitting.
procedure
(graph/linear xs ys [errors])
→ (Values renderer2d? renderer2d? renderer2d?) xs : (Listof Nonnegative-Flonum) ys : (Listof Nonnegative-Flonum) errors : (U #f (Listof Flonum)) = #f
procedure
(graph/exponential xs ys [errors])
→ (Values renderer2d? renderer2d? renderer2d?) xs : (Listof Nonnegative-Flonum) ys : (Listof Nonnegative-Flonum) errors : (U #f (Listof Flonum)) = #f
procedure
(graph/log xs ys [errors])
→ (Values renderer2d? renderer2d? renderer2d?) xs : (Listof Nonnegative-Flonum) ys : (Listof Nonnegative-Flonum) errors : (U #f (Listof Flonum)) = #f
procedure
(graph/power xs ys [errors])
→ (Values renderer2d? renderer2d? renderer2d?) xs : (Listof Nonnegative-Flonum) ys : (Listof Nonnegative-Flonum) errors : (U #f (Listof Flonum)) = #f
> (define (3x^2 x) (* 3.0 (expt x 2.0))) > (define (add-error y) (+ y (* y (/ (- (random 4) 2) 10.0)))) > (define exact (function 3x^2 #:label "exact" #:color "blue"))
> (define-values (pts fit _) (graph/power (build-list 10 (compose fl add1)) (build-list 10 (compose 3x^2 fl add1)))) > (plot (list exact fit pts))
> (define-values (pts fit err) (graph/power (build-list 10 (compose fl add1)) (build-list 10 (compose add-error 3x^2 fl add1)) (build-list 10 (const 0.2)))) > (plot (list exact fit pts err))
procedure
(linear-fit xs ys) → (-> Nonnegative-Flonum Real)
xs : (Listof Nonnegative-Flonum) ys : (Listof Nonnegative-Flonum)
procedure
(exp-fit xs ys) → (-> Nonnegative-Flonum Real)
xs : (Listof Nonnegative-Flonum) ys : (Listof Nonnegative-Flonum)
procedure
(log-fit xs ys) → (-> Nonnegative-Flonum Real)
xs : (Listof Nonnegative-Flonum) ys : (Listof Nonnegative-Flonum)
procedure
(power-fit xs ys) → (-> Nonnegative-Flonum Real)
xs : (Listof Nonnegative-Flonum) ys : (Listof Nonnegative-Flonum)
Uses Least Squares Fitting to generate a best fit function of the given type.
> (define line (linear-fit '(1.0 2.0 3.0) '(1.0 2.0 3.0))) > (line 10.0) 10.0
> (line 12.0) 12.0
procedure
(linear-fit-params xs ys) → (Values Real Real)
xs : (Listof Nonnegative-Flonum) ys : (Listof Nonnegative-Flonum)
> (linear-fit-params '(1.0 2.0 3.0) '(2.0 4.0 6.0))
0.0
2.0
procedure
(exp-fit-params xs ys) → (Values Real Real)
xs : (Listof Nonnegative-Flonum) ys : (Listof Nonnegative-Flonum)
> (exp-fit-params '(1.0 2.0 3.0) '(2.0 4.0 8.0))
1.0000000000000044
0.6931471805599438
procedure
(log-fit-params xs ys) → (Values Real Real)
xs : (Listof Nonnegative-Flonum) ys : (Listof Nonnegative-Flonum)
> (log-fit-params '(2.0 4.0 8.0) '(1.0 2.0 3.0))
1.833333333333333
0.12022458674074712
procedure
(power-fit-params xs ys) → (Values Real Real)
xs : (Listof Nonnegative-Flonum) ys : (Listof Nonnegative-Flonum)
> (power-fit-params '(1.0 2.0 3.0) '(2.0 8.0 18.0))
0.6931471805599457
1.9999999999999991