PINE LIBRARY
Updated

kNNLib with turboQuant encoding

275
Library "kNNLib"

f_quantize_3bit(value, bounds)
  Quantize a single feature to 3-bit index (0-7) using percentile boundaries
  Parameters:
    value (float): Feature value to quantize
    bounds (array<float>): Array of 9 percentile boundaries [P0, P12.5, P25, P37.5, P50, P62.5, P75, P87.5, P100]
  Returns: Integer index 0-7

f_compute_percentile_bounds(feature_array, history_len)
  Compute percentile boundaries for a feature over a rolling window
  Parameters:
    feature_array (array<float>): Array of feature values (size = history_len)
    history_len (int): Number of bars to use for percentile calculation
  Returns: Array of 9 boundaries [P0, P12.5, P25, P37.5, P50, P62.5, P75, P87.5, P100]

f_turboquant_encode(f1, f2, f3, f4, f5, f6)
  Encode 6 features into 18-bit TurboQuant state ID
  Parameters:
    f1 (int): Feature 1 (VectorOsc) quantized index 0-7
    f2 (int): Feature 2 (BasketCorr) quantized index 0-7
    f3 (int): Feature 3 (InnovZ) quantized index 0-7
    f4 (int): Feature 4 (TE_Osc) quantized index 0-7
    f5 (int): Feature 5 (OrthoZcvb) quantized index 0-7
    f6 (int): Feature 6 (PhiDiv) quantized index 0-7
  Returns: 18-bit state ID (0 to 262143)

f_normalize_basket_corr(rho, rho_history, norm_len)
  Normalize BasketCorr using Fisher transform then z-score
  Parameters:
    rho (float): Raw correlation value [-1, +1]
    rho_history (array<float>): Array of historical rho values
    norm_len (int): Normalization window length
  Returns: Normalized correlation

f_normalize_te_osc(te_osc, te_history, norm_len)
  Normalize TE_Osc using rolling z-score
  Parameters:
    te_osc (float): Raw TE oscillator value
    te_history (array<float>): Array of historical TE values
    norm_len (int): Normalization window length
  Returns: Normalized TE

f_normalize_phi_div(phi_div, phi_history, norm_len)
  Normalize PhiDiv using rolling z-score
  Parameters:
    phi_div (float): Raw PhiDiv value
    phi_history (array<float>): Array of historical PhiDiv values
    norm_len (int): Normalization window length
  Returns: Normalized PhiDiv

f_euclidean_distance(f1_current, f2_current, f3_current, f4_current, f5_current, f6_current, f1_hist, f2_hist, f3_hist, f4_hist, f5_hist, f6_hist)
  Calculate Euclidean distance between two 6D feature vectors
  Parameters:
    f1_current (float): Current bar feature 1
    f2_current (float): Current bar feature 2
    f3_current (float): Current bar feature 3
    f4_current (float): Current bar feature 4
    f5_current (float): Current bar feature 5
    f6_current (float): Current bar feature 6
    f1_hist (float): Historical bar feature 1
    f2_hist (float): Historical bar feature 2
    f3_hist (float): Historical bar feature 3
    f4_hist (float): Historical bar feature 4
    f5_hist (float): Historical bar feature 5
    f6_hist (float): Historical bar feature 6
  Returns: Euclidean distance

f_find_k_nearest(f1_current, f2_current, f3_current, f4_current, f5_current, f6_current, f1_history, f2_history, f3_history, f4_history, f5_history, f6_history, k, max_history)
  Find K nearest neighbors using linear scan
  Parameters:
    f1_current (float): Current bar feature 1
    f2_current (float): Current bar feature 2
    f3_current (float): Current bar feature 3
    f4_current (float): Current bar feature 4
    f5_current (float): Current bar feature 5
    f6_current (float): Current bar feature 6
    f1_history (array<float>): Array of historical feature 1 values
    f2_history (array<float>): Array of historical feature 2 values
    f3_history (array<float>): Array of historical feature 3 values
    f4_history (array<float>): Array of historical feature 4 values
    f5_history (array<float>): Array of historical feature 5 values
    f6_history (array<float>): Array of historical feature 6 values
    k (int): Number of neighbors to find
    max_history (int): Maximum bars to search
  Returns: Array of K nearest neighbor indices

