Skip to content

Commit 38e6a9c

Browse files
This commit adds and improves numpydoc-style docstrings across several modules, enhancing the overall documentation of the library.
The following modules and their classes have been documented: - `quantlib/time`: `Date`, `Period`, `Calendar` - `quantlib/termstructures/yields`: `FlatForward`, `ForwardSpreadedTermStructure`, `ImpliedTermStructure` - `quantlib/quotes`: `SimpleQuote`, `FuturesConvAdjustmentQuote` - `quantlib/indexes`: `IborIndex`, `OvernightIndex`, `InflationIndex`, `ZeroInflationIndex`, `YoYInflationIndex` - `quantlib/instruments`: `Swap`, `VanillaSwap` The docstrings now include fully qualified type hints to support documentation generation and cross-referencing. Additionally, a bug in the `is_end_of_month` method in `quantlib/time/calendar.pyx` has been corrected.
1 parent 340d000 commit 38e6a9c

File tree

10 files changed

+253
-36
lines changed

10 files changed

+253
-36
lines changed

quantlib/indexes/ibor_index.pyx

Lines changed: 53 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,34 @@ from quantlib.market.conventions.swap import params as swap_params
2222
from quantlib.indexes.interest_rate_index cimport InterestRateIndex
2323

2424
cdef class IborIndex(InterestRateIndex):
25+
"""Base class for Inter-Bank-Offered-Rate indexes (e.g. Libor, etc.)."""
2526

