How pfnet-research/pfhedge works
Purpose and Public Surface
PFHedge is a PyTorch library for Monte Carlo deep hedging of financial derivatives under market frictions. Users compose instruments, a hedging model, input features, and a risk criterion; then train and price through Hedger. Package exports are rooted at pfhedge/__init__.py, with main public domains in pfhedge/instruments/__init__.py and pfhedge/nn/__init__.py. Runnable usage entry points are the scripts/notebook in examples/, especially examples/example_readme.py.
Instrument Domain Model
The instrument layer separates tradable simulated underliers (“primary” instruments) from contingent claims (“derivatives”). Shared abstractions establish the contract and likely device/dtype/state behavior at pfhedge/instruments/base.py, pfhedge/instruments/primary/base.py, and pfhedge/instruments/derivative/base.py. Derivative implementations bind one or more underliers and define payoff/contract-specific attributes, e.g. pfhedge/instruments/derivative/european.py, pfhedge/instruments/derivative/lookback.py, and pfhedge/instruments/derivative/variance_swap.py.
Market Simulation Pipeline
Primary instruments model paths and market costs; concrete models include geometric Brownian, stochastic-volatility, rates, and jump processes in pfhedge/instruments/primary/brownian.py, pfhedge/instruments/primary/heston.py, and pfhedge/instruments/primary/rough_bergomi.py. The underlying stochastic-process implementations are factored into pfhedge/stochastic/engine.py, pfhedge/stochastic/brownian.py, and pfhedge/stochastic/heston.py. This boundary lets instruments expose finance-facing objects while stochastic modules generate tensor-valued paths.
Feature Assembly
Feature objects turn derivative state, underlier paths, and prior hedge decisions into model inputs. The extensible feature contract and lookup mechanism live in pfhedge/features/_base.py and pfhedge/features/_getter.py; composition/container handling is in pfhedge/features/container.py. Features are central to the recurrent control loop because prev_hedge makes each time-step decision depend on the prior position.
Hedging and Optimization Orchestration
pfhedge/nn/modules/hedger.py is the main application controller and PyTorch Module: it simulates/evaluates a derivative, constructs sequential feature tensors, invokes the strategy, derives portfolio P&L including transaction costs, applies a risk criterion, and supports fitting/pricing. Strategy networks and controls include pfhedge/nn/modules/mlp.py, pfhedge/nn/modules/clamp.py, and pfhedge/nn/modules/naked.py. Training delegates parameter updates to standard PyTorch optimizers; GPU and dtype movement follow PyTorch module/tensor semantics.
Models, Baselines, and Risk Objectives
Risk measures used as training objectives reside in pfhedge/nn/modules/loss.py, including entropic risk and expected shortfall. Analytic/model-based benchmark strategies are organized under pfhedge/nn/modules/bs/, with generic Black-Scholes machinery in pfhedge/nn/modules/bs/_base.py and derivative-specific formulas such as pfhedge/nn/modules/bs/european.py. Transaction-cost-aware Whalley–Wilmott logic is in pfhedge/nn/modules/ww.py; SVI-related modeling is in pfhedge/nn/modules/svi.py.
Differentiation, Runtime, and Delivery
pfhedge/autogreek.py differentiates user pricing functions through PyTorch autograd to calculate Greeks, crossing the pricing/autodiff boundary without separate finite-difference infrastructure. The material runtime dependency is PyTorch; no server, database, or external market-data service is represented. Packaging is defined by pyproject.toml; GitHub Actions provides CI, docs, examples, release, and version-bump workflows under .github/workflows/. Documentation is built from docs/source/.
Main entry points: hedger.py Hedger · __init__.py PFHedge exports · derivative/base.py Derivative contracts · instruments/base.py Instrument contracts
Components 20
Public Surface
- Runnable examples · Demonstrates instrument-to-training workflows.
examples/example_readme.py - PFHedge exports
pfhedge/__init__.py - Instrument exports
pfhedge/instruments/__init__.py - Neural API
pfhedge/nn/__init__.py
Instruments & Simulation
- Instrument contracts · Defines shared instrument state, device, and dtype behavior.
pfhedge/instruments/base.py - Primary instruments · Finance-facing underliers with paths and market costs.
pfhedge/instruments/primary/base.py - Derivative contracts · Bind underliers and define payoffs.
pfhedge/instruments/derivative/base.py - Derivative implementations · European, lookback, and variance-swap contracts.
pfhedge/instruments/derivative/european.py - Market model instruments · Brownian, Heston, and rough Bergomi underliers.
pfhedge/instruments/primary/brownian.py - Stochastic processes · Generates tensor-valued market paths for instruments.
pfhedge/stochastic/engine.py
Hedging Runtime
- Feature system · Resolves derivative state, paths, and prior hedge inputs.
pfhedge/features/_base.py - Feature composition
pfhedge/features/container.py - Hedger · Runs sequential hedging, P&L, fitting, and pricing.
pfhedge/nn/modules/hedger.py - Strategy networks · MLP strategies and position constraints.
pfhedge/nn/modules/mlp.py - Risk objectives · Entropic risk and expected shortfall objectives.
pfhedge/nn/modules/loss.py - Analytic benchmarks · Black-Scholes and transaction-cost-aware baselines.
pfhedge/nn/modules/bs/_base.py - AutoGreek · Computes Greeks through PyTorch autograd.
pfhedge/autogreek.py
Delivery
- Package build
pyproject.toml - GitHub Actions · Runs CI, docs, examples, releases, and version bumps.
- Documentation source
Connections 22
- Runnable examples → PFHedge exports: uses
- PFHedge exports → Instrument exports: exports
- PFHedge exports → Neural API: exports
- Instrument exports → Instrument contracts: exposes
- Instrument contracts → Primary instruments: base for
- Instrument contracts → Derivative contracts: base for
- Derivative contracts → Derivative implementations: implemented by
- Derivative contracts → Primary instruments: binds underliers
- Primary instruments → Market model instruments: implemented by
- Market model instruments → Stochastic processes: uses paths from
- Hedger → Derivative contracts: simulates and values
- Hedger → Feature composition: assembles inputs
- Feature composition → Feature system: composes
- Feature system → Derivative contracts: reads state
- Hedger → Strategy networks: invokes sequentially
- Strategy networks → Feature system: consumes feature tensors
- Hedger → Risk objectives: optimizes
- Analytic benchmarks → Strategy networks: provides baseline strategies
- AutoGreek → Hedger: differentiates pricing functions
- Package build → PFHedge exports: builds
- GitHub Actions → Package build: validates and releases
- Documentation source → PFHedge exports: documents