Skip to content

Commit 190e95d

Browse files
typing hints
1 parent 9b1cc26 commit 190e95d

File tree

4 files changed

+50
-45
lines changed

4 files changed

+50
-45
lines changed

bisection_alpha/SWCalibrate.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
def SWCalibrate(r, M, ufr, alpha):
1+
import numpy as np
2+
3+
def SWCalibrate(r: np.ndarray, M: np.ndarray, ufr: float, alpha: float)-> np.ndarray:
24
"""
35
Calculate the calibration vector using the Smith-Wilson algorithm.
46
@@ -16,7 +18,7 @@ def SWCalibrate(r, M, ufr, alpha):
1618
For more information, refer to the documentation at:
1719
https://www.eiopa.europa.eu/sites/default/files/risk_free_interest_rate/12092019-technical_documentation.pdf
1820
"""
19-
import numpy as np
21+
2022
from SWHeart import SWHeart as SWHeart
2123

2224

bisection_alpha/SWExtrapolate.py

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,32 @@
1-
def SWExtrapolate(M_Target, M_Obs, b, ufr, alpha):
1+
import numpy as np
2+
3+
def SWExtrapolate(M_Target: np.ndarray, M_Obs: np.ndarray, b: np.ndarray, ufr: float, alpha:float)->np.ndarray:
24
"""
3-
Interpolate or extrapolate rates for targeted maturities using the Smith-Wilson algorithm.
5+
Interpolate or extrapolate rates for targeted maturities using the Smith-Wilson algorithm.
6+
7+
Calculates the rates for maturities specified in `M_Target` using the calibration vector `b` obtained
8+
from observed bond maturities in `M_Obs`.
49
5-
Calculates the rates for maturities specified in `M_Target` using the calibration vector `b` obtained
6-
from observed bond maturities in `M_Obs`.
10+
Arguments:
11+
M_Target: k x 1 ndarray representing each targeted bond maturity of interest. Example: M_Target = np.array([[1], [2], [3], [5]])
12+
M_Obs: n x 1 ndarray representing the observed bond maturities used for calibrating the calibration vector `b`. Example: M_Obs = np.array([[1], [3]])
13+
b: n x 1 ndarray representing the calibration vector calculated on observed bonds.
14+
ufr: Floating number representing the ultimate forward rate. Example: ufr = 0.042
15+
alpha: Floating number representing the convergence speed parameter alpha. Example: alpha = 0.05
716
8-
Arguments:
9-
M_Target: k x 1 ndarray representing each targeted bond maturity of interest. Example: M_Target = np.array([[1], [2], [3], [5]])
10-
M_Obs: n x 1 ndarray representing the observed bond maturities used for calibrating the calibration vector `b`. Example: M_Obs = np.array([[1], [3]])
11-
b: n x 1 ndarray representing the calibration vector calculated on observed bonds.
12-
ufr: Floating number representing the ultimate forward rate. Example: ufr = 0.042
13-
alpha: Floating number representing the convergence speed parameter alpha. Example: alpha = 0.05
17+
Returns:
18+
k x 1 ndarray representing the targeted rates for zero-coupon bonds. Each rate belongs to a targeted
19+
zero-coupon bond with a maturity from `M_Target`. Example: r = np.array([0.0024, 0.0029, 0.0034, 0.0039])
20+
21+
For more information, refer to the documentation at:
22+
https://www.eiopa.europa.eu/sites/default/files/risk_free_interest_rate/12092019-technical_documentation.pdf
23+
"""
1424

15-
Returns:
16-
k x 1 ndarray representing the targeted rates for zero-coupon bonds. Each rate belongs to a targeted
17-
zero-coupon bond with a maturity from `M_Target`. Example: r = np.array([0.0024, 0.0029, 0.0034, 0.0039])
25+
from SWHeart import SWHeart as SWHeart
1826

