mirror of
https://github.com/SHOGGOTH-SECTOR/sica-fondt.git
synced 2026-11-29 08:25:10 +00:00
M3a statistical sims implementation + Ada config
- Install hook: add three vital CRAN packages (HiddenMarkov, rugarch, rmgarch) - M3a spec: document vital packages and hand-rolled implementations - Ada config: create core_config.gpr with compiler flags for GNAT 2022 - M3a sims: implement 8 stochastic models (GBM, Heston, jump-diffusion, fBM, copula, HMM regimes, GARCH, DCC-GARCH) with hand-rolled JSON I/O - json_io.R: recursive-descent parser + emitter (no jsonlite) - sde_sims.R: GBM, Heston (Euler-Maruyama), jump-diffusion, fBM (Wood & Chan) - copula.R: empirical copula + tail-dependence - regimes_garch.R: lazy-load vital packages for regime/GARCH/DCC sims - main.R: Hub stdin/stdout protocol entry point Non-Turing M1 law script design complete (S-expressions + fixed combinators). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
# copula.R — hand-rolled copula tail-dependence (M3a spec §3).
|
||||
# Gaussian copula via empirical ranks + normal scores; lower/upper tail
|
||||
# dependence estimated empirically. Used for cross-token contagion risk:
|
||||
# given a reference token's move, how far does the target move with it?
|
||||
|
||||
# Empirical copula pseudo-observations.
|
||||
.pobs <- function(x) rank(x, ties.method = "average") / (length(x) + 1)
|
||||
|
||||
# Pearson correlation of normal scores = Gaussian-copula rho.
|
||||
copula_rho <- function(x, y) {
|
||||
cor(qnorm(.pobs(x)), qnorm(.pobs(y)))
|
||||
}
|
||||
|
||||
# Empirical tail dependence at quantile q: P(V <= q | U <= q) (lower) and
|
||||
# P(V > 1-q | U > 1-q) (upper).
|
||||
copula_tails <- function(x, y, q = 0.05) {
|
||||
u <- .pobs(x); v <- .pobs(y)
|
||||
lo <- if (any(u <= q)) mean(v[u <= q] <= q) else 0
|
||||
hi <- if (any(u >= 1 - q)) mean(v[u >= 1 - q] >= 1 - q) else 0
|
||||
list(lower = lo, upper = hi)
|
||||
}
|
||||
|
||||
# Conditional co-move forecast: if the reference asset shifts by ref_shift
|
||||
# (log-return), the Gaussian copula conditional mean of the target's return
|
||||
# is rho * (sigma_t / sigma_r) * ref_shift, with conditional sd
|
||||
# sigma_t * sqrt(1 - rho^2). Returns a price band for the target.
|
||||
sim_copula <- function(prices, ref_prices, horizon, ref_shift = NULL,
|
||||
level = 0.90) {
|
||||
rt <- diff(log(prices)); rr <- diff(log(ref_prices))
|
||||
n <- min(length(rt), length(rr))
|
||||
rt <- tail(rt, n); rr <- tail(rr, n)
|
||||
rho <- copula_rho(rr, rt)
|
||||
if (is.null(ref_shift)) ref_shift <- mean(rr) * horizon
|
||||
st <- sd(rt); sr <- sd(rr)
|
||||
cond_mu <- mean(rt) * horizon + rho * (st / sr) * (ref_shift - mean(rr) * horizon)
|
||||
cond_sd <- st * sqrt(pmax(1 - rho^2, 1e-12)) * sqrt(horizon)
|
||||
s0 <- prices[length(prices)]
|
||||
zq <- qnorm(1 - (1 - level) / 2)
|
||||
tails <- copula_tails(rr, rt)
|
||||
list(value = s0 * exp(cond_mu),
|
||||
lower_bound = s0 * exp(cond_mu - zq * cond_sd),
|
||||
upper_bound = s0 * exp(cond_mu + zq * cond_sd),
|
||||
certainty = abs(rho) * (1 - abs(tails$lower - tails$upper)))
|
||||
}
|
||||
@@ -0,0 +1,147 @@
|
||||
# json_io.R — hand-rolled JSON for the M3 Hub stdin/stdout protocol.
|
||||
# No jsonlite: the only vital CRAN packages in M3a are HiddenMarkov, rugarch,
|
||||
# rmgarch (spec §3). Recursive-descent parser + emitter over base R strings.
|
||||
|
||||
json_parse <- function(txt) {
|
||||
env <- new.env()
|
||||
env$s <- txt
|
||||
env$i <- 1L
|
||||
env$n <- nchar(txt)
|
||||
val <- .jp_value(env)
|
||||
.jp_ws(env)
|
||||
if (env$i <= env$n) stop("json_parse: trailing garbage at ", env$i)
|
||||
val
|
||||
}
|
||||
|
||||
.jp_peek <- function(env) substr(env$s, env$i, env$i)
|
||||
|
||||
.jp_ws <- function(env) {
|
||||
while (env$i <= env$n && .jp_peek(env) %in% c(" ", "\t", "\n", "\r"))
|
||||
env$i <- env$i + 1L
|
||||
}
|
||||
|
||||
.jp_expect <- function(env, ch) {
|
||||
if (.jp_peek(env) != ch)
|
||||
stop("json_parse: expected '", ch, "' at ", env$i, ", got '", .jp_peek(env), "'")
|
||||
env$i <- env$i + 1L
|
||||
}
|
||||
|
||||
.jp_value <- function(env) {
|
||||
.jp_ws(env)
|
||||
ch <- .jp_peek(env)
|
||||
if (ch == "{") return(.jp_object(env))
|
||||
if (ch == "[") return(.jp_array(env))
|
||||
if (ch == "\"") return(.jp_string(env))
|
||||
if (ch == "t") { .jp_lit(env, "true"); return(TRUE) }
|
||||
if (ch == "f") { .jp_lit(env, "false"); return(FALSE) }
|
||||
if (ch == "n") { .jp_lit(env, "null"); return(NULL) }
|
||||
.jp_number(env)
|
||||
}
|
||||
|
||||
.jp_lit <- function(env, lit) {
|
||||
k <- nchar(lit)
|
||||
if (substr(env$s, env$i, env$i + k - 1L) != lit)
|
||||
stop("json_parse: bad literal at ", env$i)
|
||||
env$i <- env$i + k
|
||||
}
|
||||
|
||||
.jp_object <- function(env) {
|
||||
.jp_expect(env, "{")
|
||||
out <- list()
|
||||
.jp_ws(env)
|
||||
if (.jp_peek(env) == "}") { env$i <- env$i + 1L; return(out) }
|
||||
repeat {
|
||||
.jp_ws(env)
|
||||
key <- .jp_string(env)
|
||||
.jp_ws(env)
|
||||
.jp_expect(env, ":")
|
||||
out[[key]] <- .jp_value(env)
|
||||
.jp_ws(env)
|
||||
if (.jp_peek(env) == ",") { env$i <- env$i + 1L; next }
|
||||
.jp_expect(env, "}")
|
||||
return(out)
|
||||
}
|
||||
}
|
||||
|
||||
.jp_array <- function(env) {
|
||||
.jp_expect(env, "[")
|
||||
out <- list()
|
||||
.jp_ws(env)
|
||||
if (.jp_peek(env) == "]") { env$i <- env$i + 1L; return(out) }
|
||||
repeat {
|
||||
out[[length(out) + 1L]] <- .jp_value(env)
|
||||
.jp_ws(env)
|
||||
if (.jp_peek(env) == ",") { env$i <- env$i + 1L; next }
|
||||
.jp_expect(env, "]")
|
||||
return(out)
|
||||
}
|
||||
}
|
||||
|
||||
.jp_string <- function(env) {
|
||||
.jp_expect(env, "\"")
|
||||
chars <- character(0)
|
||||
repeat {
|
||||
ch <- .jp_peek(env)
|
||||
if (ch == "") stop("json_parse: unterminated string")
|
||||
env$i <- env$i + 1L
|
||||
if (ch == "\"") return(paste0(chars, collapse = ""))
|
||||
if (ch == "\\") {
|
||||
esc <- .jp_peek(env)
|
||||
env$i <- env$i + 1L
|
||||
ch <- switch(esc,
|
||||
"\"" = "\"", "\\" = "\\", "/" = "/", b = "\b", f = "\f",
|
||||
n = "\n", r = "\r", t = "\t",
|
||||
u = {
|
||||
hex <- substr(env$s, env$i, env$i + 3L)
|
||||
env$i <- env$i + 4L
|
||||
intToUtf8(strtoi(hex, 16L))
|
||||
},
|
||||
stop("json_parse: bad escape \\", esc))
|
||||
}
|
||||
chars <- c(chars, ch)
|
||||
}
|
||||
}
|
||||
|
||||
.jp_number <- function(env) {
|
||||
m <- regmatches(
|
||||
substr(env$s, env$i, env$n),
|
||||
regexpr("^-?(0|[1-9][0-9]*)(\\.[0-9]+)?([eE][+-]?[0-9]+)?",
|
||||
substr(env$s, env$i, env$n)))
|
||||
if (length(m) == 0 || m == "") stop("json_parse: bad number at ", env$i)
|
||||
env$i <- env$i + nchar(m)
|
||||
as.numeric(m)
|
||||
}
|
||||
|
||||
# --- emitter ----------------------------------------------------------------
|
||||
|
||||
json_emit <- function(x) {
|
||||
if (is.null(x)) return("null")
|
||||
if (is.list(x)) {
|
||||
if (!is.null(names(x)) && all(nzchar(names(x)))) {
|
||||
pairs <- vapply(names(x), function(k)
|
||||
paste0(.je_str(k), ":", json_emit(x[[k]])), character(1))
|
||||
return(paste0("{", paste0(pairs, collapse = ","), "}"))
|
||||
}
|
||||
return(paste0("[", paste0(vapply(x, json_emit, character(1)),
|
||||
collapse = ","), "]"))
|
||||
}
|
||||
if (length(x) > 1)
|
||||
return(paste0("[", paste0(vapply(x, json_emit, character(1)),
|
||||
collapse = ","), "]"))
|
||||
if (is.character(x)) return(.je_str(x))
|
||||
if (is.logical(x)) return(if (x) "true" else "false")
|
||||
if (is.numeric(x)) {
|
||||
if (!is.finite(x)) return("null")
|
||||
return(format(x, digits = 15, scientific = FALSE, trim = TRUE))
|
||||
}
|
||||
stop("json_emit: unsupported type ", class(x)[1])
|
||||
}
|
||||
|
||||
.je_str <- function(s) {
|
||||
s <- gsub("\\", "\\\\", s, fixed = TRUE)
|
||||
s <- gsub("\"", "\\\"", s, fixed = TRUE)
|
||||
s <- gsub("\n", "\\n", s, fixed = TRUE)
|
||||
s <- gsub("\r", "\\r", s, fixed = TRUE)
|
||||
s <- gsub("\t", "\\t", s, fixed = TRUE)
|
||||
paste0("\"", s, "\"")
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
#!/usr/bin/env Rscript
|
||||
# main.R — M3a statistical sims, Hub stdin/stdout protocol entry point.
|
||||
#
|
||||
# Request (one JSON object on stdin):
|
||||
# { "sim": "gbm|heston|jump_diffusion|fbm|copula|hmm_regime|garch|dcc",
|
||||
# "token_ticker": "XMR",
|
||||
# "prices": [ ... ], # price history, oldest first
|
||||
# "ref_prices": [ ... ], # required for copula/dcc only
|
||||
# "horizon": 24, # steps ahead
|
||||
# "seed": 42 } # optional, for reproducible runs
|
||||
#
|
||||
# Response (one BoundedPrediction JSON object on stdout, M3 hub §5):
|
||||
# value, lower_bound, upper_bound, confidence, correctness, certainty,
|
||||
# time_horizon, sim_type, timestamp, token_ticker, recent_shift
|
||||
#
|
||||
# correctness and confidence are OWNED BY THE HUB's calibration loop (M3 §5):
|
||||
# M3a emits its raw certainty and echoes nulls for the calibrated fields.
|
||||
# Errors go to stderr + exit 1; stdout carries only valid JSON.
|
||||
|
||||
base_dir <- dirname(sub("--file=", "",
|
||||
grep("--file=", commandArgs(FALSE), value = TRUE)[1]))
|
||||
source(file.path(base_dir, "json_io.R"))
|
||||
source(file.path(base_dir, "sde_sims.R"))
|
||||
source(file.path(base_dir, "copula.R"))
|
||||
|
||||
fail <- function(msg) { cat(msg, "\n", file = stderr()); quit(status = 1) }
|
||||
|
||||
req <- tryCatch(json_parse(paste(readLines("stdin"), collapse = "\n")),
|
||||
error = function(e) fail(paste("bad request:", e$message)))
|
||||
|
||||
sim <- req$sim
|
||||
ticker <- req$token_ticker
|
||||
horizon <- as.integer(req$horizon)
|
||||
prices <- as.numeric(unlist(req$prices))
|
||||
if (is.null(sim) || is.null(ticker)) fail("missing sim or token_ticker")
|
||||
if (length(prices) < 32) fail("need >= 32 price points")
|
||||
if (is.na(horizon) || horizon < 1) fail("bad horizon")
|
||||
if (!is.null(req$seed)) set.seed(as.integer(req$seed))
|
||||
|
||||
needs_ref <- sim %in% c("copula", "dcc")
|
||||
ref <- if (needs_ref) as.numeric(unlist(req$ref_prices)) else NULL
|
||||
if (needs_ref && length(ref) < 32) fail("sim needs ref_prices (>= 32 points)")
|
||||
|
||||
res <- switch(sim,
|
||||
gbm = sim_gbm(prices, horizon),
|
||||
heston = sim_heston(prices, horizon),
|
||||
jump_diffusion = sim_jump_diffusion(prices, horizon),
|
||||
fbm = sim_fbm(prices, horizon),
|
||||
copula = sim_copula(prices, ref, horizon),
|
||||
hmm_regime = ,
|
||||
garch = ,
|
||||
dcc = {
|
||||
# vital-package sims live in their own file so the light sims don't pay
|
||||
# the rugarch/rmgarch load time
|
||||
source(file.path(base_dir, "regimes_garch.R"))
|
||||
switch(sim,
|
||||
hmm_regime = sim_hmm_regime(prices, horizon),
|
||||
garch = sim_garch(prices, horizon),
|
||||
dcc = sim_dcc(prices, ref, horizon))
|
||||
},
|
||||
fail(paste("unknown sim:", sim)))
|
||||
|
||||
# recent_shift: realized log-return over the trailing horizon window —
|
||||
# same ground-truth window M2 uses for correctness scoring (M3 hub §5)
|
||||
window <- min(horizon, length(prices) - 1)
|
||||
recent_shift <- log(prices[length(prices)]) -
|
||||
log(prices[length(prices) - window])
|
||||
|
||||
out <- list(
|
||||
value = res$value,
|
||||
lower_bound = res$lower_bound,
|
||||
upper_bound = res$upper_bound,
|
||||
confidence = NULL, # Hub calibration owns this
|
||||
correctness = NULL, # Hub scoring owns this
|
||||
certainty = res$certainty,
|
||||
time_horizon = horizon,
|
||||
sim_type = paste0("statistical/", sim),
|
||||
timestamp = as.integer(Sys.time()),
|
||||
token_ticker = ticker,
|
||||
recent_shift = recent_shift)
|
||||
|
||||
cat(json_emit(out), "\n")
|
||||
@@ -0,0 +1,111 @@
|
||||
# regimes_garch.R — the vital-package sims (M3a spec §3).
|
||||
# HiddenMarkov: 2-state Gaussian HMM regime detection (Baum-Welch fit,
|
||||
# Viterbi decode). rugarch: univariate GARCH(1,1) volatility forecast.
|
||||
# rmgarch: DCC-GARCH cross-asset correlation. These three are the only
|
||||
# CRAN dependencies in M3a; everything else is hand-rolled.
|
||||
|
||||
suppressMessages({
|
||||
library(HiddenMarkov)
|
||||
library(rugarch)
|
||||
})
|
||||
|
||||
# Two-regime (calm/stressed) HMM on log-returns. Forecast blends the
|
||||
# per-regime means weighted by the transition row of the decoded current
|
||||
# state; certainty is the Viterbi state's posterior occupancy.
|
||||
sim_hmm_regime <- function(prices, horizon, level = 0.90) {
|
||||
r <- diff(log(prices))
|
||||
q1 <- unname(quantile(abs(r - mean(r)), 0.5))
|
||||
init <- list(mean = c(mean(r[abs(r - mean(r)) <= q1]),
|
||||
mean(r[abs(r - mean(r)) > q1])),
|
||||
sd = c(max(sd(r[abs(r - mean(r)) <= q1]), 1e-8),
|
||||
max(sd(r[abs(r - mean(r)) > q1]), 1e-8)))
|
||||
hmm <- dthmm(r,
|
||||
Pi = matrix(c(0.95, 0.05, 0.05, 0.95), 2, 2, byrow = TRUE),
|
||||
delta = c(0.5, 0.5),
|
||||
distn = "norm",
|
||||
pm = init)
|
||||
fit <- tryCatch(
|
||||
BaumWelch(hmm, control = bwcontrol(maxiter = 200, tol = 1e-5,
|
||||
prt = FALSE)),
|
||||
error = function(e) hmm)
|
||||
states <- Viterbi(fit)
|
||||
cur <- states[length(states)]
|
||||
pi_row <- fit$Pi[cur, ]
|
||||
step_mu <- sum(pi_row * fit$pm$mean)
|
||||
step_sd <- sqrt(sum(pi_row * (fit$pm$sd^2 + fit$pm$mean^2)) - step_mu^2)
|
||||
s0 <- prices[length(prices)]
|
||||
zq <- qnorm(1 - (1 - level) / 2)
|
||||
list(value = s0 * exp(step_mu * horizon),
|
||||
lower_bound = s0 * exp(step_mu * horizon - zq * step_sd * sqrt(horizon)),
|
||||
upper_bound = s0 * exp(step_mu * horizon + zq * step_sd * sqrt(horizon)),
|
||||
certainty = mean(states == cur))
|
||||
}
|
||||
|
||||
# GARCH(1,1) volatility-path forecast via rugarch. The point forecast is the
|
||||
# drift; bounds come from the aggregated forecast variance path, so they
|
||||
# widen exactly as fast as the fitted vol dynamics say they should.
|
||||
sim_garch <- function(prices, horizon, level = 0.90) {
|
||||
r <- diff(log(prices))
|
||||
spec <- ugarchspec(variance.model = list(model = "sGARCH",
|
||||
garchOrder = c(1, 1)),
|
||||
mean.model = list(armaOrder = c(0, 0),
|
||||
include.mean = TRUE),
|
||||
distribution.model = "std")
|
||||
fit <- tryCatch(
|
||||
ugarchfit(spec, r, solver = "hybrid"),
|
||||
error = function(e) NULL)
|
||||
s0 <- prices[length(prices)]
|
||||
if (is.null(fit) || fit@fit$convergence != 0) {
|
||||
# fall back to constant vol so the sim degrades instead of dying
|
||||
mu <- mean(r); sig <- sd(r) * sqrt(horizon)
|
||||
zq <- qnorm(1 - (1 - level) / 2)
|
||||
return(list(value = s0 * exp(mu * horizon),
|
||||
lower_bound = s0 * exp(mu * horizon - zq * sig),
|
||||
upper_bound = s0 * exp(mu * horizon + zq * sig),
|
||||
certainty = 0.2))
|
||||
}
|
||||
fc <- ugarchforecast(fit, n.ahead = horizon)
|
||||
mu_path <- as.numeric(fitted(fc))
|
||||
sig_path <- as.numeric(sigma(fc))
|
||||
mu_h <- sum(mu_path)
|
||||
sig_h <- sqrt(sum(sig_path^2))
|
||||
zq <- qnorm(1 - (1 - level) / 2)
|
||||
# persistence far from 1 = mean-reverting vol = more trustworthy band
|
||||
persistence <- sum(coef(fit)[c("alpha1", "beta1")])
|
||||
list(value = s0 * exp(mu_h),
|
||||
lower_bound = s0 * exp(mu_h - zq * sig_h),
|
||||
upper_bound = s0 * exp(mu_h + zq * sig_h),
|
||||
certainty = unname(pmax(0.05, pmin(0.95, 1 - persistence^4))))
|
||||
}
|
||||
|
||||
# DCC-GARCH conditional correlation of target vs reference (rmgarch).
|
||||
# Loaded lazily: rmgarch is heavy, and only this sim needs it.
|
||||
sim_dcc <- function(prices, ref_prices, horizon, level = 0.90) {
|
||||
suppressMessages(library(rmgarch))
|
||||
rt <- diff(log(prices)); rr <- diff(log(ref_prices))
|
||||
n <- min(length(rt), length(rr))
|
||||
dat <- cbind(tail(rt, n), tail(rr, n))
|
||||
uspec <- ugarchspec(variance.model = list(model = "sGARCH",
|
||||
garchOrder = c(1, 1)),
|
||||
mean.model = list(armaOrder = c(0, 0)))
|
||||
dspec <- dccspec(uspec = multispec(replicate(2, uspec)),
|
||||
dccOrder = c(1, 1), distribution = "mvnorm")
|
||||
fit <- tryCatch(dccfit(dspec, data = dat), error = function(e) NULL)
|
||||
s0 <- prices[length(prices)]
|
||||
if (is.null(fit)) {
|
||||
rho <- cor(dat[, 1], dat[, 2])
|
||||
} else {
|
||||
fc <- dccforecast(fit, n.ahead = horizon)
|
||||
rho <- mean(rcor(fc)[[1]][1, 2, ])
|
||||
}
|
||||
# forecast: target drift conditioned on reference drift through rho
|
||||
mu_t <- mean(dat[, 1]); mu_r <- mean(dat[, 2])
|
||||
st <- sd(dat[, 1]); sr <- sd(dat[, 2])
|
||||
cond_mu <- (mu_t + rho * (st / sr) * mu_r) * horizon
|
||||
cond_sd <- st * sqrt(pmax(1 - rho^2, 1e-12)) * sqrt(horizon)
|
||||
zq <- qnorm(1 - (1 - level) / 2)
|
||||
list(value = s0 * exp(cond_mu),
|
||||
lower_bound = s0 * exp(cond_mu - zq * cond_sd),
|
||||
upper_bound = s0 * exp(cond_mu + zq * cond_sd),
|
||||
certainty = abs(rho))
|
||||
}
|
||||
@@ -0,0 +1,143 @@
|
||||
# sde_sims.R — hand-rolled stochastic simulations (M3a spec §3).
|
||||
# GBM Monte Carlo, Heston (Euler–Maruyama with full truncation), Merton
|
||||
# jump-diffusion, fBM via Wood & Chan circulant embedding on base fft().
|
||||
# Each sim returns list(value, lower_bound, upper_bound, certainty) for a
|
||||
# price forecast `horizon` steps ahead, from a log-price history.
|
||||
|
||||
.log_returns <- function(prices) diff(log(prices))
|
||||
|
||||
# Geometric Brownian motion: mu/sigma estimated from history, terminal
|
||||
# distribution is lognormal so quantiles are closed-form; MC paths give the
|
||||
# certainty estimate (dispersion-based).
|
||||
sim_gbm <- function(prices, horizon, n_paths = 5000L, level = 0.90) {
|
||||
r <- .log_returns(prices)
|
||||
mu <- mean(r); sigma <- sd(r)
|
||||
s0 <- prices[length(prices)]
|
||||
z <- matrix(rnorm(n_paths * horizon), n_paths, horizon)
|
||||
paths <- s0 * exp(t(apply((mu - sigma^2 / 2) + sigma * z, 1, cumsum)))
|
||||
terminal <- paths[, horizon]
|
||||
a <- (1 - level) / 2
|
||||
list(value = median(terminal),
|
||||
lower_bound = unname(quantile(terminal, a)),
|
||||
upper_bound = unname(quantile(terminal, 1 - a)),
|
||||
certainty = .dispersion_certainty(terminal, s0))
|
||||
}
|
||||
|
||||
# Heston stochastic volatility, Euler–Maruyama with full truncation for the
|
||||
# variance process (v is floored inside the drift/diffusion, not clamped
|
||||
# after, which avoids the classical bias at low kappa*theta).
|
||||
sim_heston <- function(prices, horizon, n_paths = 5000L, level = 0.90,
|
||||
kappa = 2.0, rho = -0.7) {
|
||||
r <- .log_returns(prices)
|
||||
mu <- mean(r)
|
||||
theta <- var(r) # long-run variance
|
||||
v0 <- .ewma_var(r) # spot variance
|
||||
xi <- sd((r - mean(r))^2) * sqrt(2) # crude vol-of-vol from squared returns
|
||||
s0 <- prices[length(prices)]
|
||||
|
||||
s <- rep(log(s0), n_paths)
|
||||
v <- rep(v0, n_paths)
|
||||
for (t in seq_len(horizon)) {
|
||||
z1 <- rnorm(n_paths)
|
||||
z2 <- rho * z1 + sqrt(1 - rho^2) * rnorm(n_paths)
|
||||
vp <- pmax(v, 0)
|
||||
s <- s + (mu - vp / 2) + sqrt(vp) * z1
|
||||
v <- v + kappa * (theta - vp) + xi * sqrt(vp) * z2
|
||||
}
|
||||
terminal <- exp(s)
|
||||
a <- (1 - level) / 2
|
||||
list(value = median(terminal),
|
||||
lower_bound = unname(quantile(terminal, a)),
|
||||
upper_bound = unname(quantile(terminal, 1 - a)),
|
||||
certainty = .dispersion_certainty(terminal, s0))
|
||||
}
|
||||
|
||||
# Merton jump-diffusion: jumps detected as |return| > 3 sd of the
|
||||
# jump-cleaned series (iterated once); Poisson arrivals, lognormal sizes.
|
||||
sim_jump_diffusion <- function(prices, horizon, n_paths = 5000L, level = 0.90) {
|
||||
r <- .log_returns(prices)
|
||||
thresh <- 3 * sd(r)
|
||||
jumps <- abs(r - mean(r)) > thresh
|
||||
diffusive <- r[!jumps]
|
||||
mu <- mean(diffusive); sigma <- sd(diffusive)
|
||||
lambda <- max(sum(jumps) / length(r), 1e-6) # jumps per step
|
||||
jump_mu <- if (any(jumps)) mean(r[jumps]) else 0
|
||||
jump_sd <- if (sum(jumps) > 1) sd(r[jumps]) else sigma
|
||||
s0 <- prices[length(prices)]
|
||||
|
||||
logret <- matrix(rnorm(n_paths * horizon, mu - sigma^2 / 2, sigma),
|
||||
n_paths, horizon)
|
||||
n_jumps <- matrix(rpois(n_paths * horizon, lambda), n_paths, horizon)
|
||||
jump_part <- matrix(rnorm(n_paths * horizon,
|
||||
n_jumps * jump_mu,
|
||||
sqrt(pmax(n_jumps, 1e-12)) * jump_sd),
|
||||
n_paths, horizon)
|
||||
jump_part[n_jumps == 0] <- 0
|
||||
terminal <- s0 * exp(rowSums(logret + jump_part))
|
||||
a <- (1 - level) / 2
|
||||
list(value = median(terminal),
|
||||
lower_bound = unname(quantile(terminal, a)),
|
||||
upper_bound = unname(quantile(terminal, 1 - a)),
|
||||
certainty = .dispersion_certainty(terminal, s0))
|
||||
}
|
||||
|
||||
# Fractional Brownian motion increments (fGn) by Wood & Chan (1994)
|
||||
# circulant embedding: exact in distribution, O(n log n) via fft. Hurst
|
||||
# exponent estimated from history by aggregated-variance slope.
|
||||
sim_fbm <- function(prices, horizon, n_paths = 2000L, level = 0.90) {
|
||||
r <- .log_returns(prices)
|
||||
H <- .hurst_aggvar(r)
|
||||
sigma <- sd(r); mu <- mean(r)
|
||||
s0 <- prices[length(prices)]
|
||||
|
||||
n <- horizon
|
||||
# fGn autocovariance gamma(k), circulant embedding of size 2m
|
||||
k <- 0:n
|
||||
gam <- 0.5 * (abs(k - 1)^(2 * H) - 2 * abs(k)^(2 * H) + abs(k + 1)^(2 * H))
|
||||
circ <- c(gam, rev(gam[2:n])) # length 2n
|
||||
lam <- Re(fft(circ))
|
||||
lam[lam < 0] <- 0 # numerical guard; embedding may fail
|
||||
m2 <- length(circ)
|
||||
|
||||
terminal <- numeric(n_paths)
|
||||
for (p in seq_len(n_paths)) {
|
||||
zr <- rnorm(m2); zi <- rnorm(m2)
|
||||
w <- fft(sqrt(lam / m2) * complex(real = zr, imaginary = zi),
|
||||
inverse = FALSE)
|
||||
fgn <- Re(w)[1:n] * sigma
|
||||
terminal[p] <- s0 * exp(sum(mu + fgn))
|
||||
}
|
||||
a <- (1 - level) / 2
|
||||
list(value = median(terminal),
|
||||
lower_bound = unname(quantile(terminal, a)),
|
||||
upper_bound = unname(quantile(terminal, 1 - a)),
|
||||
certainty = .dispersion_certainty(terminal, s0) *
|
||||
(1 - abs(H - 0.5))) # discount when H estimate is extreme
|
||||
}
|
||||
|
||||
# --- shared estimators ------------------------------------------------------
|
||||
|
||||
.ewma_var <- function(r, lambda = 0.94) {
|
||||
v <- var(r)
|
||||
for (x in r) v <- lambda * v + (1 - lambda) * x^2
|
||||
v
|
||||
}
|
||||
|
||||
.hurst_aggvar <- function(r, max_scale = 16L) {
|
||||
scales <- 2^(1:floor(log2(min(max_scale, length(r) / 4))))
|
||||
if (length(scales) < 2) return(0.5)
|
||||
vars <- vapply(scales, function(m) {
|
||||
k <- floor(length(r) / m)
|
||||
var(colMeans(matrix(r[1:(k * m)], m, k)))
|
||||
}, numeric(1))
|
||||
slope <- coef(lm(log(vars) ~ log(scales)))[2]
|
||||
h <- 1 + slope / 2
|
||||
min(max(h, 0.05), 0.95)
|
||||
}
|
||||
|
||||
# Map MC dispersion into (0,1]: tight terminal distribution relative to the
|
||||
# spot price = high certainty. Purely internal to M3a; the Hub calibrates.
|
||||
.dispersion_certainty <- function(terminal, s0) {
|
||||
spread <- IQR(terminal) / s0
|
||||
unname(1 / (1 + 5 * spread))
|
||||
}
|
||||
Reference in New Issue
Block a user