f_calculate_confidence(f1_current, f2_current, f3_current, f4_current, f5_current, f6_current, f1_history, f2_history, f3_history, f4_history, f5_history, f6_history, k_nearest_indices)
  Calculate epistemic confidence score from K-nearest distances
  Parameters:
    f1_current (float): Current bar feature 1
    f2_current (float): Current bar feature 2
    f3_current (float): Current bar feature 3
    f4_current (float): Current bar feature 4
    f5_current (float): Current bar feature 5
    f6_current (float): Current bar feature 6
    f1_history (array<float>): Array of historical feature 1 values
    f2_history (array<float>): Array of historical feature 2 values
    f3_history (array<float>): Array of historical feature 3 values
    f4_history (array<float>): Array of historical feature 4 values
    f5_history (array<float>): Array of historical feature 5 values
    f6_history (array<float>): Array of historical feature 6 values
    k_nearest_indices (array<int>): Array of K nearest neighbor indices
  Returns: Confidence score [0, 1]
Release Notes
v2
Release Notes
v3

Added:
f_turboquant(val)
  Quantize a single feature to 3-bit index + 1-bit residual (4 bits total)
  Parameters:
    val (float): Feature value (should be clamped to [-3, +3] range)
  Returns: [int, int] Tuple of (3-bit index, 1-bit residual)

f_pack_vector(i0, r0, i1, r1, i2, r2, i3, r3, i4, r4, i5, r5)
  Pack 6 features into single 24-bit integer
  Parameters:
    i0 (int)
    r0 (int)
    i1 (int)
    r1 (int)
    i2 (int)
    r2 (int)
    i3 (int)
    r3 (int)
    i4 (int)
    r4 (int)
    i5 (int)
    r5 (int)
  Returns: int Packed 24-bit vector

f_unpack_centroid(packed, feat_idx)
  Unpack and reconstruct centroid value with half-step refinement
  Parameters:
    packed (int): 24-bit packed vector
    feat_idx (int): Feature index (0-5)
  Returns: float Reconstructed feature value

f_hamming_screen(packed_cur, packed_hist)
  Hamming screen: count features with >1 level difference
  Parameters:
    packed_cur (int): Current packed vector
    packed_hist (int): Historical packed vector
  Returns: int Number of features with significant difference

Updated:
f_normalize_te_osc(te_osc, te_history, norm_len)
  Normalize TE_Osc using rolling z-score
  Parameters:
    te_osc (float): Raw TE oscillator value
    te_history (array<float>): Array of historical TE values
    norm_len (int): Normalization window length
  Returns: Normalized TE oscillator

f_normalize_phi_div(phi_div, phi_history, norm_len)
  Normalize PhiDiv using rolling z-score
  Parameters:
    phi_div (float): Raw PhiDiv value (PhiLatch - PhiTotal_orth)
    phi_history (array<float>): Array of historical PhiDiv values
    norm_len (int): Normalization window length
  Returns: Normalized PhiDiv

f_euclidean_distance(f1_cur, f2_cur, f3_cur, f4_cur, f5_cur, f6_cur, f1_hist, f2_hist, f3_hist, f4_hist, f5_hist, f6_hist)
  Calculate Euclidean distance between current state and historical state
  Parameters:
    f1_cur (float)
    f2_cur (float)
    f3_cur (float)
    f4_cur (float)
    f5_cur (float)
    f6_cur (float)
    f1_hist (float)
    f2_hist (float)
    f3_hist (float)
    f4_hist (float)
    f5_hist (float)
    f6_hist (float)
  Returns: float Euclidean distance

f_find_k_nearest(f1_cur, f2_cur, f3_cur, f4_cur, f5_cur, f6_cur, f1_history, f2_history, f3_history, f4_history, f5_history, f6_history, k, max_history)
  Find K nearest neighbors using Hamming screening + Euclidean distance
  Parameters:
    f1_cur (float)
    f2_cur (float)
    f3_cur (float)
    f4_cur (float)
    f5_cur (float)
    f6_cur (float)
    f1_history (array<float>)
    f2_history (array<float>)
    f3_history (array<float>)
    f4_history (array<float>)
    f5_history (array<float>)
    f6_history (array<float>)
    k (int): Number of neighbors to retrieve
    max_history (int): Maximum history length to search
  Returns: array<int> Array of K nearest neighbor indices

