Skip to main content

Fast Black-Scholes implied volatility calculation exposed from Rust to Python.

Project description

rust-py-calc-iv

用 Rust 实现的 Black-Scholes 欧式看涨期权隐含波动率计算模块,通过 Python 扩展暴露给 Python 使用。

功能

  • 支持单个欧式看涨期权 IV 计算。
  • 支持批量输入六个 numpy.ndarray[float64],返回一个 IV 数组。
  • 批量计算时释放 GIL。
  • 波动率从 [1e-8, 10.0] 开始搜索;如果 10 仍未夹住解,会自动向上扩展。
  • 当期权价格低于或贴近理论下界时,返回 1e-8
  • 当输入非法或期权价格超过理论上界时,返回 NaN

理论边界:

lower = max(S * exp(-qT) - K * exp(-rT), 0)
upper = S * exp(-qT)

安装与构建

uv sync
uv run maturin develop --release

使用

import numpy as np
import rust_py_calc_iv

iv = rust_py_calc_iv.implied_vol_call(
    100.0, 100.0, 1.0, 0.03, 0.01, 8.827321225352122
)

ivs = rust_py_calc_iv.implied_vol_call_batch(
    np.array([100.0, 100.0]),
    np.array([100.0, 100.0]),
    np.array([1.0, 1.0]),
    np.array([0.03, 0.03]),
    np.array([0.01, 0.01]),
    np.array([8.827321225352122, 12.69400452623556]),
)

输入参数依次为:

  • S:现货价格
  • K:行权价格
  • T:剩余期限,单位为年
  • r:无风险利率,小数
  • q:股息率,小数
  • market_price:期权价格

测试

uv run pytest
cargo test
cargo clippy --all-targets -- -D warnings

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

rust_py_calc_iv-0.1.2.tar.gz (15.8 kB view details)

Uploaded Source

Built Distribution

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

rust_py_calc_iv-0.1.2-cp311-cp311-win_amd64.whl (149.8 kB view details)

Uploaded CPython 3.11Windows x86-64

File details

Details for the file rust_py_calc_iv-0.1.2.tar.gz.

File metadata

  • Download URL: rust_py_calc_iv-0.1.2.tar.gz
  • Upload date:
  • Size: 15.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: maturin/1.13.3

File hashes

Hashes for rust_py_calc_iv-0.1.2.tar.gz
Algorithm Hash digest
SHA256 cd4db96fd2ae71ab1fbdccdb49582ca0872895b3310501caaa0a478f96412bac
MD5 8ada927d6ca8237ad865d378bbd50bc5
BLAKE2b-256 ce31d4bf804e47bccc3c131e4cb3229df13c689758e7b7b2b9837916a29f05a3

See more details on using hashes here.

File details

Details for the file rust_py_calc_iv-0.1.2-cp311-cp311-win_amd64.whl.

File metadata

File hashes

Hashes for rust_py_calc_iv-0.1.2-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 1274922e804185fd83d7b3b56ad1f0bf0463e80e36cb55a856fd9b189b2679f6
MD5 355acaab914b07f56b7879322c52494b
BLAKE2b-256 665d593e020c01ab72fd6b22db3f3d3529d21248964ff35b2685ea1c5c03a5bc

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