kepler-metric is a Python library with performance and risk statistics commonly used in quantitative finance
Project description
kepler-metric | 金融风险指标计算库
概述
kepler-metric 是一个用于计算金融风险和绩效指标的 Python 库。基于 Quantopian Inc. 开发的 kepler.metric,它为量化金融专业人士和研究人员提供了全面的工具集。
特性
- 全面的指标:超过 50 个金融指标,包括收益、风险、风险调整收益和市场关系
- DataFrame 列名保留:当输入为 DataFrame 时,所有函数返回带列名的 pandas Series
- 灵活输入:支持 pandas Series/DataFrame 和 numpy 数组
- NaN 处理:稳健地处理缺失数据
- 滚动计算:大多数指标都有滚动窗口版本
安装
pip install kepler-metric
注意:虽然 PyPI 包名是 kepler-metric,但导入时仍然使用 kepler.metric:
import kepler.metric
快速开始
import pandas as pd
import numpy as np
from kepler.metric import (
max_drawdown, sharpe_ratio, annual_return,
annual_volatility, calmar_ratio
)
# 创建示例收益率数据
np.random.seed(42)
returns = pd.DataFrame(
np.random.normal(0.001, 0.02, (252, 3)),
columns=['Strategy_A', 'Strategy_B', 'Strategy_C']
)
# 计算各种指标
metrics = {
'Annual Return': annual_return(returns),
'Max Drawdown': max_drawdown(returns),
'Sharpe Ratio': sharpe_ratio(returns),
'Annual Volatility': annual_volatility(returns),
'Calmar Ratio': calmar_ratio(returns)
}
# 打印结果
for metric, values in metrics.items():
print(f"\n{metric}:")
print(values)
主要函数
收益指标
annual_return()- 年化收益率cagr()- 复合年增长率cum_returns()- 累积收益
风险指标
max_drawdown()- 最大回撤annual_volatility()- 年化波动率value_at_risk()- 风险价值 (VaR)conditional_value_at_risk()- 条件风险价值 (CVaR)
风险调整收益
sharpe_ratio()- 夏普比率sortino_ratio()- 索提诺比率calmar_ratio()- 卡玛比率omega_ratio()- 欧米茄比率
市场关系
alpha()- 阿尔法beta()- 贝塔alpha_beta()- 阿尔法和贝塔
DataFrame 支持
所有函数都支持 DataFrame 输入,并保留列名:
# DataFrame 输入
returns = pd.DataFrame({
'Stock_A': [...],
'Stock_B': [...],
'Stock_C': [...]
})
# 返回带列名的 Series
sharpe = sharpe_ratio(returns)
print(sharpe)
# Stock_A 0.75
# Stock_B 0.82
# Stock_C 0.68
# dtype: float64
开发
从源码安装:
git clone https://github.com/cloudQuant/kepler.metric
cd kepler.metric
pip install -e .
运行测试:
pytest tests/
许可证
Apache License 2.0
致谢
基于 Quantopian Inc. 开发的 kepler.metric 库。
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file kepler_metric-0.6.3.tar.gz.
File metadata
- Download URL: kepler_metric-0.6.3.tar.gz
- Upload date:
- Size: 22.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1859ce1b04a0698e9dc3be372fb6016eaa47b88161739e6d929ae7e5a33cd19b
|
|
| MD5 |
fa4d2fa701033175fec8a85ad126b2e1
|
|
| BLAKE2b-256 |
5f6d67398016ad409c6cac5a69ec7194e5767058f043c05e07f22ff98a780bfa
|
File details
Details for the file kepler_metric-0.6.3-py3-none-any.whl.
File metadata
- Download URL: kepler_metric-0.6.3-py3-none-any.whl
- Upload date:
- Size: 7.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3f8171f51e6dccc39f457df52c5a8ec171c3006ca9ad23213413753107010d99
|
|
| MD5 |
2cf92e50c9fd99fab2bab6ca58c02c4c
|
|
| BLAKE2b-256 |
2b402a4c18d7e7b992ba8bf229d23f5c65ba85085d4ceee74911a0e58c19a0c8
|