robustfpm.finance.derivatives module
This submodule implements IOption interface class, European, American, and Bermudan styled option classes.
Also provides option-generating method make_option() for seamless construction of different options.
- class robustfpm.finance.derivatives.IOption(expiry, payoff_fcn)
Bases:
abc.ABCAn abstract interface for Options
- abstract payoff(prices, t=None)
Get the value of payoff function for prices and time t
- Parameters
prices (array_like, size = (m,n) or (n,)) – Asset prices (or a set of prices), each price — a point in \(\mathbb{R}^{n}\)
t (int) – Current time
- Return type
np.ndarray
- abstract property expiry
Expiration date
- class robustfpm.finance.derivatives.EuropeanOption(expiry: int, payoff_fcn: Callable)
Bases:
robustfpm.finance.derivatives.IOptionGeneric european-style option class
- Parameters
expiry (int) – An expiration date
payoff_fcn (Callable) – Payoff function for given price(s)
- payoff(prices, t=None)
Get the value of payoff function for prices and time t
- Parameters
prices (array_like, size = (m,n) or (n,)) – Asset prices (or a set of prices), each price — a point in \(\mathbb{R}^{n}\)
t (int) – Current time
- Return type
np.ndarray
- property expiry
Expiration date
- class robustfpm.finance.derivatives.AmericanOption(payoff_fcn: Callable, expiry=None)
Bases:
robustfpm.finance.derivatives.IOptionGeneric american-style option class
- Parameters
payoff_fcn (Callable) – Payoff function for given price(s)
expiry (int, optional) – Expiration date
- payoff(prices, t=None)
Get the value of payoff function for prices and time t
- Parameters
prices (array_like, size = (m,n) or (n,)) – Asset prices (or a set of prices), each price — a point in \(\mathbb{R}^{n}\)
t (int) – Current time
- Return type
np.ndarray
- property expiry
Expiration date
- class robustfpm.finance.derivatives.BermudanOption(payoff_dates, payoff_fcn: Callable)
Bases:
robustfpm.finance.derivatives.IOptionBermudan and Canary-styled option class
- Parameters
payoff_dates (array_like) – Payoff dates (as integers)
payoff_fcn (Callable) – Payoff function for given price(s)
Notes
Bermudan and Canary options are very much alike, so we put both styles in the same class.
- payoff(prices, t=None)
Get the value of payoff function for prices and time t
- Parameters
prices (array_like, size = (m,n) or (n,)) – Asset prices (or a set of prices), each price — a point in \(\mathbb{R}^{n}\)
t (int) – Current time
- Return type
np.ndarray
- property expiry
Expiration date
- robustfpm.finance.derivatives.make_option(option_type=None, strike=None, payoff_fcn=None, payoff_dates=None)
Create a new option
You must either give a specific type and strike or provide an option payoff_fcn.
If type and strike are given, payoff_fcn is ignored.
If type is given, but strike is omitted, strike defaults to zero.
If no type is given, but strike is provided, then payoff_fcn must be a wrapper, which accepts strike keyword argument and returns a payoff function (Callable) for a given strike.
If both type and strike are omitted, payoff_fcn must be a function that accepts 1 positional argument.
- Parameters
option_type (str, optional) – A predefined type of option to be constructed.
strike (float, optional) – Strike price.
payoff_fcn (Callable, optional) – Payoff function of an option to be constructed.
payoff_dates (int or list or tuple or np.ndarray, optional) – Expiration date(s). If has only 1 element (or is int), the European option is constructed. If given an array of payoff times — Bermudan. If ommited — American.
- Returns
The constructed option
- Return type
Examples
Create a simple European call option with strike 10 and expiry 4 and evaluate its payoff at diffent points in time.
>>> from robustfpm.finance import * >>> import numpy as np >>> def call_10(x): ... return np.array(np.maximum((x - 10), np.zeros_like(x)), float).squeeze() >>> call = make_option(payoff_fcn = call_10, payoff_dates=4) >>> isinstance(call, IOption) True >>> isinstance(call, EuropeanOption) True >>> isinstance(call, BermudanOption) False >>> call.payoff(9, 3) array([-inf]) >>> call.payoff(11, 3) array([-inf]) >>> call.payoff([[9],[11]], 4) array([0., 1.]) >>> call.payoff([9, 11], 4) array([0., 1.])
Do the same with wrapper-function
>>> from robustfpm.finance import * >>> import numpy as np >>> def call_payoff(strike): ... def call_with_strike(x): ... return np.array(np.maximum((x - strike), np.zeros_like(x)), float).squeeze() ... return call_with_strike >>> call = make_option(payoff_fcn = call_payoff, strike=10, payoff_dates=4) >>> call.payoff([[9],[11]], 4) array([0., 1.]) >>> call_5 = make_option(payoff_fcn = call_payoff, strike=5, payoff_dates=4) >>> call_5.payoff([4,6,10], 4) array([0., 1., 5.])
Alternatively, create the same option as call on max with 1 asset.
>>> from robustfpm.finance import * >>> call = make_option(option_type='callonmax',strike=10, payoff_dates=4) >>> isinstance(call, IOption) True >>> isinstance(call, EuropeanOption) True >>> call.payoff(11,4) array([1.]) >>> call.payoff([[9],[11]], 4) array([0., 1.]) >>> call.payoff([9, 11], 4) array([1.]) #behaves differently since it's actually a call on max
In the same manner, create a Bermudan and American put2call1 option.
>>> from robustfpm.finance import * >>> Option1 = make_option(option_type='put2call1',payoff_dates=[1,3,5]) >>> Option2 = make_option(option_type='put2call1') >>> isinstance(Option1, BermudanOption) True >>> isinstance(Option2, AmericanOption) True >>> Option1.payoff([[1,2],[5,3], [3,5]], 3) array([0., 2., 0.]) >>> Option2.payoff([[1,2],[5,3], [3,5]], 3) array([0., 2., 0.]) >>> Option1.payoff([[1,2],[5,3], [3,5]], 4) array([-inf, -inf, -inf]) >>> Option2.payoff([[1,2],[5,3], [3,5]], 4) array([0., 2., 0.])