fit

package
v0.0.0-...-f10218a Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 12, 2021 License: BSD-3-Clause Imports: 5 Imported by: 3

Documentation

Overview

Package fit provides functions for fitting models to data.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func LOESS

func LOESS(xs, ys []float64, degree int, span float64) func(x float64) float64

LOESS computes the locally-weighted least squares polynomial regression to the data (xs[i], ys[i]). 0 < span <= 1 is the smoothing parameter, where smaller values fit the data more tightly. Degree is typically 2 and span is typically between 0.5 and 0.75.

The regression is "local" because the weights used for the polynomial regression depend on the x at which the regression function is evaluated. The weight of observation i is W((x-xs[i])/d(x)) where d(x) is the distance from x to the span*len(xs)'th closest point to x and W is the tricube weight function W(u) = (1-|u|³)³ for |u| < 1, 0 otherwise. One consequence of this is that only the span*len(xs) points closest to x affect the regression at x, and that the effect of these points falls off further from x.

References

Cleveland, William S., and Susan J. Devlin. "Locally weighted regression: an approach to regression analysis by local fitting." Journal of the American Statistical Association 83.403 (1988): 596-610.

http://www.itl.nist.gov/div898/handbook/pmd/section1/dep/dep144.htm

func LinearLeastSquares

func LinearLeastSquares(xs, ys, weights []float64, terms ...func(xs, termOut []float64)) (params []float64)

LinearLeastSquares computes the least squares fit for the function

f(x) = Β₀terms₀(x) + Β₁terms₁(x) + ...

to the data (xs[i], ys[i]). It returns the parameters Β₀, Β₁, ... that minimize the sum of the squares of the residuals of f:

∑ (ys[i] - f(xs[i]))²

If weights is non-nil, it is used to weight these residuals:

∑ weights[i] × (ys[i] - f(xs[i]))²

The function f is specified by one Go function for each linear term. For efficiency, the Go function is vectorized: it will be passed a slice of x values in xs and must fill the slice termOut with the value of the term for each value in xs.

Note that this is called a "linear" least squares fit because the fitted function is linear in the computed parameters. The function need not be linear in x.

Types

type PolynomialRegressionResult

type PolynomialRegressionResult struct {
	// Coefficients is the coefficients of the fitted polynomial.
	// Coefficients[i] is the coefficient of the x^i term.
	Coefficients []float64

	// F evaluates the fitted polynomial at x.
	F func(x float64) float64
}

PolynomialRegressionResult is the resulting polynomial from a PolynomialRegression.

TODO: Should this just be a least squares regression result? We have the terms functions, so we can construct F, though it won't be very efficient.

func PolynomialRegression

func PolynomialRegression(xs, ys, weights []float64, degree int) PolynomialRegressionResult

PolynomialRegression performs a least squares regression with a polynomial of the given degree. If weights is non-nil, it is used to weight the residuals.

func (PolynomialRegressionResult) String

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL