Documentation
¶
Index ¶
- func Difference(y []float64, d int) []float64
- func SeasonalDifference(y []float64, m, D int) []float64
- func SeasonalUndifference(diffPred, anchor []float64) []float64
- func Undifference(diffPred []float64, lastOrig float64) []float64
- type ARIMA
- func (m *ARIMA) Beta() []float64
- func (m *ARIMA) Fit(series []float64, opts ...FitOption) error
- func (m *ARIMA) Forecast(h int, opts ...ForecastOption) ([]float64, error)
- func (m *ARIMA) ForecastInterval(h int, level float64, opts ...ForecastOption) (*Forecast, error)
- func (m *ARIMA) LastE() []float64
- func (m *ARIMA) LastOrig() float64
- func (m *ARIMA) LastY() []float64
- func (m *ARIMA) Order() Order
- func (m *ARIMA) Phi() []float64
- func (m *ARIMA) SeasonalOrder() SeasonalOrder
- func (m *ARIMA) SeasonalPhi() []float64
- func (m *ARIMA) SeasonalTheta() []float64
- func (m *ARIMA) Sigma2() float64
- func (m *ARIMA) StdErrors() ([]float64, error)
- func (m *ARIMA) Summary() (*Summary, error)
- func (m *ARIMA) Theta() []float64
- type AutoOption
- type Bounds
- type Criterion
- type FitOption
- type Forecast
- type ForecastOption
- type Method
- type Order
- type ParamStat
- type SeasonalBounds
- type SeasonalOrder
- type Summary
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Difference ¶
Difference applies d-th order differencing to the series, returning a series of length len(y)-d. Each pass replaces the series with consecutive differences.
func SeasonalDifference ¶
SeasonalDifference applies D passes of lag-m differencing to y, returning a series of length len(y)-D*m. Each pass replaces y_t with y_t - y_{t-m}. A pass on a series of length <= m returns an empty slice. D == 0 returns a copy.
func SeasonalUndifference ¶
SeasonalUndifference reverses one pass of lag-m seasonal differencing by adding each value of diffPred to the value m steps earlier. anchor holds the last m values on the pre-difference scale in chronological order (oldest first): the first m results integrate onto anchor, later ones onto earlier results. The seasonal period m is len(anchor).
func Undifference ¶
Undifference reverses a single order of differencing by cumulatively summing diffPred onto lastOrig, the last observed value on the original scale.
Types ¶
type ARIMA ¶
type ARIMA struct {
// contains filtered or unexported fields
}
func AutoARIMA ¶
func AutoARIMA(series []float64, max Bounds, opts ...AutoOption) (*ARIMA, error)
AutoARIMA selects ARIMA orders automatically and returns a fitted model.
The differencing order d is chosen by the KPSS stationarity test (difference until the series tests stationary, up to max.MaxD). The non-seasonal orders p and q are then chosen by an exhaustive grid search over 0..max.MaxP and 0..max.MaxQ that minimizes an information criterion; the (0,0) combination is skipped. Candidate orders whose fit fails (e.g. too few observations) are skipped.
The criterion defaults to AIC and can be changed with WithCriterion (AIC, BIC, or AICc). Any FitOption (e.g. WithMethod) is threaded through to every candidate fit and the final refit, so candidates are scored and the final model is fit with the same options.
Note that the criterion is always computed from the residual variance (see score), even when WithMethod(MLE) is supplied: the refinement lowers each candidate's residual variance and thus influences selection, but the score is not the exact Gaussian-likelihood criterion. A likelihood-based criterion is left to a later phase.
func AutoSARIMA ¶
func AutoSARIMA(series []float64, max Bounds, seasonal SeasonalBounds, opts ...AutoOption) (*ARIMA, error)
AutoSARIMA selects seasonal ARIMA orders automatically for a known seasonal period seasonal.Period (>= 2) and returns a fitted model. It chooses the seasonal differencing order D (0 or 1) with the seasonal-strength measure, then the regular differencing order d with the KPSS test on the seasonally- differenced series, then the non-seasonal orders p,q and the seasonal AR/MA orders P,Q with the same search AutoARIMA uses, over 0..max.MaxP, 0..max.MaxQ, 0..seasonal.MaxP, 0..seasonal.MaxQ (grid by default; WithStepwise / WithParallel honored).
The criterion defaults to AIC (WithCriterion to change it). Any FitOption (e.g. WithMethod) is threaded through to every candidate fit and the final refit, exactly as in AutoARIMA.
func NewARIMA ¶
NewARIMA constructs a non-seasonal ARIMA model. It is shorthand for NewSARIMA(order, SeasonalOrder{}).
func NewSARIMA ¶
func NewSARIMA(order Order, seasonal SeasonalOrder) (*ARIMA, error)
NewSARIMA constructs a seasonal ARIMA model of the multiplicative class
φ(B)·Φₛ(Bᵐ)·(1−B)ᵈ(1−Bᵐ)ᴰ y_t = θ(B)·Θₛ(Bᵐ)·ε_t,
with non-seasonal orders order and seasonal orders seasonal. The seasonal period must be >= 2 whenever any seasonal order (P, D, or Q) is positive.
func (*ARIMA) Beta ¶
Beta returns a copy of the exogenous regression coefficients (length k), or an empty slice when the model was fit without exogenous regressors.
func (*ARIMA) Fit ¶
Fit estimates the model's coefficients from series. It differences the series (seasonally then regularly) per the configured orders, centers it, and fits the (multiplicative seasonal) ARMA factors by Hannan-Rissanen; WithMethod(CSS) or WithMethod(MLE) then refines that seed. WithExog fits regression with ARIMA errors, and WithRootRepair reflects an unstable estimate back into the stationary/invertible region instead of erroring. After a successful Fit the coefficients and forecast state are available via the accessors, and Forecast / ForecastInterval may be called.
Fit returns an error if series is too short for the requested orders, contains a NaN or ±Inf, or the ARMA estimation fails (e.g. a non-stationary/non-invertible fit without WithRootRepair). The search-only options (WithCriterion, WithStepwise, WithParallel, WithContext) are AutoOptions and cannot be passed to Fit — that is a compile-time error.
func (*ARIMA) Forecast ¶
func (m *ARIMA) Forecast(h int, opts ...ForecastOption) ([]float64, error)
Forecast returns the h-step point forecast on the original scale. If the model was fit with exogenous regressors (WithExog), the matching future regressors must be supplied via WithFutureExog (and must not be supplied otherwise); the regression mean of those regressors is then added to the forecast.
func (*ARIMA) ForecastInterval ¶
ForecastInterval returns the h-step point forecast together with a two-sided prediction interval at the given confidence level (e.g. 0.95). The forecast standard errors come from the model's MA(∞) representation: Var(k steps) = σ²·Σ_{j<k} ψ_j², with the differencing operators folded into the AR side so the variances are on the original (integrated) scale.
If the model was fit with exogenous regressors (WithExog), the matching future regressors must be supplied via WithFutureExog (and must not be supplied otherwise); their regression mean shifts Point/Lower/Upper. β estimation uncertainty is excluded from the band, matching statsmodels' default conf_int.
func (*ARIMA) SeasonalOrder ¶
func (m *ARIMA) SeasonalOrder() SeasonalOrder
SeasonalOrder returns the seasonal orders (P, D, Q) and period m.
func (*ARIMA) SeasonalPhi ¶
SeasonalPhi returns a copy of the seasonal AR coefficients (the Φₛ factor, length P).
func (*ARIMA) SeasonalTheta ¶
SeasonalTheta returns a copy of the seasonal MA coefficients (the Θₛ factor, length Q).
func (*ARIMA) Sigma2 ¶
Sigma2 returns the variance of the residuals. This is not used in forecasting but can be useful for diagnostics.
func (*ARIMA) StdErrors ¶
StdErrors returns the standard errors of the fitted coefficients (β, φ, Φₛ, θ, Θₛ) in canonical order. It requires an MLE fit (WithMethod(MLE)) and returns an error otherwise, or if the information matrix was singular. An entry is NaN when its estimated variance is non-positive (a weakly identified coefficient).
type AutoOption ¶
type AutoOption interface {
// contains filtered or unexported methods
}
AutoOption configures an AutoARIMA/AutoSARIMA order search. It is the superset of FitOption: every FitOption is an AutoOption (threaded through to the candidate and final fits), plus the search-only options WithCriterion, WithStepwise, WithParallel, and WithContext — which apply only to the search and, by not satisfying FitOption, cannot be passed to a plain Fit.
func WithContext ¶
func WithContext(ctx context.Context) AutoOption
WithContext supplies a context whose cancellation aborts an AutoARIMA/AutoSARIMA order search; the call then returns an error wrapping the context cause. This is a search-only option: it returns an AutoOption, so it cannot be passed to a plain Fit. Cancellation is observed between candidate fits (a single in-flight fit is not interrupted). A nil context is treated as context.Background().
func WithCriterion ¶
func WithCriterion(c Criterion) AutoOption
WithCriterion selects the information criterion AutoARIMA minimizes during order selection (AIC, BIC, or AICc). The default is AIC. This is a search-only option: it returns an AutoOption, so it cannot be passed to a plain Fit.
func WithParallel ¶
func WithParallel() AutoOption
WithParallel makes AutoARIMA fit candidate orders concurrently, across up to GOMAXPROCS goroutines. Selection is deterministic and identical to the serial search (results are reduced in a fixed order), so this only changes speed. This is a search-only option: it returns an AutoOption, so it cannot be passed to a plain Fit.
func WithStepwise ¶
func WithStepwise() AutoOption
WithStepwise makes AutoARIMA select p and q with a Hyndman-Khandakar stepwise neighbor search instead of the exhaustive grid. It usually fits far fewer candidates at the cost of being a heuristic (it can miss the grid's global optimum). This is a search-only option: it returns an AutoOption, so it cannot be passed to a plain Fit.
type Bounds ¶
Bounds are the non-seasonal search bounds for AutoARIMA/AutoSARIMA: the maximum AR order (MaxP), differencing order (MaxD), and MA order (MaxQ) the search considers. Each is an inclusive upper bound on the range searched from 0.
type Criterion ¶
type Criterion int
Criterion selects the information criterion AutoARIMA minimizes during order selection. The zero value is AIC, so the default selection is unchanged.
const ( // AIC is the Akaike Information Criterion: n·ln(σ²) + 2k. AIC Criterion = iota // BIC is the Bayesian (Schwarz) Information Criterion: n·ln(σ²) + k·ln(n). // It penalizes extra parameters more heavily than AIC for n > e² ≈ 7.4. BIC // AICc is the AIC with a small-sample correction: // AIC + 2k(k+1)/(n−k−1). It is +Inf when n−k−1 ≤ 0. AICc )
type FitOption ¶
type FitOption interface {
AutoOption
// contains filtered or unexported methods
}
FitOption configures optional Fit behavior; every FitOption is also an AutoOption, since the auto searches fit too. The zero set of options keeps the default Hannan-Rissanen estimator. Fit-relevant options are WithMethod, WithExog, and WithRootRepair.
func WithExog ¶
WithExog supplies an n×k matrix of exogenous regressors X (n = len(series)), fitting regression with ARIMA errors: y_t = X_t·β + η_t, where η follows the ARIMA model. The estimated β is available via Beta(); forecasts must pass the matching future regressors via WithFutureExog to Forecast / ForecastInterval.
func WithMethod ¶
WithMethod selects the estimator (HannanRissanen, CSS, or MLE). The default, used when WithMethod is not supplied, is HannanRissanen.
func WithRootRepair ¶
func WithRootRepair() FitOption
WithRootRepair makes Fit project an unstable Hannan-Rissanen estimate back into the stationary/invertible region — reflecting any AR/MA root on or inside the unit circle to its reciprocal (see roots.go) — instead of returning an error. It is off by default. It composes with WithMethod(CSS)/WithMethod(MLE) (repair yields a valid seed that the optimizer then refines) and threads through AutoARIMA/AutoSARIMA, where it makes otherwise-rejected orders eligible.
type Forecast ¶
type Forecast struct {
Point []float64 // point forecast
Lower []float64 // Point − z·StdErr
Upper []float64 // Point + z·StdErr
StdErr []float64 // forecast standard error, sqrt(forecast-error variance)
}
Forecast bundles an h-step point forecast with its prediction interval. Every slice has length h; Point is identical to what Forecast(h) returns.
type ForecastOption ¶
type ForecastOption interface {
// contains filtered or unexported methods
}
ForecastOption configures Forecast/ForecastInterval. The only option is WithFutureExog, required exactly when the model was fit with WithExog.
func WithFutureExog ¶
func WithFutureExog(X [][]float64) ForecastOption
WithFutureExog supplies the future regressor rows for forecasting a model fit with WithExog. futureX must be exactly h×k (k = number of regressors at fit time); Forecast/ForecastInterval add the regression mean futureX·β. It is required exactly when the model was fit with exogenous regressors.
type Method ¶
type Method int
Method selects the estimator Fit uses. The default (HannanRissanen) is a linear-algebra seed with no optimizer; CSS and MLE refine that seed and are never worse than it (a refined estimate is kept only if it is stationary, invertible, and strictly improves the objective, otherwise the seed is used).
const ( // HannanRissanen is the default: a two-stage linear-algebra estimate with no // iterative optimizer (see estimate.go). HannanRissanen Method = iota // CSS refines the Hannan-Rissanen seed by minimizing the conditional sum of // squares (see refine.go). CSS // MLE refines the Hannan-Rissanen seed by exact Gaussian maximum likelihood // via the Kalman filter (see mle.go and statespace.go). It matches the // exact-likelihood fit of modern statsmodels (method="statespace"). MLE )
type Order ¶
Order holds the non-seasonal ARIMA orders (p, d, q): the AR order, the differencing order, and the MA order. The fields carry the conventional lowercase ARIMA letters p/d/q (capitalized because Go exports capitalized fields); the seasonal counterparts live in SeasonalOrder.
type ParamStat ¶
type ParamStat struct {
Name string
Coef float64
StdErr float64
ZScore float64 // Coef / StdErr
PValue float64 // two-sided, standard normal
CILower float64 // 95% by default
CIUpper float64
}
ParamStat is one row of a Summary: a coefficient with its inference stats.
type SeasonalBounds ¶
SeasonalBounds are the seasonal search bounds for AutoSARIMA: the maximum seasonal AR order (MaxP) and seasonal MA order (MaxQ), plus the fixed seasonal Period (m >= 2). There is no seasonal MaxD — the seasonal differencing order D is auto-selected as 0 or 1 by the seasonal-strength test.
type SeasonalOrder ¶
type SeasonalOrder struct {
P int // seasonal AR order
D int // seasonal differencing order
Q int // seasonal MA order
Period int // seasonal period m
}
SeasonalOrder holds the multiplicative seasonal orders (P, D, Q) and the seasonal period m. Period must be >= 2 whenever any of P, D, or Q is positive.





