PINE LIBRARY
Cập nhật CausalityLib - granger casuality and transfer entropy helpers

Library "CausalityLib"
Causality Analysis Library - Transfer Entropy, Granger Causality, and Causality Filtering
f_shannon_entropy(data, num_bins)
Calculate Shannon entropy of data distribution
Parameters:
data (array<float>): Array of continuous values
num_bins (int): Number of bins for discretization
Returns: Entropy value (higher = more randomness)
f_calculate_te_score(primary_arr, ticker_arr, window, bins, lag)
Calculate Transfer Entropy from source to target
Parameters:
primary_arr (array<float>): Target series (e.g., primary ticker returns)
ticker_arr (array<float>): Source series (e.g., basket ticker returns)
window (int): Window size for TE calculation
bins (int): Number of bins for discretization
lag (int): Lag for source series
Returns: [te_score, direction] - TE score and direction (-1 or 1)
f_correlation_at_lag(primary_arr, ticker_arr, lag, window, correlation_method)
Calculate Pearson correlation at specific lag
Parameters:
primary_arr (array<float>): Primary series
ticker_arr (array<float>): Ticker series
lag (int): Lag value (positive = ticker lags primary)
window (int): Window size for correlation
correlation_method (string): Correlation method to use ("Pearson", "Spearman", "Kendall")
Returns: Correlation coefficient [-1, 1]
f_calculate_granger_score(primary_arr, ticker_arr, window, max_lag, correlation_method)
Calculate Granger causality score with lag testing
Parameters:
primary_arr (array<float>): Primary series
ticker_arr (array<float>): Ticker series
window (int): Window size for correlation
max_lag (int): Maximum lag to test
correlation_method (string): Correlation method to use
Returns: [granger_score, beta_sum] - Granger score and directional beta
f_partial_correlation(x_arr, y_arr, z_arr, window)
Calculate partial correlation between X and Y controlling for Z
Parameters:
x_arr (array<float>): First series
y_arr (array<float>): Second series
z_arr (array<float>): Mediator series
window (int): Window size for correlation
Returns: Partial correlation coefficient [-1, 1]
f_pcmci_filter_score(raw_score, primary_arr, ticker_arr, mediator1, mediator2, mediator3, mediator4, window)
PCMCI Filter: Adjust Granger score by checking for mediating tickers
Parameters:
raw_score (float): Original Granger score
primary_arr (array<float>): Primary series
ticker_arr (array<float>): Ticker series
mediator1 (array<float>): First potential mediator series
mediator2 (array<float>): Second potential mediator series
mediator3 (array<float>): Third potential mediator series
mediator4 (array<float>): Fourth potential mediator series
window (int): Window size for correlation
Returns: Filtered score (reduced if causality is indirect/spurious)
Causality Analysis Library - Transfer Entropy, Granger Causality, and Causality Filtering
f_shannon_entropy(data, num_bins)
Calculate Shannon entropy of data distribution
Parameters:
data (array<float>): Array of continuous values
num_bins (int): Number of bins for discretization
Returns: Entropy value (higher = more randomness)
f_calculate_te_score(primary_arr, ticker_arr, window, bins, lag)
Calculate Transfer Entropy from source to target
Parameters:
primary_arr (array<float>): Target series (e.g., primary ticker returns)
ticker_arr (array<float>): Source series (e.g., basket ticker returns)
window (int): Window size for TE calculation
bins (int): Number of bins for discretization
lag (int): Lag for source series
Returns: [te_score, direction] - TE score and direction (-1 or 1)
f_correlation_at_lag(primary_arr, ticker_arr, lag, window, correlation_method)
Calculate Pearson correlation at specific lag
Parameters:
primary_arr (array<float>): Primary series
ticker_arr (array<float>): Ticker series
lag (int): Lag value (positive = ticker lags primary)
window (int): Window size for correlation
correlation_method (string): Correlation method to use ("Pearson", "Spearman", "Kendall")
Returns: Correlation coefficient [-1, 1]
f_calculate_granger_score(primary_arr, ticker_arr, window, max_lag, correlation_method)
Calculate Granger causality score with lag testing
Parameters:
primary_arr (array<float>): Primary series
ticker_arr (array<float>): Ticker series
window (int): Window size for correlation
max_lag (int): Maximum lag to test
correlation_method (string): Correlation method to use
Returns: [granger_score, beta_sum] - Granger score and directional beta
f_partial_correlation(x_arr, y_arr, z_arr, window)
Calculate partial correlation between X and Y controlling for Z
Parameters:
x_arr (array<float>): First series
y_arr (array<float>): Second series
z_arr (array<float>): Mediator series
window (int): Window size for correlation
Returns: Partial correlation coefficient [-1, 1]
f_pcmci_filter_score(raw_score, primary_arr, ticker_arr, mediator1, mediator2, mediator3, mediator4, window)
PCMCI Filter: Adjust Granger score by checking for mediating tickers
Parameters:
raw_score (float): Original Granger score
primary_arr (array<float>): Primary series
ticker_arr (array<float>): Ticker series
mediator1 (array<float>): First potential mediator series
mediator2 (array<float>): Second potential mediator series
mediator3 (array<float>): Third potential mediator series
mediator4 (array<float>): Fourth potential mediator series
window (int): Window size for correlation
Returns: Filtered score (reduced if causality is indirect/spurious)
Phát hành các Ghi chú
v2Updated:
f_calculate_granger_score(primary_arr, ticker_arr, window, max_lag, correlation_method)
Calculate Granger causality score with lag testing
Parameters:
primary_arr (array<float>): Primary series
ticker_arr (array<float>): Ticker series
window (int): Window size for correlation
max_lag (int): Maximum lag to test
correlation_method (string): Correlation method to use
Returns: [granger_score, beta_sum, best_lag] - Granger score, directional beta, and best lag (highest |correlation|)
Phát hành các Ghi chú
v3Phát hành các Ghi chú
v4Added:
f_discretize(data, bins)
Discretize continuous data into bins (for entropy calculations)
Parameters:
data (array<float>): Array of continuous values
bins (int): Number of bins
Returns: Array of bin indices
Phát hành các Ghi chú
v5Added:
f_shannon_entropy_v2(data, num_bins)
Calculate Shannon entropy of data distribution using explicit discretize->probability pipeline
Parameters:
data (array<float>): Array of continuous values
num_bins (int): Number of bins for discretization
Returns: Entropy value (higher = more randomness)
f_calculate_te_score_v2(primary_arr, ticker_arr, window, bins, lag)
Calculate Transfer Entropy from source to target using normalized TE formulation
Parameters:
primary_arr (array<float>): Target series (e.g., primary ticker returns)
ticker_arr (array<float>): Source series (e.g., basket ticker returns)
window (int): Window size for TE calculation
bins (int): Number of bins for discretization
lag (int): Lag for source series
Returns: [te_score, direction] - normalized TE score and direction (-1 or 1)
Phát hành các Ghi chú
v6Added:
f_calculate_te_multilag(primary_arr, ticker_arr, window, bins, max_lag)
Parameters:
primary_arr (array<float>)
ticker_arr (array<float>)
window (int)
bins (int)
max_lag (int)
f_calculate_te_gated(primary_arr, ticker_arr, window, bins, max_lag, coupling_damping, coupling_floor)
Parameters:
primary_arr (array<float>)
ticker_arr (array<float>)
window (int)
bins (int)
max_lag (int)
coupling_damping (float)
coupling_floor (float)
f_calculate_net_te(primary_arr, ticker_arr, window, bins, lag)
Parameters:
primary_arr (array<float>)
ticker_arr (array<float>)
window (int)
bins (int)
lag (int)
f_kalman_hedge_ratio(measurement, observation, prev_beta, prev_P, Q, R)
Parameters:
measurement (float)
observation (float)
prev_beta (float)
prev_P (float)
Q (float)
R (float)
Phát hành các Ghi chú
v7Added:
f_compute_te_ensemble(pri_arr, b1, b2, b3, b4, b5, window, bins, smoothing, self1, self2, self3, self4, self5, use5)
Parameters:
pri_arr (array<float>)
b1 (array<float>)
b2 (array<float>)
b3 (array<float>)
b4 (array<float>)
b5 (array<float>)
window (int)
bins (int)
smoothing (simple int)
self1 (bool)
self2 (bool)
self3 (bool)
self4 (bool)
self5 (bool)
use5 (bool)
Phát hành các Ghi chú
v8Updated:
f_compute_te_ensemble(pri_arr, b1, b2, b3, b4, b5, window, bins, smoothing, self1, self2, self3, self4, self5, use5)
Parameters:
pri_arr (array<float>)
b1 (array<float>)
b2 (array<float>)
b3 (array<float>)
b4 (array<float>)
b5 (array<float>)
window (int)
bins (int)
smoothing (int)
self1 (bool)
self2 (bool)
self3 (bool)
self4 (bool)
self5 (bool)
use5 (bool)
Phát hành các Ghi chú
v9Added:
f_calculate_te_score_debug(primary_arr, ticker_arr, window, bins, lag)
Calculate Transfer Entropy with diagnostics for debugging data sufficiency and bin occupancy
Parameters:
primary_arr (array<float>): Target series (e.g., primary ticker returns)
ticker_arr (array<float>): Source series (e.g., basket ticker returns)
window (int): Window size for TE calculation
bins (int): Number of bins for discretization
lag (int): Lag for source series
Returns: [te_score, direction, win_size, valid_count, min_x, max_x, min_y, max_y, b0_share, b1_share, b2_share, b3_share, b4_share, b5_share, b6_share, b7_share]
Phát hành các Ghi chú
v10Phát hành các Ghi chú
v11Phát hành các Ghi chú
v12Phát hành các Ghi chú
v13Updated:
f_compute_te_ensemble(pri_arr, b1, b2, b3, b4, b5, window, bins, smoothing, self1, self2, self3, self4, self5, use5)
Parameters:
pri_arr (array<float>)
b1 (array<float>)
b2 (array<float>)
b3 (array<float>)
b4 (array<float>)
b5 (array<float>)
window (int)
bins (int)
smoothing (simple int)
self1 (bool)
self2 (bool)
self3 (bool)
self4 (bool)
self5 (bool)
use5 (bool)
Phát hành các Ghi chú
v14Added:
f_compute_te_wrapper(te_primary, te_b1, te_b2, te_b3, te_b4, te_win, te_b, te_smooth, self1, self2, self3, self4, is_hard, bar_idx)
Wrapper for Transfer Entropy calculation with ensemble and fallback logic
Parameters:
te_primary (array<float>): Primary asset return array
te_b1 (array<float>): Basket asset 1 return array
te_b2 (array<float>): Basket asset 2 return array
te_b3 (array<float>): Basket asset 3 return array
te_b4 (array<float>): Basket asset 4 return array
te_win (int): TE window size
te_b (int): TE bins count
te_smooth (simple int): TE smoothing length
self1 (bool): Whether basket 1 is self-reference
self2 (bool): Whether basket 2 is self-reference
self3 (bool): Whether basket 3 is self-reference
self4 (bool): Whether basket 4 is self-reference
is_hard (bool): Whether in hard zone (recent bars)
bar_idx (int): Current bar index
Returns: [te_osc, te_strength, te_direction] - TE oscillator, strength, and direction
Phát hành các Ghi chú
v15Phát hành các Ghi chú
v16Added:
fmobiusnew()
fmobiusupdate(st, cur, prev, alpha)
Parameters:
st (MobiusState)
cur (float)
prev (float)
alpha (float)
fmobiuscompose(s1, s2)
Parameters:
s1 (MobiusState)
s2 (MobiusState)
fmobiusapply(st, x)
Parameters:
st (MobiusState)
x (float)
fcausalcovupdate(sigma_tri, state, mu, net_te, te_threshold, alpha)
Parameters:
sigma_tri (array<float>)
state (array<float>)
mu (array<float>)
net_te (float)
te_threshold (float)
alpha (float)
fcausalweights(te0, te1, te2, alpha)
Parameters:
te0 (float)
te1 (float)
te2 (float)
alpha (float)
MobiusState
Fields:
a (series float)
b (series float)
c (series float)
Phát hành các Ghi chú
v17Added:
fharrvforecast(rv_buffer, beta_d, beta_w, beta_m)
Parameters:
rv_buffer (array<float>)
beta_d (float)
beta_w (float)
beta_m (float)
fdurbinwatson(window)
Parameters:
window (array<float>)
fpcaexplained(cov_tri, shrinkage)
Parameters:
cov_tri (array<float>)
shrinkage (float)
Thư viện Pine
Theo đúng tinh thần TradingView, tác giả đã công bố mã Pine này như một thư viện mã nguồn mở để các lập trình viên Pine khác trong cộng đồng có thể tái sử dụng. Chúc mừng tác giả! Bạn có thể sử dụng thư viện này cho mục đích cá nhân hoặc trong các ấn phẩm mã nguồn mở khác, nhưng việc tái sử dụng mã này trong các ấn phẩm phải tuân theo Nội Quy.
Thông báo miễn trừ trách nhiệm
Thông tin và các ấn phẩm này không nhằm mục đích, và không cấu thành, lời khuyên hoặc khuyến nghị về tài chính, đầu tư, giao dịch hay các loại khác do TradingView cung cấp hoặc xác nhận. Đọc thêm tại Điều khoản Sử dụng.
Thư viện Pine
Theo đúng tinh thần TradingView, tác giả đã công bố mã Pine này như một thư viện mã nguồn mở để các lập trình viên Pine khác trong cộng đồng có thể tái sử dụng. Chúc mừng tác giả! Bạn có thể sử dụng thư viện này cho mục đích cá nhân hoặc trong các ấn phẩm mã nguồn mở khác, nhưng việc tái sử dụng mã này trong các ấn phẩm phải tuân theo Nội Quy.
Thông báo miễn trừ trách nhiệm
Thông tin và các ấn phẩm này không nhằm mục đích, và không cấu thành, lời khuyên hoặc khuyến nghị về tài chính, đầu tư, giao dịch hay các loại khác do TradingView cung cấp hoặc xác nhận. Đọc thêm tại Điều khoản Sử dụng.