|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from typing import TYPE_CHECKING, Protocol |
| 4 | + |
| 5 | +from pandas import DataFrame, Series |
| 6 | + |
| 7 | +from rateslib.enums.generics import NoInput |
| 8 | +from rateslib.periods.protocols.fixings import ( |
| 9 | + _replace_fixings_with_ad_variables, |
| 10 | + _reset_fixings_data, |
| 11 | + _structure_sensitivity_data, |
| 12 | +) |
| 13 | + |
| 14 | +if TYPE_CHECKING: |
| 15 | + from rateslib.typing import ( |
| 16 | + CurvesT_, |
| 17 | + DualTypes, |
| 18 | + FXForwards_, |
| 19 | + Sequence, |
| 20 | + Solver_, |
| 21 | + VolT_, |
| 22 | + datetime_, |
| 23 | + int_, |
| 24 | + str_, |
| 25 | + ) |
| 26 | + |
| 27 | + |
| 28 | +class _WithFixings(Protocol): |
| 29 | + """ |
| 30 | + Protocol for determining fixing sensitivity for a *Period* with AD. |
| 31 | +
|
| 32 | + .. rubric:: Provided methods |
| 33 | +
|
| 34 | + .. autosummary:: |
| 35 | +
|
| 36 | + ~_WithFixings.reset_fixings |
| 37 | +
|
| 38 | + """ |
| 39 | + |
| 40 | + def npv( |
| 41 | + self, |
| 42 | + *, |
| 43 | + curves: CurvesT_ = NoInput(0), |
| 44 | + solver: Solver_ = NoInput(0), |
| 45 | + fx: FXForwards_ = NoInput(0), |
| 46 | + vol: VolT_ = NoInput(0), |
| 47 | + base: str_ = NoInput(0), |
| 48 | + local: bool = False, |
| 49 | + settlement: datetime_ = NoInput(0), |
| 50 | + forward: datetime_ = NoInput(0), |
| 51 | + ) -> DualTypes | dict[str, DualTypes]: ... |
| 52 | + |
| 53 | + def reset_fixings(self, state: int_ = NoInput(0)) -> None: |
| 54 | + """ |
| 55 | + Resets any fixings values of the *Instrument* derived using the given data state. |
| 56 | +
|
| 57 | + .. role:: green |
| 58 | +
|
| 59 | + Parameters |
| 60 | + ---------- |
| 61 | + state: int, :green:`optional` |
| 62 | + The *state id* of the data series that set the fixing. Only fixings determined by this |
| 63 | + data will be reset. If not given resets all fixings. |
| 64 | +
|
| 65 | + Returns |
| 66 | + ------- |
| 67 | + None |
| 68 | + """ |
| 69 | + if hasattr(self, "legs"): |
| 70 | + for leg in self.legs: |
| 71 | + leg.reset_fixings(state) |
| 72 | + elif hasattr(self, "instruments"): |
| 73 | + for inst in self.instruments: |
| 74 | + inst.reset_fixings(state) |
| 75 | + |
| 76 | + def local_fixings( |
| 77 | + self, |
| 78 | + identifiers: Sequence[tuple[str, Series]], |
| 79 | + scalars: Sequence[float] | NoInput = NoInput(0), |
| 80 | + curves: CurvesT_ = NoInput(0), |
| 81 | + solver: Solver_ = NoInput(0), |
| 82 | + fx: FXForwards_ = NoInput(0), |
| 83 | + vol: VolT_ = NoInput(0), |
| 84 | + settlement: datetime_ = NoInput(0), |
| 85 | + forward: datetime_ = NoInput(0), |
| 86 | + ) -> DataFrame: |
| 87 | + """ |
| 88 | + Calculate the sensitivity to fixings of the *Instrument*, expressed in local |
| 89 | + settlement currency. |
| 90 | +
|
| 91 | + .. role:: red |
| 92 | +
|
| 93 | + .. role:: green |
| 94 | +
|
| 95 | + Parameters |
| 96 | + ---------- |
| 97 | + identifiers: Sequence of tuple[str, Series], :red:`required` |
| 98 | + These are the series string identifiers and the data values that will be used in each |
| 99 | + Series to determine the sensitivity against. |
| 100 | + scalars: Sequence of floats, :green:`optional (each set as 1.0)` |
| 101 | + A sequence of scalars to multiply the sensitivities by for each on of the |
| 102 | + ``identifiers``. |
| 103 | + curves: _Curves, :green:`optional` |
| 104 | + Pricing objects. See **Pricing** on each *Instrument* for details of allowed inputs. |
| 105 | + solver: Solver, :green:`optional` |
| 106 | + A :class:`~rateslib.solver.Solver` object containing *Curve*, *Smile*, *Surface*, or |
| 107 | + *Cube* mappings for pricing. |
| 108 | + fx: FXForwards, :green:`optional` |
| 109 | + The :class:`~rateslib.fx.FXForwards` object used for forecasting FX rates, if necessary. |
| 110 | + vol: _Vol, :green:`optional` |
| 111 | + Pricing objects. See **Pricing** on each *Instrument* for details of allowed inputs. |
| 112 | + settlement: datetime, :green:`optional` |
| 113 | + The assumed settlement date of the *PV* determination. Used only to evaluate |
| 114 | + *ex-dividend* status. |
| 115 | + forward: datetime, :green:`optional` |
| 116 | + The future date to project the *PV* to using the ``disc_curve``. |
| 117 | +
|
| 118 | + Returns |
| 119 | + ------- |
| 120 | + DataFrame |
| 121 | + """ |
| 122 | + original_data, index, state = _replace_fixings_with_ad_variables(identifiers) |
| 123 | + # Extract sensitivity data |
| 124 | + pv: dict[str, DualTypes] = self.npv( # type: ignore[assignment] |
| 125 | + curves=curves, |
| 126 | + solver=solver, |
| 127 | + fx=fx, |
| 128 | + vol=vol, |
| 129 | + settlement=settlement, |
| 130 | + forward=forward, |
| 131 | + local=True, |
| 132 | + ) |
| 133 | + df = _structure_sensitivity_data(pv, index, identifiers, scalars) |
| 134 | + _reset_fixings_data(self, original_data, state, identifiers) |
| 135 | + return df |
0 commit comments