longbridge-quant-stats
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
Chineselongbridge-quant-stats
longbridge-quant-stats
Apply rigorous statistical methods to financial time-series data retrieved from Longbridge — test assumptions before modelling, diagnose residuals, and produce statistically sound inferences.
Response language: match the user's input language — Simplified Chinese / Traditional Chinese / English.
对从Longbridge获取的金融时间序列数据应用严谨的统计方法——在建模前检验假设、诊断残差,并生成统计可靠的推断结果。
响应语言:匹配用户输入语言——简体中文 / 繁体中文 / 英文。
When to use
使用场景
- "帮我做 ADF 单位根检验", "run an ADF test on this price series", "幫我做 ADF 單位根檢驗"
- "AAPL 和 MSFT 有没有协整关系", "are AAPL and MSFT cointegrated"
- "用 GARCH 建模波动率", "model volatility with GARCH"
- "回归残差有没有自相关", "check residual autocorrelation (Durbin-Watson)"
- "用 Bootstrap 估计置信区间", "bootstrap confidence interval for Sharpe ratio"
For factor IC/IR testing, use . For pairs-trading cointegration application, use .
longbridge-factor-researchlongbridge-pairs-trading- "帮我做 ADF 单位根检验", "run an ADF test on this price series", "幫我做 ADF 單位根檢驗"
- "AAPL 和 MSFT 有没有协整关系", "are AAPL and MSFT cointegrated"
- "用 GARCH 建模波动率", "model volatility with GARCH"
- "回归残差有没有自相关", "check residual autocorrelation (Durbin-Watson)"
- "用 Bootstrap 估计置信区间", "bootstrap confidence interval for Sharpe ratio"
若需进行因子IC/IR检验,请使用。若需基于协整的配对交易应用,请使用。
longbridge-factor-researchlongbridge-pairs-tradingPrerequisites
前置依赖
bash
pip install statsmodels scipy numpy pandasbash
pip install statsmodels scipy numpy pandasWorkflow and test catalogue
工作流程与测试目录
Step 1 — Fetch price data
步骤1 — 获取价格数据
bash
longbridge kline --help
longbridge kline <SYMBOL> --period day --count 252 --format jsonExtract the price series. Compute log returns: .
closer_t = ln(P_t / P_{t-1})bash
longbridge kline --help
longbridge kline <SYMBOL> --period day --count 252 --format json提取收盘价序列。计算对数收益率:。
closer_t = ln(P_t / P_{t-1})Step 2 — Stationarity: ADF Unit Root Test
步骤2 — 平稳性检验:ADF单位根检验
When to use: Before regression or time-series modelling — most models require stationary series.
Python (statsmodels):
python
from statsmodels.tsa.stattools import adfuller
result = adfuller(series, autolag='AIC')适用场景:在回归或时间序列建模前——大多数模型要求序列具备平稳性。
Python(statsmodels库):
python
from statsmodels.tsa.stattools import adfuller
result = adfuller(series, autolag='AIC')result: (adf_stat, p_value, lags, n_obs, critical_values, icbest)
result: (adf_stat, p_value, lags, n_obs, critical_values, icbest)
**Interpretation**:
- p < 0.05 → reject unit root → series is stationary.
- p ≥ 0.05 → fail to reject → series has unit root → difference the series.
- Log prices: usually non-stationary. Log returns: usually stationary.
**结果解读**:
- p < 0.05 → 拒绝单位根假设 → 序列平稳。
- p ≥ 0.05 → 无法拒绝假设 → 序列存在单位根 → 需对序列进行差分处理。
- 对数价格:通常非平稳。对数收益率:通常平稳。Step 3 — Cointegration Test
步骤3 — 协整检验
When to use: Two non-stationary series may share a long-run equilibrium (pairs trading).
Engle-Granger (two-series):
python
from statsmodels.tsa.stattools import coint
t_stat, p_value, critical_values = coint(series_A, series_B)适用场景:两个非平稳序列可能存在长期均衡关系(配对交易场景)。
Engle-Granger法(双序列):
python
from statsmodels.tsa.stattools import coint
t_stat, p_value, critical_values = coint(series_A, series_B)p < 0.05 → cointegrated
p < 0.05 → 存在协整关系
**Johansen (multivariate)**:
```python
from statsmodels.tsa.vector_ar.vecm import coint_johansen
result = coint_johansen(df, det_order=0, k_ar_diff=1)
**Johansen法(多变量)**:
```python
from statsmodels.tsa.vector_ar.vecm import coint_johansen
result = coint_johansen(df, det_order=0, k_ar_diff=1)trace statistic vs critical values at 90%/95%/99%
迹统计量与90%/95%/99%置信水平下的临界值对比
Report: test statistic, p-value, critical values, and cointegrating vector.
输出内容:检验统计量、p值、临界值以及协整向量。Step 4 — GARCH Volatility Modelling
步骤4 — GARCH波动率建模
When to use: Financial returns show volatility clustering (ARCH effects).
python
from arch import arch_model
model = arch_model(returns * 100, vol='Garch', p=1, q=1)
res = model.fit(disp='off')
print(res.summary())Note: required in addition to statsmodels.
pip install archOutput: omega, alpha (ARCH), beta (GARCH) coefficients. Persistence = alpha + beta. If > 0.95, volatility is highly persistent.
ARCH-LM test first (to verify ARCH effects exist):
python
from statsmodels.stats.diagnostic import het_arch
lm_stat, p_value, f_stat, f_p = het_arch(residuals)适用场景:金融收益率存在波动率聚类(ARCH效应)时。
python
from arch import arch_model
model = arch_model(returns * 100, vol='Garch', p=1, q=1)
res = model.fit(disp='off')
print(res.summary())注意:除statsmodels外,还需执行安装依赖库。
pip install arch输出:omega、alpha(ARCH项)、beta(GARCH项)系数。持续性 = alpha + beta。若该值>0.95,说明波动率具备高度持续性。
先进行ARCH-LM检验(验证是否存在ARCH效应):
python
from statsmodels.stats.diagnostic import het_arch
lm_stat, p_value, f_stat, f_p = het_arch(residuals)Step 5 — Regression Diagnostics
步骤5 — 回归诊断
After running OLS (), check:
statsmodels.api.OLS| Test | Purpose | Command |
|---|---|---|
| Durbin-Watson | Serial autocorrelation in residuals | |
| Breusch-Pagan | Heteroskedasticity | |
| Jarque-Bera | Normality of residuals | |
| VIF | Multicollinearity | |
Interpret Durbin-Watson: ~2.0 = no autocorrelation; < 1.5 = positive autocorrelation; > 2.5 = negative autocorrelation.
运行OLS回归()后,需检查以下内容:
statsmodels.api.OLS| 检验方法 | 用途 | 命令 |
|---|---|---|
| Durbin-Watson | 检验残差的序列自相关性 | |
| Breusch-Pagan | 检验异方差性 | |
| Jarque-Bera | 检验残差的正态性 | |
| VIF | 检验多重共线性 | |
Durbin-Watson结果解读:~2.0 = 无自相关性;<1.5 = 正自相关性;>2.5 = 负自相关性。
Step 6 — Bootstrap Confidence Intervals
步骤6 — Bootstrap置信区间估计
When to use: Non-normal distributions; small samples; estimating CI for Sharpe ratio, IC, or any statistic.
python
import numpy as np
def bootstrap_ci(data, stat_fn, n_boot=10000, ci=0.95):
boots = [stat_fn(np.random.choice(data, len(data), replace=True))
for _ in range(n_boot)]
lo = np.percentile(boots, (1 - ci) / 2 * 100)
hi = np.percentile(boots, (1 + ci) / 2 * 100)
return lo, hi适用场景:数据分布非正态、样本量较小、估计夏普比率/IC或其他统计量的置信区间时。
python
import numpy as np
def bootstrap_ci(data, stat_fn, n_boot=10000, ci=0.95):
boots = [stat_fn(np.random.choice(data, len(data), replace=True))
for _ in range(n_boot)]
lo = np.percentile(boots, (1 - ci) / 2 * 100)
hi = np.percentile(boots, (1 + ci) / 2 * 100)
return lo, hiExample: Sharpe ratio CI
示例:夏普比率置信区间
sharpe_lo, sharpe_hi = bootstrap_ci(returns, lambda x: x.mean() / x.std() * np.sqrt(252))
undefinedsharpe_lo, sharpe_hi = bootstrap_ci(returns, lambda x: x.mean() / x.std() * np.sqrt(252))
undefinedStep 7 — Hypothesis Tests
步骤7 — 假设检验
| Test | Use case | Function |
|---|---|---|
| t-test (one sample) | Is mean IC > 0? | |
| t-test (two sample) | Is long portfolio return > short portfolio? | |
| F-test / ANOVA | Are returns different across deciles? | |
| Mann-Whitney U | Non-parametric alternative to t-test | |
Always report: test statistic, p-value, degrees of freedom, and conclusion at 5% significance level.
| 检验方法 | 适用场景 | 函数 |
|---|---|---|
| 单样本t检验 | 因子IC均值是否>0? | |
| 双样本t检验 | 多头组合收益率是否>空头组合? | |
| F检验/方差分析 | 不同分位数组合的收益率是否存在差异? | |
| Mann-Whitney U检验 | t检验的非参数替代方法 | |
需始终报告:检验统计量、p值、自由度,以及在5%显著性水平下的结论。
CLI
命令行工具(CLI)
bash
longbridge kline --help
longbridge kline <SYMBOL> --period day --count 252 --format jsonbash
longbridge kline --help
longbridge kline <SYMBOL> --period day --count 252 --format jsonOutput
输出内容
For each test present:
- Test name and null hypothesis.
- Test statistic and p-value.
- Critical values (where applicable).
- Conclusion at 5% significance.
- Practical implication for the user's use case.
对于每个执行的检验:
- 检验名称与原假设。
- 检验统计量与p值。
- 临界值(如适用)。
- 在5%显著性水平下的结论。
- 对用户实际场景的实用建议。
Error handling
错误处理
| Situation | 简体回复 | 繁體回覆 | English reply |
|---|---|---|---|
| 请安装 longbridge-terminal 或检查 MCP 配置。 | 請安裝 longbridge-terminal 或檢查 MCP 配置。 | Install longbridge-terminal or check MCP config. |
| 请运行 | 請執行 | Run |
| Insufficient data (< 30 observations) | 样本量过小,统计结论可靠性有限,建议延长数据期。 | 樣本量過小,建議延長數據期。 | Sample too small; extend the data period for reliable results. |
| ARCH module missing for GARCH | 请运行 | 請執行 | Run |
| 场景 | 简体回复 | 繁体回覆 | English reply |
|---|---|---|---|
| 请安装 longbridge-terminal 或检查 MCP 配置。 | 請安裝 longbridge-terminal 或檢查 MCP 配置。 | Install longbridge-terminal or check MCP config. |
| 请运行 | 請執行 | Run |
| 数据不足(<30个观测值) | 样本量过小,统计结论可靠性有限,建议延长数据期。 | 樣本量過小,建議延長數據期。 | Sample too small; extend the data period for reliable results. |
| GARCH所需ARCH模块缺失 | 请运行 | 請執行 | Run |
Related skills
相关技能
- — IC/IR factor testing
longbridge-factor-research - — cointegration-based pairs trading
longbridge-pairs-trading - — cross-asset correlation analysis
longbridge-correlation - — volatility modelling and trading
longbridge-volatility-strategy
- — 因子IC/IR检验
longbridge-factor-research - — 基于协整的配对交易
longbridge-pairs-trading - — 跨资产相关性分析
longbridge-correlation - — 波动率建模与交易
longbridge-volatility-strategy
File layout
文件结构
skills/longbridge-quant-stats/
└── SKILL.mdskills/longbridge-quant-stats/
└── SKILL.md