Skip to main content

Kiyosi

PyPI version CI License: MIT

简体中文 | English

Kiyosi 是一个衍生品定价库,核心引擎用 Rust 编写,为 Python 用户提供易用的 API。

特性

  • 高性能内核:核心计算由 Rust 实现;耗时计算期间释放 GIL,支持 Python 多线程并发,并内置并行蒙特卡洛线程池。
  • 产品覆盖:包括香草、数字、障碍和亚式期权,以及雪球、凤凰、累购/累沽、区间累计等国内常见的场外结构化产品。
  • 合约与引擎解耦:同一合约可切换解析解、数值积分、二叉树、有限差分和蒙特卡洛等定价引擎,便于交叉验证。
  • 模型支持:提供 Black–Scholes–Merton(BSM)、局部波动率和 Heston 模型。
  • 场外实务支持:内置上交所交易日历、交易时间波动率惯例与观察日程生成;支持隐含波动率、雪球/凤凰隐含票息反解及存续期历史状态估值。
  • 批量求解与类型注解:支持行权价与多合约批量定价、有限差分网格跨现货复用与曲面求解,提供不可变 .replace(...) 情景更新与完整 .pyi 类型注解。

安装

要求: Python ≥ 3.12

通过 PyPI 安装(推荐,提供 Linux x86_64 glibc 与 Windows x86_64 预编译 wheel):

pip install kiyosi

在其他平台(如 macOS、Linux ARM64)安装或从源码构建时,需先安装 Rust ≥ 1.85 和本机编译工具链:

git clone https://github.com/lilkui/kiyosi.git
cd kiyosi
pip install .

快速上手

公开 API 分为三个子模块:kiyosi.instruments(合约定义)、kiyosi.market(市场参数、利率曲线与交易日历)和 kiyosi.pricing(定价引擎与反解器)。

使用 BSM 解析解为欧式看涨期权定价、提取希腊值并反解隐含波动率:

from datetime import datetime

from kiyosi.instruments import EuropeanOption
from kiyosi.market import BlackScholesMertonParameters, PricingContext
from kiyosi.pricing import AnalyticEuropeanEngine, implied_volatility

start, end = datetime(2025, 1, 1), datetime(2026, 1, 1)
option = EuropeanOption(
    option_type="call",
    strike=100.0,
    effective_time=start,
    expiry_time=end,
)
context = PricingContext(
    model_parameters=BlackScholesMertonParameters(
        risk_free_rate=0.05,
        dividend_yield=0.02,
        volatility=0.20,
    ),
    spot_price=100.0,
    valuation_time=start,
)
engine = AnalyticEuropeanEngine()
result = engine.price_with_greeks(option, context, greeks="delta")
price, delta = result.price, result.require("delta")
sigma = implied_volatility(
    engine, option, context, observed_price=price, lower_bound=0.1, upper_bound=0.3
)
print(f"Price: {price:.6f}")  # Price: 9.227006
print(f"Delta: {delta:.6f}")  # Delta: 0.586851
print(f"Implied volatility: {sigma:.2%}")  # Implied volatility: 20.00%
示例:标准雪球定价与平价票息反解

结合上交所交易日历与交易时间波动率,为标准雪球定价并反解平价票息:

from datetime import datetime, time

from kiyosi.instruments import SnowballOption
from kiyosi.market import (
    BlackScholesMertonParameters,
    ObservationSchedule,
    PricingContext,
    TradingCalendar,
)
from kiyosi.pricing import FiniteDifferenceAutocallableEngine, implied_coupon

start, end = datetime(2025, 1, 2, 15), datetime(2026, 1, 5, 15)
calendar = TradingCalendar.sse()
schedule = ObservationSchedule.monthly(
    start_time=start,
    end_time=end,
    first_observation_month=1,
    observation_clock_time=time(15),
    calendar=calendar,
)
note = SnowballOption.standard(
    coupon_rate=0.12,
    initial_spot=100.0,
    knock_in_level=80.0,
    knock_out_level=103.0,
    autocall_times=schedule,
    knock_in_observation_clock_time=time(15),
    effective_time=start,
    expiry_time=end,
)
context = PricingContext(
    model_parameters=BlackScholesMertonParameters(
        risk_free_rate=0.02,
        dividend_yield=0.0,
        volatility=0.20,
        variance_time_convention="business",
        calendar=calendar,
    ),
    spot_price=100.0,
    valuation_time=start,
)
engine = FiniteDifferenceAutocallableEngine()
unit_price = engine.price(note, context)
quote = implied_coupon(
    engine, note, context, observed_price=1.0, quote_convention="shift_maturity_coupon"
)
print(f"Unit price: {unit_price:.6f}")  # Unit price: 0.989426
print(f"Par coupon: {quote['first_observation_coupon']:.2%}")  # Par coupon: 15.82%

