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
파인 라이브러리
트레이딩뷰의 진정한 정신에 따라, 작성자는 이 파인 코드를 오픈소스 라이브러리로 게시하여 커뮤니티의 다른 파인 프로그래머들이 재사용할 수 있도록 했습니다. 작성자에게 경의를 표합니다! 이 라이브러리는 개인적으로 사용하거나 다른 오픈소스 게시물에서 사용할 수 있지만, 이 코드의 게시물 내 재사용은 하우스 룰에 따라 규제됩니다.
면책사항
해당 정보와 게시물은 금융, 투자, 트레이딩 또는 기타 유형의 조언이나 권장 사항으로 간주되지 않으며, 트레이딩뷰에서 제공하거나 보증하는 것이 아닙니다. 자세한 내용은 이용 약관을 참조하세요.
파인 라이브러리
트레이딩뷰의 진정한 정신에 따라, 작성자는 이 파인 코드를 오픈소스 라이브러리로 게시하여 커뮤니티의 다른 파인 프로그래머들이 재사용할 수 있도록 했습니다. 작성자에게 경의를 표합니다! 이 라이브러리는 개인적으로 사용하거나 다른 오픈소스 게시물에서 사용할 수 있지만, 이 코드의 게시물 내 재사용은 하우스 룰에 따라 규제됩니다.
면책사항
해당 정보와 게시물은 금융, 투자, 트레이딩 또는 기타 유형의 조언이나 권장 사항으로 간주되지 않으며, 트레이딩뷰에서 제공하거나 보증하는 것이 아닙니다. 자세한 내용은 이용 약관을 참조하세요.