Coverage for rivapy/models/scott_chesney.py: 17%

23 statements  

« prev     ^ index     » next       coverage.py v7.8.2, created at 2025-06-05 14:27 +0000

1import numpy as np 

2 

3class ScottChesneyModel: 

4 def __init__(self, 

5 kappa: float, 

6 theta: float, 

7 alpha: float, 

8 correlation: float, 

9 y0: float, 

10 ): 

11 """ Scott-Chesney Model 

12 Generates a timeseries according to 

13 

14 .. math:: dS = e^y S dW_S 

15 .. math:: dy = `{\kappa}` (`{\theta}`-y)dt `{\alpha}` dW_y 

16 .. math:: E[dW_s\\dot dW_y] = \\rho dt 

17 

18 

19 

20 Args: 

21 kappa (float): speed of mean reversion 

22 theta (float): mean reversion level 

23 alpha (float): vol of (log)vol 

24 correlation (float): correlation between (log)vol and spot 

25 y0: start value (float): (log) vol 

26  

27 """ 

28 self.kappa = kappa 

29 self.theta = theta 

30 self.alpha = alpha 

31 self._correlation =correlation 

32 self.y0 = y0 

33 

34 

35 def apply_mc_step(self, x: np.ndarray, t0: float, t1: float, rnd: np.ndarray, inplace: bool = True, slv: np.ndarray= None): 

36 """Apply a MC-Euler step for the Scott-Chesney Model for n different paths. 

37 

38 Args: 

39 x (np.ndarray): 2-d array containing the start values for the spot and variance. The first column contains the spot, the second the variance values. 

40 t0 ([type]): [description] 

41 t1 ([type]): [description] 

42 rnd ([type]): [description] 

43 slv (np.ndarray): Stochastic local variance (for each path) to be multiplied with the heston variance. This is used by the StochasticVolatilityModel. 

44 """ 

45 if not inplace: 

46 x_ = x.copy() 

47 else: 

48 x_ = x 

49 rnd_corr_S = np.sqrt(1.0-self._correlation**2)*rnd[:,0] + self._correlation*rnd[:,1] 

50 rnd_V = rnd[:,1] 

51 S = x_[:,0] 

52 y = x_[:,1] 

53 dt = t1-t0 

54 sqrt_dt = np.sqrt(dt) 

55 if slv is None: 

56 slv=1.0 

57 

58 S += np.sqrt(slv)*np.exp(y) * S * rnd_corr_S * sqrt_dt 

59 y += self.kappa * (self.theta - y) * dt + self.alpha * rnd_V * sqrt_dt 

60 return x_ 

61 

62