Yield Curve Construction

torchquantlib.utils.yield_curve_construction.bootstrap_yield_curve(cash_flows: Tensor, prices: Tensor) Tensor[source]

Construct a yield curve using the bootstrap method.

The bootstrap method iteratively calculates yields for each maturity, using the previously calculated yields for shorter maturities.

Parameters:
  • cash_flows (Tensor) – A 2D tensor where each row represents the cash flows for an instrument.

  • prices (Tensor) – A 1D tensor of market prices for each instrument.

Returns:

A 1D tensor of yields corresponding to each maturity.

Return type:

Tensor

Note

This method assumes that the cash flows and prices are sorted by maturity. It also assumes a simple structure where each instrument has a single cash flow at maturity.

torchquantlib.utils.yield_curve_construction.nelson_siegel_yield_curve(tau: Tensor, beta0: Tensor, beta1: Tensor, beta2: Tensor) Tensor[source]

Construct a yield curve using the Nelson-Siegel model.

The Nelson-Siegel model is a parametric method for modeling the yield curve, using four parameters to generate a smooth and flexible curve shape.

Parameters:
  • tau (Tensor) – A 1D tensor of time to maturities.

  • beta0 (Tensor) – Long-term interest rate level parameter.

  • beta1 (Tensor) – Short-term interest rate parameter.

  • beta2 (Tensor) – Medium-term interest rate parameter.

Returns:

A 1D tensor of yields corresponding to each maturity in tau.

Return type:

Tensor

Note

This implementation uses a simplified version of the Nelson-Siegel model where the decay parameter lambda is assumed to be 1.

Bootstrap Yield Curve

torchquantlib.utils.yield_curve_construction.bootstrap_yield_curve(cash_flows: Tensor, prices: Tensor) Tensor[source]

Construct a yield curve using the bootstrap method.

The bootstrap method iteratively calculates yields for each maturity, using the previously calculated yields for shorter maturities.

Parameters:
  • cash_flows (Tensor) – A 2D tensor where each row represents the cash flows for an instrument.

  • prices (Tensor) – A 1D tensor of market prices for each instrument.

Returns:

A 1D tensor of yields corresponding to each maturity.

Return type:

Tensor

Note

This method assumes that the cash flows and prices are sorted by maturity. It also assumes a simple structure where each instrument has a single cash flow at maturity.

Usage Example

import torch
from torchquantlib.utils.yield_curve_construction import bootstrap_yield_curve

# Example cash flows for 3 instruments with different maturities
cash_flows = torch.tensor([
    [100, 0, 0],
    [0, 100, 0],
    [0, 0, 100]
])

# Corresponding market prices
prices = torch.tensor([98, 95, 90])

# Calculate yields
yields = bootstrap_yield_curve(cash_flows, prices)
print(yields)

Nelson-Siegel Yield Curve

torchquantlib.utils.yield_curve_construction.nelson_siegel_yield_curve(tau: Tensor, beta0: Tensor, beta1: Tensor, beta2: Tensor) Tensor[source]

Construct a yield curve using the Nelson-Siegel model.

The Nelson-Siegel model is a parametric method for modeling the yield curve, using four parameters to generate a smooth and flexible curve shape.

Parameters:
  • tau (Tensor) – A 1D tensor of time to maturities.

  • beta0 (Tensor) – Long-term interest rate level parameter.

  • beta1 (Tensor) – Short-term interest rate parameter.

  • beta2 (Tensor) – Medium-term interest rate parameter.

Returns:

A 1D tensor of yields corresponding to each maturity in tau.

Return type:

Tensor

Note

This implementation uses a simplified version of the Nelson-Siegel model where the decay parameter lambda is assumed to be 1.

Usage Example

import torch
from torchquantlib.utils.yield_curve_construction import nelson_siegel_yield_curve

# Time to maturities
tau = torch.tensor([0.5, 1, 2, 3, 5, 10])

# Nelson-Siegel parameters
beta0 = torch.tensor(0.03)
beta1 = torch.tensor(-0.02)
beta2 = torch.tensor(0.01)

# Calculate yields
yields = nelson_siegel_yield_curve(tau, beta0, beta1, beta2)
print(yields)