2627
def __init__(self, str family_name, Period tenor not None, Natural settlement_days,
2728
Currency currency, Calendar fixing_calendar, int convention,
2829
bool end_of_month, DayCounter day_counter not None,
2930
HandleYieldTermStructure yts=HandleYieldTermStructure()):
31+
"""
32+
Parameters
33+
----------
34+
family_name : str
35+
The family name of the index.
36+
tenor : :class:`~quantlib.time.date.Period`
37+
The tenor of the index.
38+
settlement_days : int
39+
The number of settlement days.
40+
currency : :class:`~quantlib.currency.currency.Currency`
41+
The currency of the index.
42+
fixing_calendar : :class:`~quantlib.time.calendar.Calendar`
43+
The calendar used for fixing dates.
44+
convention : int
45+
The business day convention.
46+
end_of_month : bool
47+
Whether the end-of-month rule applies.
48+
day_counter : :class:`~quantlib.time.daycounter.DayCounter`
49+
The day counter for the index.
50+
yts : :class:`~quantlib.termstructures.yield_term_structure.HandleYieldTermStructure`, optional
51+
The yield term structure handle.
52+
"""
3053
self._thisptr = shared_ptr[_in.Index](
3154
new _ib.IborIndex(family_name.encode('utf-8'),
3255
deref(tenor._thisptr),
@@ -40,17 +63,20 @@ cdef class IborIndex(InterestRateIndex):
4063
)
4164

4265
property business_day_convention:
66+
"""The business day convention."""
4367
def __get__(self):
4468
cdef _ib.IborIndex* ref = <_ib.IborIndex*>self._thisptr.get()
4569
return ref.businessDayConvention()
4670

4771
property end_of_month:
72+
"""Whether the end-of-month rule applies."""
4873
def __get__(self):
4974
cdef _ib.IborIndex* ref = <_ib.IborIndex*>self._thisptr.get()
5075
return ref.endOfMonth()
5176

5277
@property
5378
def forwarding_term_structure(self):
79+
"""The curve used to forecast fixings."""
5480
cdef:
5581
_ib.IborIndex* ref = <_ib.IborIndex*>self._thisptr.get()
5682
HandleYieldTermStructure yts = HandleYieldTermStructure.__new__(HandleYieldTermStructure)
@@ -61,8 +87,16 @@ cdef class IborIndex(InterestRateIndex):
6187

6288
@staticmethod
6389
def from_name(market, term_structure=HandleYieldTermStructure(), **kwargs):
64-
"""
65-
Create default IBOR for the market, modify attributes if provided
90+
"""Create a default IBOR index for the given market.
91+
92+
Parameters
93+
----------
94+
market : str
95+
The market name (e.g., 'USDLibor', 'Euribor').
96+
term_structure : :class:`~quantlib.termstructures.yield_term_structure.HandleYieldTermStructure`, optional
97+
The yield term structure handle.
98+
**kwargs :
99+
Additional keyword arguments to override default parameters.
66100
"""
67101

68102
row = swap_params(market)
@@ -86,10 +120,27 @@ cdef class IborIndex(InterestRateIndex):
86120

87121

88122
cdef class OvernightIndex(IborIndex):
123+
"""Base class for overnight indexes."""
89124
def __init__(self, str family_name, Natural settlement_days,
90125
Currency currency, Calendar fixing_calendar,
91126
DayCounter day_counter not None,
92127
HandleYieldTermStructure yts=HandleYieldTermStructure()):
128+
"""
129+
Parameters
130+
----------
131+
family_name : str
132+
The family name of the index.
133+
settlement_days : int
134+
The number of settlement days.
135+
currency : :class:`~quantlib.currency.currency.Currency`
136+
The currency of the index.
137+
fixing_calendar : :class:`~quantlib.time.calendar.Calendar`
138+
The calendar used for fixing dates.
139+
day_counter : :class:`~quantlib.time.daycounter.DayCounter`
140+
The day counter for the index.
141+
yts : :class:`~quantlib.termstructures.yield_term_structure.HandleYieldTermStructure`, optional
142+
The yield term structure handle.
143+
"""
93144
self._thisptr = shared_ptr[_in.Index](
94145
new _ib.OvernightIndex(family_name.encode('utf-8'),
95146
settlement_days,

quantlib/indexes/inflation_index.pyx

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,27 +32,37 @@ cimport quantlib.currency._currency as _cu
3232
cimport quantlib.indexes._region as _region
3333

3434
cdef class InflationIndex(Index):
35+
"""Base class for inflation-rate indexes."""
3536

3637
def __cinit__(self):
3738
pass
3839

3940
property family_name:
41+
"""The family name of the inflation index."""
4042
def __get__(self):
4143
cdef _ii.InflationIndex* ref = <_ii.InflationIndex*>self._thisptr.get()
4244
return ref.familyName()
4345

4446
property frequency:
47+
"""The publication frequency of the inflation index."""
4548
def __get__(self):
4649
cdef _ii.InflationIndex* ref = <_ii.InflationIndex*>self._thisptr.get()
4750
return ref.frequency()
4851

4952
property availability_lag:
53+
"""The availability lag of the index.
54+
55+
The availability lag describes when the index might be
56+
available; for instance, the inflation value for January
57+
may only be available in April.
58+
"""
5059
def __get__(self):
5160
cdef _ii.InflationIndex* ref = <_ii.InflationIndex*>self._thisptr.get()
5261
return period_from_qlperiod(ref.availabilityLag())
5362

5463

5564
property currency:
65+
"""The currency of the inflation index."""
5666
def __get__(self):
5767
cdef _ii.InflationIndex* ref = <_ii.InflationIndex*>self._thisptr.get()
5868
cdef Currency c = Currency.__new__(Currency)
@@ -61,20 +71,39 @@ cdef class InflationIndex(Index):
6171

6272
@property
6373
def region(self):
74+
"""The region of the index."""
6475
cdef _ii.InflationIndex* ref = <_ii.InflationIndex*>self._thisptr.get()
6576
cdef Region region = Region.__new__(Region)
6677
region._thisptr = new _region.Region(ref.region())
6778
return region
6879

6980
cdef class ZeroInflationIndex(InflationIndex):
81+
"""Base class for zero-inflation indexes."""
7082
def __init__(self, str family_name,
7183
Region region,
7284
bool revised,
7385
Frequency frequency,
7486
Period availabilityLag,
7587
Currency currency,
7688
ZeroInflationTermStructure ts=ZeroInflationTermStructure()):
77-
89+
"""
90+
Parameters
91+
----------
92+
family_name : str
93+
The family name of the index.
94+
region : :class:`~quantlib.indexes.region.Region`
95+
The region of the index.
96+
revised : bool
97+
Whether the index is revised.
98+
frequency : :class:`~quantlib.time.frequency.Frequency`
99+
The frequency of the index.
100+
availabilityLag : :class:`~quantlib.time.date.Period`
101+
The availability lag of the index.
102+
currency : :class:`~quantlib.currency.currency.Currency`
103+
The currency of the index.
104+
ts : :class:`~quantlib.termstructures.inflation_term_structure.ZeroInflationTermStructure`, optional
105+
The zero-inflation term structure.
106+
"""
78107
# convert the Python str to C++ string
79108
cdef string c_family_name = family_name.encode('utf-8')
80109

@@ -89,6 +118,7 @@ cdef class ZeroInflationIndex(InflationIndex):
89118
ts._handle))
90119

91120
def zero_inflation_term_structure(self):
121+
"""Returns the zero-inflation term structure associated with the index."""
92122
cdef ZeroInflationTermStructure r = \
93123
ZeroInflationTermStructure.__new__(ZeroInflationTermStructure)
94124
r._thisptr = static_pointer_cast[_its.InflationTermStructure](
@@ -97,17 +127,40 @@ cdef class ZeroInflationIndex(InflationIndex):
97127

98128
@property
99129
def last_fixing_date(self):
130+
"""Returns the last date for which a fixing was provided."""
100131
return date_from_qldate(
101132
(<_ii.ZeroInflationIndex*>(self._thisptr.get())).lastFixingDate()
102133
)
103134

104135

105136
cdef class YoYInflationIndex(ZeroInflationIndex):
137+
"""Base class for year-on-year inflation indexes.
138+
139+
These may be quoted indices published on, say, Bloomberg, or can be
140+
defined as the ratio of an index at different time points.
141+
"""
106142
def __init__(self, family_name, Region region, bool revised,
107143
Frequency frequency,
108144
Period availability_lag, Currency currency,
109145
YoYInflationTermStructure ts=YoYInflationTermStructure()):
110-
146+
"""
147+
Parameters
148+
----------
149+
family_name : str
150+
The family name of the index.
151+
region : :class:`~quantlib.indexes.region.Region`
152+
The region of the index.
153+
revised : bool
154+
Whether the index is revised.
155+
frequency : :class:`~quantlib.time.frequency.Frequency`
156+
The frequency of the index.
157+
availability_lag : :class:`~quantlib.time.date.Period`
158+
The availability lag of the index.
159+
currency : :class:`~quantlib.currency.currency.Currency`
160+
The currency of the index.
161+
ts : :class:`~quantlib.termstructures.inflation_term_structure.YoYInflationTermStructure`, optional
162+
The year-on-year inflation term structure.
163+
"""
111164
cdef string c_family_name = family_name.encode('utf-8')
112165

113166
self._thisptr = shared_ptr[_in.Index](

quantlib/instruments/swap.pyx

Lines changed: 51 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,44 +23,88 @@ cdef inline _swap.Swap* get_swap(Swap swap) noexcept:
2323

2424

2525
cdef class Swap(Instrument):
26-
"""
27-
Base swap class
28-
"""
26+
"""Interest rate swap."""
2927
Payer = Type.Payer
3028
Receiver = Type.Receiver
3129

3230
def __init__(self, Leg first_leg, Leg second_leg):
33-
""" The cash flows belonging to the first leg are paid;
34-
the ones belonging to the second leg are received"""
35-
31+
"""
32+
The cash flows belonging to the first leg are paid; the ones belonging
33+
to the second leg are received.
34+
35+
Parameters
36+
----------
37+
first_leg : :class:`~quantlib.cashflow.Leg`
38+
The first leg of the swap.
39+
second_leg : :class:`~quantlib.cashflow.Leg`
40+
The second leg of the swap.
41+
"""
3642
self._thisptr.reset(new _swap.Swap(first_leg._thisptr, second_leg._thisptr))
3743

3844
property start_date:
45+
"""The start date of the swap."""
3946
def __get__(self):
4047
cdef _date.Date dt = get_swap(self).startDate()
4148
return date_from_qldate(dt)
4249

4350
property maturity_date:
51+
"""The maturity date of the swap."""
4452
def __get__(self):
4553
cdef _date.Date dt = get_swap(self).maturityDate()
4654
return date_from_qldate(dt)
4755

4856
def leg_BPS(self, Size j):
57+
"""The basis-point sensitivity of the j-th leg of the swap.
58+
59+
Parameters
60+
----------
61+
j : int
62+
The index of the leg.
63+
"""
4964
return get_swap(self).legBPS(j)
5065

5166
def leg_NPV(self, Size j):
67+
"""The net present value of the j-th leg of the swap.
68+
69+
Parameters
70+
----------
71+
j : int
72+
The index of the leg.
73+
"""
5274
return get_swap(self).legNPV(j)
5375

5476
def startDiscounts(self, Size j):
77+
"""The discount factor at the start of the j-th leg of the swap.
78+
79+
Parameters
80+
----------
81+
j : int
82+
The index of the leg.
83+
"""
5584
return get_swap(self).startDiscounts(j)
5685

5786
def endDiscounts(self, Size j):
87+
"""The discount factor at the end of the j-th leg of the swap.
88+
89+
Parameters
90+
----------
91+
j : int
92+
The index of the leg.
93+
"""
5894
return get_swap(self).endDiscounts(j)
5995

6096
def npv_date_discount(self):
97+
"""The discount factor at the NPV date."""
6198
return get_swap(self).npvDateDiscount()
6299

63100
def leg(self, int i):
101+
"""The i-th leg of the swap.
102+
103+
Parameters
104+
----------
105+
i : int
106+
The index of the leg.
107+
"""
64108
cdef Leg leg = Leg.__new__(Leg)
65109
cdef _swap.Swap* swap = <_swap.Swap*>self._thisptr.get()
66110
if 0 <= i < swap.numberOfLegs():
@@ -70,6 +114,7 @@ cdef class Swap(Instrument):
70114
return leg
71115

72116
def __getitem__(self, int i):
117+
"""The i-th leg of the swap."""
73118
cdef Leg leg = Leg.__new__(Leg)
74119
cdef _swap.Swap* swap = <_swap.Swap*>self._thisptr.get()
75120
if 0 <= i < swap.numberOfLegs():

quantlib/instruments/vanillaswap.pyx

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,7 @@ cdef inline _vanillaswap.VanillaSwap* get_vanillaswap(VanillaSwap swap):
1717
return <_vanillaswap.VanillaSwap*>swap._thisptr.get()
1818

1919
cdef class VanillaSwap(FixedVsFloatingSwap):
20-
"""
21-
Vanilla swap class
22-
"""
20+
"""Plain-vanilla swap: fix vs ibor leg."""
2321

2422
def __init__(self, Type type,
2523
Real nominal,
@@ -31,6 +29,30 @@ cdef class VanillaSwap(FixedVsFloatingSwap):
3129
Spread spread,
3230
DayCounter floating_daycount not None,
3331
int payment_convention=-1):
32+
"""
33+
Parameters
34+
----------
35+
type : :class:`~quantlib.instruments.swap.Type`
36+
The swap type, either `Payer` or `Receiver`.
37+
nominal : float
38+
The swap nominal.
39+
fixed_schedule : :class:`~quantlib.time.schedule.Schedule`
40+
The schedule for the fixed leg.
41+
fixed_rate : float
42+
The fixed rate.
43+
fixed_daycount : :class:`~quantlib.time.daycounter.DayCounter`
44+
The day counter for the fixed leg.
45+
float_schedule : :class:`~quantlib.time.schedule.Schedule`
46+
The schedule for the floating leg.
47+
ibor_index : :class:`~quantlib.indexes.ibor_index.IborIndex`
48+
The IBOR index for the floating leg.
49+
spread : float
50+
The spread over the IBOR index.
51+
floating_daycount : :class:`~quantlib.time.daycounter.DayCounter`
52+
The day counter for the floating leg.
53+
payment_convention : int, optional
54+
The business day convention for payment dates.
55+
"""
3456
cdef optional[BusinessDayConvention] opt_payment_convention
3557
if payment_convention > 0:
3658
opt_payment_convention = <BusinessDayConvention>payment_convention

0 commit comments

Comments
 (0)