BSEuropeanOption

class pfhedge.nn.BSEuropeanOption(call=True, strike=1.0, derivative=None)[source]

Black-Scholes formula for a European option.

Note

Risk-free rate is set to zero.

See also

References

  • John C. Hull, 2003. Options futures and other derivatives. Pearson.

Parameters
  • call (bool, default=True) – Specifies whether the option is call or put.

  • strike (float, default=1.0) – The strike price of the option.

Shape:
  • Input: \((N, *, 3)\) where \(*\) means any number of additional dimensions. See inputs() for the names of input features.

  • Output: \((N, *, 1)\). All but the last dimension are the same shape as the input.

Examples

>>> import torch
>>> from pfhedge.nn import BSEuropeanOption
>>>
>>> m = BSEuropeanOption()
>>> m.inputs()
['log_moneyness', 'time_to_maturity', 'volatility']
>>> input = torch.tensor([
...     [-0.01, 0.1, 0.2],
...     [ 0.00, 0.1, 0.2],
...     [ 0.01, 0.1, 0.2]])
>>> m(input)
tensor([[0.4497],
        [0.5126],
        [0.5752]])
delta(log_moneyness=None, time_to_maturity=None, volatility=None)[source]

Returns delta of the derivative.

Parameters
  • log_moneyness (torch.Tensor, optional) – Log moneyness of the underlying asset.

  • time_to_maturity (torch.Tensor, optional) – Time to expiry of the option.

  • volatility (torch.Tensor, optional) – Volatility of the underlying asset.

Shape:
  • log_moneyness: \((N, *)\) where \(*\) means any number of additional dimensions.

  • time_to_maturity: \((N, *)\)

  • volatility: \((N, *)\)

  • output: \((N, *)\)

Returns

torch.Tensor

Note

Parameters are not optional if the module has not accepted a derivative in its initialization.

forward(input)

Returns delta of the derivative.

Parameters

input (torch.Tensor) – The input tensor. Features are concatenated along the last dimension. See inputs() for the names of the input features.

Returns

torch.Tensor

classmethod from_derivative(derivative)[source]

Initialize a module from a derivative.

Parameters

derivative (pfhedge.instruments.EuropeanOption) – The derivative to get the Black-Scholes formula.

Returns

BSEuropeanOption

Examples

>>> from pfhedge.instruments import BrownianStock
>>> from pfhedge.instruments import EuropeanOption
>>>
>>> derivative = EuropeanOption(BrownianStock(), call=False)
>>> m = BSEuropeanOption.from_derivative(derivative)
>>> m
BSEuropeanOption(call=False, strike=1.)
gamma(log_moneyness=None, time_to_maturity=None, volatility=None)[source]

Returns gamma of the derivative.

Parameters
  • log_moneyness (torch.Tensor, optional) – Log moneyness of the underlying asset.

  • time_to_maturity (torch.Tensor, optional) – Time to expiry of the option.

  • volatility (torch.Tensor, optional) – Volatility of the underlying asset.

Shape:
  • log_moneyness: \((N, *)\) where \(*\) means any number of additional dimensions.

  • time_to_maturity: \((N, *)\)

  • volatility: \((N, *)\)

  • output: \((N, *)\)

Returns

torch.Tensor

Note

args are not optional if it doesn’t accept derivative in this initialization.

implied_volatility(log_moneyness=None, time_to_maturity=None, price=None, precision=1e-06)[source]

Returns implied volatility of the derivative.

Parameters
  • log_moneyness (torch.Tensor, optional) – Log moneyness of the underlying asset.

  • time_to_maturity (torch.Tensor, optional) – Time to expiry of the option.

  • price (torch.Tensor) – Price of the derivative.

  • precision (float) – Computational precision of the implied volatility.

Shape:
  • log_moneyness: \((N, *)\) where \(*\) means any number of additional dimensions.

  • time_to_maturity: \((N, *)\)

  • price: \((N, *)\)

  • output: \((N, *)\)

Returns

torch.Tensor

Note

args are not optional if it doesn’t accept derivative in this initialization. price seems optional in typing, but it isn’t. It is set for the compatibility to the previous versions.

inputs()

Returns the names of input features.

Returns

list

price(log_moneyness=None, time_to_maturity=None, volatility=None)[source]

Returns price of the derivative.

Parameters
  • log_moneyness (torch.Tensor, optional) – Log moneyness of the underlying asset.

  • time_to_maturity (torch.Tensor, optional) – Time to expiry of the option.

  • volatility (torch.Tensor, optional) – Volatility of the underlying asset.

Shape:
  • log_moneyness: \((N, *)\) where \(*\) means any number of additional dimensions.

  • time_to_maturity: \((N, *)\)

  • volatility: \((N, *)\)

  • output: \((N, *)\)

Returns

torch.Tensor

Note

args are not optional if it doesn’t accept derivative in this initialization.

theta(log_moneyness=None, time_to_maturity=None, volatility=None)[source]

Returns theta of the derivative.

Parameters
  • log_moneyness (torch.Tensor, optional) – Log moneyness of the underlying asset.

  • time_to_maturity (torch.Tensor, optional) – Time to expiry of the option.

  • volatility (torch.Tensor, optional) – Volatility of the underlying asset.

Shape:
  • log_moneyness: \((N, *)\) where \(*\) means any number of additional dimensions.

  • time_to_maturity: \((N, *)\)

  • volatility: \((N, *)\)

  • output: \((N, *)\)

Note

Risk-free rate is set to zero.

Returns

torch.Tensor

Note

args are not optional if it doesn’t accept derivative in this initialization.

vega(log_moneyness=None, time_to_maturity=None, volatility=None)[source]

Returns vega of the derivative.

Parameters
  • log_moneyness (torch.Tensor, optional) – Log moneyness of the underlying asset.

  • time_to_maturity (torch.Tensor, optional) – Time to expiry of the option.

  • volatility (torch.Tensor, optional) – Volatility of the underlying asset.

Shape:
  • log_moneyness: \((N, *)\) where \(*\) means any number of additional dimensions.

  • time_to_maturity: \((N, *)\)

  • volatility: \((N, *)\)

  • output: \((N, *)\)

Returns

torch.Tensor

Note

args are not optional if it doesn’t accept derivative in this initialization.