Skip to content

Commit b741d45

Browse files
mikelyncattack68
andauthored
DOC: credit (#255) (#1187)
Co-authored-by: JHM Darbyshire <[email protected]> Co-authored-by: JHM Darbyshire (M1) <[email protected]>
1 parent 6bc6351 commit b741d45

File tree

13 files changed

+107
-73
lines changed

13 files changed

+107
-73
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ dev = [
4343
"mypy>=1.13,<1.20",
4444
"pandas-stubs>2.0,<3.0",
4545
# doc building
46-
"sphinx>=9.1,<9.2",
46+
"sphinx>=8.1,<9.0",
4747
"sphinx-automodapi>=0.16.0,<1.0",
4848
"sphinxcontrib-googleanalytics>=0.4,<1.0",
4949
"sphinx-tabs>=3.4,<4.0",

python/rateslib/data/fixings.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,12 @@
6464
LegFixings,
6565
PeriodFixings,
6666
Result,
67+
StubInference,
6768
_BaseCurve_,
6869
bool_,
6970
datetime_,
7071
int_,
7172
str_,
72-
StubInference,
7373
)
7474

7575

python/rateslib/default.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
from rateslib._spec_loader import INSTRUMENT_SPECS
2323
from rateslib.enums.generics import NoInput, _drb
2424
from rateslib.enums.parameters import FloatFixingMethod
25-
from rateslib.rs import Adjuster, Convention, NamedCal, StubInference
25+
from rateslib.rs import Adjuster, Convention, NamedCal
2626

2727
PlotOutput = tuple[plt.Figure, plt.Axes, list[plt.Line2D]] # type: ignore[name-defined]
2828

python/rateslib/instruments/cds.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,6 @@ class CDS(_BaseInstrument):
165165
leg2_payment_lag: Adjuster, int, :green:`optional (inherited from leg1)`
166166
leg2_payment_lag_exchange: Adjuster, int, :green:`optional (inherited from leg1)`
167167
leg2_ex_div: Adjuster, int, :green:`optional (inherited from leg1)`
168-
leg2_convention: str, :green:`optional (inherited from leg1)`
169168
170169
.. note::
171170
@@ -265,7 +264,7 @@ def __init__(
265264
leg2_calendar: CalInput = NoInput(1),
266265
leg2_payment_lag: int_ = NoInput(1),
267266
leg2_payment_lag_exchange: int_ = NoInput(1),
268-
leg2_convention: str_ = NoInput(1),
267+
# leg2_convention: str_ = NoInput(1),
269268
leg2_ex_div: int_ = NoInput(1),
270269
# settlement
271270
notional: float_ = NoInput(0),
@@ -313,7 +312,7 @@ def __init__(
313312
leg2_ex_div=leg2_ex_div,
314313
leg2_notional=leg2_notional,
315314
leg2_amortization=leg2_amortization,
316-
leg2_convention=leg2_convention,
315+
# leg2_convention=leg2_convention,
317316
# rate and credit
318317
premium_accrued=premium_accrued,
319318
fixed_rate=fixed_rate,

python/rateslib/legs/credit.py

Lines changed: 71 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,31 @@
4040

4141
class CreditPremiumLeg(_BaseLeg, _WithExDiv):
4242
"""
43-
Define a *Leg* containing :class:`~rateslib.periods.CreditPremiumPeriod`.
43+
A *Leg* containing :class:`~rateslib.periods.CreditPremiumPeriod`.
44+
45+
.. rubric:: Examples
46+
47+
.. ipython:: python
48+
:suppress:
49+
50+
from rateslib import Schedule
51+
from rateslib.legs import CreditPremiumLeg
52+
from datetime import datetime as dt
53+
54+
.. ipython:: python
55+
56+
cpl = CreditPremiumLeg(
57+
schedule=Schedule(
58+
effective=dt(2000, 3, 20),
59+
termination=dt(2001, 3, 20),
60+
frequency="Q",
61+
modifier="FEX",
62+
),
63+
convention="Act360",
64+
fixed_rate=1.0,
65+
notional=10e6,
66+
)
67+
cpl.cashflows()
4468
4569
.. role:: red
4670
@@ -87,15 +111,6 @@ class CreditPremiumLeg(_BaseLeg, _WithExDiv):
87111
premium_accrued: bool, :green:`optional (set by 'defaults')`
88112
Whether an accrued premium is paid on the event of mid-period credit default.
89113
90-
91-
Notes
92-
-----
93-
TODO
94-
95-
Examples
96-
--------
97-
See :ref:`Leg Examples<legs-doc>`
98-
99114
"""
100115

101116
@property
@@ -111,6 +126,8 @@ def periods(self) -> list[CreditPremiumPeriod]:
111126

112127
@property
113128
def fixed_rate(self) -> DualTypes_:
129+
"""The fixed rate parameter of each composited
130+
:class:`~rateslib.periods.CreditPremiumPeriod`."""
114131
return self._fixed_rate
115132

116133
@fixed_rate.setter
@@ -121,10 +138,14 @@ def fixed_rate(self, value: DualTypes_) -> None:
121138

122139
@property
123140
def schedule(self) -> Schedule:
141+
"""The :class:`~rateslib.scheduling.Schedule` object of *Leg*."""
124142
return self._schedule
125143

126144
@property
127145
def amortization(self) -> Amortization:
146+
"""
147+
The :class:`~rateslib.legs.Amortization` object associated with the schedule.
148+
"""
128149
return self._amortization
129150

130151
def accrued(self, settlement: datetime) -> DualTypes:
@@ -225,50 +246,51 @@ def spread(
225246

226247
class CreditProtectionLeg(_BaseLeg):
227248
"""
228-
Create a credit protection leg composed of :class:`~rateslib.periods.CreditProtectionPeriod` s.
249+
A *Leg* containing :class:`~rateslib.periods.CreditProtectionPeriod`.
229250
230-
Parameters
231-
----------
232-
args : tuple
233-
Required positional args to :class:`BaseLeg`.
234-
kwargs : dict
235-
Required keyword arguments to :class:`BaseLeg`.
251+
.. rubric:: Examples
236252
237-
Notes
238-
-----
239-
The NPV of a credit protection leg is the sum of the period NPVs.
253+
.. ipython:: python
254+
:suppress:
240255
241-
.. math::
256+
from rateslib import dt, CreditProtectionLeg, Schedule
242257
243-
P = \\sum_{i=1}^n P_i
258+
.. ipython:: python
244259
245-
The analytic delta is the sum of the period analytic deltas.
260+
cpl = CreditProtectionLeg(
261+
schedule=Schedule(
262+
effective=dt(2000, 3, 20),
263+
termination=dt(2001, 3, 30),
264+
frequency="Z",
265+
),
266+
notional=10e6,
267+
)
268+
cpl.cashflows()
246269
247-
.. math::
270+
.. role:: red
248271
249-
A = -\\frac{\\partial P}{\\partial S} = \\sum_{i=1}^n -\\frac{\\partial P_i}{\\partial S}
272+
.. role:: green
250273
251-
Examples
252-
--------
274+
Parameters
275+
----------
276+
schedule: Schedule, :red:`required`
277+
The :class:`~rateslib.scheduling.Schedule` object which structures contiguous *Periods*.
278+
The schedule object also contains data for payment dates, payment dates for notional
279+
exchanges and ex-dividend dates for each period.
253280
254-
.. ipython:: python
255-
:suppress:
281+
.. note::
256282
257-
from rateslib.curves import Curve
258-
from rateslib.scheduling import Schedule
259-
from rateslib.legs import CreditProtectionLeg
260-
from datetime import datetime as dt
283+
The following define generalised **settlement** parameters.
261284
262-
.. ipython:: python
285+
currency : str, :green:`optional (set by 'defaults')`
286+
The local settlement currency of the leg (3-digit code).
287+
notional : float, Dual, Dual2, Variable, :green:`optional (set by 'defaults')`
288+
The initial leg notional, defined in units of *reference currency*.
289+
amortization: float, Dual, Dual2, Variable, str, Amortization, :green:`optional (set as zero)`
290+
Set a non-constant notional per *Period*. If a scalar value, adjusts the ``notional`` of
291+
each successive period by that same value. Should have
292+
sign equal to that of notional if the notional is to reduce towards zero.
263293
264-
disc_curve = Curve({dt(2022, 1, 1): 1.0, dt(2023, 1, 1): 0.98})
265-
hazard_curve = Curve({dt(2022, 1, 1): 1.0, dt(2023, 1, 1): 0.995})
266-
protection_leg = CreditProtectionLeg(
267-
schedule=Schedule(dt(2022, 1, 1), "9M", "Z"),
268-
notional=1000000,
269-
)
270-
protection_leg.cashflows(rate_curve=hazard_curve, disc_curve=disc_curve)
271-
protection_leg.npv(rate_curve=hazard_curve, disc_curve=disc_curve)
272294
""" # noqa: E501
273295

274296
@property
@@ -284,10 +306,14 @@ def periods(self) -> list[CreditProtectionPeriod]:
284306

285307
@property
286308
def schedule(self) -> Schedule:
309+
"""The :class:`~rateslib.scheduling.Schedule` object of *Leg*."""
287310
return self._schedule
288311

289312
@property
290313
def amortization(self) -> Amortization:
314+
"""
315+
The :class:`~rateslib.legs.Amortization` object associated with the schedule.
316+
"""
291317
return self._amortization
292318

293319
def __init__(
@@ -299,15 +325,15 @@ def __init__(
299325
amortization: DualTypes_ | list[DualTypes] | Amortization | str = NoInput(0),
300326
currency: str_ = NoInput(0),
301327
# period
302-
convention: str_ = NoInput(0),
328+
# convention: str_ = NoInput(0),
303329
) -> None:
304330
self._schedule = schedule
305331
self._notional: DualTypes = _drb(defaults.notional, notional)
306332
self._amortization: Amortization = _get_amortization(
307333
amortization, self._notional, self.schedule.n_periods
308334
)
309335
self._currency: str = _drb(defaults.base_currency, currency).lower()
310-
self._convention: str = _drb(defaults.convention, convention)
336+
# self._convention: str = _drb(defaults.convention, convention)
311337

312338
self._regular_periods = tuple(
313339
[
@@ -321,7 +347,7 @@ def __init__(
321347
start=self.schedule.aschedule[i],
322348
end=self.schedule.aschedule[i + 1],
323349
frequency=self.schedule.frequency_obj,
324-
convention=self._convention,
350+
# convention=self._convention,
325351
termination=self.schedule.aschedule[-1],
326352
stub=self.schedule._stubs[i],
327353
roll=NoInput(0), # defined by Frequency

python/rateslib/legs/float.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
import rateslib.errors as err
2020
from rateslib import defaults
21-
from rateslib.data.fixings import _leg_fixings_to_list, _get_float_fixing_method
21+
from rateslib.data.fixings import _get_float_fixing_method, _leg_fixings_to_list
2222
from rateslib.dual import ift_1dim
2323
from rateslib.enums.generics import NoInput, _drb
2424
from rateslib.enums.parameters import FloatFixingMethod, LegMtm, SpreadCompoundMethod, _get_leg_mtm
@@ -31,7 +31,6 @@
3131
from rateslib.periods.parameters.rate import _init_float_rate_series
3232
from rateslib.scheduling.schedule import Schedule
3333

34-
3534
if TYPE_CHECKING:
3635
from rateslib.typing import ( # pragma: no cover
3736
CurveOption_,

python/rateslib/periods/credit.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
from rateslib.periods.protocols import _BasePeriod
2828
from rateslib.periods.protocols.npv import _screen_ex_div_and_forward
2929
from rateslib.periods.utils import _maybe_local, _try_validate_base_curve, _validate_credit_curves
30-
from rateslib.scheduling import Frequency, get_calendar
30+
from rateslib.scheduling import Convention, Frequency, get_calendar
3131
from rateslib.scheduling.adjuster import _get_adjuster
3232
from rateslib.scheduling.convention import _get_convention
3333
from rateslib.scheduling.frequency import _get_frequency
@@ -129,6 +129,9 @@ class CreditPremiumPeriod(_BasePeriod):
129129
stub: bool, str, :green:`optional (set as False)`
130130
Whether the *Period* is defined as a stub according to some external
131131
:class:`~rateslib.scheduling.Schedule`.
132+
roll: RollDay, int, str, :green:`optional (set by 'frequency')`
133+
The rollday associated with any monthly :class:`~rateslib.scheduling.Frequency`, if
134+
not directly associated with that object.
132135
adjuster: Adjuster, :green:`optional`
133136
The date :class:`~rateslib.scheduling.Adjuster` applied to unadjusted dates in the
134137
external :class:`~rateslib.scheduling.Schedule` to arrive at adjusted accrual dates.
@@ -431,15 +434,16 @@ class CreditProtectionPeriod(_BasePeriod):
431434
The identified end date of the *Period*.
432435
frequency: Frequency, str, :red:`required`
433436
The :class:`~rateslib.scheduling.Frequency` associated with the *Period*.
434-
convention: Convention, str, :green:`optional` (set by 'defaults')
435-
The day count :class:`~rateslib.scheduling.Convention` associated with the *Period*.
436437
termination: datetime, :green:`optional`
437438
The termination date of an external :class:`~rateslib.scheduling.Schedule`.
438439
calendar: Calendar, :green:`optional`
439440
The calendar associated with the *Period*.
440441
stub: bool, str, :green:`optional (set as False)`
441442
Whether the *Period* is defined as a stub according to some external
442443
:class:`~rateslib.scheduling.Schedule`.
444+
roll: RollDay, int, str, :green:`optional (set by 'frequency')`
445+
The rollday associated with any monthly :class:`~rateslib.scheduling.Frequency`, if
446+
not directly associated with that object.
443447
adjuster: Adjuster, :green:`optional`
444448
The date :class:`~rateslib.scheduling.Adjuster` applied to unadjusted dates in the
445449
external :class:`~rateslib.scheduling.Schedule` to arrive at adjusted accrual dates.
@@ -468,7 +472,7 @@ def __init__(
468472
start: datetime,
469473
end: datetime,
470474
frequency: Frequency | str,
471-
convention: str_ = NoInput(0),
475+
# convention: str_ = NoInput(0),
472476
termination: datetime_ = NoInput(0),
473477
stub: bool = False,
474478
roll: RollDay | int | str_ = NoInput(0),
@@ -492,7 +496,7 @@ def __init__(
492496
_calendar=get_calendar(calendar),
493497
_adjuster=NoInput(0) if isinstance(adjuster, NoInput) else _get_adjuster(adjuster),
494498
_stub=stub,
495-
_convention=_get_convention(_drb(defaults.convention, convention)),
499+
_convention=Convention.One, # _get_convention(_drb(defaults.convention, convention)),
496500
_termination=termination,
497501
)
498502

python/rateslib/periods/fixed_period.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,9 @@ class FixedPeriod(_BasePeriodStatic):
131131
stub: bool, str, :green:`optional (set as False)`
132132
Whether the *Period* is defined as a stub according to some external
133133
:class:`~rateslib.scheduling.Schedule`.
134+
roll: RollDay, int, str, :green:`optional (set by 'frequency')`
135+
The rollday associated with any monthly :class:`~rateslib.scheduling.Frequency`, if
136+
not directly associated with that object.
134137
adjuster: Adjuster, :green:`optional`
135138
The date :class:`~rateslib.scheduling.Adjuster` applied to unadjusted dates in the
136139
external :class:`~rateslib.scheduling.Schedule` to arrive at adjusted accrual dates.

python/rateslib/periods/float_period.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,9 @@ class FloatPeriod(_BasePeriodStatic):
160160
stub: bool, str, :green:`optional (set as False)`
161161
Whether the *Period* is defined as a stub according to some external
162162
:class:`~rateslib.scheduling.Schedule`.
163+
roll: RollDay, int, str, :green:`optional (set by 'frequency')`
164+
The rollday associated with any monthly :class:`~rateslib.scheduling.Frequency`, if
165+
not directly associated with that object.
163166
adjuster: Adjuster, :green:`optional`
164167
The date :class:`~rateslib.scheduling.Adjuster` applied to unadjusted dates in the
165168
external :class:`~rateslib.scheduling.Schedule` to arrive at adjusted accrual dates.
@@ -764,7 +767,7 @@ def __init__(
764767
# meta-args:
765768
metric: str_ = NoInput(0),
766769
) -> None:
767-
self._rate_metric = _drb("compounding", metric).lower()
770+
self._rate_metric: str = _drb("compounding", metric).lower()
768771
self._schedule = schedule
769772
self._settlement_params = _init_SettlementParams_with_fx_pair(
770773
_currency=_drb(defaults.base_currency, currency).lower(),

python/rateslib/typing.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,6 @@
102102
from rateslib.periods.parameters import _SettlementParams as _SettlementParams
103103
from rateslib.periods.protocols import _BasePeriod as _BasePeriod
104104
from rateslib.rs import Adjuster as Adjuster
105-
from rateslib.rs import StubInference as StubInference
106105
from rateslib.rs import (
107106
Cal,
108107
FlatBackwardInterpolator,
@@ -114,6 +113,7 @@
114113
NullInterpolator,
115114
UnionCal,
116115
)
116+
from rateslib.rs import StubInference as StubInference
117117

118118
CurveInterpolator: TypeAlias = "FlatBackwardInterpolator | FlatForwardInterpolator | LinearInterpolator | LogLinearInterpolator | LinearZeroRateInterpolator | NullInterpolator"
119119

0 commit comments

Comments
 (0)