hmix: hmix: an algorithm for time series analysis that leverages...

View source: R/main1.R

hmixR Documentation

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:

  • model: The HMM model along with its estimated parameters.

    • hmm_model: The object includes classified observations, initial HMM and trained HMM.

    • pred_funs: Prediction functions for each point in horizon. 'pfun', 'qfun' and 'rfun' use the empirical samples; 'dfun' is a smoothed density estimate for visualization and is not the derivative of the empirical CDF.

    • random_path: A function taking a nonnegative integer 'n' and returning an 'n' by 'horizon' matrix of forecast paths. It resamples complete simulated paths with replacement, preserving their dependence.

  • error_sets: A list of error metrics calculated for each testing point (at this time, CRPS).

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))



hmix documentation built on Dec. 2, 2026, 1:09 a.m.