Skip to content

Tabular, time-series, scientific, and geometric deep learning · Time-series forecasting and representation · SPE016 · Model

DeepAR

DeepAR fits a shared autoregressive recurrent model across many related time series, producing probabilistic forecasts rather than only point predictions.

Runs in CodeSplash (Experimental) Component demo, not a trained model CodeSplash's run and the reference's record

In CodeSplash

A CodeSplash method runs this entry in Python and R. Set it up from the method's page. The methods that run it are Experimental.

Do it in CodeSplash Run it on your own data. It's in the Experimental lane in the app.

Availability
Runs in CodeSplash (Experimental)
Code example
Component demo, not a trained model
Who ran it
CodeSplash's run and the reference's record
CodeSplash's run
Verified in Python and R through forecast.deepar. The reference's example ran in Python and R here, and they agree.
The reference's record
Python: Ran in the reference's review. The report doesn't record which version of the code ran. R: Reviewed but not run: R wasn't available to the reference's authors.

Purpose

DeepAR fits a shared autoregressive recurrent model across many related time series, producing probabilistic forecasts rather than only point predictions. It can share statistical strength across series with limited individual history.

Question / hypotheses

The operative question is whether the specified representation, symmetry, likelihood or physical constraint improves held-out behavior for this data domain. This is a learning framework, not a null-hypothesis test; losses, attention weights and predictive confidence are not statistical p-values.

Architecture / computational structure

A recurrent state receives previous target values and covariates. Distributional heads map the state to likelihood parameters; training uses observed lagged targets, and forecasting recursively samples or conditions through future horizons.

Assumptions

Only genuinely known future covariates may be used. Conditional likelihood misspecification, distribution shift and recursive error can impair uncertainty. Related-series training does not justify leakage across time or using target-derived future features.

Formula / computational definition

ht=fθ(ht−1,yt−1,ct)h_t=f_\theta(h_{t-1},y_{t-1},c_t) and p(yt:T∣past)=∏s=tTpψ(hs)(ys)p(y_{t:T}\mid past)=\prod_{s=t}^T p_{\psi(h_s)}(y_s). Minimize summed negative log likelihood; ψ\psi maps state to valid Gaussian, negative-binomial or another specified likelihood parameterization.

Parameters and notation

Learned or estimated: Shared recurrent weights, series/categorical embeddings and likelihood-parameter heads.

Hyperparameters and effects: Context/prediction length, recurrent depth/width, lag selection, target scaling, likelihood, covariates, sampling count and optimizer. Likelihood must match support; simulation count controls forecast approximation, not learned capacity.

Fitting, fine-tuning, and adaptation

Train with rolling-origin validation and train-only scaling. For transfer, adapt distributional heads/series embeddings first, then recurrent layers with lower learning rates. Refit calibration on target validation horizons and monitor source-series forecasts. Distinguish updating recurrent hidden state with new observations from gradient fine-tuning network weights.

How to use and calibrate

Define the independent evaluation unit before preprocessing: row/entity, whole set/object, molecule/trajectory, input function or chronological forecasting origin. Keep target construction and normalization within permitted training data. Report proper scoring rules, interval coverage/width, quantile loss, point error and performance by horizon/series scale. Compare seasonal-naive and classical probabilistic baselines under identical information availability.

Example application

Forecast a collection of related demand or sensor series with predictive intervals. This is a proposed use case, not a claim of validated performance on supplied real data.

Interpretation / pitfalls

Watch out

Assess model error against domain baselines and the stated assumptions. Component-level mathematical correctness does not establish paper reproduction or scientific validity. Use an independent validation design for model selection and a final held-out evaluation for claims; uncertainty and calibration require separate assessment.

Python and R code examples

Scope: DeepAR Gaussian likelihood head loss with a positive scale transformation. Use the shared setup. Dependencies: NumPy / base R. Validation: Python executed; R unexecuted.

Full-model route: Python: GluonTS or another verified DeepAR implementation. R: native torch custom autoregressive likelihood model or Python bridge. The example computes Gaussian negative log likelihood from valid per-time mean/scale outputs.

Python and R

The reference's own programs, shown as written. A component demo shows one central piece of the method; it doesn't train a full model.

Python

Component demo, not a trained model — DeepAR Gaussian likelihood head loss with a positive scale transformation. Use the shared setup.

Shared setup for this chapter
import numpy as np
def sm(x):
    x=np.asarray(x); e=np.exp(x-x.max(axis=-1,keepdims=True)); return e/e.sum(axis=-1,keepdims=True)
y=np.array([1.,1.5,.8]); mu=np.array([.9,1.3,1.])
raw_scale=np.array([-.5,-.2,0.]); sigma=np.log1p(np.exp(raw_scale))+1e-4
nll=.5*np.log(2*np.pi)+np.log(sigma)+.5*((y-mu)/sigma)**2
assert np.all(sigma>0)
print(sigma,nll.mean())
R

Component demo, not a trained model — DeepAR Gaussian likelihood head loss with a positive scale transformation. Use the shared setup.

Shared setup for this chapter
sm <- function(x) {
  if(is.null(dim(x))) { e<-exp(x-max(x)); return(e/sum(e)) }
  t(apply(x,1,function(v) { e<-exp(v-max(v)); e/sum(e) }))
}
y<-c(1,1.5,.8); mu<-c(.9,1.3,1); raw_scale<-c(-.5,-.2,0)
sigma<-log1p(exp(raw_scale))+1e-4
nll<-.5*log(2*pi)+log(sigma)+.5*((y-mu)/sigma)^2
stopifnot(all(sigma>0)); print(list(scale=sigma,mean_NLL=mean(nll)))

Sources

From the CodeSplash reference, entry SPE016.