Skip to content

Latest commit

 

History

16 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Computational Finance Lab

comp_fin_lab is a compact Python library for computational finance, covering derivative pricing, stochastic simulation, Monte Carlo methods, sensitivities, numerical integration, and transform-based valuation.

The library is function-oriented and keeps implementations close to the underlying mathematics. Numerical methods are tested against closed-form solutions, known identities, limiting cases, and independent implementations where possible.


Methods

Area Implemented methods
Black-Scholes European call/put pricing, delta, implied volatility
Lattice methods European and American CRR, barrier options, tree-based delta
American options Perpetual American put, Longstaff-Schwartz Monte Carlo
Stochastic simulation Exact Black-Scholes paths, Euler-Maruyama, Milstein, Heston
Numerical integration Risk-neutral expectation pricing
Transform pricing Laplace inversion and FFT-based Fourier pricing
Characteristic functions Black-Scholes and Heston
Monte Carlo sensitivities Finite-differences, infinitesimal perturbation, likelihood ratio
Random sampling Inverse-transform sampling, acceptance-rejection
Variance reduction Antithetic variables
Payoffs Vanilla call/put and power-call payoffs

Installation

Install directly from GitHub:

pip install git+https://github.com/Palit2308/computational_finance_lab.git

Quick Start

Black-Scholes pricing

from comp_fin_lab.bs import eu_bs

price = eu_bs(
    t=0,
    St=100,
    K=100,
    T=1,
    r=0.05,
    sigma=0.20,
    call=1
)

print(price)
10.450583572185565

CRR pricing

from comp_fin_lab.crr import eu_crr
from comp_fin_lab.payoffs import vcall

S0 = 100.0
K = 100.0
T = 1.0
r = 0.05
sigma = 0.20

M = 1000

call = lambda S: vcall(S, K)

price = eu_crr(call, S0, T, r, sigma, M)
print(price)
10.44906865899621

Exact Black-Scholes simulation

from comp_fin_lab.paths import bs_paths

S = bs_paths(
    St=100,
    r=0.05,
    sigma=0.20,
    T=1,
    t=0,
    sims=10_000,
    steps=252
)

The path simulator uses the exact geometric brownian motion representation rather than an Euler approximation.

Numerical Design

The design is function-oriented. Generic numerical methods such as Euler-Maruyama, Milstein, finite-difference sensitivities, and sampling routines accept user-supplied functions. Model-specific algorithms such as CRR, Heston simulation, and transform pricing remain explicit.

Simulation functions expose random seeds to make numerical experiments reproducible.

Testing and Numerical Reliability

Tests are organized around numerical properties of the methods rather than only individual code paths.

Methods are tested using combinations of:

Test principle Examples
Analytical benchmarks Black-Scholes prices and deltas
Mathematical identities Put-call parity, characteristic-function normalization
Limiting cases Heston to constant-volatility GBM, zero-volatility paths
Cross-method agreement CRR vs Black-Scholes, transform pricing vs closed form
Distributional properties RNG moments, empirical CDFs, Heston variance moments
Monte Carlo theory Confidence-based tolerances, antithetic variance reduction
Numerical contracts Shapes, types, reproducibility, invalid-input handling

Run the test suite with:

pytest

Run coverage with:

pytest --cov=comp_fin_lab --cov-branch --cov-report=term-missing

The current test suite exercises approximately 99% of the package code. Deterministic routines are compared with analytical solutions or known identities. Stochastic tests use fixed seeds where reproducibility is required and statistical tolerances where exact equality would be inappropriate.

Package Structure

src/comp_fin_lab/
├── american.py
├── bs.py
├── char_fun.py
├── crr.py
├── heston.py
├── integration.py
├── int_transforms.py
├── paths.py
├── payoffs.py
├── rng.py
├── sim_sensitivities.py
└── var_reduction.py

Each module corresponds closely to a model family or numerical method.


Conventions

  • t denotes current time and T maturity.
  • Black-Scholes pricing and simulation use risk-neutral dynamics.
  • In the Heston module, gamma denotes instantaneous variance.
  • In Heston simulations, sigma denotes volatility of variance.
  • CRR barrier types use DnO, UnO, DnI, and UnI.
  • Monte Carlo routines expose seeds where reproducibility is relevant.

Version 2.0.0

Major changes from v1:

  • Reorganized modules around model and numerical-method families
  • Added new functions for simulation of sensitivities, random number generation and variance reduction.
  • Updated tests using pytest.

Finite-difference PDE methods and larger numerical validation studies are planned for later.

Previous releases remain available through Git tags.

References

  • [FLL+99] E. Fournié, J. Lasry, J. Lebuchoux, P. Lions, and N. Touzi. Applications of Malliavin Calculus to Monte Carlo Methods in Finance. Finance and Stochastics, 3:391–412, 1999.
  • [Gla03] P. Glasserman. Monte Carlo Methods in Financial Engineering. Springer, Berlin, 2003.
  • [JLL90] P. Jaillet, D. Lamberton, and B. Lapeyre. Variational Inequalities and the Pricing of American Options. Acta Applicandae Mathematica, 21(3):263–289, 1990.
  • [Lee04] R. Lee. Option Pricing by Transform Methods: Extensions, Unification and Error Control. Journal of Computational Finance, 7:51–86, 2004.
  • [LS01] F. A. Longstaff and E. S. Schwartz. Valuing American Options by Simulation: A Simple Least-Squares Approach. The Review of Financial Studies, 14(1):113–147, 2001.
  • [PV94] A. Pelsser and T. C. F. Vorst. The Binomial Model and the Greeks. The Journal of Derivatives, 1(3):45–49, 1994.
  • [AMST07] H. Albrecher, P. A. Mayer, W. Schoutens, and J. Tistaert. The Little Heston Trap. Wilmott, 1:83–92, 2007.

About

A modular Python project for computational finance and stochastic modelling.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages