yieldcurveml.interpolatecurve API documentation

# yieldcurveml.interpolatecurve

View Source

    1from .interpolatecurve import CurveInterpolator
    2
    3__all__ = ['CurveInterpolator']

class CurveInterpolator(yieldcurveml.stripcurve.bootstrapcurve.RateCurveBootstrapper, yieldcurveml.stripcurve.stripcurve.CurveStripper): View Source

     27class CurveInterpolator(RateCurveBootstrapper, CurveStripper):
     28    """Yield curve interpolator.
     29
     30    Parameters
     31    ----------
     32    estimator : sklearn estimator, default=None
     33        Scikit-learn estimator to use for fitting. If None, uses Ridge.
     34    lambda1 : float, default=2.5
     35        First lambda parameter for NSS function
     36    lambda2 : float, default=4.5
     37        Second lambda parameter for NSS function
     38    type_regressors : str, default="laguerre"
     39        Type of basis functions, one of "laguerre", "cubic"
     40    """
     41    def __init__(
     42        self,
     43        estimator=None,
     44        lambda1: float = 2.5,
     45        lambda2: float = 4.5,
     46        type_regressors: Optional[Literal["laguerre", "cubic", "kernel"]] = "cubic",
     47        kernel_type: Optional[Literal['matern', 'rbf', 'rationalquadratic', 'smithwilson']] = None,
     48        interpolation: Literal['linear', 'cubic'] = 'linear',
     49        **kwargs
     50    ):
     51        self.estimator = estimator
     52        self.lambda1 = lambda1
     53        self.lambda2 = lambda2
     54        self.type_regressors = type_regressors
     55        self.kernel_type = kernel_type
     56        self.interpolation = interpolation
     57        self.maturities = None
     58        if self.type_regressors != "kernel":
     59            self.kernel_type = None
     60        self.maturities = None
     61        self.swap_rates = None
     62        self.tenor_swaps = None
     63        self.T_UFR = None
     64        self.kernel_params_ = kwargs  # Store kernel parameters
     65        self.coef_ = None
     66        self.cashflows_ = None
     67        self.maturities = None
     68        self.curve_rates_ = None
     69
     70    def fit(
     71        self,
     72        maturities: np.ndarray,
     73        yields: np.ndarray,
     74        T_UFR: Optional[float] = None,
     75        **kwargs
     76    ) -> "CurveInterpolator":
     77        """Fit the curve Interpolator model.
     78
     79        Parameters
     80        ----------
     81        maturities : np.ndarray
     82            Maturities of the swap rates
     83        yields: np.ndarray
     84            Yields to interpolate
     85        T_UFR : float, default=None
     86            UFR to use for the Smith-Wilson method
     87
     88        Returns
     89        -------
     90        self : CurveInterpolator
     91            Fitted curve Interpolator model
     92        """
     93        assert len(maturities) == len(yields), "Maturities and yields must have the same length"
     94        self.maturities = np.asarray(maturities)
     95        self.yields = np.asarray(yields)
     96        self.T_UFR = T_UFR
     97        # Store inputs
     98        self.rates_ = YieldsContainer(maturities=self.maturities, yields=self.yields)
     99        # Calculate discount factors from actual yields
    100        V = np.exp(-self.maturities * self.yields)
    101        # Get basis functions
    102        X = self._get_basis_functions(self.maturities)
    103        if X.ndim == 1:
    104            X = X.reshape(-1, 1)
    105        # Use actual discount factors to calculate target values
    106        y = (1 - V) / self.maturities
    107        # Fit the model
    108        self.estimator.fit(X=X, y=y, **kwargs)
    109        predictions = self.estimator.predict(X)
    110        self.residuals_ = predictions - y
    111        return self
    112
    113    def predict(self, maturities: np.ndarray) -> np.ndarray:
    114        """Predict interpolated rates at given maturities."""
    115        check_is_fitted(self)
    116        X = self._get_basis_functions(maturities)
    117        if X.ndim == 1:
    118            X = X.reshape(-1, 1)
    119        # Model predicts (1-V)/T values
    120        y_pred = self.estimator.predict(X)
    121        # Convert back to spot rates
    122        spot_rates = -np.log(1 - y_pred * maturities) / maturities
    123        discount_factors = np.exp(-maturities * spot_rates)
    124        forward_rates = self._calculate_forward_rates(maturities, spot_rates, discount_factors)
    125
    126        return CurveRates(
    127            maturities=maturities,
    128            spot_rates=spot_rates,
    129            forward_rates=forward_rates,
    130            discount_factors=discount_factors
    131        )

