Stochastic Processes
Ornstein-Uhlenbeck
- class rivapy.models.OrnsteinUhlenbeck(speed_of_mean_reversion: float | Callable, volatility: float | Callable, mean_reversion_level: float | Callable = 0)[source]
Bases:
FactoryObject
Ornstein Uhlenbeck stochastic process.
\[dX = \lambda(t) (\theta(t)-X)dt + \sigma(t) dW_t\]where \(\lambda(t)\) is the speed of mean reversion that determines how fast the process returns to the so-called mean reversion level \(\theta(t)\) and \(\sigma\) is the volatility of the process. The higher \(\lambda\), the faster the process return to the mean level, which can be seen in the following figure
- Parameters:
speed_of_mean_reversion (Union[float, Callable]) – The
volatility (Union[float, Callable]) – _description_
mean_reversion_level (Union[float, Callable], optional) – _description_. Defaults to 0.
- apply_mc_step(x: ndarray, t0: float, t1: float, rnd: ndarray, inplace: bool = True, slv: ndarray = None)[source]
- calibrate(data: ndarray, dt: float, method: str = 'maximum_likelihood')[source]
Calibrate the Ornstein Uhlenbeck model with constant parameters to the given data.
- Parameters:
data (np.ndarray) – Array of values the model is fitted to (uniform timegrid is assumed).
dt (float) – Time step size between two datapoints from data (uniform timegrid is assumed.
method (str, optional) – Determines if maximum likelihood (‘maximum_likelihood’) or minimum least square (‘minimum_least_square’) is used for calibration. Defaults to ‘maximum_likelihood’.
- compute_call_price(X0: float | ndarray, K: float, ttm: float)[source]
Computes the price of a call option with strike K and time to maturity ttm for a spot following the Ornstein-Uhlenbeck process.
It works only for constant speed of mean reversion, volatility and mean reversion level and throws a NotImplementedError otherwise. It does not encounter dividends or interest rates.
- Parameters:
X0 (Union[float, np.ndarray]) – Start value of the process.
K (float) – Strike of the call option.
ttm (float) – Time to maturity of the call option.
- Raises:
NotImplementedError – If speed of mean reversion, volatility or mean reversion level are not constant.
- Returns:
Price of the call option.
- Return type:
float
- conditional_probability_density(X_delta_t, delta_t, X0, volatility=None, speed_of_mean_reversion=None, mean_reversion_level=None)[source]
- simulate(timegrid, start_value, rnd)[source]
Simulate the Ornstein Uhlenbeck process on the given timegrid using simple explicit euler scheme:
\[X_{t+\delta t} = X_t + \theta (\mu(t) - X_t )\delta t +\sigma(t) \varepsilon \sqrt{\delta t}\]where \(\varepsilon\) is a (0,1)-normal random variate.
- Args:
timegrid (np.ndarray): One dimensional array containing the time points where the process will be simulated (containing 0.0 as the first timepoint). start_value (Union[float, np.ndarray]): Either a float or an array (for each path) with the start value of the simulation. rnd (np.ndarray): Array of random normal (variance equal to one) variates used within the discretization (:math:`
arepsilon` in the above description). Here, shape[0] equals the number of timestes and shape[1] teh number of simulations.
- Returns:
np.ndarray: Array r containing the simulations where r[:,i] is the path of the i-th simulation (r.shape[0] equals number of timepoints, r.shape[1] the number of simulations).
Lucia-Schwartz
The Lucia-Schwartz two-factor model for power prices is a mathematical model used to explain the behavior of electricity prices. The model assumes that electricity prices are determined by two factors: a short-term factor and a long-term factor. It was introduced in discussed in Lucia and Schwartz[1]. The short-term factor is related to the current supply and demand conditions in the electricity market. This factor is influenced by factors such as weather conditions, maintenance schedules, and unexpected events such as power plant failures. The short-term factor is modeled using a mean-reverting process, which means that prices tend to move back towards their long-term average over time. The long-term factor is related to the overall trends in the economy and the energy market. This factor is influenced by factors such as changes in fuel prices, technological advances, and government policies. The long-term factor is modeled using a random walk process, which means that prices tend to move in a random and unpredictable manner over time. Overall, the Lucia-Schwartz two-factor model provides a useful framework for understanding the complex factors that influence electricity prices. By separating short-term and long-term factors, the model can help energy traders and analysts make more informed decisions about their trading strategies and risk management.
- class rivapy.models.LuciaSchwartz(rho: float, kappa: float | Callable = None, sigma1: float | Callable = None, mu: float | Callable = None, sigma2: float | Callable = None, f: Callable[[float | ndarray], float | ndarray] = None)[source]
Bases:
FactoryObject
Lucia Schwartz two factor model.
The model may be used to simulate spot/forward prices via
\[ \begin{align}\begin{aligned}S(t) = f(t) + X_1(t) + X_2(t)\\dX_1(t) = -\kappa X_1(t)+\sigma_1dW_1(t)\\dX_2(t) = \mu dt + \sigma_2 dW_2\end{aligned}\end{align} \]where \(f(t)\) is a deterministic function, \(\kappa\) the speed of mean reversion for the first process that may be interpreted as the long-term factor and \(\sigma_1\) the respective volatility. The second factor \(X_2\) may be interpreted as a short-term factor that is influenced by \(W_2\) and has drift \(\mu\). \(X_1\) and \(X_2\) may be correlated with correlation \(\rho\). Note that this class just simulates
- Parameters:
kappa (Union[float, Callable]) – The speed of mean reversion for the first factor \(X_1\). Can be either constant or time dependent.
sigma1 (Union[float, Callable]) – The volatility of the first factor \(X_1\). Can be either constant or time dependent.
mu (Union[float, Callable]) – The drift of teh second factor \(X_2\). Can be either constant or time dependent.
sigma2 (Union[float, Callable]) – The volatility of the second factor \(X_2\). Can be either constant or time dependent.
rho (float) – Correlation between X1 and X2.
f (Union[float, Callable], optional) – Deterministic function of time. Defaults to 0.
- compute_expected_value(x0: ndarray, T: float | ndarray) float | ndarray [source]
Computes the expected value of the model.
- Parameters:
x0 (Union[float, np.ndarray]) – Start value, either onedimensional (containing just the start value for X0 or X1) or twodimensional (different start values for X0 and X1 from e.g. a MC simulation).
T (Union[float, np.ndarray]) – Terminal time.
- Returns:
Expected value.
- Return type:
Union[float, np.ndarray]
- compute_fwd_value(x0: ndarray, T1: float, T2: float, qm: Callable[[Callable, float], float] = None, **qm_kwargs) float [source]
Compute the forward value for a forward (swap) with continuos delivery between two time points.
In more detail, the forward value is computed as
\[F(t,t_1,t_2) = \frac{E_t[\int_{T_1}^{T_2} F(t,s) ds]}{T_2-T_1}\]where \(F(t,s)\) is the expected spot price at time \(T\). The expectation is taken under the risk neutral measure \(Q^M\).
- Parameters:
T1 (float) – Start point of period.
T2 (float) – End point of period. If None, the expected value of the spot price \(F(t,T_1)\) at T1 is returned.
qm (Callable[[Callable,float,float]], optional) – Quadrature method used for the integral. If None, scipy.integrate.romberg will be used. Defaults to None.
**qm_kwargs – Keyword arguments for the quadrature method.
- Returns:
Forward value.
- Return type:
float
- simulate(timegrid, start_value, rnd, forwards: List[Tuple[float, float]] = None, forward_start_values: ndarray = None)[source]
Simulates the model.
- Parameters:
timegrid (np.ndarray) – Timegrid for simulation.
start_value (Union[float, np.ndarray]) – Start value for simulation.
rnd (np.ndarray) – Random numbers for simulation.
forwards (List[Tuple[float,float]], optional) – Forwards for simulation. Defaults to None.
forward_start_values (np.ndarray, optional) – Initial values for forwards. Defaults to None. If forwards is specified and this argument is None, the initial values will be computed with the method compute_fwd_value.