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.4.tar.gz (43.3 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.4-py3-none-any.whl (28.6 kB view details)

Uploaded Python 3

File details

Details for the file kepler_metric-0.6.4.tar.gz.

File metadata

  • Download URL: kepler_metric-0.6.4.tar.gz
  • Upload date:
  • Size: 43.3 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.4.tar.gz
Algorithm Hash digest
SHA256 717956ef2be24324bf1b3b1db5ec76e1c8a79e1308f54f30c1cecb67d32c8b25
MD5 71029e6a9246cab56d1e5d846100aefc
BLAKE2b-256 312348027ca245f0e60894431e4eeebd1a2be2d3ba6ca00f80f67c3d17d41cd3

See more details on using hashes here.

File details

Details for the file kepler_metric-0.6.4-py3-none-any.whl.

File metadata

  • Download URL: kepler_metric-0.6.4-py3-none-any.whl
  • Upload date:
  • Size: 28.6 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.4-py3-none-any.whl
Algorithm Hash digest
SHA256 6d3198001989480f8dfc15c44b0d6bb092aaf7c2c0c9df5be3acd53a985f6abe
MD5 b2a3003bedfc3c8428bb11e0d654ee54
BLAKE2b-256 cec444c5a500ad9e5e3176dabf42e3a18d844dff6925031413f9ff9535dcc6fd

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