diff --git a/tf_quant_finance/math/optimizer/conjugate_gradient.py b/tf_quant_finance/math/optimizer/conjugate_gradient.py index a0fa65197..43f732db6 100644 --- a/tf_quant_finance/math/optimizer/conjugate_gradient.py +++ b/tf_quant_finance/math/optimizer/conjugate_gradient.py @@ -108,7 +108,7 @@ @attr.s -class ConjugateGradientParams(object): +class ConjugateGradientParams: """Adjustable parameters of conjugate gradient algorithm.""" # Real number. Sufficient decrease parameter for Wolfe conditions. # Corresponds to `delta` in [HZ2006]. diff --git a/tf_quant_finance/math/pde/fd_solvers.py b/tf_quant_finance/math/pde/fd_solvers.py index 258f2cc86..af43e59cb 100644 --- a/tf_quant_finance/math/pde/fd_solvers.py +++ b/tf_quant_finance/math/pde/fd_solvers.py @@ -691,7 +691,7 @@ def _is_callable(var_or_fn): """Returns whether an object is callable or not.""" # Python 2.7 as well as Python 3.x with x > 2 support 'callable'. # In between, callable was removed hence we need to do a more expansive check - if hasattr(var_or_fn, '__call__'): + if callable(var_or_fn): return True try: return callable(var_or_fn) diff --git a/tf_quant_finance/math/piecewise.py b/tf_quant_finance/math/piecewise.py index 9499fa99d..59a4beaa3 100644 --- a/tf_quant_finance/math/piecewise.py +++ b/tf_quant_finance/math/piecewise.py @@ -17,7 +17,7 @@ import tensorflow.compat.v2 as tf -class PiecewiseConstantFunc(object): +class PiecewiseConstantFunc: """Creates a piecewise constant function.""" def __init__(self, jump_locations, values, dtype=None, name=None): diff --git a/tf_quant_finance/models/hjm/gaussian_hjm_test.py b/tf_quant_finance/models/hjm/gaussian_hjm_test.py index 7d67d03ce..e86e5ba1f 100644 --- a/tf_quant_finance/models/hjm/gaussian_hjm_test.py +++ b/tf_quant_finance/models/hjm/gaussian_hjm_test.py @@ -37,12 +37,11 @@ def _true_std_time_dep(t, intervals, vol, k): var = 0.0 for j in range(len(intervals) - 1): if tt >= intervals[j] and tt < intervals[j + 1]: - var = var + vol[j]**2 / 2 / k * ( - np.exp(2 * k * tt) - np.exp(2 * k * intervals[j])) - break - else: - var = var + vol[j]**2 / 2 / k * ( - np.exp(2 * k * intervals[j + 1]) - np.exp(2 * k * intervals[j])) + var = var + vol[j]**2 / 2 / k * ( + np.exp(2 * k * tt) - np.exp(2 * k * intervals[j])) + break + var = var + vol[j]**2 / 2 / k * ( + np.exp(2 * k * intervals[j + 1]) - np.exp(2 * k * intervals[j])) else: var = var + vol[-1]**2/2/k *(np.exp(2*k*tt)-np.exp(2*k*intervals[-1])) res[i] = np.exp(-k*tt) * np.sqrt(var) diff --git a/tf_quant_finance/models/hjm/quasi_gaussian_hjm_test.py b/tf_quant_finance/models/hjm/quasi_gaussian_hjm_test.py index 24460bbd9..94dd8d87f 100644 --- a/tf_quant_finance/models/hjm/quasi_gaussian_hjm_test.py +++ b/tf_quant_finance/models/hjm/quasi_gaussian_hjm_test.py @@ -61,12 +61,11 @@ def _true_std_time_dep(t, intervals, vol, k): var = 0.0 for j in range(len(intervals) - 1): if tt >= intervals[j] and tt < intervals[j + 1]: - var = var + vol[j]**2 / 2 / k * ( - np.exp(2 * k * tt) - np.exp(2 * k * intervals[j])) - break - else: - var = var + vol[j]**2 / 2 / k * ( - np.exp(2 * k * intervals[j + 1]) - np.exp(2 * k * intervals[j])) + var = var + vol[j]**2 / 2 / k * ( + np.exp(2 * k * tt) - np.exp(2 * k * intervals[j])) + break + var = var + vol[j]**2 / 2 / k * ( + np.exp(2 * k * intervals[j + 1]) - np.exp(2 * k * intervals[j])) else: var = var + vol[-1]**2/2/k *(np.exp(2*k*tt)-np.exp(2*k*intervals[-1])) res[i] = np.exp(-k*tt) * np.sqrt(var) diff --git a/tf_quant_finance/models/hull_white/hull_white_test.py b/tf_quant_finance/models/hull_white/hull_white_test.py index bd5c8575a..e2b997626 100644 --- a/tf_quant_finance/models/hull_white/hull_white_test.py +++ b/tf_quant_finance/models/hull_white/hull_white_test.py @@ -60,12 +60,11 @@ def _true_std_time_dep(t, intervals, vol, k): var = 0.0 for j in range(len(intervals) - 1): if tt >= intervals[j] and tt < intervals[j + 1]: - var = var + vol[j]**2 / 2 / k * ( - np.exp(2 * k * tt) - np.exp(2 * k * intervals[j])) - break - else: - var = var + vol[j]**2 / 2 / k * ( - np.exp(2 * k * intervals[j + 1]) - np.exp(2 * k * intervals[j])) + var = var + vol[j]**2 / 2 / k * ( + np.exp(2 * k * tt) - np.exp(2 * k * intervals[j])) + break + var = var + vol[j]**2 / 2 / k * ( + np.exp(2 * k * intervals[j + 1]) - np.exp(2 * k * intervals[j])) else: var = var + vol[-1]**2/2/k *(np.exp(2*k*tt)-np.exp(2*k*intervals[-1])) res[i] = np.exp(-k*tt) * np.sqrt(var) diff --git a/tf_quant_finance/models/hull_white/swaption.py b/tf_quant_finance/models/hull_white/swaption.py index b7f2eb32e..3a29a1b91 100644 --- a/tf_quant_finance/models/hull_white/swaption.py +++ b/tf_quant_finance/models/hull_white/swaption.py @@ -209,7 +209,7 @@ def _map_payoff_to_sim_times(indices, payoff, num_samples): """ indices = tf.expand_dims(indices, axis=0) indices = tf.repeat(indices, num_samples, axis=0) - index_list = list() + index_list = [] tensor_shape = np.array(indices.shape.as_list()) output_shape = indices.shape.as_list()[:-1] + [ tf.math.reduce_max(indices) + 1 @@ -272,7 +272,7 @@ def _analytic_valuation(expiries, floating_leg_start_times, name=name + '_jamshidian_decomposition') bond_strike_rank = breakeven_bond_option_strikes.shape.rank - perm = [bond_strike_rank-1] + [x for x in range(0, bond_strike_rank - 1)] + perm = [bond_strike_rank-1] + list(range(0, bond_strike_rank - 1)) breakeven_bond_option_strikes = tf.transpose( breakeven_bond_option_strikes, perm=perm) bond_option_prices = zcb.bond_option_price( diff --git a/tf_quant_finance/models/hull_white/zero_coupon_bond_option.py b/tf_quant_finance/models/hull_white/zero_coupon_bond_option.py index 2fdcfe464..aa192a1f1 100644 --- a/tf_quant_finance/models/hull_white/zero_coupon_bond_option.py +++ b/tf_quant_finance/models/hull_white/zero_coupon_bond_option.py @@ -240,7 +240,7 @@ def _analytic_valuation(discount_rate_fn, model, strikes, expiries, maturities, # Make `dim` as the last dimension and return. return tf.transpose( option_value, - perm=[i for i in range(1, len(option_value.shape.as_list()))] + [0]) + perm=list(range(1, len(option_value.shape.as_list()))) + [0]) # TODO(b/158501671): Clean-up this implementation. diff --git a/tf_quant_finance/models/ito_process.py b/tf_quant_finance/models/ito_process.py index e8c895884..852efa2ca 100644 --- a/tf_quant_finance/models/ito_process.py +++ b/tf_quant_finance/models/ito_process.py @@ -38,7 +38,7 @@ @six.add_metaclass(abc.ABCMeta) -class ItoProcess(object): +class ItoProcess: """Interface for specifying Ito processes. Interface for defining stochastic process defined by the Ito SDE: diff --git a/tf_quant_finance/models/legacy/brownian_motion_utils.py b/tf_quant_finance/models/legacy/brownian_motion_utils.py index 8826e7c81..0b827bbfd 100644 --- a/tf_quant_finance/models/legacy/brownian_motion_utils.py +++ b/tf_quant_finance/models/legacy/brownian_motion_utils.py @@ -22,7 +22,7 @@ def is_callable(var_or_fn): """Returns whether an object is callable or not.""" # Python 2.7 as well as Python 3.x with x > 2 support 'callable'. # In between, callable was removed hence we need to do a more expansive check - if hasattr(var_or_fn, '__call__'): + if callable(var_or_fn): return True try: return callable(var_or_fn) diff --git a/tf_quant_finance/models/legacy/ito_process.py b/tf_quant_finance/models/legacy/ito_process.py index 0d2f130f4..6bb36e445 100644 --- a/tf_quant_finance/models/legacy/ito_process.py +++ b/tf_quant_finance/models/legacy/ito_process.py @@ -41,7 +41,7 @@ @six.add_metaclass(abc.ABCMeta) -class ItoProcess(object): +class ItoProcess: """Base class for Ito processes. Represents a general Ito process: diff --git a/tf_quant_finance/models/sabr/sabr_model.py b/tf_quant_finance/models/sabr/sabr_model.py index 922bac679..e837de207 100644 --- a/tf_quant_finance/models/sabr/sabr_model.py +++ b/tf_quant_finance/models/sabr/sabr_model.py @@ -518,7 +518,7 @@ def _is_callable(var_or_fn): """Returns whether an object is callable or not.""" # Python 2.7 as well as Python 3.x with x > 2 support 'callable'. # In between, callable was removed hence we need to do a more expansive check - if hasattr(var_or_fn, '__call__'): + if callable(var_or_fn): return True try: return callable(var_or_fn)