Package {hmix}


Type: Package
Title: Hidden Markov Model for Predicting Time Sequences with Mixture Sampling
Version: 2.0.0
Maintainer: Giancarlo Vercellino <giancarlo.vercellino@gmail.com>
Description: Probabilistic time series forecasting using clustered windows and hidden Markov models. Provides empirical marginal forecasts, simulated prediction paths, rolling-origin evaluation, and model complexity selection.
License: GPL-3
Encoding: UTF-8
LazyData: true
Imports: purrr (≥ 1.0.1), HMM (≥ 1.0.1), mc2d (≥ 0.2.0), Rcpp (≥ 1.0.0), parallel
LinkingTo: Rcpp
URL: https://rpubs.com/giancarlo_vercellino/hmix
Suggests: testthat (≥ 3.0.0), knitr, rmarkdown
VignetteBuilder: knitr
Config/testthat/edition: 3
Depends: R (≥ 4.1.0)
NeedsCompilation: yes
Author: Giancarlo Vercellino [aut, cre, cph]
Config/roxygen2/version: 8.1.1
Packaged: 2026-12-01 15:51:52 UTC; CodexSandboxOffline
Repository: CRAN
Date/Publication: 2026-12-01 17:30:02 UTC

hmix: an algorithm for time series analysis that leverages hidden Markov models, cluster analysis, and mixture distributions to segment data, detect patterns and predict future sequences.

Description

Probabilistic time series forecasting using clustered windows and hidden Markov models. Provides empirical marginal forecasts, simulated prediction paths, rolling-origin evaluation, and model complexity selection.

hmix function segments the time series with k-means clustering, fits an HMM to model state transitions, and generates future predictions over a specified horizon. It evaluates model accuracy by calculating the Continuous Ranked Probability Score (CRPS) across multiple test points, producing error metrics that assess the model's predictive performance and robustness.

Usage

hmix(
  ts,
  horizon,
  centers = 10,
  n_hidden = 4,
  seed = 42,
  n_tests = 20,
  warmup = 0.5,
  transform = c("auto", "log", "difference", "return"),
  stride = 1,
  n_samples = 1000,
  max_iterations = 1000,
  tolerance = 1e-09,
  training_window = NULL,
  kmeans_starts = 10,
  workers = 1,
  engine = c("compiled", "reference")
)

Arguments

ts

A numeric vector representing the time series data.

horizon

Integer. The prediction horizon, specifying how many future points to forecast.

centers

Integer. Number of clusters for k-means clustering. Default: 10.

n_hidden

Integer. Number of hidden states in the Hidden Markov Model. Default: 4.

seed

Integer. Random seed for reproducibility. Default: 42.

n_tests

Integer. Number of testing points for back-testing. Default: 20.

warmup

Numeric. Proportion of the time series used as the warm-up period before testing. Default: 0.5.

transform

Transformation for the series: '"auto"' uses log changes for strictly positive data and differences otherwise; '"log"', '"difference"', and '"return"' select a specific transformation.

stride

Spacing between training windows. Must divide 'horizon'. Default 1 uses overlapping windows.

n_samples

Number of forecast paths to sample.

max_iterations

Maximum HMM training iterations.

tolerance

HMM convergence tolerance.

training_window

Maximum number of recent observations to use per fit, or NULL for all available data.

kmeans_starts

Number of k-means initializations.

workers

Number of parallel workers for backtesting: 1 (default) or 2.

engine

HMM training implementation: compiled (default) or reference.

Value

This function returns a list containing:

Author(s)

Maintainer: Giancarlo Vercellino giancarlo.vercellino@gmail.com [copyright holder]

Authors:

See Also

Useful links:

Examples


# Example usage of hmix function:
result <- hmix(dummy_set$AMZN, horizon = 10, centers = 5, n_hidden = 3, n_tests = 2)
print(result$model)
print(result$error_sets)

# Random sampling for each point in predicted horizon
result$model$pred_funs$t1$rfun(10)

# Sample 10 complete paths (one per row)
result$model$random_path(10)

# ICDF for each point in horizon
result$model$pred_funs$t5$qfun(c(0, 1))

# PDF for each point in horizon
result$model$pred_funs$t8$dfun(tail(dummy_set$AMZN))

# CDF for each point in horizon
result$model$pred_funs$t10$pfun(tail(dummy_set$AMZN))



A simple data set with stock close prices

Description

A data frame with the close prices for AMZN, NVDA and IBM

Usage

dummy_set

Format

data frame with 4 columns and 1925 rows.

Source

Yahoo Finance


Select model complexity with rolling-origin CRPS

Description

Compare candidate cluster and state counts on chronological validation origins. Reserve a separate final holdout before calling this function.

Usage

tune_hmix(
  ts,
  horizon,
  candidates = expand.grid(centers = c(2L, 4L, 6L), n_hidden = c(2L, 3L)),
  n_tests = 3L,
  warmup = 0.6,
  seed = 42L,
  transform = "auto",
  stride = 1L,
  n_samples = 1000L,
  max_iterations = 1000L,
  tolerance = 1e-09,
  training_window = NULL,
  kmeans_starts = 10L,
  workers = 1L,
  engine = c("compiled", "reference")
)

Arguments

ts

Numeric training and validation series, excluding the final holdout.

horizon

Forecast horizon.

candidates

Data frame with integer columns 'centers' and 'n_hidden'.

n_tests

Number of validation origins.

warmup

Fraction of the series before the earliest validation origin.

seed

Random seed.

transform

Transformation passed to [hmix()].

stride

Window stride passed to [hmix()].

n_samples

Number of forecast paths.

max_iterations

Maximum HMM training iterations.

tolerance

HMM convergence tolerance.

training_window

Maximum observations used per fit, or NULL.

kmeans_starts

Number of k-means initializations.

workers

Number of parallel candidate workers: 1 (default) or 2.

engine

HMM training implementation, compiled or reference.

Value

A list with 'best' (one candidate row) and 'results' (all scores, elapsed seconds and error messages). Failed candidates are not selected.