Skip to main content

kepler-metric is a Python library with performance and risk statistics commonly used in quantitative finance

Project description

kepler-metric | 金融风险指标计算库

Version 0.6.2 Python Versions License: Apache 2.0

概述

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

kepler_metric-0.6.3.tar.gz (22.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

kepler_metric-0.6.3-py3-none-any.whl (7.4 kB view details)

Uploaded Python 3

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

Hashes for kepler_metric-0.6.3.tar.gz
Algorithm Hash digest
SHA256 1859ce1b04a0698e9dc3be372fb6016eaa47b88161739e6d929ae7e5a33cd19b
MD5 fa4d2fa701033175fec8a85ad126b2e1
BLAKE2b-256 5f6d67398016ad409c6cac5a69ec7194e5767058f043c05e07f22ff98a780bfa

See more details on using hashes here.

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

Hashes for kepler_metric-0.6.3-py3-none-any.whl
Algorithm Hash digest
SHA256 3f8171f51e6dccc39f457df52c5a8ec171c3006ca9ad23213413753107010d99
MD5 2cf92e50c9fd99fab2bab6ca58c02c4c
BLAKE2b-256 2b402a4c18d7e7b992ba8bf229d23f5c65ba85085d4ceee74911a0e58c19a0c8

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page