Skip to content

Commit 39e7b6a

Browse files
committed
Pylint fixes
1 parent be66b82 commit 39e7b6a

38 files changed

+628
-452
lines changed

financepy/market/curves/composite_discount_curve.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,18 @@ class CompositeDiscountCurve(DiscountCurve):
1818
A discount curve that is a sum (in rates) of 'children' discount curves
1919
"""
2020

21-
###############################################################################
21+
###########################################################################
2222

2323
def __init__(self, child_curves: List[DiscountCurve]):
2424
"""
25-
Create a discount curve that is a sum (in rates) of other discount curves
25+
Create a discount curve that is a sum (in rates) of other
26+
discount curves
2627
"""
2728

2829
check_argument_types(self.__init__, locals())
29-
assert len(child_curves) > 0, "Empty list of child curves is not supported"
30+
assert (
31+
len(child_curves) > 0
32+
), "Empty list of child curves is not supported"
3033

3134
self._children = child_curves
3235

@@ -38,12 +41,13 @@ def __init__(self, child_curves: List[DiscountCurve]):
3841
# Read off the first child
3942
self.dc_type = self._children[0].dc_type
4043

41-
###############################################################################
44+
###########################################################################
4245

4346
def df_t(self, t: Union[float, np.ndarray]):
4447
"""
4548
Return discount factors given a single or vector of dates.
46-
ParentRate = Sum of children rates => Parent DF = product of children dfs
49+
ParentRate = Sum of children rates => Parent DF = product of
50+
children dfs
4751
"""
4852

4953
dfs = np.ones_like(np.atleast_1d(t), dtype=float)
@@ -53,14 +57,14 @@ def df_t(self, t: Union[float, np.ndarray]):
5357

5458
return dfs
5559

56-
###############################################################################
60+
###########################################################################
5761

5862
def __repr__(self):
5963
s = label_to_string("OBJECT TYPE", type(self).__name__)
6064
s += label_to_string("CHILDREN", (self._children))
6165
return s
6266

63-
###############################################################################
67+
###########################################################################
6468

6569
def _print(self):
6670
"""Simple print function for backward compatibility."""

financepy/market/curves/discount_curve.py

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,18 @@ def dfs(self) -> np.ndarray:
9999

100100
###########################################################################
101101

102+
def set_df(self, index, df):
103+
"""Set the discount factor at a specific index."""
104+
105+
n_points = len(self._dfs)
106+
107+
if index < 0 or index >= n_points:
108+
raise IndexError("Index out of bounds")
109+
110+
self._dfs[index] = df
111+
112+
###########################################################################
113+
102114
def _zero_to_df(
103115
self,
104116
value_dt: Date, # TODO: why is value_dt not used ?
@@ -122,12 +134,12 @@ def _zero_to_df(
122134
df = np.exp(-rates * t)
123135
elif freq_type == FrequencyTypes.SIMPLE:
124136
df = 1.0 / (1.0 + rates * t)
125-
elif (
126-
freq_type == FrequencyTypes.ANNUAL
127-
or freq_type == FrequencyTypes.SEMI_ANNUAL
128-
or freq_type == FrequencyTypes.QUARTERLY
129-
or freq_type == FrequencyTypes.MONTHLY
130-
):
137+
elif freq_type in {
138+
FrequencyTypes.ANNUAL,
139+
FrequencyTypes.SEMI_ANNUAL,
140+
FrequencyTypes.QUARTERLY,
141+
FrequencyTypes.MONTHLY,
142+
}:
131143
df = 1.0 / np.power(1.0 + rates / f, f * t)
132144
else:
133145
raise FinError("Unknown Frequency type")
@@ -217,15 +229,15 @@ def zero_rate(
217229

218230
if isinstance(dts, Date):
219231
return zero_rates[0]
220-
else:
221-
return np.array(zero_rates)
222232

223-
return zero_rates
233+
return np.array(zero_rates)
224234

225235
###########################################################################
226236

227237
def cc_rate(
228-
self, dts: Union[list, Date], dc_type: DayCountTypes = DayCountTypes.SIMPLE
238+
self,
239+
dts: Union[list, Date],
240+
dc_type: DayCountTypes = DayCountTypes.SIMPLE,
229241
):
230242
"""Calculation of zero rates with continuous compounding. This
231243
function can return a vector of cc rates given a vector of
@@ -262,7 +274,8 @@ def swap_rate(
262274

263275
if freq_type == FrequencyTypes.SIMPLE:
264276
raise FinError("Cannot calculate par rate with simple yield freq.")
265-
elif freq_type == FrequencyTypes.CONTINUOUS:
277+
278+
if freq_type == FrequencyTypes.CONTINUOUS:
266279
raise FinError("Cannot calculate par rate with continuous freq.")
267280

268281
if isinstance(maturity_dt, Date):
@@ -305,8 +318,8 @@ def swap_rate(
305318

306319
if isinstance(maturity_dts, Date):
307320
return par_rates[0]
308-
else:
309-
return par_rates
321+
322+
return par_rates
310323

311324
###########################################################################
312325

@@ -378,8 +391,8 @@ def fwd(self, dts: Date):
378391

379392
if isinstance(dts, Date):
380393
return fwd[0]
381-
else:
382-
return np.array(fwd)
394+
395+
return np.array(fwd)
383396

384397
###########################################################################
385398

@@ -461,8 +474,8 @@ def fwd_rate(
461474

462475
if isinstance(start_dt, Date):
463476
return fwd_rates[0]
464-
else:
465-
return np.array(fwd_rates)
477+
478+
return np.array(fwd_rates)
466479

467480
###########################################################################
468481

financepy/market/curves/discount_curve_flat.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class DiscountCurveFlat(DiscountCurve):
2828
It is used for quick and dirty analysis and when limited information is
2929
available. It inherits several methods from DiscountCurve."""
3030

31-
###############################################################################
31+
###########################################################################
3232

3333
def __init__(
3434
self,
@@ -65,7 +65,7 @@ def __init__(
6565
self._times = times_from_dates(dts, self.value_dt, dc_type)
6666
self._dfs = self.df(dts)
6767

68-
###############################################################################
68+
###########################################################################
6969

7070
@property
7171
def times(self) -> np.ndarray:
@@ -77,7 +77,7 @@ def dfs(self) -> np.ndarray:
7777
"""Return the cached grid of discount factors."""
7878
return self._dfs
7979

80-
###############################################################################
80+
###########################################################################
8181

8282
def df(self, dts: Union[Date, list]):
8383
"""Return discount factors given a single or vector of dts. The
@@ -90,27 +90,34 @@ def df(self, dts: Union[Date, list]):
9090
dc_times = times_from_dates(dts, self.value_dt, self.dc_type)
9191

9292
dfs = self._zero_to_df(
93-
self.value_dt, self.flat_rate, dc_times, self.freq_type, self.dc_type
93+
self.value_dt,
94+
self.flat_rate,
95+
dc_times,
96+
self.freq_type,
97+
self.dc_type,
9498
)
9599

96100
if isinstance(dts, Date):
97101
return dfs[0]
98102

99103
return np.array(dfs)
100104

101-
###############################################################################
105+
###########################################################################
102106

103107
def bump(self, bump_size: float):
104108
"""Create a new FinDiscountCurveFlat object with the entire curve
105109
bumped up by the bumpsize. All other parameters are preserved."""
106110

107111
rate_bumped = self.flat_rate + bump_size
108112
disc_curve = DiscountCurveFlat(
109-
self.value_dt, rate_bumped, freq_type=self.freq_type, dc_type=self.dc_type
113+
self.value_dt,
114+
rate_bumped,
115+
freq_type=self.freq_type,
116+
dc_type=self.dc_type,
110117
)
111118
return disc_curve
112119

113-
###############################################################################
120+
###########################################################################
114121

115122
def __repr__(self):
116123
s = label_to_string("OBJECT TYPE", type(self).__name__)
@@ -120,7 +127,7 @@ def __repr__(self):
120127
s += label_to_string("DAY COUNT", (self.dc_type))
121128
return s
122129

123-
###############################################################################
130+
###########################################################################
124131

125132
def _print(self):
126133
"""Simple print function for backward compatibility."""

financepy/market/curves/discount_curve_ns.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ def __init__(
3838
dc_type: DayCountTypes = DayCountTypes.ACT_ACT_ISDA,
3939
):
4040
"""Creation of a FinDiscountCurveNS object. Parameters are provided
41-
individually for beta_0, beta_1, beta_2 and tau. The zero rates produced
42-
by this parametrisation have an implicit compounding convention that
43-
defaults to continuous but which can be overridden."""
41+
individually for beta_0, beta_1, beta_2 and tau. The zero rates
42+
produced by this parametrisation have an implicit compounding
43+
convention that defaults to continuous but which can be overridden."""
4444

4545
check_argument_types(self.__init__, locals())
4646

@@ -128,8 +128,8 @@ def df(self, dates: Union[Date, list]):
128128

129129
if isinstance(dates, Date):
130130
return df[0]
131-
else:
132-
return df
131+
132+
return df
133133

134134
###########################################################################
135135

financepy/market/curves/discount_curve_nss.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,8 @@ def zero_rate(
100100

101101
if isinstance(dts, Date):
102102
return zero_rates[0]
103-
else:
104-
return np.array(zero_rates)
103+
104+
return np.array(zero_rates)
105105

106106
###########################################################################
107107

@@ -142,8 +142,8 @@ def df(self, dates: Union[Date, list]):
142142

143143
if isinstance(dates, Date):
144144
return df[0]
145-
else:
146-
return df
145+
146+
return df
147147

148148
###########################################################################
149149

financepy/market/curves/discount_curve_pwf_onf.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
# Copyright (C) 2018, 2019, 2020 Dominic O'Kane
33
##############################################################################
44

5-
import numpy as np
65
from typing import Union
6+
import numpy as np
77
from scipy import interpolate
88

99
from ...utils.date import Date
@@ -56,12 +56,16 @@ def __init__(
5656
self.freq_type = FrequencyTypes.CONTINUOUS
5757
self.dc_type = DayCountTypes.SIMPLE
5858

59-
dc_times = times_from_dates(self._knot_dts, self.value_dt, self.dc_type)
59+
dc_times = times_from_dates(
60+
self._knot_dts, self.value_dt, self.dc_type
61+
)
6062

6163
self._times = np.atleast_1d(dc_times)
6264

6365
# it is easier to deal in log(dfs), log(df[Ti]) = -\int_0^T_i f(u) du
64-
self._logdfs = -np.cumsum(np.diff(self._times, prepend=0.0) * self._onfwd_rates)
66+
self._logdfs = -np.cumsum(
67+
np.diff(self._times, prepend=0.0) * self._onfwd_rates
68+
)
6569
self._logdfs_interp = interpolate.interp1d(
6670
np.concatenate(([0.0], self._times)),
6771
np.concatenate(([0.0], self._logdfs)),
@@ -101,7 +105,9 @@ def brick_wall_curve(
101105
###############################################################################
102106

103107
@classmethod
104-
def flat_curve(cls, valuation_date: Date, level: float = 1.0 * G_BASIS_POINT):
108+
def flat_curve(
109+
cls, valuation_date: Date, level: float = 1.0 * G_BASIS_POINT
110+
):
105111
knot_dts = [valuation_date.add_tenor("1Y")]
106112
onfwd_rates = [level]
107113
return cls(valuation_date, knot_dts, onfwd_rates)

financepy/market/curves/discount_curve_zeros.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class DiscountCurveZeros(DiscountCurve):
3232
between the zero rates given and for this we must specify an interpolation
3333
convention. The class inherits methods from DiscountCurve."""
3434

35-
###############################################################################
35+
###########################################################################
3636

3737
def __init__(
3838
self,
@@ -64,7 +64,9 @@ def __init__(
6464
raise FinError("Unknown Frequency type " + str(freq_type))
6565

6666
if dc_type not in DayCountTypes:
67-
raise FinError("Unknown Cap Floor DayCountRule type " + str(dc_type))
67+
raise FinError(
68+
"Unknown Cap Floor DayCountRule type " + str(dc_type)
69+
)
6870

6971
self.value_dt = value_dt
7072
self.freq_type = freq_type
@@ -79,7 +81,11 @@ def __init__(
7981
raise FinError("Times or dates are not sorted in increasing order")
8082

8183
dfs = self._zero_to_df(
82-
self.value_dt, self._zero_rates, self._times, self.freq_type, self.dc_type
84+
self.value_dt,
85+
self._zero_rates,
86+
self._times,
87+
self.freq_type,
88+
self.dc_type,
8389
)
8490

8591
self._dfs = np.array(dfs)
@@ -88,26 +94,26 @@ def __init__(
8894
self._interpolator = Interpolator(self._interp_type)
8995
self.fit(self._times, self._dfs)
9096

91-
# ###############################################################################
97+
# #########################################################################
9298

9399
# def bump(self, bump_size):
94-
# """ Calculate the continuous forward rate at the forward date. """
100+
# """ Calculate the continuous forward rate at the forward date."""
95101

96102
# times = self.times.copy()
97103
# discount_factors = self._discount_factors.copy()
98104

99105
# n = len(self.times)
100106
# for i in range(0, n):
101107
# t = times[i]
102-
# discount_factors[i] = discount_factors[i] * np.exp(-bump_size*t)
108+
# discount_factors[i] = discount_factors[i] * np.exp(-bump_size*t)
103109

104110
# disc_curve = DiscountCurve(self.value_dt, times,
105111
# discount_factors,
106112
# self._interp_type)
107113

108114
# return disc_curve
109115

110-
###############################################################################
116+
###########################################################################
111117

112118
def __repr__(self):
113119

@@ -126,7 +132,7 @@ def __repr__(self):
126132

127133
return s
128134

129-
###############################################################################
135+
###########################################################################
130136

131137
def _print(self):
132138
"""Simple print function for backward compatibility."""

0 commit comments

Comments
 (0)