pfhedge.autogreek¶
pfhedge.autogreek provides functions implementing automatic evaluation of greeks (susceptibility of derivative price to its input parameters) using automatic differentiation.
Delta¶
- pfhedge.autogreek.delta(pricer, *, create_graph=False, **params)[source]¶
Computes and returns delta of a derivative using automatic differentiation.
Delta is a differentiation of a derivative price with respect to a price of underlying instrument.
Note
The keyword argument
**params
should contain at least one of the following combinations:spot
moneyness
andstrike
log_moneyness
andstrike
- Parameters
pricer (callable) – Pricing formula of a derivative.
create_graph (bool, default=False) – If
True
, graph of the derivative will be constructed, allowing to compute higher order derivative products.**params – Parameters passed to
pricer
.
- Returns
torch.Tensor
Examples
Delta of a European option can be evaluated as follows.
>>> import pfhedge.autogreek as autogreek >>> from pfhedge.nn import BSEuropeanOption >>> >>> # TODO(simaki): Rewrite using functional >>> pricer = BSEuropeanOption().price >>> autogreek.delta( ... pricer, ... log_moneyness=torch.zeros(3), ... time_to_maturity=torch.ones(3), ... volatility=torch.tensor([0.18, 0.20, 0.22]), ... strike=1.0, ... ) tensor([0.5359, 0.5398, 0.5438])
The result matches the analytical solution (as it should).
>>> BSEuropeanOption().delta( ... log_moneyness=torch.zeros(3), ... time_to_maturity=torch.ones(3), ... volatility=torch.tensor([0.18, 0.20, 0.22]), ... ) tensor([0.5359, 0.5398, 0.5438])
One can evaluate greeks of a price computed by a hedger.
>>> from pfhedge.instruments import BrownianStock >>> from pfhedge.instruments import EuropeanOption >>> from pfhedge.nn import WhalleyWilmott >>> from pfhedge.nn import Hedger >>> >>> _ = torch.manual_seed(42) >>> >>> derivative = EuropeanOption(BrownianStock(cost=1e-4)).to(torch.float64) >>> model = WhalleyWilmott(derivative) >>> hedger = Hedger(model, model.inputs()).to(torch.float64) >>> >>> def pricer(spot): ... return hedger.price( ... derivative, init_state=(spot,), enable_grad=True ... ) >>> >>> autogreek.delta(pricer, spot=torch.tensor(1.0)) tensor(0.5...)
Gamma¶
- pfhedge.autogreek.gamma(pricer, *, create_graph=False, **params)[source]¶
Computes and returns gamma of a derivative.
Delta is a second-order differentiation of a derivative price with respect to a price of underlying instrument.
Note
The keyword argument
**params
should contain at least one of the following combinations:spot
moneyness
andstrike
log_moneyness
andstrike
- Parameters
pricer (callable) – Pricing formula of a derivative.
create_graph (bool, default=False) – If
True
, graph of the derivative will be constructed, allowing to compute higher order derivative products.**params – Parameters passed to
pricer
.
- Returns
torch.Tensor
Examples
Gamma of a European option can be evaluated as follows.
>>> import pfhedge.autogreek as autogreek >>> from pfhedge.nn import BSEuropeanOption >>> >>> pricer = BSEuropeanOption().price >>> autogreek.gamma( ... pricer, ... strike=torch.ones(3), ... log_moneyness=torch.zeros(3), ... time_to_maturity=torch.ones(3), ... volatility=torch.tensor([0.18, 0.20, 0.22]), ... ) tensor([2.2074, 1.9848, 1.8024])
The result matches the analytical solution (as it should).
>>> BSEuropeanOption().gamma( ... log_moneyness=torch.zeros(3), ... time_to_maturity=torch.ones(3), ... volatility=torch.tensor([0.18, 0.20, 0.22]), ... ) tensor([2.2074, 1.9848, 1.8024])
Vega¶
- pfhedge.autogreek.vega(pricer, *, create_graph=False, **params)[source]¶
Computes and returns vega of a derivative using automatic differentiation.
Vega is a differentiation of a derivative price with respect to a variance of underlying instrument.
Note
The keyword argument
**params
should contain at least one of the following parameters:volatility
variance
- Parameters
pricer (callable) – Pricing formula of a derivative.
create_graph (bool, default=False) – If
True
, graph of the derivative will be constructed, allowing to compute higher order derivative products.**params – Parameters passed to
pricer
.
- Returns
torch.Tensor
Examples
Vega of a European option can be evaluated as follows.
>>> import pfhedge.autogreek as autogreek >>> from pfhedge.nn import BSEuropeanOption >>> >>> pricer = BSEuropeanOption().price >>> autogreek.vega( ... pricer, ... log_moneyness=torch.zeros(3), ... time_to_maturity=torch.ones(3), ... volatility=torch.tensor([0.18, 0.20, 0.22]), ... ) tensor([0.3973, 0.3970, 0.3965])
Theta¶
- pfhedge.autogreek.theta(pricer, *, create_graph=False, **params)[source]¶
Computes and returns theta of a derivative using automatic differentiation.
Theta is a differentiation of a derivative price with respect to time.
Note
The keyword argument
**params
should contain at least one of the following parameters:time_to_maturity
- Parameters
pricer (callable) – Pricing formula of a derivative.
create_graph (bool, default=False) – If
True
, graph of the derivative will be constructed, allowing to compute higher order derivative products.**params – Parameters passed to
pricer
.
- Returns
torch.Tensor
Examples
Theta of a European option can be evaluated as follows.
>>> import pfhedge.autogreek as autogreek >>> from pfhedge.nn import BSEuropeanOption ... >>> pricer = BSEuropeanOption().price >>> autogreek.theta( ... pricer, ... log_moneyness=torch.zeros(3), ... time_to_maturity=torch.tensor([0.1, 0.2, 0.3]), ... volatility=torch.tensor([0.20, 0.20, 0.20]), ... ) tensor([-0.1261, -0.0891, -0.0727])