models

package
v0.3.5 Latest Latest
Warning

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

Go to latest
Published: Jan 14, 2025 License: MIT Imports: 13 Imported by: 0

Documentation

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 added in v0.3.1

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 added in v0.3.1

func NewDefaultLassoAutoOptions() *LassoAutoOptions

NewDefaultLassoAutoOptions returns a default set of Lasso Auto Regression options

func (*LassoAutoOptions) Validate added in v0.3.1

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

Validate runs basic validation on Lasso Auto options

type LassoAutoRegression added in v0.3.1

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 added in v0.3.1

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

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

func (*LassoAutoRegression) Coef added in v0.3.1

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 added in v0.3.1

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

Fit the model according to the given training data

func (*LassoAutoRegression) Intercept added in v0.3.1

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 added in v0.3.1

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

Predict using the Lasso model

func (*LassoAutoRegression) Score added in v0.3.1

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 added in v0.2.1

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 added in v0.2.1

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

NewLassoRegression initializes a Lasso model ready for fitting

func (*LassoRegression) Coef added in v0.2.1

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 added in v0.2.1

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

Fit the model according to the given training data

func (*LassoRegression) Intercept added in v0.2.1

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 added in v0.2.1

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

Predict using the Lasso model

func (*LassoRegression) Score added in v0.2.1

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

Score computes the coefficient of determination of the prediction

type Model added in v0.3.3

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 added in v0.2.1

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 added in v0.2.1

func NewDefaultOLSOptions() *OLSOptions

NewDefaultOLSOptions returns a default set of OLS Regression options

func (*OLSOptions) Validate added in v0.2.1

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

Validate runs basic validation on OLS options

type OLSRegression added in v0.2.1

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

OLSRegression computes ordinary least squares using QR factorization

func NewOLSRegression added in v0.2.1

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

NewOLSRegression initializes an ordinary least squares model ready for fitting

func (*OLSRegression) Coef added in v0.2.1

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 added in v0.2.1

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

Fit the model according to the given training data

func (*OLSRegression) Intercept added in v0.2.1

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 added in v0.2.1

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

Predict using the OLS model

func (*OLSRegression) Score added in v0.2.1

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