| Title: | Clock of Regimes for Regime-Switching Fragility Analysis |
|---|---|
| Description: | Implements the Clock of Regimes (KRONX) framework for regime-switching fragility analysis of financial time series. The package fits Gaussian and Student-t Hidden Markov Models (HMMs) to return data, constructs a hazard-adjusted transition operator Q, derives the associated generator K = Q - I, and computes the fundamental matrix N = -K inverse to characterize expected residence times under structural fragility. |
| Authors: | Oscar Linares [aut, cre] |
| Maintainer: | Oscar Linares <[email protected]> |
| License: | GPL (>= 3) |
| Version: | 0.1.0 |
| Built: | 2026-06-08 06:37:35 UTC |
| Source: | https://github.com/cran/KRONX |
Compute log returns from a close-price vector.
compute_log_returns(close)compute_log_returns(close)
close |
Numeric vector of close prices. |
Numeric vector of log returns (length = length(close) - 1).
Fit a Gaussian or Student-t HMM via EM with multiple random restarts.
fit_hmm_em( x, K = 3L, model = c("gaussian", "student"), n_starts = 5L, max_iter = 200L, tol = 1e-06, nu_grid = c(3:30, 40, 60, 100), verbose = TRUE )fit_hmm_em( x, K = 3L, model = c("gaussian", "student"), n_starts = 5L, max_iter = 200L, tol = 1e-06, nu_grid = c(3:30, 40, 60, 100), verbose = TRUE )
x |
Numeric vector of observations (log returns). |
K |
Integer number of hidden states. |
model |
|
n_starts |
Number of random restarts. |
max_iter |
Maximum EM iterations per restart. |
tol |
Log-likelihood convergence tolerance. |
nu_grid |
Candidate degrees-of-freedom for Student-t grid search. |
verbose |
Logical; if |
A list with components:
Model type string.
K x K transition matrix.
Length-K initial state probability vector.
List with mu, sigma, and (Student-t) nu.
T x K posterior state probabilities.
(T-1) x K x K posterior joint transition probabilities.
Scalar log-likelihood at convergence.
Number of observations.
Number of states.
The function is case-insensitive: it accepts close, Close,
CLOSE, or any capitalisation. The first matching column is used.
read_close_series(file, close_col = "close")read_close_series(file, close_col = "close")
file |
Path to the CSV file. |
close_col |
Name of the close-price column (default |
A numeric vector of finite close prices (length >= 10).
Runs the KRONX pipeline on either a CSV file of prices or a numeric vector
of returns supplied directly. When file is used, the function reads
the CSV file, extracts the close-price column (case-insensitive match on
"close" or "Close"), and computes log returns. When
x is supplied, it is treated as a numeric return series and the
file-ingestion step is skipped.
run_kronx( file = NULL, x = NULL, close_col = "close", K = 3L, n_starts = 5L, max_iter = 200L, tol = 1e-06, seed = 123L, epsilon_min = 0.01, c_hazard = 0.05, tail_alpha = 0.01, ruin_horizon = 250L, nu_grid = c(3:30, 40, 60, 100), verbose = TRUE, output_dir = "kronx_output" )run_kronx( file = NULL, x = NULL, close_col = "close", K = 3L, n_starts = 5L, max_iter = 200L, tol = 1e-06, seed = 123L, epsilon_min = 0.01, c_hazard = 0.05, tail_alpha = 0.01, ruin_horizon = 250L, nu_grid = c(3:30, 40, 60, 100), verbose = TRUE, output_dir = "kronx_output" )
file |
Path to a CSV file containing a close-price column. Ignored if
|
x |
Optional numeric vector of returns. If supplied, |
close_col |
Name of the close-price column. The function first tries
the exact name, then a case-insensitive match, and finally falls back to
any column whose name contains |
K |
Number of hidden states. Default: |
n_starts |
Number of EM random restarts. Default: |
max_iter |
Maximum EM iterations per restart. Default: |
tol |
Log-likelihood convergence tolerance. Default: |
seed |
Random seed for reproducibility. Default: |
epsilon_min |
Minimum baseline Talebian hazard. Default: |
c_hazard |
Hazard sensitivity to tail thickness. Default:
|
tail_alpha |
Left-tail probability used for the empirical loss
threshold. Default: |
ruin_horizon |
Horizon, in observations, used in the risk bound.
Default: |
nu_grid |
Candidate degrees-of-freedom values for Student-t fitting.
Default: |
verbose |
Logical; if |
output_dir |
Directory for output files. Created if it does not exist.
Pass |
The function then fits Gaussian and Student-t Hidden Markov Models (HMMs),
builds the KRONX operator chain Q K N,
and computes a residence-weighted upper bound on extreme downside risk.
Results are returned as a named list and, optionally, written to
output_dir.
A named list containing:
Numeric vector of raw close prices, or NULL when
x is supplied directly.
Numeric vector of log returns or supplied returns.
Number of hidden states.
Fitted Gaussian HMM object.
Fitted Student-t HMM object.
Gaussian transition matrix.
Student-t transition matrix.
Per-state Talebian hazard vector.
Hazard-adjusted operator.
KRONX generator matrix.
Fundamental matrix.
Quasi-stationary distribution.
Residence-weight vector.
Empirical left-tail return threshold.
Per-state tail probability.
Residence-weighted tail probability.
Residence-weighted hazard.
Upper bound over ruin_horizon.
The tail_alpha value used.
The ruin_horizon value used.
A data.frame summarising per-state
quantities.
Integer vector of Viterbi-decoded Student-t state labels.
Character vector of paths to written output files, or
NULL if output_dir is NULL.
A short label describing the data source used.
# Small self-contained example suitable for automatic checking set.seed(123) x <- rnorm(120, mean = 0, sd = 0.01) res <- run_kronx( x = x, K = 2L, n_starts = 1L, max_iter = 10L, tol = 1e-4, tail_alpha = 0.05, ruin_horizon = 25L, verbose = FALSE, output_dir = NULL ) res$ruin_bound dim(res$A_t) # Self-contained file-based example tmp <- tempfile(fileext = ".csv") dat <- data.frame( date = sprintf("2026-01-%02d", 1:20), close = c( 100.0, 100.2, 100.1, 100.5, 100.4, 100.8, 101.0, 100.9, 101.3, 101.1, 101.6, 101.5, 101.9, 102.0, 101.8, 102.2, 102.4, 102.3, 102.7, 102.9 ) ) utils::write.csv(dat, tmp, row.names = FALSE) res_file <- run_kronx( file = tmp, close_col = "close", K = 2L, n_starts = 1L, max_iter = 8L, tol = 1e-4, tail_alpha = 0.10, ruin_horizon = 10L, verbose = FALSE, output_dir = NULL ) res_file$ruin_bound# Small self-contained example suitable for automatic checking set.seed(123) x <- rnorm(120, mean = 0, sd = 0.01) res <- run_kronx( x = x, K = 2L, n_starts = 1L, max_iter = 10L, tol = 1e-4, tail_alpha = 0.05, ruin_horizon = 25L, verbose = FALSE, output_dir = NULL ) res$ruin_bound dim(res$A_t) # Self-contained file-based example tmp <- tempfile(fileext = ".csv") dat <- data.frame( date = sprintf("2026-01-%02d", 1:20), close = c( 100.0, 100.2, 100.1, 100.5, 100.4, 100.8, 101.0, 100.9, 101.3, 101.1, 101.6, 101.5, 101.9, 102.0, 101.8, 102.2, 102.4, 102.3, 102.7, 102.9 ) ) utils::write.csv(dat, tmp, row.names = FALSE) res_file <- run_kronx( file = tmp, close_col = "close", K = 2L, n_starts = 1L, max_iter = 8L, tol = 1e-4, tail_alpha = 0.10, ruin_horizon = 10L, verbose = FALSE, output_dir = NULL ) res_file$ruin_bound
Decode the most likely hidden-state path via the Viterbi algorithm.
viterbi_decode(x, fit)viterbi_decode(x, fit)
x |
Numeric vector of observations (log returns). |
fit |
A fitted HMM object returned by |
An integer vector of length length(x) with state labels
(1-indexed).