Skip to content

fit #

Linear Fit

vsl.fit provides simple linear regression utilities for the model:

y(x) = a + b*x

Current API:

  • linear(x, y) -> (a, b)
  • linear_sigma(x, y) -> (a, b, sigma_a, sigma_b, chi_2)

When to use each function

  • Use linear when you only need intercept and slope.
  • Use linear_sigma when you also need uncertainty estimates and fit quality.

Quick usage

import vsl.fit

x := [1.0, 2.0, 3.0, 4.0]
y := [2.1, 4.0, 6.2, 8.1]

a, b := fit.linear(x, y)
println('intercept=${a}, slope=${b}')

a2, b2, sigma_a, sigma_b, chi2 := fit.linear_sigma(x, y)
println('a=${a2} b=${b2} sigma_a=${sigma_a} sigma_b=${sigma_b} chi2=${chi2}')

Examples

Representative examples live under fit/examples:

  • line_calibration: calibrating a biased sensor against a reference.
  • trend_quality: fitting a trend and inspecting uncertainty / chi-square.

fn linear #

fn linear(x []f64, y []f64) (f64, f64)

linear computes linear fitting parameters. Errors on y-direction only

y(x) = a + b⋅x

See page 780 of [1] Reference: [1] Press WH, Teukolsky SA, Vetterling WT, Fnannery BP (2007) Numerical Recipes: The Art of Scientific Computing. Third Edition. Cambridge University Press. 1235p.

fn linear_sigma #

fn linear_sigma(x []f64, y []f64) (f64, f64, f64, f64, f64)

linear_sigma computes linear fitting parameters and variances (sigma_a, sigma_b) in the estimates of a and b Errors on y-direction only

y(x) = a + b⋅x

See page 780 of [1] Reference: [1] Press WH, Teukolsky SA, Vetterling WT, Fnannery BP (2007) Numerical Recipes: The Art of Scientific Computing. Third Edition. Cambridge University Press. 1235p.