PINE LIBRARY
RegimeLib

Library "RegimeLib"
RegimeLib — Bayesian online change-point detection (BOCPD) with score-driven variance.
Implements Tsaknaki, Lillo, Mazzarisi 2023 (arXiv:2307.02375) score-driven extension
to Adams & MacKay 2007 BOCPD. Maintains run-length distribution P(r_t | data_{1:t})
and detects regime changes when MAP run length resets.
Layer L0 (imports NumLib/4 for math helpers only).
DESIGN DECISIONS:
1. Score-driven variance adaptation (GARCH-like) handles heteroskedasticity.
2. Broadcast pattern: single BOCPD instance shared across multiple setups via regime_id.
3. varip state arrays (run_length_probs, run_means, run_variances) persist across bars.
4. Compute cost: O(L) per bar where L = max_run_length (default 100).
f_default_regime_reading()
Create default RegimeReading
Returns: Empty RegimeReading
f_bocpd_regime_score_driven(x, hazard, max_run_length, omega, alpha, beta, rl_probs, run_means, run_vars, prev_map, regime_counter)
BOCPD with score-driven variance adaptation (Tsaknaki et al. 2023)
Parameters:
x (float): Current observation (e.g. log return)
hazard (simple float): Hazard rate H (probability of regime change per bar), default 1/250
max_run_length (simple int): Maximum run length L to track (caps array size), default 100
omega (simple float): Score-driven variance ω parameter, default 1e-4
alpha (simple float): Score-driven variance α parameter (score weight), default 0.05
beta (simple float): Score-driven variance β parameter (persistence), default 0.94
rl_probs (array<float>): varip array<float> run-length probabilities (size L+1, caller-managed)
run_means (array<float>): varip array<float> online means per run (size L+1, caller-managed)
run_vars (array<float>): varip array<float> online variances per run (size L+1, caller-managed)
prev_map (int): varip int previous MAP run length (caller-managed)
regime_counter (float): varip float regime ID counter (caller-managed)
Returns: RegimeReading
f_bocpd_regime(x, hazard, max_run_length, rl_probs, run_means, run_vars, prev_map, regime_counter)
Vanilla BOCPD (fixed variance, no score-driven adaptation)
Parameters:
x (float): Current observation
hazard (simple float): Hazard rate H
max_run_length (simple int): Maximum run length L
rl_probs (array<float>): varip array<float> run-length probabilities (caller-managed)
run_means (array<float>): varip array<float> online means per run (caller-managed)
run_vars (array<float>): varip array<float> online variances per run (caller-managed)
prev_map (int): varip int previous MAP run length (caller-managed)
regime_counter (float): varip float regime ID counter (caller-managed)
Returns: RegimeReading
f_init_bocpd_state(max_run_length, initial_mean, initial_var)
Initialize BOCPD state arrays (call once on first bar)
Parameters:
max_run_length (simple int): Maximum run length L
initial_mean (float): Initial mean estimate
initial_var (float): Initial variance estimate
Returns: Tuple of (rl_probs, run_means, run_vars)
RegimeReading
RegimeReading — output of BOCPD regime detector
Fields:
map_run_length (series int): Current MAP run length (bars since last regime change)
change_point_detected (series bool): True if regime change detected on this bar
regime_id (series float): Incrementing counter per regime (resets on change-point)
predictive_likelihood (series float): Diagnostic: P(x_t | data_in_run_r*)
current_variance (series float): Score-driven variance estimate at MAP run length
RegimeLib — Bayesian online change-point detection (BOCPD) with score-driven variance.
Implements Tsaknaki, Lillo, Mazzarisi 2023 (arXiv:2307.02375) score-driven extension
to Adams & MacKay 2007 BOCPD. Maintains run-length distribution P(r_t | data_{1:t})
and detects regime changes when MAP run length resets.
Layer L0 (imports NumLib/4 for math helpers only).
DESIGN DECISIONS:
1. Score-driven variance adaptation (GARCH-like) handles heteroskedasticity.
2. Broadcast pattern: single BOCPD instance shared across multiple setups via regime_id.
3. varip state arrays (run_length_probs, run_means, run_variances) persist across bars.
4. Compute cost: O(L) per bar where L = max_run_length (default 100).
f_default_regime_reading()
Create default RegimeReading
Returns: Empty RegimeReading
f_bocpd_regime_score_driven(x, hazard, max_run_length, omega, alpha, beta, rl_probs, run_means, run_vars, prev_map, regime_counter)
BOCPD with score-driven variance adaptation (Tsaknaki et al. 2023)
Parameters:
x (float): Current observation (e.g. log return)
hazard (simple float): Hazard rate H (probability of regime change per bar), default 1/250
max_run_length (simple int): Maximum run length L to track (caps array size), default 100
omega (simple float): Score-driven variance ω parameter, default 1e-4
alpha (simple float): Score-driven variance α parameter (score weight), default 0.05
beta (simple float): Score-driven variance β parameter (persistence), default 0.94
rl_probs (array<float>): varip array<float> run-length probabilities (size L+1, caller-managed)
run_means (array<float>): varip array<float> online means per run (size L+1, caller-managed)
run_vars (array<float>): varip array<float> online variances per run (size L+1, caller-managed)
prev_map (int): varip int previous MAP run length (caller-managed)
regime_counter (float): varip float regime ID counter (caller-managed)
Returns: RegimeReading
f_bocpd_regime(x, hazard, max_run_length, rl_probs, run_means, run_vars, prev_map, regime_counter)
Vanilla BOCPD (fixed variance, no score-driven adaptation)
Parameters:
x (float): Current observation
hazard (simple float): Hazard rate H
max_run_length (simple int): Maximum run length L
rl_probs (array<float>): varip array<float> run-length probabilities (caller-managed)
run_means (array<float>): varip array<float> online means per run (caller-managed)
run_vars (array<float>): varip array<float> online variances per run (caller-managed)
prev_map (int): varip int previous MAP run length (caller-managed)
regime_counter (float): varip float regime ID counter (caller-managed)
Returns: RegimeReading
f_init_bocpd_state(max_run_length, initial_mean, initial_var)
Initialize BOCPD state arrays (call once on first bar)
Parameters:
max_run_length (simple int): Maximum run length L
initial_mean (float): Initial mean estimate
initial_var (float): Initial variance estimate
Returns: Tuple of (rl_probs, run_means, run_vars)
RegimeReading
RegimeReading — output of BOCPD regime detector
Fields:
map_run_length (series int): Current MAP run length (bars since last regime change)
change_point_detected (series bool): True if regime change detected on this bar
regime_id (series float): Incrementing counter per regime (resets on change-point)
predictive_likelihood (series float): Diagnostic: P(x_t | data_in_run_r*)
current_variance (series float): Score-driven variance estimate at MAP run length
Biblioteca Pine
Fiel al espíritu de TradingView, el autor ha publicado este código de Pine como biblioteca de código abierto, para que otros programadores de nuestra comunidad puedan reutilizarlo. ¡Enhorabuena al autor! Puede usar esta biblioteca de forma privada o en otras publicaciones de código abierto, pero su reutilización en publicaciones está sujeta a nuestras Normas internas.
Exención de responsabilidad
La información y las publicaciones no constituyen, ni deben considerarse como, asesoramiento o recomendaciones financieras, de inversión, de trading u otro tipo, proporcionadas o respaldadas por TradingView. Obtenga más información en Condiciones de uso.
Biblioteca Pine
Fiel al espíritu de TradingView, el autor ha publicado este código de Pine como biblioteca de código abierto, para que otros programadores de nuestra comunidad puedan reutilizarlo. ¡Enhorabuena al autor! Puede usar esta biblioteca de forma privada o en otras publicaciones de código abierto, pero su reutilización en publicaciones está sujeta a nuestras Normas internas.
Exención de responsabilidad
La información y las publicaciones no constituyen, ni deben considerarse como, asesoramiento o recomendaciones financieras, de inversión, de trading u otro tipo, proporcionadas o respaldadas por TradingView. Obtenga más información en Condiciones de uso.