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.0.tar.gz (15.2 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.0-cp311-cp311-win_amd64.whl (148.9 kB view details)

Uploaded CPython 3.11Windows x86-64

File details

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

File metadata

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

File hashes

Hashes for rust_py_calc_iv-0.1.0.tar.gz
Algorithm Hash digest
SHA256 ba742642a053a630894491c72e4863b12df3e5748631b3f7105d133213a59273
MD5 e5bb0bc5d5eb9b030f6aa5d14ae38dc5
BLAKE2b-256 4489f34edbaf7eb0e5f0869427694de13161b4263bae556720ebe308e27cbe47

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for rust_py_calc_iv-0.1.0-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 fe14708963af89733af4152a1e89a2f7f7fdbd3170d4d5be05193fc19009dd19
MD5 eea9800d50f9f571a5225b969c11d6f6
BLAKE2b-256 4c1b4f677f0bbcd1f3c9f1797539ee064a715244d44e3bd837a902f1493c3055

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