BrownianStock¶
- class pfhedge.instruments.BrownianStock(sigma=0.2, mu=0.0, cost=0.0, dt=0.004, dtype=None, device=None)[source]¶
A stock of which spot prices follow the geometric Brownian motion.
See also
pfhedge.stochastic.generate_geometric_brownian()
: The stochastic process.
- Parameters
sigma (float, default=0.2) – The parameter
, which stands for the volatility of the spot price.mu (float, default=0.0) – The parameter
, which stands for the drift of the spot price.cost (float, default=0.0) – The transaction cost rate.
dt (float, default=1/250) – The intervals of the time steps.
dtype (torch.device, optional) – Desired device of returned tensor. Default: If None, uses a global default (see
torch.set_default_tensor_type()
).device (torch.device, optional) – Desired device of returned tensor. Default: if None, uses the current device for the default tensor type (see
torch.set_default_tensor_type()
).device
will be the CPU for CPU tensor types and the current CUDA device for CUDA tensor types.
- Buffers:
spot (
torch.Tensor
): The spot prices of the instrument. This attribute is set by a methodsimulate()
. The shape is where is the number of simulated paths and is the number of time steps.
Examples
>>> from pfhedge.instruments import BrownianStock >>> >>> _ = torch.manual_seed(42) >>> stock = BrownianStock() >>> stock.simulate(n_paths=2, time_horizon=5 / 250) >>> stock.spot tensor([[1.0000, 1.0016, 1.0044, 1.0073, 0.9930, 0.9906], [1.0000, 0.9919, 0.9976, 1.0009, 1.0076, 1.0179]])
Using custom
dtype
anddevice
.>>> stock = BrownianStock() >>> stock.to(dtype=torch.float64, device="cuda:0") BrownianStock(..., dtype=torch.float64, device='cuda:0')
- property default_init_state¶
Returns the default initial state of simulation.
- simulate(n_paths=1, time_horizon=0.08, init_state=None)[source]¶
Simulate the spot price and add it as a buffer named
spot
.The shape of the spot is
, where is the number of simulated paths and is the number of time steps. The number of time steps is determinded fromdt
andtime_horizon
.- Parameters
n_paths (int, default=1) – The number of paths to simulate.
time_horizon (float, default=20/250) – The period of time to simulate the price.
init_state (tuple[torch.Tensor | float], optional) – The initial state of the instrument. This is specified by a tuple
where is the initial value of the spot price. IfNone
(default), it uses the default value (Seedefault_init_state
). It also accepts afloat
or atorch.Tensor
.
Examples
>>> _ = torch.manual_seed(42) >>> stock = BrownianStock() >>> stock.simulate(n_paths=2, time_horizon=5 / 250, init_state=(2.0,)) >>> stock.spot tensor([[2.0000, 2.0031, 2.0089, 2.0146, 1.9860, 1.9812], [2.0000, 1.9838, 1.9952, 2.0018, 2.0153, 2.0358]])
- to(*args, **kwargs)¶
Moves and/or casts the buffers of the instrument.
This can be called as
- to(device=None, dtype=None)¶
- to(tensor)¶
- to(instrument)¶
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 totorch.float32
.double()
: Cast totorch.float64
.half()
: Cast totorch.float16
.bfloat16()
: Cast totorch.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
- property variance¶
Returns the volatility of self.
It is a tensor filled with the square of
self.sigma
.
- property volatility¶
Returns the volatility of self.
It is a tensor filled with
self.sigma
.