更多示例见 examples/python/。

支持产品与算法
金融产品 BSM 局部波动率 Heston
欧式香草期权 解析解、数值积分、二叉树、有限差分、蒙特卡洛模拟 有限差分 解析解、有限差分
美式香草期权 Bjerksund–Stensland 2002 近似、数值积分、二叉树、有限差分、最小二乘蒙特卡洛模拟 有限差分 有限差分
数字期权 解析解、数值积分、二叉树、有限差分、蒙特卡洛模拟 有限差分 —
几何平均亚式期权 解析解(连续平均)、蒙特卡洛模拟 — —
算术平均亚式期权 Turnbull–Wakeman 连续平均近似、蒙特卡洛模拟 — —
单障碍期权 解析解、数值积分、二叉树、有限差分、蒙特卡洛模拟 有限差分 —
双障碍期权 解析解(级数)、有限差分、蒙特卡洛模拟 有限差分 —
二元障碍、单触碰与单不触碰期权 解析解、有限差分 有限差分 —
双触碰与双不触碰期权 解析解(级数)、有限差分 有限差分 —
雪球、二元雪球、三元雪球 数值积分、有限差分、蒙特卡洛模拟 有限差分 —
凤凰结构 数值积分、有限差分、蒙特卡洛模拟 有限差分 —
累购、累沽 数值积分、有限差分、蒙特卡洛模拟 有限差分 —
区间累计 解析解(区间概率求和) — —

基准测试

在相同精度门限下,对比 Kiyosi(Rust)与 QuantLib 1.44(C++ -O3)的原生计算延迟。结果见 Kiyosi 与 QuantLib 性能对比报告,本地复现见 benchmarks/quantlib/。

免责声明

本项目仅供研究与学习,用于生产环境前请自行验证。本库的公开 API 不保证稳定。

许可证

MIT

Metadata

Release files for kiyosi 0.24.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for kiyosi 0.24.0
File Size Uploaded Provenance Actions
kiyosi-0.24.0.tar.gz 1.8 MB With provenance Details for kiyosi-0.24.0.tar.gz

Built distributions (wheels)

Table of built distributions (wheels) for kiyosi 0.24.0
File Interpreter ABI Platform Actions
kiyosi-0.24.0-cp312-abi3-win_amd64.whl CPython 3.12 abi3 Windows x86-64 Details for kiyosi-0.24.0-cp312-abi3-win_amd64.whl
kiyosi-0.24.0-cp312-abi3-manylinux_2_28_x86_64.whl CPython 3.12 abi3 Linux glibc 2.28+ x86-64 Details for kiyosi-0.24.0-cp312-abi3-manylinux_2_28_x86_64.whl

Total release size: 14.1 MB

Release files / kiyosi-0.24.0.tar.gz

Download URL kiyosi-0.24.0.tar.gz
Size 1.8 MB
Tags Source
SHA-256 checksum
How to use checksums
9bdbf24959eba5e6a87ff49df827f98bd8a726538eecee8942bafa9dc578efb5
BLAKE2b-256 checksum
How to use checksums
7f015466e83c79a03c9fa1e0713d1d6006d0670820467c15e800f3bb33f8bcf4
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Dec 2, 2026.

Transparency log

Release files / kiyosi-0.24.0-cp312-abi3-win_amd64.whl

Download URL kiyosi-0.24.0-cp312-abi3-win_amd64.whl
Size 6.3 MB
Tags CPython 3.12 Windows x86-64 abi3
SHA-256 checksum
How to use checksums
87339e77f3ac4fdab40c8a2fdec35e3a99adc70e976b1e3be0d1b3ed97f0e2db
BLAKE2b-256 checksum
How to use checksums
8da560ee28d8911e7473c081d2cad7033a5fd62a94519fd650e3dfebeb976822
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Dec 2, 2026.

Transparency log

Release files / kiyosi-0.24.0-cp312-abi3-manylinux_2_28_x86_64.whl

Download URL kiyosi-0.24.0-cp312-abi3-manylinux_2_28_x86_64.whl
Size 6.1 MB
Tags CPython 3.12 Linux glibc 2.28+ x86-64 abi3
SHA-256 checksum
How to use checksums
e4529d67a8ad9ba5744c8b55f2d8aee3849c6f9bfa0a30d63eba901d71ed2044
BLAKE2b-256 checksum
How to use checksums
bf27309f92bcfb54ceb85c8139cf4aa33e0f661bb4b6d44be7f140322b77024c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Dec 2, 2026.

Transparency log

Security for kiyosi 0.24.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

All 3 files in this release have been uploaded with provenance metadata.

Select from 3 files

Attestation for kiyosi-0.24.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Dec 2, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page