19-
For more information, refer to the documentation at:
20-
https://www.eiopa.europa.eu/sites/default/files/risk_free_interest_rate/12092019-technical_documentation.pdf
21-
"""
22-
23-
import numpy as np
24-
from SWHeart import SWHeart as SWHeart
25-
C = np.identity(M_Obs.size)
26-
d = np.exp(-np.log(1+ufr) * M_Obs) # Calculate vector d described in paragraph 138
27-
Q = np.diag(d) @ C # Matrix Q described in paragraph 139
28-
H = SWHeart(M_Target, M_Obs, alpha) # Heart of the Wilson function from paragraph 132
29-
p = np.exp(-np.log(1+ufr)* M_Target) + np.diag(np.exp(-np.log(1+ufr) * M_Target)) @ H @ Q @ b # Discount pricing function for targeted maturities from paragraph 147
30-
return p ** (-1/ M_Target) -1 # Convert obtained prices to rates and return prices
27+
C = np.identity(M_Obs.size)
28+
d = np.exp(-np.log(1+ufr) * M_Obs) # Calculate vector d described in paragraph 138
29+
Q = np.diag(d) @ C # Matrix Q described in paragraph 139
30+
H = SWHeart(M_Target, M_Obs, alpha) # Heart of the Wilson function from paragraph 132
31+
p = np.exp(-np.log(1+ufr)* M_Target) + np.diag(np.exp(-np.log(1+ufr) * M_Target)) @ H @ Q @ b # Discount pricing function for targeted maturities from paragraph 147
32+
return p ** (-1/ M_Target) -1 # Convert obtained prices to rates and return prices

bisection_alpha/SWHeart.py

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,25 @@
1-
def SWHeart(u, v, alpha):
1+
import numpy as np
2+
3+
def SWHeart(u: np.ndarray, v: np.ndarray, alpha: float)->np.ndarray:
24
"""
3-
Calculate the heart of the Wilson function.
5+
Calculate the heart of the Wilson function.
46
5-
Calculates the matrix H (Heart of the Wilson function) for maturities specified by vectors u and v.
6-
The formula is taken from the EIOPA technical specifications paragraph 132.
7+
Calculates the matrix H (Heart of the Wilson function) for maturities specified by vectors u and v.
8+
The formula is taken from the EIOPA technical specifications paragraph 132.
79
8-
Arguments:
9-
u: n_1 x 1 vector of maturities. Example: u = [1, 3]
10-
v: n_2 x 1 vector of maturities. Example: v = [1, 2, 3, 5]
11-
alpha: 1 x 1 floating number representing the convergence speed parameter alpha. Example: alpha = 0.05
10+
Arguments:
11+
u: n_1 x 1 vector of maturities. Example: u = [1, 3]
12+
v: n_2 x 1 vector of maturities. Example: v = [1, 2, 3, 5]
13+
alpha: 1 x 1 floating number representing the convergence speed parameter alpha. Example: alpha = 0.05
1214
13-
Returns:
14-
n_1 x n_2 matrix representing the Heart of the Wilson function for selected maturities and parameter alpha.
15-
H is calculated as described in paragraph 132 of the EIOPA documentation.
15+
Returns:
16+
n_1 x n_2 matrix representing the Heart of the Wilson function for selected maturities and parameter alpha.
17+
H is calculated as described in paragraph 132 of the EIOPA documentation.
1618
17-
For more information, see:
18-
https://www.eiopa.europa.eu/sites/default/files/risk_free_interest_rate/12092019-technical_documentation.pdf
19-
"""
20-
import numpy as np
21-
19+
For more information, see:
20+
https://www.eiopa.europa.eu/sites/default/files/risk_free_interest_rate/12092019-technical_documentation.pdf
21+
"""
22+
2223
u_Mat = np.tile(u, [v.size, 1]).transpose()
2324
v_Mat = np.tile(v, [u.size, 1])
2425
return 0.5 * (alpha * (u_Mat + v_Mat) + np.exp(-alpha * (u_Mat + v_Mat)) - alpha * np.absolute(u_Mat-v_Mat) - np.exp(-alpha * np.absolute(u_Mat-v_Mat))); # Heart of the Wilson function from paragraph 132

bisection_alpha/bisection_alpha.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from SWCalibrate import SWCalibrate as SWCalibrate
33
from SWExtrapolate import SWExtrapolate as SWExtrapolate
44

5-
def Galfa(M_Obs: np.ndarray, r_Obs: np.ndarray, ufr, alpha, Tau):
5+
def Galfa(M_Obs: np.ndarray, r_Obs: np.ndarray, ufr: float, alpha: float, Tau: float)->float:
66
"""
77
Calculates the gap at the convergence point between the allowable tolerance Tau and the curve extrapolated using the Smith-Wilson algorithm.
88
interpolation and extrapolation of rates.
@@ -44,7 +44,7 @@ def Galfa(M_Obs: np.ndarray, r_Obs: np.ndarray, ufr, alpha, Tau):
4444
K = (1+alpha * M_Obs @ Q@ b) / (np.sinh(alpha * M_Obs.transpose())@ Q@ b) # Calculate kappa as defined in the paragraph 155
4545
return( alpha/np.abs(1 - K*np.exp(alpha*T))-Tau) # Size of the gap at the convergence point between the allowable tolerance Tau and the actual curve. Defined in paragraph 158
4646

47-
def BisectionAlpha(xStart, xEnd, M_Obs, r_Obs, ufr, Tau, Precision, maxIter):
47+
def BisectionAlpha(xStart: float, xEnd: float, M_Obs: np.ndarray, r_Obs: np.ndarray, ufr: float, Tau: float, Precision: float, maxIter: int)->float:
4848
"""
4949
Bisection root finding algorithm for finding the root of a function. The function here is the allowed difference between the ultimate forward rate and the extrapolated curve using Smith & Wilson.
5050

0 commit comments

Comments
 (0)