CIRRate¶
- class pfhedge.instruments.CIRRate(kappa=1.0, theta=0.04, sigma=0.2, cost=0.0, dt=0.004, dtype=None, device=None)[source]¶
A rate which follow the CIR process.
See also
pfhedge.stochastic.generate_cir()
: The stochastic process.
- Parameters
kappa (float, default=1.0) – The parameter \(\kappa\).
theta (float, default=0.04) – The parameter \(\theta\).
sigma (float, default=2.0) – The parameter \(\sigma\).
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 rate of the instrument. This attribute is set by a methodsimulate()
. The shape is \((N, T)\) where \(N\) is the number of simulated paths and \(T\) is the number of time steps.
Examples
>>> from pfhedge.instruments import CIRRate ... >>> _ = torch.manual_seed(42) >>> rate = CIRRate() >>> rate.simulate(n_paths=2, time_horizon=5/250) >>> rate.spot tensor([[0.0400, 0.0408, 0.0411, 0.0417, 0.0422, 0.0393], [0.0400, 0.0457, 0.0440, 0.0451, 0.0458, 0.0472]])
- 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 rate and add it as a buffer named
spot
.The shape of the spot is \((N, T)\), where \(N\) is the number of simulated paths and \(T\) is the number of time steps. The number of time steps is determinded from
dt
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 \((S(0),)\) where \(S(0)\) is the initial values of of spot. If
None
(default), it uses the default value (Seedefault_init_state
).
- 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