Yield curve interpolator.

## Parameters

estimator : sklearn estimator, default=None Scikit-learn estimator to use for fitting. If None, uses Ridge. lambda1 : float, default=2.5 First lambda parameter for NSS function lambda2 : float, default=4.5 Second lambda parameter for NSS function type_regressors : str, default="laguerre" Type of basis functions, one of "laguerre", "cubic"

def fit( self, maturities: numpy.ndarray, yields: numpy.ndarray, T_UFR: Optional[float] = None, **kwargs) -> CurveInterpolator: View Source

     70    def fit(
     71        self,
     72        maturities: np.ndarray,
     73        yields: np.ndarray,
     74        T_UFR: Optional[float] = None,
     75        **kwargs
     76    ) -> "CurveInterpolator":
     77        """Fit the curve Interpolator model.
     78
     79        Parameters
     80        ----------
     81        maturities : np.ndarray
     82            Maturities of the swap rates
     83        yields: np.ndarray
     84            Yields to interpolate
     85        T_UFR : float, default=None
     86            UFR to use for the Smith-Wilson method
     87
     88        Returns
     89        -------
     90        self : CurveInterpolator
     91            Fitted curve Interpolator model
     92        """
     93        assert len(maturities) == len(yields), "Maturities and yields must have the same length"
     94        self.maturities = np.asarray(maturities)
     95        self.yields = np.asarray(yields)
     96        self.T_UFR = T_UFR
     97        # Store inputs
     98        self.rates_ = YieldsContainer(maturities=self.maturities, yields=self.yields)
     99        # Calculate discount factors from actual yields
    100        V = np.exp(-self.maturities * self.yields)
    101        # Get basis functions
    102        X = self._get_basis_functions(self.maturities)
    103        if X.ndim == 1:
    104            X = X.reshape(-1, 1)
    105        # Use actual discount factors to calculate target values
    106        y = (1 - V) / self.maturities
    107        # Fit the model
    108        self.estimator.fit(X=X, y=y, **kwargs)
    109        predictions = self.estimator.predict(X)
    110        self.residuals_ = predictions - y
    111        return self

Fit the curve Interpolator model.

## Parameters

maturities : np.ndarray Maturities of the swap rates yields: np.ndarray Yields to interpolate T_UFR : float, default=None UFR to use for the Smith-Wilson method

## Returns

self : CurveInterpolator Fitted curve Interpolator model

def predict(self, maturities: numpy.ndarray) -> numpy.ndarray: View Source

    113    def predict(self, maturities: np.ndarray) -> np.ndarray:
    114        """Predict interpolated rates at given maturities."""
    115        check_is_fitted(self)
    116        X = self._get_basis_functions(maturities)
    117        if X.ndim == 1:
    118            X = X.reshape(-1, 1)
    119        # Model predicts (1-V)/T values
    120        y_pred = self.estimator.predict(X)
    121        # Convert back to spot rates
    122        spot_rates = -np.log(1 - y_pred * maturities) / maturities
    123        discount_factors = np.exp(-maturities * spot_rates)
    124        forward_rates = self._calculate_forward_rates(maturities, spot_rates, discount_factors)
    125
    126        return CurveRates(
    127            maturities=maturities,
    128            spot_rates=spot_rates,
    129            forward_rates=forward_rates,
    130            discount_factors=discount_factors
    131        )

Predict interpolated rates at given maturities.