BaseDerivative

class pfhedge.instruments.BaseDerivative[source]

Base class for all derivatives.

A derivative is a financial instrument whose payoff is contingent on a primary instrument (or a set of primary instruments). A (over-the-counter) derivative is not traded on the market and therefore the price is not directly accessible. Examples include options and swaps.

A derivative relies on primary assets (See BasePrimary for details), such as stocks, bonds, commodities, and currencies.

underlier

The underlying asset on which the derivative’s payoff relies.

Type

BasePrimary

dtype

The dtype with which the simulated time-series are represented.

Type

torch.dtype

device

The device where the simulated time-series are.

Type

torch.device

add_clause(name, clause)[source]

Adds a clause to the derivative.

The clause will be called after payoff_fn() method has computed the payoff and modify the payoff tensor. It should have the following signature:

clause(derivative, payoff) -> modified payoff
Parameters
cpu()

Moves all buffers of this instrument and its underlier to the CPU.

Note

This method modifies the instrument in-place.

Returns

self

cuda(device=None)

Moves all buffers of this instrument and its underlier to the GPU.

Note

This method modifies the instrument in-place.

Parameters

device (int, optional) – If specified, all buffers will be copied to that device.

Returns

self

delist()[source]

Make self a delisted derivative.

After this method self will be a private derivative.

double()

Casts all floating point parameters and buffers to torch.float64 datatype.

Note

This method modifies the instrument in-place.

Returns

self

float()

Casts all floating point parameters and buffers to torch.float32 datatype.

Note

This method modifies the instrument in-place.

Returns

self

half()

Casts all floating point parameters and buffers to torch.float16 datatype.

Note

This method modifies the instrument in-place.

Returns

self

list(pricer, cost=0.0)[source]

Make self a listed derivative.

After this method self will be a exchange-traded derivative which can be transacted at any time with the spot price given by self.spot.

See an example in EuropeanOption for a usage.

Parameters
  • pricer (Callable[[BaseDerivative], Tensor]]) – A function that takes self and returns the spot price tensor of self.

  • cost (float, optional) – The transaction cost rate.

payoff()[source]

Returns the payoff of the derivative.

Shape:
  • Output: \((N)\) where \(N\) stands for the number of simulated paths.

Returns

torch.Tensor

abstract payoff_fn()[source]

Defines the payoff function of the derivative.

This should be overridden by all subclasses.

Note

Although the payoff function needs to be defined within this function, one should use the payoff() method afterwards instead of this since the former takes care of applying the registered clauses (See add_clause()) while the latter silently ignores them.

Shape:
  • Output: \((N)\) where \(N\) stands for the number of simulated paths.

Returns

torch.Tensor

simulate(n_paths=1, init_state=None)[source]

Simulate time series associated with the underlier.

Parameters
  • n_paths (int) – The number of paths to simulate.

  • init_state (tuple[torch.Tensor | float], optional) – The initial state of the underlier.

  • **kwargs – Other parameters passed to self.underlier.simulate().

property spot

Returns self.pricer(self) if self is listed.

See list() for details.

to(*args, **kwargs)[source]

Moves and/or casts the buffers of the instrument.

This can be called as

to(device=None, dtype=None)[source]
to(tensor)[source]
to(instrument)[source]

Its signature is similar to torch.nn.Module.to(). It only accepts floating point dtypes. See Instrument dtype and device for details.

Note

This method modifies the instrument in-place.

See also

  • float(): Cast to torch.float32.

  • double(): Cast to torch.float64.

  • half(): Cast to torch.float16.

  • bfloat16(): Cast to torch.bfloat16.

  • cuda(): Move to CUDA memory.

  • cpu(): Move to CPU memory.

Parameters
  • dtype (torch.dtype) – The desired floating point dtype of the buffers in this instrument.

  • device (torch.device) – The desired device of the buffers in this instrument.

  • tensor (torch.Tensor) – Tensor whose dtype and device are the desired dtype and device of the buffers in this instrument.

  • instrument (BaseInstrument) – Instrument whose dtype and device are the desired dtype and device of the buffers in this instrument.

Returns

self

ul(index=0)[source]

Alias for self.underlier.