linearmodel

package
v0.3.14 Latest Latest
Warning

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

Go to latest
Published: Dec 2, 2025 License: MIT Imports: 15 Imported by: 0

Documentation

Overview

Package linearmodel is a collection of linear regression fitting implementations to be used in the forecaster

Index

Constants

View Source
const (
	DefaultLambda     = 1.0
	DefaultIterations = 1000
	DefaultTolerance  = 1e-4
)

Variables

View Source
var (
	ErrNoOptions          = errors.New("no initialized model options")
	ErrTargetLenMismatch  = errors.New("target length does not match target rows")
	ErrNoTrainingMatrix   = errors.New("no training matrix")
	ErrNoTargetMatrix     = errors.New("no target matrix")
	ErrNoDesignMatrix     = errors.New("no design matrix for inference")
	ErrFeatureLenMismatch = errors.New("number of features does not match number of model coefficients")
)
View Source
var (
	ErrNegativeLambda     = errors.New("negative lambda")
	ErrNegativeIterations = errors.New("negative iterations")
	ErrNegativeTolerance  = errors.New("negative tolerance")
	ErrWarmStartBetaSize  = errors.New("warm start beta does not have the same number of coefficients as training features")
	ErrNoLambdas          = errors.New("no lambdas provided to fit with")
)

Functions

func SoftThreshold

func SoftThreshold(x, gamma float64) float64

SoftThreshold returns 0.0 if the value is less than or equal to the gamma input

Types

type LassoAutoOptions

type LassoAutoOptions struct {
	// Lambda represents the L1 multiplier, controlling the regularization. Must be a non-negative. 0.0 results in converging
	// to Ordinary Least Squares (OLS).
	Lambdas []float64

	// Iterations is the maximum number of times the fit loops through training all coefficients.
	Iterations int

	// Tolerance is the smallest coefficient channge on each iteration to determine when to stop iterating.
	Tolerance float64

	// FitIntercept adds a constant 1.0 feature as the first column if set to true
	FitIntercept bool

	// Parallelization sets how many fits to run in parallel. More will increase memory and compute usage.
	Parallelization int
}

LassoAutoOptions represents input options to run the Lasso Regression with optimal regularization parameter lambda

func NewDefaultLassoAutoOptions

func NewDefaultLassoAutoOptions() *LassoAutoOptions

NewDefaultLassoAutoOptions returns a default set of Lasso Auto Regression options

func (*LassoAutoOptions) Validate

func (l *LassoAutoOptions) Validate() (*LassoAutoOptions, error)

Validate runs basic validation on Lasso Auto options

type LassoAutoRegression

type LassoAutoRegression struct {
	// contains filtered or unexported fields
}

LassoAutoRegression computes the lasso regression using coordinate descent. lambda is derived by finding the optimal regularization parameter

func NewLassoAutoRegression

func NewLassoAutoRegression(opt *LassoAutoOptions) (*LassoAutoRegression, error)

NewLassoAutoRegression initializes a Lasso model ready for fitting using automated lambad parameter selection

func (*LassoAutoRegression) Coef

func (l *LassoAutoRegression) Coef() []float64

Coef returns a slice of the trained coefficients in the same order of the training feature Matrix by column.

func (*LassoAutoRegression) Fit

func (l *LassoAutoRegression) Fit(x, y mat.Matrix) error

Fit the model according to the given training data

func (*LassoAutoRegression) Intercept

func (l *LassoAutoRegression) Intercept() float64

Intercept returns the computed intercept if FitIntercept is set to true. Defaults to 0.0 if not set.

func (*LassoAutoRegression) Predict

func (l *LassoAutoRegression) Predict(x mat.Matrix) ([]float64, error)

Predict using the Lasso model

func (*LassoAutoRegression) Score

func (l *LassoAutoRegression) Score(x, y mat.Matrix) (float64, error)

Score computes the coefficient of determination of the prediction

type LassoOptions