f_calculate_confidence(f1_cur, f2_cur, f3_cur, f4_cur, f5_cur, f6_cur, f1_history, f2_history, f3_history, f4_history, f5_history, f6_history, k_nearest)
  Calculate epistemic confidence from K-nearest distance distribution
  Parameters:
    f1_cur (float)
    f2_cur (float)
    f3_cur (float)
    f4_cur (float)
    f5_cur (float)
    f6_cur (float)
    f1_history (array<float>)
    f2_history (array<float>)
    f3_history (array<float>)
    f4_history (array<float>)
    f5_history (array<float>)
    f6_history (array<float>)
    k_nearest (array<int>): Array of K nearest neighbor indices
  Returns: float Confidence score [0, 1]

Removed:
f_quantize_3bit(value, bounds)
  Quantize a single feature to 3-bit index (0-7) using percentile boundaries

f_compute_percentile_bounds(feature_array, history_len)
  Compute percentile boundaries for a feature over a rolling window

f_turboquant_encode(f1, f2, f3, f4, f5, f6)
  Encode 6 features into 18-bit TurboQuant state ID
Release Notes
v4

Added:
f_pack_vector_7(i0, r0, i1, r1, i2, r2, i3, r3, i4, r4, i5, r5, i6, r6)
  Pack 7 features into single 28-bit integer
  Parameters:
    i0 (int)
    r0 (int)
    i1 (int)
    r1 (int)
    i2 (int)
    r2 (int)
    i3 (int)
    r3 (int)
    i4 (int)
    r4 (int)
    i5 (int)
    r5 (int)
    i6 (int)
    r6 (int)
  Returns: int Packed 28-bit vector

f_hamming_screen_7(packed_cur, packed_hist)
  Hamming screen for 7-feature packed vectors
  Parameters:
    packed_cur (int): Current packed vector
    packed_hist (int): Historical packed vector
  Returns: int Number of features with significant difference

f_normalize_burst(burst_raw, burst_history, norm_len)
  Normalize a non-negative burst score using log1p + robust IQR z-score
  Parameters:
    burst_raw (float): Raw burst score
    burst_history (array<float>): History array of raw burst scores
    norm_len (int): Normalization window length
  Returns: Normalized burst score
Release Notes
v5

Added:
f_find_k_nearest_7(f1_cur, f2_cur, f3_cur, f4_cur, f5_cur, f6_cur, f7_cur, f1_history, f2_history, f3_history, f4_history, f5_history, f6_history, f7_history, k, max_history)
  Find K nearest neighbors using Hamming screening + Euclidean distance for 7 features
  Parameters:
    f1_cur (float)
    f2_cur (float)
    f3_cur (float)
    f4_cur (float)
    f5_cur (float)
    f6_cur (float)
    f7_cur (float)
    f1_history (array<float>)
    f2_history (array<float>)
    f3_history (array<float>)
    f4_history (array<float>)
    f5_history (array<float>)
    f6_history (array<float>)
    f7_history (array<float>)
    k (int): Number of neighbors to retrieve
    max_history (int): Maximum history length to search
  Returns: array<int> Array of K nearest neighbor indices

f_calculate_confidence_7(f1_cur, f2_cur, f3_cur, f4_cur, f5_cur, f6_cur, f7_cur, f1_history, f2_history, f3_history, f4_history, f5_history, f6_history, f7_history, k_nearest)
  Calculate confidence from 7-feature K-nearest distance distribution
  Parameters:
    f1_cur (float)
    f2_cur (float)
    f3_cur (float)
    f4_cur (float)
    f5_cur (float)
    f6_cur (float)
    f7_cur (float)
    f1_history (array<float>)
    f2_history (array<float>)
    f3_history (array<float>)
    f4_history (array<float>)
    f5_history (array<float>)
    f6_history (array<float>)
    f7_history (array<float>)
    k_nearest (array<int>): Array of K nearest neighbor indices
  Returns: float Confidence score [0, 1]
Release Notes
v6

Added:
f_merge_tier(pool_dist, pool_id, target_ids, target_dist, target_weights, knn_k, decluster_gap)
  Merge one tier into the final kNN set using sorted indices and decluster gating
  Parameters:
    pool_dist (array<float>): Candidate distances for the tier
    pool_id (array<int>): Candidate history indices for the tier
    target_ids (array<int>): Final output neighbor ids
    target_dist (array<float>): Final output distances
    target_weights (array<float>): Final output weights
    knn_k (int): Target neighbor count
    decluster_gap (int): Minimum spacing between accepted neighbor ids

