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:
spot (
torch.Tensor
): The spot price of the instrument.
- dtype¶
The dtype with which the simulated time-series are represented.
- Type
- device¶
The device where the simulated time-series are.
- Type
- 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.
- 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)
wheren_steps
is the minimum integer that satisfiesn_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 (Seedefault_init_state
).
- to(*args, **kwargs)[source]¶
Moves and/or casts the buffers of the instrument.
This can be called as
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
- 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