forecast

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: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrUninitializedForecast    = errors.New("uninitialized forecast")
	ErrInsufficientTrainingData = errors.New("insufficient training data after removing Nans")
	ErrLabelExists              = errors.New("label already exists in TimeDataset")
	ErrMismatchedDataLen        = errors.New("input data has different length than time")
	ErrFeatureLabelsInitialized = errors.New("feature labels already initialized")
	ErrNoModelCoefficients      = errors.New("no model coefficients from fit")
	ErrUntrainedForecast        = errors.New("forecast has not been trained yet")
)
View Source
var ErrResLenMismatch = errors.New("predicted and actual have different lengths")
View Source
var ErrUnknownFeatureType = errors.New("unknown feature type")

Functions

func MAPE

func MAPE(predicted, actual []float64) (float64, error)

MAPE calculates the mean average percent error. This is the same as sum(abs((y-yhat)/y)). A score of 0 means a perfect match with no errors.

func MSE

func MSE(predicted, actual []float64) (float64, error)

MSE computes the mean squared error. This is the same as sum((y-yhat)^2). A score of 0 means a perfect match with no errors.

func RSquared

func RSquared(predicted, actual []float64) (float64, error)

RSquared computes the r squared value between the predicted and actual where 1.0 means perfect fit and 0 represents no relationship

Types

type Components added in v0.1.4

type Components struct {
	Trend       []float64 `json:"trend"`
	Seasonality []float64 `json:"seasonality"`
	Event       []float64 `json:"event"`
}

type FeatureWeight

type FeatureWeight struct {
	Labels map[string]string   `json:"labels"`
	Type   feature.FeatureType `json:"type"`
	Value  float64             `json:"value"`
}

FeatureWeight represents a feature described with a type e.g. changepoint, labels and the value

func NewFeatureWeight added in v0.2.0

func NewFeatureWeight(f feature.Feature, val float64) FeatureWeight

func (*FeatureWeight) ToFeature

func (fw *FeatureWeight) ToFeature() (feature.Feature, error)

ToFeature transforms the Type and Labels into a feature type

type Forecast

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

Forecast represents a single forecast model of a time series. This is a linear model using coordinate descent to calculate the weights. This will decompose the series into an intercept, trend components (based on changepoint times), and seasonal components.

func New

func New(opt *options.Options) (*Forecast, error)

New creates a new forecast instance withh thhe given options. If none are provided, a default is used

func NewFromModel

func NewFromModel(model Model) (*Forecast, error)

NewFromModel creates a new forecast instance given a forecast Model to initialize. This instance can be used for inference immediately and does not need to be trained again.

func (*Forecast) Coefficients

func (f *Forecast) Coefficients() (map[string]float64, error)

Coefficients returns a forecast model map of coefficients keyed by the string representation of each feature label

func (*Forecast) EventComponent added in v0.2.11

func (f *Forecast) EventComponent() []float64

EventComponent represents the overall event components in the model

func (*Forecast) FeatureLabels

func (f *Forecast) FeatureLabels() ([]feature.Feature, error)

FeatureLabels returns the slice of feature labels in the order of the coefficients

func (*Forecast) Fit

func (f *Forecast) Fit(t []time.Time, y []float64) error

Fit takes the input training data and fits a forecast model for possible changepoints, seasonal components, and intercept

func (*Forecast) Intercept

func (f *Forecast) Intercept() float64

Intercept returns the intercept of the forecast model

func (*Forecast) Model

func (f *Forecast) Model() (Model, error)

Model returns the serializeable format of the forecast model composing of the forecast options, intercept, coefficients with their feature labels, and the model fit scores

func (*Forecast) ModelEq

func (f *Forecast) ModelEq() (string, error)

ModelEq returns a string representation of the model linear equation in the format of y ~ b + m1x1 + m2x2 + ...

func (*Forecast) Predict

func (f *Forecast) Predict(t []time.Time) ([]float64, Components, error)

Predict takes a slice of times in any order and produces the predicted value for those times given a pre-trained model.

func (*Forecast) Residuals

func (f *Forecast) Residuals() []float64

Residuals returns a slice of values representing the difference between the training data and the fit data

func (*Forecast) Score added in v0.2.1

func (f *Forecast) Score(x []time.Time, y []float64) (float64, error)

Score computes the coefficient of determination of the prediction

func (*Forecast) Scores

func (f *Forecast) Scores() Scores

Scores returns the fit scores for evaluating how well the resulting model fit the training data

func (*Forecast) SeasonalityComponent

func (f *Forecast) SeasonalityComponent() []float64

SeasonalityComponent represents the overall seasonal component of the model

func (*Forecast) TrendComponent

func (f *Forecast) TrendComponent() []float64

TrendComponent represents the overall trend component of the model which is determined by the changepoints.

type Model

type Model struct {
	TrainEndTime time.Time        `json:"train_end_time"`
	Options      *options.Options `json:"options"`
	Scores       *Scores          `json:"scores"`
	Weights      Weights          `json:"weights"`
}

Model represents a serializeable format of a forecast storing the forecast options, fit scores, and coefficients

func (Model) TablePrint added in v0.3.1

func (m Model) TablePrint(w io.Writer, prefix, indent string) error

type Scores

type Scores struct {
	MSE  float64 `json:"mean_squared_error"`
	MAPE float64 `json:"mean_average_percent_error"`
	R2   float64 `json:"r_squared"`
}

Scores tracks the fit scores

func NewScores

func NewScores(predicted, actual []float64) (*Scores, error)

NewScores calculates the fit scores given the predicted and actual input slice values

type Weights

type Weights struct {
	Coef      []FeatureWeight `json:"coefficients"`
	Intercept float64         `json:"intercept"`
}

Weights stores the intercept and the coefficients for the forecast model

func (*Weights) Coefficients

func (w *Weights) Coefficients() []float64

Coefficients returns a slice copy of the coefficients ignoring the intercept.

func (*Weights) FeatureLabels

func (w *Weights) FeatureLabels() ([]feature.Feature, error)

FeatureLabels returns all of the feature labels in the same order as the coefficients

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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