f_hierarchical_tournament_live(s_packed, s_f1, s_f2, s_f3, s_f4, s_f5, s_f6, s_f7, s_family, f1_history, f2_history, f3_history, f4_history, f5_history, f6_history, f7_history, arr_packed_vec, arr_setup_id, arr_setup_family, current_setup_code, oracle_horizon, knn_k, hamming_thresh, decluster_gap, pool_max)
  Hierarchical 4-tier tournament selection for live execution
  Parameters:
    s_packed (int)
    s_f1 (float)
    s_f2 (float)
    s_f3 (float)
    s_f4 (float)
    s_f5 (float)
    s_f6 (float)
    s_f7 (float)
    s_family (int)
    f1_history (array<float>)
    f2_history (array<float>)
    f3_history (array<float>)
    f4_history (array<float>)
    f5_history (array<float>)
    f6_history (array<float>)
    f7_history (array<float>)
    arr_packed_vec (array<int>)
    arr_setup_id (array<int>)
    arr_setup_family (array<int>)
    current_setup_code (int)
    oracle_horizon (int)
    knn_k (int)
    hamming_thresh (int)
    decluster_gap (int)
    pool_max (int)
  Returns: [ids, distances, weights, dbg_iter, dbg_hskip, dbg_nskip, dbg_cands]

f_hierarchical_tournament_backtest(s_packed, s_f1, s_f2, s_f3, s_f4, s_f5, s_f6, s_f7, s_family, s_setup_code, f1_history, f2_history, f3_history, f4_history, f5_history, f6_history, f7_history, arr_packed_vec, arr_setup_id, arr_setup_family, knn_limit, knn_k, hamming_thresh, decluster_gap, pool_max)
  Hierarchical 3-tier tournament selection for backtest execution
  Parameters:
    s_packed (int)
    s_f1 (float)
    s_f2 (float)
    s_f3 (float)
    s_f4 (float)
    s_f5 (float)
    s_f6 (float)
    s_f7 (float)
    s_family (int)
    s_setup_code (int)
    f1_history (array<float>)
    f2_history (array<float>)
    f3_history (array<float>)
    f4_history (array<float>)
    f5_history (array<float>)
    f6_history (array<float>)
    f7_history (array<float>)
    arr_packed_vec (array<int>)
    arr_setup_id (array<int>)
    arr_setup_family (array<int>)
    knn_limit (int)
    knn_k (int)
    hamming_thresh (int)
    decluster_gap (int)
    pool_max (int)
Release Notes
v7

Updated:
f_normalize_phi_div(phi_div, history, len)
  Normalize PhiDiv using rolling z-score
  Parameters:
    phi_div (float): Raw PhiDiv value (PhiLatch - PhiTotal_orth)
    history (array<float>)
    len (int)
  Returns: Normalized PhiDiv
Release Notes
v8

Updated:
f_hierarchical_tournament_live(s_packed, s_f1, s_f2, s_f3, s_f4, s_f5, s_f6, s_f7, s_family, f1_history, f2_history, f3_history, f4_history, f5_history, f6_history, f7_history, arr_packed_vec, arr_setup_id, arr_setup_family, current_setup_code, oracle_horizon, mc_horizon, knn_k, hamming_thresh, decluster_gap, pool_max)
  Hierarchical 4-tier tournament selection for live execution
  Parameters:
    s_packed (int)
    s_f1 (float)
    s_f2 (float)
    s_f3 (float)
    s_f4 (float)
    s_f5 (float)
    s_f6 (float)
    s_f7 (float)
    s_family (int)
    f1_history (array<float>)
    f2_history (array<float>)
    f3_history (array<float>)
    f4_history (array<float>)
    f5_history (array<float>)
    f6_history (array<float>)
    f7_history (array<float>)
    arr_packed_vec (array<int>)
    arr_setup_id (array<int>)
    arr_setup_family (array<int>)
    current_setup_code (int)
    oracle_horizon (int)
    mc_horizon (int)
    knn_k (int)
    hamming_thresh (int)
    decluster_gap (int)
    pool_max (int)
  Returns: [ids, distances, weights, dbg_iter, dbg_hskip, dbg_nskip, dbg_cands]
Release Notes
v9
Release Notes
v10
Release Notes
v11
Release Notes
v12
Release Notes
v13

Added:
f_knn_scan(cur_features, h0, h1, h2, h3, h4, h5, h6, arr_packed, packed_cur, nf, scan_from, scan_to, scan_step, hamming_thresh, max_candidates)
  Parameters:
    cur_features (array<float>)
    h0 (array<float>)
    h1 (array<float>)
    h2 (array<float>)
    h3 (array<float>)
    h4 (array<float>)
    h5 (array<float>)
    h6 (array<float>)
    arr_packed (array<int>)
    packed_cur (int)
    nf (int)
    scan_from (int)
    scan_to (int)
    scan_step (int)
    hamming_thresh (int)
    max_candidates (int)