type LassoOptions struct {
	// WarmStartBeta is used to prime the coordinate descent to reduce the training time if a previous
	// fit has been performed.
	WarmStartBeta []float64

	// Lambda represents the L1 multiplier, controlling the regularization. Must be a non-negative. 0.0 results in converging
	// to Ordinary Least Squares (OLS).
	Lambda float64

	// Iterations is the maximum number of times the fit loops through training all coefficients.
	Iterations int

	// Tolerance is the smallest coefficient channge on each iteration to determine when to stop iterating.
	Tolerance float64

	// FitIntercept adds a constant 1.0 feature as the first column if set to true
	FitIntercept bool
}

LassoOptions represents input options to run the Lasso Regression

func NewDefaultLassoOptions

func NewDefaultLassoOptions() *LassoOptions

NewDefaultLassoOptions returns a default set of Lasso Regression options

func (*LassoOptions) Validate

func (l *LassoOptions) Validate() (*LassoOptions, error)

Validate runs basic validation on Lasso options

type LassoRegression

type LassoRegression struct {
	// contains filtered or unexported fields
}

LassoRegression computes the lasso regression using coordinate descent. lambda = 0 converges to OLS

func NewLassoRegression

func NewLassoRegression(opt *LassoOptions) (*LassoRegression, error)

NewLassoRegression initializes a Lasso model ready for fitting

func (*LassoRegression) Coef

func (l *LassoRegression) Coef() []float64

Coef returns a slice of the trained coefficients in the same order of the training feature Matrix by column.

func (*LassoRegression) Fit

func (l *LassoRegression) Fit(x, y mat.Matrix) error

Fit the model according to the given training data

func (*LassoRegression) Intercept

func (l *LassoRegression) Intercept() float64

Intercept returns the computed intercept if FitIntercept is set to true. Defaults to 0.0 if not set.

func (*LassoRegression) Predict

func (l *LassoRegression) Predict(x mat.Matrix) ([]float64, error)

Predict using the Lasso model

func (*LassoRegression) Score

func (l *LassoRegression) Score(x, y mat.Matrix) (float64, error)

Score computes the coefficient of determination of the prediction

type Model

type Model interface {
	Fit(x, y mat.Matrix) error
	Predict(x mat.Matrix) ([]float64, error)
	Score(x, y mat.Matrix) (float64, error)
	Intercept() float64
	Coef() []float64
}

type OLSOptions

type OLSOptions struct {
	// FitIntercept adds a constant 1.0 feature as the first column if set to true
	FitIntercept bool
}

OLSOptions represents input options to run the OLS Regression

func NewDefaultOLSOptions

func NewDefaultOLSOptions() *OLSOptions

NewDefaultOLSOptions returns a default set of OLS Regression options

func (*OLSOptions) Validate

func (o *OLSOptions) Validate() (*OLSOptions, error)

Validate runs basic validation on OLS options

type OLSRegression

type OLSRegression struct {
	// contains filtered or unexported fields
}

OLSRegression computes ordinary least squares using QR factorization

func NewOLSRegression

func NewOLSRegression(opt *OLSOptions) (*OLSRegression, error)

NewOLSRegression initializes an ordinary least squares model ready for fitting

func (*OLSRegression) Coef

func (o *OLSRegression) Coef() []float64

Coef returns a slice of the trained coefficients in the same order of the training feature Matrix by column.

func (*OLSRegression) Fit

func (o *OLSRegression) Fit(x, y mat.Matrix) error

Fit the model according to the given training data

func (*OLSRegression) Intercept

func (o *OLSRegression) Intercept() float64

Intercept returns the computed intercept if FitIntercept is set to true. Defaults to 0.0 if not set.

func (*OLSRegression) Predict

func (o *OLSRegression) Predict(x mat.Matrix) ([]float64, error)

Predict using the OLS model

func (*OLSRegression) Score

func (o *OLSRegression) Score(x, y mat.Matrix) (float64, error)

Score computes the coefficient of determination of the prediction

Jump to

Keyboard shortcuts

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