Native uncertainty quantification for time series with NGBoost

R-bloggers 2025-10-15

[This article was first published on T. Moudiki's Webpage - R, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

2 days ago, I presented a Cythonized implementation of NGBoost. NGBoost is a probabilistic boosting algorithm that provides uncertainty estimates along with predictions. It works by fitting a base learner (like decision trees or linear models) to the negative gradient of a specified loss function, and was first introduced by Stanford Machine Learning Group in the paper “NGBoost: Natural Gradient Boosting for Probabilistic Prediction” by Duan et al. (2019).

In this post, we will explore how to use NGBoost, a powerful library for probabilistic forecasting, in conjunction with the nnetsauce and cybooster libraries to perform time series analysis with native uncertainty quantification. The difference with the previous post is that we will use the native uncertainty quantification capabilities of NGBoost.

!pip install git+https://github.com/Techtonique/nnetsauce.git!pip install git+https://github.com/Techtonique/cybooster.git

https://docs.techtonique.net/cybooster/index.html

https://docs.techtonique.net/nnetsauce/index.html

1 – Python version

ice_cream_vs_heater

import nnetsauce as nsimport pandas as pdimport numpy as npfrom cybooster import NGBRegressor, NGBClassifier, SkNGBRegressorfrom sklearn.datasets import load_diabetes, fetch_openmlfrom sklearn.model_selection import train_test_splitfrom sklearn.datasets import load_breast_cancer, load_iris, load_wine, load_digitsfrom sklearn.metrics import accuracy_score, mean_squared_error, root_mean_squared_errorfrom sklearn.linear_model import LinearRegression, Ridge, BayesianRidgefrom sklearn.tree import ExtraTreeRegressorfrom time import timeurl = "https://raw.githubusercontent.com/Techtonique/"url += "datasets/main/time_series/multivariate/"url += "ice_cream_vs_heater.csv"df_temp = pd.read_csv(url)df_temp.index = pd.DatetimeIndex(df_temp.date)# must have# first other differencedf_icecream = df_temp.drop(columns=['date']).diff().dropna()regr = ns.MTS(obj=SkNGBRegressor(),              lags=20,              type_pi="gaussian",              show_progress=True)regr.fit(df_icecream, return_std=True)preds = regr.predict(h=30) # Store prediction resultsregr.plot()100%|██████████| 2/2 [00:08<00:00,  4.38s/it]

image-title-here

USAccDeaths

url = "https://raw.githubusercontent.com/Techtonique/"url += "datasets/main/time_series/univariate/"url += "USAccDeaths.csv"df_temp = pd.read_csv(url)df_temp.index = pd.DatetimeIndex(df_temp.date)# must have# first other differencedf = df_temp.drop(columns=['date'])regr = ns.MTS(obj=SkNGBRegressor(),              lags=20,              type_pi="gaussian",              show_progress=True)regr.fit(df, return_std=True)preds = regr.predict(h=30) # Store prediction resultsregr.plot()100%|██████████| 1/1 [00:01<00:00,  1.25s/it]

image-title-here

nile

url = "https://raw.githubusercontent.com/Techtonique/"url += "datasets/main/time_series/univariate/"url += "nile.csv"df_temp = pd.read_csv(url)df_temp.index = pd.DatetimeIndex(df_temp.date)# must have# first other differencedf = df_temp.drop(columns=['date'])regr = ns.MTS(obj=SkNGBRegressor(),              lags=20,              type_pi="gaussian",              show_progress=True)regr.fit(df, return_std=True)preds = regr.predict(h=30) # Store prediction resultsregr.plot()100%|██████████| 1/1 [00:02<00:00,  2.36s/it]

image-title-here

from sklearn.linear_model import LinearRegressionurl = "https://raw.githubusercontent.com/Techtonique/"url += "datasets/main/time_series/univariate/"url += "AirPassengers.csv"df_temp = pd.read_csv(url)df_temp.index = pd.DatetimeIndex(df_temp.date)# must have# first other differencedf = df_temp.drop(columns=['date'])regr = ns.MTS(obj=SkNGBRegressor(LinearRegression()),              lags=20,              type_pi="gaussian",              show_progress=True)regr.fit(df, return_std=True)preds = regr.predict(h=30) # Store prediction resultsregr.plot()100%|██████████| 1/1 [00:01<00:00,  1.11s/it]

image-title-here

from sklearn.linear_model import Ridgeurl = "https://raw.githubusercontent.com/Techtonique/"url += "datasets/main/time_series/univariate/"url += "a10.csv"df_temp = pd.read_csv(url)df_temp.index = pd.DatetimeIndex(df_temp.date)# must have# first other differencedf = df_temp.drop(columns=['date'])regr = ns.MTS(obj=SkNGBRegressor(Ridge()),              lags=15,              type_pi="gaussian",              show_progress=True)regr.fit(df, return_std=True)preds = regr.predict(h=30) # Store prediction resultsregr.plot()100%|██████████| 1/1 [00:00<00:00,  1.01it/s]

image-title-here

2 - R version

%load_ext rpy2.ipython%%Rinstall.packages("pak")pak::pak("reticulate")%%Rpak::pak(c("readr", "xts", "ggplot2"))%%R# Load necessary librarieslibrary(reticulate)library(readr)library(xts)library(ggplot2)# Import Python packagesns <- import("nnetsauce")cyb <- import("cybooster")sklearn <- import("sklearn")# Load the dataseturl <- "https://raw.githubusercontent.com/Techtonique/datasets/main/time_series/multivariate/ice_cream_vs_heater.csv"df_temp <- read.csv(url)%%Rhead(df_temp)        date heater icecream1 2004-01-01     27       132 2004-02-01     18       153 2004-03-01     14       164 2004-04-01     13       195 2004-05-01     13       216 2004-06-01     13       24%%Rnp <- import("numpy")# Assuming SkNGBRegressor is available in the sklearn R package or a similar implementation# If not, you might need to use a different model or wrap the Python versionregr <- ns$MTS(obj = cyb$SkNGBRegressor(),               lags = 20L,               type_pi = "gaussian",               show_progress = TRUE)%%Rdf <- df_temp[, -1]rownames(df) <- df_temp$date%%Rdf           heater icecream2004-01-01     27       132004-02-01     18       152004-03-01     14       162004-04-01     13       192004-05-01     13       212004-06-01     13       242004-07-01     13       272004-08-01     14       202004-09-01     15       182004-10-01     20       152004-11-01     24       152004-12-01     29       142005-01-01     27       152005-02-01     17       152005-03-01     15       172005-04-01     14       192005-05-01     13       222005-06-01     13       282005-07-01     12       292005-08-01     13       212005-09-01     16       162005-10-01     25       142005-11-01     25       142005-12-01     31       142006-01-01     21       142006-02-01     20       152006-03-01     16       162006-04-01     14       192006-05-01     13       232006-06-01     13       272006-07-01     13       322006-08-01     13       242006-09-01     16       192006-10-01     22       162006-11-01     23       162006-12-01     25       172007-01-01     25       162007-02-01     23       172007-03-01     16       182007-04-01     14       202007-05-01     13       252007-06-01     13       302007-07-01     12       292007-08-01     12       232007-09-01     15       192007-10-01     20       152007-11-01     26       152007-12-01     29       162008-01-01     26       152008-02-01     20       172008-03-01     16       172008-04-01     15       202008-05-01     14       252008-06-01     14       282008-07-01     14       282008-08-01     14       232008-09-01     17       182008-10-01     26       152008-11-01     28       152008-12-01     31       142009-01-01     29       152009-02-01     21       172009-03-01     17       182009-04-01     15       222009-05-01     14       272009-06-01     14       322009-07-01     13       342009-08-01     13       302009-09-01     16       242009-10-01     24       192009-11-01     23       202009-12-01     33       182010-01-01     30       182010-02-01     22       192010-03-01     17       212010-04-01     15       232010-05-01     14       282010-06-01     12       302010-07-01     11       342010-08-01     12       282010-09-01     14       222010-10-01     21       182010-11-01     27       172010-12-01     32       162011-01-01     31       242011-02-01     24       242011-03-01     18       252011-04-01     15       452011-05-01     14       342011-06-01     14       412011-07-01     13       462011-08-01     14       352011-09-01     17       302011-10-01     25       302011-11-01     31       272011-12-01     32       292012-01-01     28       302012-02-01     21       302012-03-01     17       352012-04-01     15       392012-05-01     14       462012-06-01     13       532012-07-01     13       552012-08-01     13       412012-09-01     16       312012-10-01     25       242012-11-01     32       232012-12-01     29       232013-01-01     30       242013-02-01     23       252013-03-01     20       272013-04-01     16       312013-05-01     15       372013-06-01     14       442013-07-01     14       482013-08-01     14       372013-09-01     17       282013-10-01     27       222013-11-01     36       212013-12-01     39       212014-01-01     39       242014-02-01     28       242014-03-01     21       282014-04-01     17       322014-05-01     16       392014-06-01     15       452014-07-01     15       512014-08-01     16       402014-09-01     19       282014-10-01     26       232014-11-01     45       212014-12-01     32       222015-01-01     36       242015-02-01     32       262015-03-01     21       332015-04-01     17       402015-05-01     17       462015-06-01     17       492015-07-01     16       572015-08-01     17       452015-09-01     19       352015-10-01     29       272015-11-01     37       262015-12-01     35       252016-01-01     40       302016-02-01     28       322016-03-01     21       382016-04-01     20       452016-05-01     19       512016-06-01     18       612016-07-01     17       712016-08-01     17       522016-09-01     21       422016-10-01     29       392016-11-01     39       462016-12-01     52       662017-01-01     40       352017-02-01     27       392017-03-01     25       442017-04-01     20       552017-05-01     21       602017-06-01     20       742017-07-01     19       892017-08-01     19       642017-09-01     23       482017-10-01     33       402017-11-01     43       362017-12-01     56       352018-01-01     56       402018-02-01     33       422018-03-01     27       512018-04-01     24       562018-05-01     22       712018-06-01     21       792018-07-01     21       912018-08-01     21       662018-09-01     24       492018-10-01     39       392018-11-01     53       342018-12-01     48       362019-01-01     49       392019-02-01     39       422019-03-01     30       532019-04-01     24       572019-05-01     23       652019-06-01     22       822019-07-01     21      1002019-08-01     21       682019-09-01     24       512019-10-01     40       402019-11-01     56       362019-12-01     46       362020-01-01     41       432020-02-01     34       452020-03-01     25       442020-04-01     25       532020-05-01     27       702020-06-01     24       74%%R# Fit the modelregr$fit(df)100%|██████████| 2/2 [00:05<00:00,  2.66s/it]MTS(lags=20, obj=SkNGBRegressor(), type_pi='gaussian')%%Rlibrary(ggplot2)# Make predictionspreds <- regr$predict(h = 30L, return_std=TRUE)# Plot the resultsregr$plot("heater")regr$plot("icecream")

image-title-here

image-title-here

%%RpredsDescribeResult(mean=            heater  icecreamdate                        2020-07-01   22.07     93.222020-08-01   22.04     69.472020-09-01   23.94     54.682020-10-01   40.38     42.042020-11-01   52.47     39.012020-12-01   45.44     38.332021-01-01   42.34     41.622021-02-01   35.54     45.682021-03-01   25.94     45.462021-04-01   25.93     54.192021-05-01   27.34     69.472021-06-01   24.67     74.852021-07-01   22.86     93.392021-08-01   22.07     73.812021-09-01   23.86     52.582021-10-01   40.81     46.882021-11-01   51.47     46.632021-12-01   47.05     41.832022-01-01   42.96     42.512022-02-01   37.37     45.352022-03-01   30.64     44.622022-04-01   27.21     53.502022-05-01   27.05     69.652022-06-01   24.48     72.622022-07-01   22.68     91.982022-08-01   22.01     71.782022-09-01   23.78     54.592022-10-01   38.75     52.852022-11-01   48.41     54.602022-12-01   46.83     48.62, lower=            heater  icecreamdate                        2020-07-01   20.34     90.502020-08-01   20.31     66.752020-09-01   22.21     51.962020-10-01   38.65     39.322020-11-01   50.75     36.282020-12-01   43.71     35.612021-01-01   40.61     38.902021-02-01   33.81     42.962021-03-01   24.21     42.732021-04-01   24.20     51.472021-05-01   25.61     66.752021-06-01   22.95     72.132021-07-01   21.14     90.672021-08-01   20.34     71.092021-09-01   22.13     49.862021-10-01   39.09     44.162021-11-01   49.74     43.912021-12-01   45.33     39.112022-01-01   41.24     39.782022-02-01   35.64     42.632022-03-01   28.91     41.902022-04-01   25.48     50.772022-05-01   25.32     66.922022-06-01   22.76     69.902022-07-01   20.95     89.262022-08-01   20.28     69.062022-09-01   22.05     51.872022-10-01   37.02     50.132022-11-01   46.69     51.882022-12-01   45.10     45.90, upper=            heater  icecreamdate                        2020-07-01   23.80     95.942020-08-01   23.77     72.192020-09-01   25.67     57.402020-10-01   42.11     44.772020-11-01   54.20     41.732020-12-01   47.16     41.052021-01-01   44.06     44.352021-02-01   37.27     48.402021-03-01   27.67     48.182021-04-01   27.65     56.912021-05-01   29.07     72.192021-06-01   26.40     77.582021-07-01   24.59     96.122021-08-01   23.79     76.542021-09-01   25.58     55.312021-10-01   42.54     49.602021-11-01   53.20     49.362021-12-01   48.78     44.552022-01-01   44.69     45.232022-02-01   39.09     48.082022-03-01   32.36     47.342022-04-01   28.93     56.222022-05-01   28.78     72.372022-06-01   26.21     75.342022-07-01   24.40     94.702022-08-01   23.73     74.512022-09-01   25.50     57.322022-10-01   40.47     55.572022-11-01   50.14     57.332022-12-01   48.56     51.34)
To leave a comment for the author, please follow the link and comment on their blog: T. Moudiki's Webpage - R.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
Continue reading: Native uncertainty quantification for time series with NGBoost