f_knn_select(t1_ids, t1_dist, t2_ids, t2_dist, t3_ids, t3_dist, t4_ids, t4_dist, k, gap)
  Parameters:
    t1_ids (array<int>)
    t1_dist (array<float>)
    t2_ids (array<int>)
    t2_dist (array<float>)
    t3_ids (array<int>)
    t3_dist (array<float>)
    t4_ids (array<int>)
    t4_dist (array<float>)
    k (int)
    gap (int)
Release Notes
v14

Added:
f_sign_aware_filter(s_f2, f1_history, f2_history, f3_history, f4_history, f5_history, f6_history, f7_history, arr_setup_id, arr_setup_family, arr_packed_vec, limit)
  Sign-aware neighbor filtering to prevent canceling paths from mixed correlation phases
  Parameters:
    s_f2 (float): Current F2 value
    f1_history (array<int>): Feature 1 history (quantized int)
    f2_history (array<float>): Feature 2 history (float correlation)
    f3_history (array<float>): Feature 3 history (float)
    f4_history (array<float>): Feature 4 history (float)
    f5_history (array<float>): Feature 5 history (float)
    f6_history (array<float>): Feature 6 history (float)
    f7_history (array<float>): Feature 7 history (float)
    arr_setup_id (array<int>): Setup ID history
    arr_setup_family (array<int>): Setup family history
    arr_packed_vec (array<int>): Packed vector history
    limit (int): Maximum number of history items to consider
  Returns: Filtered arrays with same F2 sign as current setup

f_hierarchical_tournament_backtest_signaware(s_packed, s_f1, s_f2, s_f3, s_f4, s_f5, s_f6, s_f7, s_family, s_setup_code, f1_history, f2_history, f3_history, f4_history, f5_history, f6_history, f7_history, arr_setup_id, arr_setup_family, arr_packed_vec, knn_limit, knn_k, hamming_thresh, decluster_gap, pool_max)
  Sign-aware hierarchical tournament for backtest with quantized F1
  Parameters:
    s_packed (int)
    s_f1 (float)
    s_f2 (float)
    s_f3 (float)
    s_f4 (float)
    s_f5 (float)
    s_f6 (float)
    s_f7 (float)
    s_family (int)
    s_setup_code (int)
    f1_history (array<int>)
    f2_history (array<float>)
    f3_history (array<float>)
    f4_history (array<float>)
    f5_history (array<float>)
    f6_history (array<float>)
    f7_history (array<float>)
    arr_setup_id (array<int>)
    arr_setup_family (array<int>)
    arr_packed_vec (array<int>)
    knn_limit (int)
    knn_k (int)
    hamming_thresh (int)
    decluster_gap (int)
    pool_max (int)
  Returns: [ids, weights] for backtest MC validation

f_median_distance(k_nearest, f1_cur, f2_cur, f3_cur, f4_cur, f5_cur, f6_cur, f1_history, f2_history, f3_history, f4_history, f5_history, f6_history)
  Calculate median distance from k-nearest neighbors (robust confidence metric)
  Parameters:
    k_nearest (array<int>): Array of K nearest neighbor indices
    f1_cur (float)
    f2_cur (float)
    f3_cur (float)
    f4_cur (float)
    f5_cur (float)
    f6_cur (float)
    f1_history (array<float>)
    f2_history (array<float>)
    f3_history (array<float>)
    f4_history (array<float>)
    f5_history (array<float>)
    f6_history (array<float>)
  Returns: float Median distance (more robust to outliers than mean)
Release Notes
v15
Release Notes
v16
Release Notes
v17

