1-
2- from math import exp , log
1+ from math import log
32import numpy as np
3+ from typing import Union , List
4+
45
5- def calculate_prices (rates : np .ndarray , t : np .ndarray ) -> np .ndarray :
6+ def calculate_prices (rates : Union [ np .ndarray , List [ float ]], t : Union [ np .ndarray , List [ float ]] ) -> np .ndarray :
67 """Calculate prices from zero-coupon rates
78
89 Args:
@@ -13,10 +14,14 @@ def calculate_prices(rates: np.ndarray, t: np.ndarray) -> np.ndarray:
1314 Prices as vector of length n
1415 """
1516
17+ # Convert list into numpy array
18+ rates = np .array (rates )
19+ t = np .array (t )
20+
1621 return np .power (1 + rates , - t )
1722
1823
19- def ufr_discount_factor (ufr : float , t : np .ndarray ) -> np .ndarray :
24+ def ufr_discount_factor (ufr : float , t : Union [ np .ndarray , List [ float ]] ) -> np .ndarray :
2025 """Calculate Ultimate Forward Rate (UFR) discount factors.
2126
2227 Takes the UFR with a vector of maturities and returns for each of the
@@ -37,10 +42,14 @@ def ufr_discount_factor(ufr: float, t: np.ndarray) -> np.ndarray:
3742
3843 # Convert annualized ultimate forward rate to log-return
3944 ufr = log (1 + ufr )
45+
46+ # Convert list into numpy array
47+ t = np .array (t )
48+
4049 return np .exp (- ufr * t )
4150
4251
43- def wilson_function (t1 : np .ndarray , t2 : np .ndarray , alpha : float , ufr : float ) -> np .ndarray :
52+ def wilson_function (t1 : Union [ np .ndarray , List [ float ]], t2 : Union [ np .ndarray , List [ float ]] , alpha : float , ufr : float ) -> np .ndarray :
4453 """Calculate matrix of Wilson functions
4554
4655 The Smith-Wilson method requires the calculation of a series of Wilson
@@ -85,7 +94,7 @@ def wilson_function(t1: np.ndarray, t2: np.ndarray, alpha: float, ufr: float) ->
8594 return W
8695
8796
88- def fit_parameters (rates : np .ndarray , t : np .ndarray , alpha : float , ufr : float ) -> np .ndarray :
97+ def fit_parameters (rates : Union [ np .ndarray , List [ float ]], t : Union [ np .ndarray , List [ float ]] , alpha : float , ufr : float ) -> np .ndarray :
8998 """Calculate Smith-Wilson parameter vector ζ
9099
91100 Given the Wilson-matrix, vector of discount factors and prices,
@@ -115,13 +124,13 @@ def fit_parameters(rates: np.ndarray, t: np.ndarray, alpha: float, ufr: float) -
115124 # Calculate vector of parameters (p. 17)
116125 # To invert the Wilson-matrix, conversion to type matrix is required
117126 zeta = np .matrix (W ).I * (mu - P )
118- zeta = np .array (zeta ) # Convert back to more general array type
127+ zeta = np .array (zeta ) # Convert back to more general array type
119128
120129 return zeta
121130
122131
123- def fit_smithwilson_rates (rates_obs : np .ndarray , t_obs : np .ndarray , t_target : np . ndarray ,
124- alpha : float , ufr : float ) -> np .ndarray :
132+ def fit_smithwilson_rates (rates_obs : Union [ np .ndarray , List [ float ]], t_obs : Union [ np .ndarray , List [ float ]] ,
133+ t_target : Union [ np . ndarray , List [ float ]], alpha : float , ufr : float ) -> np .ndarray :
125134 """Calculate zero-coupon yields with Smith-Wilson method based on observed rates.
126135
127136 This function expects the rates and initial maturity vector to be
@@ -170,7 +179,7 @@ def fit_smithwilson_rates(rates_obs: np.ndarray, t_obs: np.ndarray, t_target: np
170179
171180 # Price vector - equivalent to discounting with zero-coupon yields 1/(1 + r)^t
172181 # for prices where t_obs = t_target. All other matuirites are interpolated or extrapolated
173- P = ufr_disc - W @ zeta # '@' in numpy is the dot product of two matrices
182+ P = ufr_disc - W @ zeta # '@' in numpy is the dot product of two matrices
174183
175184 # Transform price vector to zero-coupon rate vector (1/P)^(1/t) - 1
176- return np .power (1 / P , 1 / t_target ) - 1
185+ return np .power (1 / P , 1 / t_target ) - 1
0 commit comments