opendeviationbar-core 13.82.0

Core open deviation bar construction algorithm with temporal integrity guarantees
Documentation
//! Mathematical constants for volatility and entropy normalization
//!
//! Shared across intra-bar and inter-bar feature computation.
//! Centralizes constants to ensure consistency and prevent bit-exact divergence.

/// Garman-Klass volatility coefficient: 2*ln(2) - 1
///
/// Exact value: 0.3862943611198906 (IEEE-754 f64 round-to-nearest)
/// Reference: Garman, M. B., & Klass, M. J. (1980). "On the estimation of security price volatilities from historical data."
/// Journal of Business, 53(1), 67-78.
///
/// Used by both inter-bar (`lookback_garman_klass_vol`) and intra-bar (`intra_garman_klass_vol`)
/// features for consistent volatility estimation across lookback window boundaries.
///
/// Formula: GK = sqrt(0.5 * (high/low)² - GARMAN_KLASS_COEFFICIENT * (close/open)²)
///
/// Issue #454: Fixed incorrect coefficient in intrabar/features.rs (was 0.6137, which is 2-2*ln(2)).
pub const GARMAN_KLASS_COEFFICIENT: f64 = 0.386_294_361_119_890_6;

/// Entropy threshold for Hurst exponent early-exit optimization
///
/// Default: 0.75 (normalized Shannon entropy, range [0, 1])
///
/// Heuristic: Sequences with entropy > 0.75 exhibit random-walk behavior, so Hurst ≈ 0.5
/// with high confidence. Skipping the expensive DFA computation saves ~1-2 µs per bar
/// and improves throughput by ~2-4% in ranging markets (per issue #460 source comment).
///
/// Rationale: Random walks have Hurst exponent ≈ 0.5 as a theoretical constant.
/// High entropy (>0.75) is an empirical proxy for random-walk-like behavior.
/// This is NOT a domain constant (unlike GARMAN_KLASS_COEFFICIENT, which is paper-cited).
/// It is a tuning constant that can be overridden per deployment needs.
///
/// Caveat: This threshold is hardcoded and NOT currently env-tunable. Issue #460 recommends
/// parameterization via `InterBarTrackerConfig::entropy_skip_threshold` if asset-specific
/// overrides are needed, but as of v13.78 this is NOT yet implemented.
///
/// Current status: Feature is disabled in production via OPENDEVIATIONBAR_COMPUTE_HURST=false
/// (per .mise.toml) until Axis-3 (cross-asset) divergence is resolved.
///
/// Issue #460: Documented the threshold's provenance and assigned it a named constant
/// for code clarity and potential future parameterization.
pub const HURST_ENTROPY_SHORTCIRCUIT_THRESHOLD: f64 = 0.75;

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn garman_klass_coefficient_precision() {
        // Verify the constant matches the theoretical formula to full f64 precision
        let computed = 2.0 * 2.0_f64.ln() - 1.0;
        assert_eq!(GARMAN_KLASS_COEFFICIENT, computed);
    }
}