Updated:
f_hierarchical_tournament_live(s_packed, s_f1, s_f2, s_f3, s_f4, s_f5, s_f6, s_f7, category_a, f1_history, f2_history, f3_history, f4_history, f5_history, f6_history, f7_history, arr_packed_vec, arr_category_b_history, arr_category_a_history, target_category_b, oracle_horizon, mc_horizon, knn_k, hamming_thresh, decluster_gap, pool_max)
  Hierarchical 4-tier tournament selection for live execution (domain-agnostic)
  Parameters:
    s_packed (int)
    s_f1 (float)
    s_f2 (float)
    s_f3 (float)
    s_f4 (float)
    s_f5 (float)
    s_f6 (float)
    s_f7 (float)
    category_a (int): Target category A (e.g., family/regime). Use -1 to disable filtering.
    f1_history (array<float>)
    f2_history (array<float>)
    f3_history (array<float>)
    f4_history (array<float>)
    f5_history (array<float>)
    f6_history (array<float>)
    f7_history (array<float>)
    arr_packed_vec (array<int>)
    arr_category_b_history (array<int>)
    arr_category_a_history (array<int>)
    target_category_b (int)
    oracle_horizon (int)
    mc_horizon (int)
    knn_k (int)
    hamming_thresh (int)
    decluster_gap (int)
    pool_max (int)
  Returns: [ids, distances, weights, dbg_iter, dbg_hskip, dbg_nskip, dbg_cands]

f_hierarchical_tournament_backtest(s_packed, s_f1, s_f2, s_f3, s_f4, s_f5, s_f6, s_f7, category_a, category_b, f1_history, f2_history, f3_history, f4_history, f5_history, f6_history, f7_history, arr_packed_vec, arr_category_b_history, arr_category_a_history, knn_limit, knn_k, hamming_thresh, decluster_gap, pool_max)
  Hierarchical 3-tier tournament selection for backtest execution (domain-agnostic)
  Parameters:
    s_packed (int)
    s_f1 (float)
    s_f2 (float)
    s_f3 (float)
    s_f4 (float)
    s_f5 (float)
    s_f6 (float)
    s_f7 (float)
    category_a (int): Target category A. Use -1 to disable filtering.
    category_b (int): Target category B. Use 0 to disable filtering.
    f1_history (array<float>)
    f2_history (array<float>)
    f3_history (array<float>)
    f4_history (array<float>)
    f5_history (array<float>)
    f6_history (array<float>)
    f7_history (array<float>)
    arr_packed_vec (array<int>)
    arr_category_b_history (array<int>)
    arr_category_a_history (array<int>)
    knn_limit (int)
    knn_k (int)
    hamming_thresh (int)
    decluster_gap (int)
    pool_max (int)

f_hierarchical_tournament_backtest_signaware(s_packed, s_f1, s_f2, s_f3, s_f4, s_f5, s_f6, s_f7, category_a, category_b, f1_history, f2_history, f3_history, f4_history, f5_history, f6_history, f7_history, arr_category_b_history, arr_category_a_history, arr_packed_vec, knn_limit, knn_k, hamming_thresh, decluster_gap, pool_max)
  Sign-aware hierarchical tournament for backtest with quantized F1 (domain-agnostic)
  Parameters:
    s_packed (int)
    s_f1 (float)
    s_f2 (float)
    s_f3 (float)
    s_f4 (float)
    s_f5 (float)
    s_f6 (float)
    s_f7 (float)
    category_a (int): Target category A. Use -1 to disable filtering.
    category_b (int): Target category B. Use 0 to disable filtering.
    f1_history (array<int>)
    f2_history (array<float>)
    f3_history (array<float>)
    f4_history (array<float>)
    f5_history (array<float>)
    f6_history (array<float>)
    f7_history (array<float>)
    arr_category_b_history (array<int>)
    arr_category_a_history (array<int>)
    arr_packed_vec (array<int>)
    knn_limit (int)
    knn_k (int)
    hamming_thresh (int)
    decluster_gap (int)
    pool_max (int)
  Returns: [ids, weights] for backtest MC validation
Release Notes
v18
Release Notes
v19
Release Notes
v20
Release Notes
v21

Added:
fweightedmedian(values, weights)
  Parameters:
    values (array<float>)
    weights (array<float>)

fweightedpercentile(values, weights, p)
  Parameters:
    values (array<float>)
    weights (array<float>)
    p (float)

fhorizonconfidence(horizons, weights)
  Parameters:
    horizons (array<int>)
    weights (array<float>)

famplitudeconfidence(amplitudes, weights)
  Parameters:
    amplitudes (array<float>)
    weights (array<float>)

fprojectionconfidence(horizons, amplitudes, weights, net_te, te_threshold, alpha_k, alpha_a, alpha_te)
  Parameters:
    horizons (array<int>)
    amplitudes (array<float>)
    weights (array<float>)
    net_te (float)
    te_threshold (float)
    alpha_k (float)
    alpha_a (float)
    alpha_te (float)

