diff --git a/examples/scripts/rates/example_inflation_swap.py b/examples/scripts/rates/example_inflation_swap.py new file mode 100644 index 00000000..e07a4775 --- /dev/null +++ b/examples/scripts/rates/example_inflation_swap.py @@ -0,0 +1,61 @@ +# ============================================================================ +# FINANCEPY EXAMPLES - InflationSwap +# ============================================================================ +# +# Copyright (C) 2018-2026 Dominic O'Kane +# +# This example demonstrates the valuation of Zero-Coupon Inflation Swaps +# (ZCIS) and Year-on-Year Inflation Swaps (YYIS) against a DiscountCurve and +# an InflationIndexCurve. +# +# ============================================================================ + +from financepy.utils.date import Date +from financepy.utils.day_count import DayCountTypes +from financepy.utils.frequency import FrequencyTypes +from financepy.utils.global_types import SwapTypes +from financepy.market.curves.flat_discount_curve import FlatDiscountCurve +from financepy.market.curves.inflation_index_curve import InflationIndexCurve +from financepy.products.rates.inflation_swap import ( + InflationSwap, + InflationSwapTypes, +) + +value_dt = Date(15, 1, 2025) +effective_dt = Date(15, 1, 2025) + +discount_curve = FlatDiscountCurve(value_dt, 0.04) + +# Build a 3-month lagged CPI curve with 2.5% annualised inflation +index_dts = [value_dt.add_months(m) for m in range(-4, 65)] +index_vals = [100.0 * (1.025 ** ((dt - value_dt) / 365.0)) for dt in index_dts] +infl_curve = InflationIndexCurve(index_dts, index_vals, lag_in_months=3) + +# 1. Zero-Coupon Inflation Swap (5Y) +zcis = InflationSwap( + effective_dt, + "5Y", + SwapTypes.PAY, + 0.02, + swap_type=InflationSwapTypes.ZERO_COUPON, + dc_type=DayCountTypes.ACT_365F, + notional=1_000_000.0, +) +zcis_par = zcis.fair_rate(value_dt, discount_curve, infl_curve) +zcis_pv = zcis.value(value_dt, discount_curve, infl_curve) +print(f"5Y ZCIS Par Rate : {zcis_par * 100:.4f}% | MtM PV (K=2.0%): {zcis_pv:,.2f}") + +# 2. Year-on-Year Inflation Swap (5Y, Annual) +yyis = InflationSwap( + effective_dt, + "5Y", + SwapTypes.PAY, + 0.02, + swap_type=InflationSwapTypes.YEAR_ON_YEAR, + freq_type=FrequencyTypes.ANNUAL, + dc_type=DayCountTypes.ACT_365F, + notional=1_000_000.0, +) +yyis_par = yyis.fair_rate(value_dt, discount_curve, infl_curve) +yyis_pv = yyis.value(value_dt, discount_curve, infl_curve) +print(f"5Y YYIS Par Rate : {yyis_par * 100:.4f}% | MtM PV (K=2.0%): {yyis_pv:,.2f}") diff --git a/financepy/products/rates/__init__.py b/financepy/products/rates/__init__.py index 0258aef1..6dd4b2d4 100644 --- a/financepy/products/rates/__init__.py +++ b/financepy/products/rates/__init__.py @@ -11,6 +11,7 @@ from .ibor_conventions import * from .ibor_swap import * from .ibor_swaption import * + from .inflation_swap import * from .ois_basis_swap import * from .ois_curve import * from .ois import * diff --git a/financepy/products/rates/inflation_swap.py b/financepy/products/rates/inflation_swap.py index 23c177aa..56109d96 100644 --- a/financepy/products/rates/inflation_swap.py +++ b/financepy/products/rates/inflation_swap.py @@ -1,158 +1,284 @@ -######################################################################################## -# Copyright (C) 2018, 2019, 2020 Dominic O'Kane -######################################################################################## +############################################################################## +# Copyright (C) 2018-2026 Dominic O'Kane +############################################################################## +from enum import Enum from typing import Union from ...utils.error import FinError from ...utils.date import Date -from ...utils.calendar import Calendar -from ...utils.calendar import CalendarTypes -from ...utils.calendar import BusDayAdjustTypes +from ...utils.calendar import Calendar, CalendarTypes +from ...utils.calendar import BusDayAdjustTypes, DateGenRuleTypes from ...utils.day_count import DayCount, DayCountTypes +from ...utils.frequency import FrequencyTypes +from ...utils.schedule import Schedule +from ...utils.global_vars import ONE_MILLION +from ...utils.global_types import SwapTypes from ...utils.helpers import label_to_string, check_argument_types from ...utils.check_values import check_curve_dt from ...market.curves.discount_curve import DiscountCurve -from ...utils.valuation_log import log_value, log_table, log_row +from ...market.curves.inflation_index_curve import InflationIndexCurve ######################################################################################## -class InflationSwap: - """Class for managing LIBOR forward rate agreements. A forward rate - agreement is an agreement to exchange a fixed pre-agreed rate for a - floating rate linked to LIBOR that is not known until some specified - future fixing date. The FRA payment occurs on or soon after this date - on the FRA settlement date. Typically the timing gap is two days. +class InflationSwapTypes(Enum): + """Supported inflation swap payoff structures.""" - A FRA is used to hedge a Ibor quality loan or lend of some agreed - notional amount. This period starts on the settlement date of the - FRA and ends on the maturity date of the FRA. For example a 1x4 FRA - relates to a Ibor starting in 1 month for a loan period ending in 4 - months. Hence it links to 3-month Ibor rate. The amount received by a - payer of fixed rate at settlement is: + ZERO_COUPON = 1 + YEAR_ON_YEAR = 2 - acc(1,2) * (Ibor(1,2) - FRA RATE) / (1 + acc(0,1) x Ibor(0,1)) - So the value at time 0 is +######################################################################################## + - acc(1,2) * (FWD Ibor(1,2) - FRA RATE) x df(0,2) +class InflationSwap: + """Class for managing inflation swaps: Zero-Coupon Inflation Swaps (ZCIS) + and Year-on-Year Inflation Swaps (YYIS). - If the base date of the curve is before the value date then we - forward adjust this amount to that value date. For simplicity I have - assumed that the fixing date and the settlement date are the same date. - This should be amended later.""" + In a Zero-Coupon Inflation Swap, a single fixed compounding payment + N * ((1 + K)^T - 1) is exchanged at maturity for the cumulative realised + inflation return N * (I(T_mat) / I(T_start) - 1). + + In a Year-on-Year Inflation Swap, periodic fixed coupons N * K * alpha_i + are exchanged on each schedule date t_i for periodic inflation coupons + N * (I(t_i) / I(t_{i-1}) - 1). + + Following FinancePy swap conventions, ``SwapTypes.PAY`` pays the fixed + inflation rate and receives the floating inflation leg; ``SwapTypes.RECEIVE`` + receives the fixed rate and pays the inflation leg. + """ def __init__( self, - start_dt: Date, # The date the FRA starts to accrue - # End of the Ibor rate period - maturity_dt_or_tenor: Union[Date, str], - fra_rate: float, # The fixed contractual FRA rate - accrual_dc_type: DayCountTypes, # For interest period - notional: float = 100.0, - pay_fixed_rate: bool = True, # True if the FRA rate is being paid + effective_dt: Date, + term_dt_or_tenor: Union[Date, str], + fixed_leg_type: SwapTypes, + fixed_rate: float, + swap_type: InflationSwapTypes = InflationSwapTypes.ZERO_COUPON, + freq_type: FrequencyTypes = FrequencyTypes.ANNUAL, + dc_type: DayCountTypes = DayCountTypes.ACT_365F, + notional: float = ONE_MILLION, cal_type: CalendarTypes = CalendarTypes.WEEKEND, bd_type: BusDayAdjustTypes = BusDayAdjustTypes.MODIFIED_FOLLOWING, + dg_type: DateGenRuleTypes = DateGenRuleTypes.BACKWARD, + end_of_month: bool = False, ) -> None: - """Create a Forward Rate Agreeement object.""" - - print("DO NOT USE") - raise FinError("DO NOT USE") + """Create an inflation swap contract.""" check_argument_types(self.__init__, locals()) - self.cal_type = cal_type - self.bd_type = bd_type + if notional <= 0.0: + raise FinError("Notional must be positive") - if isinstance(maturity_dt_or_tenor, Date): - maturity_dt = maturity_dt_or_tenor + if fixed_rate <= -1.0: + raise FinError("Fixed rate must be greater than -1.0") + + if isinstance(term_dt_or_tenor, Date): + termination_dt = term_dt_or_tenor else: - maturity_dt = start_dt.add_tenor(maturity_dt_or_tenor) - calendar = Calendar(self.cal_type) - maturity_dt = calendar.adjust(maturity_dt, self.bd_type) + termination_dt = effective_dt.add_tenor(term_dt_or_tenor) + + calendar = Calendar(cal_type) + maturity_dt = calendar.adjust(termination_dt, bd_type) - if start_dt > maturity_dt: - raise FinError("Settlement date after maturity date") + if effective_dt >= maturity_dt: + raise FinError("Effective date must be before maturity date") - self.start_dt = start_dt + self.effective_dt = effective_dt + self.termination_dt = termination_dt self.maturity_dt = maturity_dt - self.fra_rate = fra_rate - self.pay_fixed_rate = pay_fixed_rate - self.accrual_dc_type = accrual_dc_type + self.fixed_leg_type = fixed_leg_type + self.fixed_rate = fixed_rate + self.swap_type = swap_type + self.freq_type = freq_type + self.dc_type = dc_type self.notional = notional + self.cal_type = cal_type + self.bd_type = bd_type + self.dg_type = dg_type + self.end_of_month = end_of_month + + if self.swap_type == InflationSwapTypes.ZERO_COUPON: + self.payment_dts = [self.effective_dt, self.maturity_dt] + else: + schedule = Schedule( + self.effective_dt, + self.termination_dt, + self.freq_type, + self.cal_type, + self.bd_type, + self.dg_type, + end_of_month=self.end_of_month, + ) + self.payment_dts = schedule.adjusted_dts ########################################################################### - def value(self, value_dt: Date, libor_curve: DiscountCurve): - """Determine mark to market value of a FRA contract based on the - market FRA rate. The same curve is used for calculating the forward - Ibor and for doing discounting on the expected forward payment.""" + def fixed_leg_value( + self, + value_dt: Date, + discount_curve: DiscountCurve, + ) -> float: + """Present value of the fixed leg (unsigned, before pay/receive sign).""" + + check_curve_dt(value_dt, discount_curve) - check_curve_dt(value_dt, libor_curve) + if value_dt >= self.maturity_dt: + return 0.0 - dc = DayCount(self.accrual_dc_type) - acc_factor = dc.year_frac(self.start_dt, self.maturity_dt)[0] - df1 = libor_curve.df(self.start_dt) - df2 = libor_curve.df(self.maturity_dt) - libor_fwd = (df1 / df2 - 1.0) / acc_factor - v = acc_factor * (libor_fwd - self.fra_rate) * df2 + day_counter = DayCount(self.dc_type) - # print(df1, df2, acc_factor, libor_fwd, v) - # Forward value the FRA to the value date - df_to_value_dt = libor_curve.df(value_dt) - v = v * self.notional / df_to_value_dt + if self.swap_type == InflationSwapTypes.ZERO_COUPON: + alpha = day_counter.year_frac(self.effective_dt, self.maturity_dt)[0] + df = discount_curve.df(self.maturity_dt) + return float(self.notional * ((1.0 + self.fixed_rate) ** alpha - 1.0) * df) - if self.pay_fixed_rate: - v *= -1.0 - return v + pv = 0.0 + for i in range(1, len(self.payment_dts)): + prev_dt = self.payment_dts[i - 1] + pay_dt = self.payment_dts[i] + if pay_dt > value_dt: + alpha = day_counter.year_frac(prev_dt, pay_dt)[0] + df = discount_curve.df(pay_dt) + pv += self.notional * self.fixed_rate * alpha * df + + return float(pv) ########################################################################### - def maturity_df(self, libor_curve: DiscountCurve): - """Determine the maturity date discount factor needed to refit - the FRA given the libor curve anbd the contract FRA rate.""" + def inflation_leg_value( + self, + value_dt: Date, + discount_curve: DiscountCurve, + inflation_curve: InflationIndexCurve, + ) -> float: + """Present value of the floating inflation leg (unsigned).""" + + check_curve_dt(value_dt, discount_curve) + + if value_dt >= self.maturity_dt: + return 0.0 + + if self.swap_type == InflationSwapTypes.ZERO_COUPON: + cpi_start = inflation_curve.index_value(self.effective_dt) + cpi_end = inflation_curve.index_value(self.maturity_dt) + if cpi_start <= 0.0: + raise FinError("Start CPI index value must be positive") + df = discount_curve.df(self.maturity_dt) + return float(self.notional * (cpi_end / cpi_start - 1.0) * df) + + pv = 0.0 + for i in range(1, len(self.payment_dts)): + prev_dt = self.payment_dts[i - 1] + pay_dt = self.payment_dts[i] + if pay_dt > value_dt: + cpi_prev = inflation_curve.index_value(prev_dt) + cpi_curr = inflation_curve.index_value(pay_dt) + if cpi_prev <= 0.0: + raise FinError("Period start CPI index value must be positive") + df = discount_curve.df(pay_dt) + pv += self.notional * (cpi_curr / cpi_prev - 1.0) * df + + return float(pv) - dc = DayCount(self.accrual_dc_type) - df1 = libor_curve.df(self.start_dt) - acc_factor = dc.year_frac(self.start_dt, self.maturity_dt)[0] - df2 = df1 / (1.0 + acc_factor * self.fra_rate) - return df2 + ########################################################################### + + def value( + self, + value_dt: Date, + discount_curve: DiscountCurve, + inflation_curve: InflationIndexCurve, + ) -> float: + """Mark-to-market value of the inflation swap.""" + + pv_fixed = self.fixed_leg_value(value_dt, discount_curve) + pv_infl = self.inflation_leg_value(value_dt, discount_curve, inflation_curve) + + if self.fixed_leg_type == SwapTypes.PAY: + return pv_infl - pv_fixed + return pv_fixed - pv_infl ########################################################################### - def print_payments(self, value_dt: Date): - """Determine the value of the Deposit given a Ibor curve.""" + def pv01( + self, + value_dt: Date, + discount_curve: DiscountCurve, + ) -> float: + """Present value of a 1-unit fixed-rate annuity (per unit notional) + for Year-on-Year inflation swaps, or discounted accrual factor for ZCIS.""" - flow_settle = self.notional - dc = DayCount(self.accrual_dc_type) - acc_factor = dc.year_frac(self.start_dt, self.maturity_dt)[0] - flow_maturity = (1.0 + acc_factor * self.fra_rate) * self.notional + check_curve_dt(value_dt, discount_curve) - if self.pay_fixed_rate is True: - print(self.start_dt, -flow_settle) - print(self.maturity_dt, flow_maturity) - else: - print(self.start_dt, flow_settle) - print(self.maturity_dt, -flow_maturity) + if value_dt >= self.maturity_dt: + raise FinError("No remaining cashflows after maturity date") + + day_counter = DayCount(self.dc_type) + + if self.swap_type == InflationSwapTypes.ZERO_COUPON: + alpha = day_counter.year_frac(self.effective_dt, self.maturity_dt)[0] + return float(alpha * discount_curve.df(self.maturity_dt)) + + annuity = 0.0 + for i in range(1, len(self.payment_dts)): + prev_dt = self.payment_dts[i - 1] + pay_dt = self.payment_dts[i] + if pay_dt > value_dt: + alpha = day_counter.year_frac(prev_dt, pay_dt)[0] + annuity += alpha * discount_curve.df(pay_dt) + + return float(annuity) + + ########################################################################### + + def fair_rate( + self, + value_dt: Date, + discount_curve: DiscountCurve, + inflation_curve: InflationIndexCurve, + ) -> float: + """Par fixed inflation rate that sets the swap value to zero.""" + + check_curve_dt(value_dt, discount_curve) + + if value_dt >= self.maturity_dt: + raise FinError("No remaining cashflows after maturity date") + + day_counter = DayCount(self.dc_type) + + if self.swap_type == InflationSwapTypes.ZERO_COUPON: + cpi_start = inflation_curve.index_value(self.effective_dt) + cpi_end = inflation_curve.index_value(self.maturity_dt) + if cpi_start <= 0.0 or cpi_end <= 0.0: + raise FinError("CPI index values must be positive") + alpha = day_counter.year_frac(self.effective_dt, self.maturity_dt)[0] + return float((cpi_end / cpi_start) ** (1.0 / alpha) - 1.0) + + pv_infl_unit = self.inflation_leg_value( + value_dt, discount_curve, inflation_curve + ) / self.notional + annuity = self.pv01(value_dt, discount_curve) + return float(pv_infl_unit / annuity) ########################################################################### - def __repr__(self): + def __repr__(self) -> str: s = label_to_string("OBJECT TYPE", type(self).__name__) - s += label_to_string("START ACCD DATE", self.start_dt) + s += label_to_string("EFFECTIVE DATE", self.effective_dt) s += label_to_string("MATURITY DATE", self.maturity_dt) - s += label_to_string("FRA RATE", self.fra_rate) - s += label_to_string("NOTIONAL", self.notional) - s += label_to_string("PAY FIXED RATE", self.pay_fixed_rate) - s += label_to_string("DAY COUNT", self.accrual_dc_type) - s += label_to_string("BUS DAY RULE", self.bd_type) - s += label_to_string("CALENDAR", self.cal_type) + s += label_to_string("SWAP TYPE", self.swap_type) + s += label_to_string("FIXED LEG TYPE", self.fixed_leg_type) + s += label_to_string("FIXED RATE", self.fixed_rate) + s += label_to_string("FREQUENCY", self.freq_type) + s += label_to_string("DAY COUNT", self.dc_type) + s += label_to_string("NOTIONAL", self.notional, "") return s ########################################################################### - def _print(self): + def _print(self) -> None: + """Simple print function for backward compatibility.""" print(self) diff --git a/tests/unit/test_inflation_swap.py b/tests/unit/test_inflation_swap.py new file mode 100644 index 00000000..20d4bcc9 --- /dev/null +++ b/tests/unit/test_inflation_swap.py @@ -0,0 +1,201 @@ +# Copyright (C) 2018-2026 Dominic O'Kane + +import numpy as np +import pytest + +from financepy.market.curves.flat_discount_curve import FlatDiscountCurve +from financepy.market.curves.inflation_index_curve import InflationIndexCurve +from financepy.products.rates.inflation_swap import ( + InflationSwap, + InflationSwapTypes, +) +from financepy.utils.calendar import BusDayAdjustTypes, CalendarTypes +from financepy.utils.date import Date +from financepy.utils.day_count import DayCountTypes +from financepy.utils.error import FinError +from financepy.utils.frequency import FrequencyTypes +from financepy.utils.global_types import SwapTypes + + +def _build_cpi_curve(base_dt: Date, annual_infl: float, years: int = 12, lag_months: int = 3): + """Build a monthly CPI curve growing at a constant annualised rate.""" + index_dts = [base_dt.add_months(m) for m in range(-lag_months - 1, years * 12 + 2)] + base_cpi = 100.0 + index_vals = [ + base_cpi * ((1.0 + annual_infl) ** ((dt - base_dt) / 365.0)) + for dt in index_dts + ] + return InflationIndexCurve(index_dts, index_vals, lag_in_months=lag_months) + + +def test_zcis_par_rate_and_round_trip(): + """ZCIS fair rate matches analytical index ratio and prices to zero.""" + value_dt = Date(15, 1, 2025) + effective_dt = Date(15, 1, 2025) + discount_curve = FlatDiscountCurve(value_dt, 0.04) + infl_curve = _build_cpi_curve(value_dt, annual_infl=0.025, lag_months=3) + + swap = InflationSwap( + effective_dt, + "5Y", + SwapTypes.PAY, + 0.025, + swap_type=InflationSwapTypes.ZERO_COUPON, + dc_type=DayCountTypes.ACT_365F, + notional=1_000_000.0, + cal_type=CalendarTypes.NONE, + bd_type=BusDayAdjustTypes.NONE, + ) + + par = swap.fair_rate(value_dt, discount_curve, infl_curve) + assert par == pytest.approx(0.025, abs=1e-4) + + par_swap = InflationSwap( + effective_dt, + "5Y", + SwapTypes.PAY, + par, + swap_type=InflationSwapTypes.ZERO_COUPON, + dc_type=DayCountTypes.ACT_365F, + notional=1_000_000.0, + cal_type=CalendarTypes.NONE, + bd_type=BusDayAdjustTypes.NONE, + ) + assert par_swap.value(value_dt, discount_curve, infl_curve) == pytest.approx( + 0.0, abs=1e-8 + ) + + +def test_yyis_par_rate_and_round_trip(): + """YYIS fair rate sets swap NPV to zero and matches annual inflation.""" + value_dt = Date(1, 3, 2025) + effective_dt = Date(1, 3, 2025) + discount_curve = FlatDiscountCurve(value_dt, 0.035) + infl_curve = _build_cpi_curve(value_dt, annual_infl=0.03, lag_months=3) + + yyis = InflationSwap( + effective_dt, + "5Y", + SwapTypes.PAY, + 0.02, + swap_type=InflationSwapTypes.YEAR_ON_YEAR, + freq_type=FrequencyTypes.ANNUAL, + dc_type=DayCountTypes.ACT_365F, + notional=2_000_000.0, + ) + + par = yyis.fair_rate(value_dt, discount_curve, infl_curve) + assert par == pytest.approx(0.03, abs=5e-4) + + par_yyis = InflationSwap( + effective_dt, + "5Y", + SwapTypes.PAY, + par, + swap_type=InflationSwapTypes.YEAR_ON_YEAR, + freq_type=FrequencyTypes.ANNUAL, + dc_type=DayCountTypes.ACT_365F, + notional=2_000_000.0, + ) + assert par_yyis.value(value_dt, discount_curve, infl_curve) == pytest.approx( + 0.0, abs=1e-8 + ) + + +@pytest.mark.parametrize( + "swap_type", + [InflationSwapTypes.ZERO_COUPON, InflationSwapTypes.YEAR_ON_YEAR], +) +def test_pay_receive_symmetry(swap_type): + """Receiving fixed produces the exact negative NPV of paying fixed.""" + value_dt = Date(10, 6, 2025) + discount_curve = FlatDiscountCurve(value_dt, 0.04) + infl_curve = _build_cpi_curve(value_dt, annual_infl=0.028, lag_months=3) + + pay_swap = InflationSwap( + value_dt, "3Y", SwapTypes.PAY, 0.02, swap_type=swap_type, notional=1_000_000.0 + ) + rec_swap = InflationSwap( + value_dt, "3Y", SwapTypes.RECEIVE, 0.02, swap_type=swap_type, notional=1_000_000.0 + ) + + v_pay = pay_swap.value(value_dt, discount_curve, infl_curve) + v_rec = rec_swap.value(value_dt, discount_curve, infl_curve) + assert v_pay > 0.0 + assert v_rec == pytest.approx(-v_pay, rel=1e-12) + + +def test_midlife_and_maturity_valuation(): + """Mid-life valuation prices only remaining cashflows; at/after maturity NPV is 0.""" + start_dt = Date(1, 1, 2025) + discount_curve_0 = FlatDiscountCurve(start_dt, 0.03) + infl_curve = _build_cpi_curve(start_dt, annual_infl=0.025, lag_months=3) + + yyis = InflationSwap( + start_dt, + "3Y", + SwapTypes.PAY, + 0.02, + swap_type=InflationSwapTypes.YEAR_ON_YEAR, + freq_type=FrequencyTypes.ANNUAL, + cal_type=CalendarTypes.NONE, + bd_type=BusDayAdjustTypes.NONE, + ) + + # After 2 years, only the 3rd-year coupon remains + mid_dt = Date(1, 1, 2027) + discount_curve_mid = FlatDiscountCurve(mid_dt, 0.03) + pv_mid = yyis.value(mid_dt, discount_curve_mid, infl_curve) + assert np.isfinite(pv_mid) + assert pv_mid > 0.0 + + # At maturity, value is 0 + mat_dt = yyis.maturity_dt + discount_curve_mat = FlatDiscountCurve(mat_dt, 0.03) + assert yyis.value(mat_dt, discount_curve_mat, infl_curve) == 0.0 + assert yyis.value(start_dt, discount_curve_0, infl_curve) > pv_mid + + +def test_indexation_lag_effect(): + """Changing CPI indexation lag changes the effective index ratio when CPI is non-uniform.""" + start_dt = Date(1, 1, 2025) + discount_curve = FlatDiscountCurve(start_dt, 0.03) + + # Accelerating CPI growth: later months grow faster than earlier months + index_dts = [start_dt.add_months(m) for m in range(-6, 40)] + index_vals = [100.0 + 0.1 * (i**1.4) for i in range(len(index_dts))] + curve_lag0 = InflationIndexCurve(index_dts, index_vals, lag_in_months=0) + curve_lag3 = InflationIndexCurve(index_dts, index_vals, lag_in_months=3) + + zcis = InflationSwap( + start_dt, + "2Y", + SwapTypes.PAY, + 0.02, + swap_type=InflationSwapTypes.ZERO_COUPON, + ) + + par0 = zcis.fair_rate(start_dt, discount_curve, curve_lag0) + par3 = zcis.fair_rate(start_dt, discount_curve, curve_lag3) + assert par0 != pytest.approx(par3, abs=1e-6) + + +def test_validation_errors(): + """Invalid swap parameters or post-maturity par-rate calls raise FinError.""" + start_dt = Date(1, 1, 2025) + discount_curve = FlatDiscountCurve(start_dt, 0.03) + infl_curve = _build_cpi_curve(start_dt, annual_infl=0.02, lag_months=3) + + with pytest.raises(FinError): + InflationSwap(start_dt, "2Y", SwapTypes.PAY, 0.02, notional=-100.0) + + with pytest.raises(FinError): + InflationSwap(start_dt, "2Y", SwapTypes.PAY, -1.0) + + with pytest.raises(FinError): + InflationSwap(Date(1, 1, 2027), Date(1, 1, 2026), SwapTypes.PAY, 0.02) + + zcis = InflationSwap(start_dt, "1Y", SwapTypes.PAY, 0.02) + mat_curve = FlatDiscountCurve(zcis.maturity_dt, 0.03) + with pytest.raises(FinError): + zcis.fair_rate(zcis.maturity_dt, mat_curve, infl_curve)