vollib

repository·master·Indexed 19 days ago

https://github.com/vollib/py_vollib

A Python library for calculating option prices, implied volatility, and Greeks. Built upon Peter Jaeckel's 'LetsBeRational' algorithm, it provides high-speed and high-accuracy computations for Black, Black-Scholes, and Black-Scholes-Merton models. The library includes support for both analytical and numerical Greek calculations, probability distributions, and reference Python implementations for model verification.

Tokens
3.1K
Snippets
4
Records
29
Agent score
64%

What's inside vollib

  1. Use the vollib.black_scholes package

    master

    The vollib.black_scholes package provides tools for Black-Scholes option pricing and related calculations. It is organized into subpackages and submodules to handle different aspects of financial mathematics.

    Key components include:

    • vollib.black_scholes.greeks: A subpackage containing functions to calculate various Greeks (sensitivities).
    • vollib.black_scholes.implied_volatility: A submodule dedicated to calculating implied volatility from option prices.
    • Core vollib.black_scholes module: Provides the primary interface for Black-Scholes calculations.
  2. Use the vollib.ref_python.black_scholes package

    master

    The vollib.ref_python.black_scholes package provides a reference Python implementation of the Black-Scholes model. It is organized into subpackages and modules for calculating option Greeks and implied volatility.

    Key components include:

    • Subpackages: vollib.ref_python.black_scholes.greeks for calculating various option Greeks.
    • Modules: vollib.ref_python.black_scholes.implied_volatility for calculating implied volatility from option prices.
  3. Use the vollib.black_scholes_merton package

    master

    The vollib.black_scholes_merton package provides tools for Black-Scholes-Merton option pricing and analysis. It is organized into several functional areas:

    • Core Pricing and Greeks: The main package provides access to Black-Scholes-Merton pricing models and Greeks (via the vollib.black_scholes_merton.greeks subpackage).
    • Implied Volatility: The vollib.black_scholes_merton.implied_volatility submodule is used to calculate implied volatility from option prices.
  4. Use the vollib.black package for Black-Scholes calculations

    master

    The vollib.black package provides implementations of the Black-Scholes model. It is organized into subpackages and modules for calculating implied volatility and various Greeks.

    Key components include:

    • vollib.black.implied_volatility: Module for calculating implied volatility.
    • vollib.black.greeks: Subpackage containing various Greek sensitivities (e.g., Delta, Gamma, Vega, Theta, Rho).

    Note: This package is part of the vollib ecosystem and relies on specific mathematical implementations (often referencing 'Let's be Rational' logic) to provide high-precision financial calculations.

  5. Use the vollib.ref_python.black_scholes_merton package

    master

    The vollib.ref_python.black_scholes_merton package provides a reference Python implementation of the Black-Scholes-Merton model. It is organized into subpackages and submodules for specific financial calculations.

    Key components include:

    • vollib.ref_python.black_scholes_merton.greeks: A subpackage containing Greek calculations (e.g., Delta, Gamma, Vega, Theta, Rho).
    • vollib.ref_python.black_scholes_merton.implied_volatility: A submodule specifically for calculating implied volatility.

    Note that this is a reference implementation, likely intended for verification or parity with other versions of the library.

  6. Explore the vollib.helpers package submodules

    master

    The vollib.helpers package provides utility modules for financial calculations, statistical distributions, and numerical approximations. The package is organized into the following submodules:

    • vollib.helpers.constants: Contains mathematical and financial constants.
    • vollib.helpers.distributions: Provides functions for various probability distributions.
    • vollib.helpers.numerical_greeks: Contains tools for calculating numerical Greeks (sensitivities).
    • vollib.helpers.exceptions: Defines custom exception classes for the library.
    • vollib.helpers.doctest_helper: Internal utility for doctest execution.
  7. Explore the vollib subpackages

    master

    The vollib package is organized into several specialized subpackages for quantitative finance calculations. Depending on your needs, you should import from the following modules:

    • vollib.black: Implements the Black model.
    • vollib.black_scholes: Implements the Black-Scholes model.
    • vollib.black_scholes_merton: Implements the Black-Scholes-Merton model.
    • vollib.helpers: Provides utility functions and helpers.
    • vollib.ref_python: Contains the reference Python implementation.
  8. Calculate Black-Scholes Greeks using analytical and numerical methods

    master

    The vollib.ref_python.black.greeks package provides tools to calculate the Greeks (sensitivities) for options using the Black-Scholes model. It is divided into two submodules based on the calculation method:

    1. Analytical methods: Found in vollib.ref_python.black.greeks.analytical. These use closed-form mathematical formulas to calculate Greeks.
    2. Numerical methods: Found in vollib.ref_python.black.greeks.numerical. These use finite difference methods (approximations) to calculate Greeks.

    You can access the combined functionality through the main vollib.ref_python.black.greeks module.

  9. Handle strike price (K) constraints

    master

    The pricing functions in vollib require strictly positive strike prices (K > 0). The library does not special-case the boundary where K = 0. If your application needs to support a zero strike, you must handle that logic before calling vollib functions.

    Error behavior for Strike (K):

    Strike inputCallsPuts
    K = 0raises ZeroDivisionErrorraises ZeroDivisionError
    K < 0raises ValueErrorraises ValueError
  10. Use vollib.ref_python for sanity checking

    master

    The vollib.ref_python module provides a pure Python implementation of the functions found in vollib.*. It has no dependencies on lets_be_rational or py_lets_be_rational.

    Warning: This is intended strictly as a reference implementation for sanity checking and is not recommended for production use.

  11. Install vollib via pip

    master

    To install vollib, use pip. This will automatically install the core dependency py_lets_be_rational. Ensure that Python and pip are installed on your system before running the command.

    vollib 1.0.7+ supports Python 3.9 through 3.12.

    pip install vollib
  12. Migrate from py_vollib to vollib

    master

    The canonical package name has returned to vollib. While py_vollib.* imports are maintained as a compatibility namespace for the current transition, they are deprecated.

    Recommended Import Pattern: Instead of: from py_vollib.black_scholes import black_scholes

    Use: from vollib.black_scholes import black_scholes

    from vollib.black_scholes import black_scholes