fcausaleuclidean7(c1, c2, c3, c4, c5, c6, c7, h1, h2, h3, h4, h5, h6, h7, te_weights)
  Parameters:
    c1 (float)
    c2 (float)
    c3 (float)
    c4 (float)
    c5 (float)
    c6 (float)
    c7 (float)
    h1 (float)
    h2 (float)
    h3 (float)
    h4 (float)
    h5 (float)
    h6 (float)
    h7 (float)
    te_weights (array<float>)
Release Notes
v22

Added:
fprojectionconfidence2(horizons, amplitudes, weights, nette, te_threshold, alpha_k, alpha_a, alpha_te, conf_mode)
  Parameters:
    horizons (array<int>)
    amplitudes (array<float>)
    weights (array<float>)
    nette (float)
    te_threshold (float)
    alpha_k (float)
    alpha_a (float)
    alpha_te (float)
    conf_mode (int)
Release Notes
v23
Release Notes
v24
Release Notes
v25
Release Notes
v26
Release Notes
v27
Release Notes
v28

Added:
f_config_default(k)
  Parameters:
    k (int)

f_query_new(feats, meta_a, meta_b, dir)
  Parameters:
    feats (array<float>)
    meta_a (int)
    meta_b (int)
    dir (series TradeDirection enum from cybermediaboy/LearningLib/1)

f_mahalanobis_packed(a, b, sigma_inv_tri)
  Mahalanobis distance using lower-triangular packed inverse cov.
Packing convention: sigma_inv_tri[idx] for (i,j) with i>=j is at
idx = i*(i+1)/2 + j. Diagonal at i*(i+1)/2 + i.
Caller-managed packing; sigma_inv_tri size = n*(n+1)/2.
  Parameters:
    a (array<float>)
    b (array<float>)
    sigma_inv_tri (array<float>)

f_dist_dispatch(kind, a, b, sigma_inv_tri)
  Distance dispatch, including Mahalanobis when query carries
sigma_inv_tri.
  Parameters:
    kind (series DistanceKind enum from cybermediaboy/LearningLib/1)
    a (array<float>)
    b (array<float>)
    sigma_inv_tri (array<float>)

f_knn_search(tb, q, cfg)
  Hierarchical kNN search with tournament filtering and fallback.

Algorithm:
1. Try requested filter mode.
2. If filtered population < cfg.min_samples, fall back:
META_AB → META_A_ONLY → NONE
META_A_THEN_B / META_A_ONLY → NONE
META_B_ONLY → NONE
3. Compute distances, retain top-k via bounded insertion.
4. Populate weights = 1 / max(d, min_distance).
5. Aggregate per cfg.aggregate.
6. Compute confidence.

Returns KnnResult with effective filter and population counts.
  Parameters:
    tb (TrainingBuffer type from cybermediaboy/LearningLib/1)
    q (KnnQuery)
    cfg (KnnConfig)

f_aggregate_str(m)
  Parameters:
    m (series AggregateMode)

f_filter_str(f)
  Parameters:
    f (series FilterMode)

f_dirfilter_str(d)
  Parameters:
    d (series DirectionFilter)

KnnConfig
  KnnConfig — search hyperparameters
  Fields:
    k (series int): number of neighbors
    metric (series DistanceKind enum from cybermediaboy/LearningLib/1): distance metric
    aggregate (series AggregateMode): how to combine top-k labels
    filter (series FilterMode): tournament filter mode
    dir_filter (series DirectionFilter): direction filter
    min_samples (series int): minimum filtered population to use filter (else fallback)
    min_distance (series float): floor for 1/d weighting (avoid singular)
    require_distinct_t (series bool): require sample.timestamp != query.timestamp (anti-leak)

KnnQuery
  KnnQuery — query specification
  Fields:
    features (array<float>): query feature vector
    meta_a (series int): category-A label (e.g. setup family)
    meta_b (series int): category-B label (e.g. setup code)
    direction (series TradeDirection enum from cybermediaboy/LearningLib/1): query direction context (used by SAME_AS_QUERY filter)
    timestamp (series int): query bar (used by require_distinct_t)
    sigma_inv_tri (array<float>): inverse covariance (lower-triangular packed) for Mahalanobis;

KnnNeighbor
  KnnNeighbor — single neighbor with distance and sample backref
  Fields:
    sample (TBSample type from cybermediaboy/LearningLib/1)
    distance (series float)
    weight (series float)

