BasePrimary

class pfhedge.instruments.BasePrimary[source]

Base class for all primary instruments.

A primary instrument is a basic financial instrument which is traded on a market and therefore the price is accessible as the market price. Examples include stocks, bonds, commodities, and currencies.

Derivatives are issued based on primary instruments (See BaseDerivative for details).

Buffers:
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

buffers()[source]

Returns an iterator over module buffers.

Yields

torch.Tensor – module buffer

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

property default_init_state

Returns the default initial state of simulation.

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

get_buffer(name)[source]

Returns the buffer given by target if it exists, otherwise throws an error.

Parameters

name (str) – the name of the buffer.

Returns

torch.Tensor

half()

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

Note

This method modifies the instrument in-place.

Returns

self

named_buffers()[source]

Returns an iterator over module buffers, yielding both the name of the buffer as well as the buffer itself.

Yields

(string, torch.Tensor) – Tuple containing the name and buffer

register_buffer(name, tensor)[source]

Adds a buffer to the instrument. The dtype and device of the buffer are the instrument’s dtype and device.

Buffers can be accessed as attributes using given names.

Parameters
  • name (str) – name of the buffer. The buffer can be accessed from this module using the given name.

  • tensor (Tensor or None) – buffer to be registered. If None, then operations that run on buffers, such as cuda, are ignored.

abstract simulate(n_paths, time_horizon, init_state=None)[source]

Simulate time series associated with the instrument and add them as buffers.

The shapes of the registered buffers should be (n_paths, n_steps) where n_steps is the minimum integer that satisfies n_steps * self.dt >= time_horizon.

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

  • time_horizon (float) – The period of time to simulate the price.

  • init_state (tuple[torch.Tensor | float], optional) – The initial state of the instrument. If None (default), it uses the default value (See default_init_state).

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