Version: v0.0.0-...-a103044 Latest Latest

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

Go to latest
Published: Oct 25, 2021 License: MIT Imports: 5 Imported by: 9



Package forecast provides an interface for custom forecasting algorithms.



This section is empty.


View Source
var ErrIndeterminate = errors.New("indeterminate")

ErrIndeterminate indicates that the result of a calculation is indeterminate.

View Source
var ErrInsufficientDataPoints = errors.New("insufficient data points or nil values found")

ErrInsufficientDataPoints signifies that a particular forecasting algorithm requires more data points to operate.

View Source
var ErrMismatchLen = errors.New("mismatch length")

ErrMismatchLen signifies that there is a mismatch between the length of 2 Series or slices.


func ConfidenceLevelToZ

func ConfidenceLevelToZ(level float64) float64

ConfidenceLevelToZ returns the Z value for a given confidence level. level must be between 0 and 1 (exclusive).

level: 0.75 (75%) => 1.15 (approx)

level: 0.95 (95%) => 1.96 (approx)

level: 0.99 (99%) => 2.58 (approx)



type Confidence

type Confidence map[float64]ConfidenceInterval

Confidence contains the confidence intervals for various confidence levels. The key must be between 0 and 1 (exclusive). A confidence level of 95% is represented by 0.95.

func Forecast

func Forecast(ctx context.Context, sdf interface{}, r *dataframe.Range, alg ForecastingAlgorithm, cfg interface{}, n uint, evalFunc EvaluationFunc) (interface{}, []Confidence, float64, error)

Forecast predicts the next n values of sdf using the forecasting algorithm alg. cfg is required to configure the parameters of the algorithm. r is used to select a subset of sdf to be the "training set". Values after r form the "validation set". evalFunc can be set to measure the quality of the predictions. sdf can be a SeriesFloat64 or a DataFrame. DataFrame input is not yet implemented.

NOTE: You can find basic forecasting algorithms in forecast/algs subpackage.

type ConfidenceInterval

type ConfidenceInterval struct {
	// Upper bounds
	Upper float64

	// Lower bounds
	Lower float64

	// Normal can be set to signify that errors are normally distributed with a mean of zero.
	Normal bool

ConfidenceInterval represents an estimated range of values that includes the forecasted value within its bounds.

func DriftConfidenceInterval

func DriftConfidenceInterval(pred, level, sigmaHat float64, T, h uint) ConfidenceInterval

DriftConfidenceInterval - see

func MeanConfidenceInterval

func MeanConfidenceInterval(pred, level, sigmaHat float64, T uint) ConfidenceInterval

MeanConfidenceInterval - see

func NaïveConfidenceInterval

func NaïveConfidenceInterval(pred, level, sigmaHat float64, h uint) ConfidenceInterval

NaïveConfidenceInterval - see

func SeasonalNaïveConfidenceInterval

func SeasonalNaïveConfidenceInterval(pred, level, sigmaHat float64, h, seasonalPeriod uint) ConfidenceInterval

SeasonalNaïveConfidenceInterval - see

func (ConfidenceInterval) NormalError

func (c ConfidenceInterval) NormalError() float64

NormalError returns the error, assuming it is normally distributed with a mean of zero.

func (ConfidenceInterval) String

func (c ConfidenceInterval) String() string

String implements fmt.Stringer interface.

type EvaluationFunc

type EvaluationFunc func(ctx context.Context, validationSet, forecastSet []float64, opts *EvaluationFuncOptions) (float64, int, error)

EvaluationFunc compares the validationSet and forecastSet and calculates the error. See the validation subpackage for various approaches to calculating the error.

type EvaluationFuncOptions

type EvaluationFuncOptions struct {

	// SkipInvalids will skip Inf and NaN values.
	// If set to false (default) and an invalid value is encountered, then an ErrIndeterminate is returned.
	SkipInvalids bool

EvaluationFuncOptions is used to modify the behavior of the EvaluationFunc.

type ForecastingAlgorithm

type ForecastingAlgorithm interface {

	// Configure sets the various parameters for the algorithm.
	// config must be a struct that the particular algorithm recognizes.
	Configure(config interface{}) error

	// Load loads historical data.
	// Some forecasting algorithms do not tolerate nil values and will require interpolation.
	// r is used to limit which rows of sf are loaded. Prediction will always begin
	// from the row after that defined by r. r can be thought of as defining a "training set".
	Load(ctx context.Context, sf *dataframe.SeriesFloat64, r *dataframe.Range) error

	// Predict forecasts the next n values for the loaded data.
	// NOTE: Not all forecasting algorithms return the confidence values.
	Predict(ctx context.Context, n uint) (*dataframe.SeriesFloat64, []Confidence, error)

	// Evaluate will measure the quality of the predicted values based on the evaluation calculation defined by evalFunc.
	// It will compare the error between sf and the values from the end of the loaded data ("validation set").
	// sf is usually the output of the Predict method.
	// NOTE: You can use the functions directly from the validation subpackage if you need to do something
	// other than that described above.
	Evaluate(ctx context.Context, sf *dataframe.SeriesFloat64, evalFunc EvaluationFunc) (float64, error)

ForecastingAlgorithm defines the methods that all forecasting algorithms must implement.


Path Synopsis
Package hw implements the Holt-Winters forecasting algorithm.
Package hw implements the Holt-Winters forecasting algorithm.
Package ses implements the simple exponential smooting forecasting algorithm.
Package ses implements the simple exponential smooting forecasting algorithm.
Package interpolation implements various algorithms to fill in missing values in a Series or DataFrame.
Package interpolation implements various algorithms to fill in missing values in a Series or DataFrame.

Jump to

Keyboard shortcuts

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