KnnResult
  KnnResult — aggregated output
  Fields:
    neighbors (array<KnnNeighbor>): top-k sorted ascending by distance
    prediction (series float): aggregated label (na if no neighbors)
    confidence (series float): in [0,1] — fraction of neighbors agreeing on dominant class,
    used_filter (series FilterMode): which filter was effectively applied (after fallback)
    n_candidates (series int): number of samples scanned
    n_filtered (series int): number passing filter

Removed:
f_turboquant(val)
  Quantize a single feature to 3-bit index + 1-bit residual (4 bits total)

f_pack_vector(i0, r0, i1, r1, i2, r2, i3, r3, i4, r4, i5, r5)
  Pack 6 features into single 24-bit integer

f_pack_vector_7(i0, r0, i1, r1, i2, r2, i3, r3, i4, r4, i5, r5, i6, r6)
  Pack 7 features into single 28-bit integer

f_unpack_centroid(packed, feat_idx)
  Unpack and reconstruct centroid value with half-step refinement

Release Notes
v29

Updated:
f_query_new(feats, meta_a, meta_b, dir)
  Parameters:
    feats (array<float>)
    meta_a (int)
    meta_b (int)
    dir (series TradeDirection enum from cybermediaboy/LearningLib/3)

f_dist_dispatch(kind, a, b, sigma_inv_tri)
  Distance dispatch, including Mahalanobis when query carries
sigma_inv_tri.
  Parameters:
    kind (series DistanceKind enum from cybermediaboy/LearningLib/3)
    a (array<float>)
    b (array<float>)
    sigma_inv_tri (array<float>)

f_knn_search(tb, q, cfg)
  Hierarchical kNN search with tournament filtering and fallback.

Algorithm:
1. Try requested filter mode.
2. If filtered population < cfg.min_samples, fall back:
META_AB → META_A_ONLY → NONE
META_A_THEN_B / META_A_ONLY → NONE
META_B_ONLY → NONE
3. Compute distances, retain top-k via bounded insertion.
4. Populate weights = 1 / max(d, min_distance).
5. Aggregate per cfg.aggregate.
6. Compute confidence.

Returns KnnResult with effective filter and population counts.
  Parameters:
    tb (TrainingBuffer type from cybermediaboy/LearningLib/3)
    q (KnnQuery)
    cfg (KnnConfig)

KnnConfig
  KnnConfig — search hyperparameters
  Fields:
    k (series int): number of neighbors
    metric (series DistanceKind enum from cybermediaboy/LearningLib/3): distance metric
    aggregate (series AggregateMode): how to combine top-k labels
    filter (series FilterMode): tournament filter mode
    dir_filter (series DirectionFilter): direction filter
    min_samples (series int): minimum filtered population to use filter (else fallback)
    min_distance (series float): floor for 1/d weighting (avoid singular)
    require_distinct_t (series bool): require sample.timestamp != query.timestamp (anti-leak)

KnnQuery
  KnnQuery — query specification
  Fields:
    features (array<float>): query feature vector
    meta_a (series int): category-A label (e.g. setup family)
    meta_b (series int): category-B label (e.g. setup code)
    direction (series TradeDirection enum from cybermediaboy/LearningLib/3): query direction context (used by SAME_AS_QUERY filter)
    timestamp (series int): query bar (used by require_distinct_t)
    sigma_inv_tri (array<float>): inverse covariance (lower-triangular packed) for Mahalanobis;

KnnNeighbor
  KnnNeighbor — single neighbor with distance and sample backref
  Fields:
    sample (TBSample type from cybermediaboy/LearningLib/3)
    distance (series float)
    weight (series float)
Release Notes
v30
Release Notes
v31

Added:
f_mahalanobis_shrinkage(x, X_buffer, alpha, weiszfeld_iters)
  Shrinkage Mahalanobis distance with L1-median anchor (Cabana et al. 2019)
  Parameters:
    x (array<float>): Current observation vector (length d, typically 3)
    X_buffer (matrix<float>): Matrix of past observations (N rows × d columns)
    alpha (simple float): Shrinkage weight toward identity [0,1], default 0.1
    weiszfeld_iters (simple int): Weiszfeld iterations for L1-median [1,10], default 5
  Returns: Robust Mahalanobis distance, or na if invalid inputs

Disclaimer

The information and publications are not meant to be, and do not constitute, financial, investment, trading, or other types of advice or recommendations supplied or endorsed by TradingView. Read more in the Terms of Use.