Package 'hmix'

Title: Hidden Markov Model for Predicting Time Sequences with Mixture Sampling
Description: 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.
Authors: Giancarlo Vercellino [aut, cre, cph]
Maintainer: Giancarlo Vercellino <[email protected]>
License: GPL-3
Version: 1.0.2
Built: 2025-01-09 07:01:34 UTC
Source: CRAN

Help Index


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

An object of class data.frame with 1925 rows and 4 columns.

Source

Yahoo Finance


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

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.

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
)

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.

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 (rfun, dfun, pfun, qfun)

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

Author(s)

Maintainer: Giancarlo Vercellino [email protected] [copyright holder]

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)

# 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(ts))

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