### R code from vignette source 'kronx.Rnw' ################################################### ### code chunk number 1: setup ################################################### options(width = 80) ################################################### ### code chunk number 2: quick-start ################################################### set.seed(123) x <- rnorm(120, mean = 0, sd = 0.01) res <- KRONX::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) ################################################### ### code chunk number 3: exports (eval = FALSE) ################################################### ## getNamespaceExports("KRONX") ################################################### ### code chunk number 4: run-kronx-signature (eval = FALSE) ################################################### ## KRONX::run_kronx( ## file = NULL, ## x = NULL, ## close_col = "close", ## K = 3L, ## n_starts = 5L, ## max_iter = 200L, ## tol = 1e-6, ## 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" ## ) ################################################### ### code chunk number 5: fit-hmm ################################################### set.seed(123) x <- rnorm(100, 0, 0.01) fit <- KRONX::fit_hmm_em( x = x, K = 2L, model = "gaussian", n_starts = 1L, max_iter = 8L, tol = 1e-4, verbose = FALSE ) fit$logLik ################################################### ### code chunk number 6: viterbi ################################################### states <- KRONX::viterbi_decode(x, fit) table(states) ################################################### ### code chunk number 7: data-io (eval = FALSE) ################################################### ## px <- KRONX::read_close_series("my_prices.csv", close_col = "close") ## ret <- KRONX::compute_log_returns(px) ## summary(ret) ################################################### ### code chunk number 8: operator-chain ################################################### set.seed(123) x <- rnorm(120, 0, 0.01) t_fit <- KRONX::fit_hmm_em( x = x, K = 2L, model = "student", n_starts = 1L, max_iter = 8L, tol = 1e-4, nu_grid = c(3, 5, 10), verbose = FALSE ) epsilon <- KRONX:::hazard_from_nu( nu = t_fit$params$nu, epsilon_min = 0.01, c_hazard = 0.05 ) Q <- KRONX:::build_Q(t_fit$A, epsilon) K_mat <- KRONX:::build_K(Q) N_mat <- KRONX:::fundamental_matrix(K_mat) epsilon Q ################################################### ### code chunk number 9: ruin-components ################################################### loss_threshold <- stats::quantile(x, probs = 0.05) q_state <- KRONX:::left_tail_state_probs(t_fit, loss_threshold) pi_qs <- KRONX:::quasi_stationary_distribution(Q) pi_res <- KRONX:::residence_weight_vector(N_mat, init = pi_qs) bar_q <- sum(pi_res * q_state) bar_lambda <- sum(pi_res * epsilon) ruin_bound <- 1 - exp(-bar_lambda * bar_q * 25) c(bar_q = bar_q, bar_lambda = bar_lambda, ruin_bound = ruin_bound) ################################################### ### code chunk number 10: file-workflow (eval = FALSE) ################################################### ## res_file <- KRONX::run_kronx( ## file = "IBKR_ES_2Y1h.csv", ## close_col = "close", ## verbose = FALSE, ## output_dir = NULL ## ) ## ## res_file$ruin_bound ################################################### ### code chunk number 11: tests (eval = FALSE) ################################################### ## devtools::test() ## testthat::test_package("KRONX")