From 7d2bdbb53fbbfebc5e7ab2e4a54d8afff9e47513 Mon Sep 17 00:00:00 2001 From: shubhendra Date: Thu, 25 Mar 2021 23:28:37 +0530 Subject: [PATCH 1/7] Add .deepsource.toml Signed-off-by: shubhendra --- .deepsource.toml | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 .deepsource.toml diff --git a/.deepsource.toml b/.deepsource.toml new file mode 100644 index 000000000..96121e293 --- /dev/null +++ b/.deepsource.toml @@ -0,0 +1,15 @@ +version = 1 + +test_patterns = ["tf_quant_finance/**/*_test.py"] + +exclude_patterns = [ + "api_docs/**", + "third_party/sobol_data/**" +] + +[[analyzers]] +name = "python" +enabled = true + + [analyzers.meta] + runtime_version = "3.x.x" From bf4a1e81517a1fceeb830260f4e8a0f4704efa5c Mon Sep 17 00:00:00 2001 From: shubhendra Date: Thu, 25 Mar 2021 23:28:37 +0530 Subject: [PATCH 2/7] Remove unnecessary use of comprehension Signed-off-by: shubhendra --- tf_quant_finance/models/hull_white/swaption.py | 2 +- tf_quant_finance/models/hull_white/zero_coupon_bond_option.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tf_quant_finance/models/hull_white/swaption.py b/tf_quant_finance/models/hull_white/swaption.py index b7f2eb32e..227134da9 100644 --- a/tf_quant_finance/models/hull_white/swaption.py +++ b/tf_quant_finance/models/hull_white/swaption.py @@ -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. From c3a52e52b1d6099114175fa04b263484bc235e0c Mon Sep 17 00:00:00 2001 From: shubhendra Date: Thu, 25 Mar 2021 23:28:38 +0530 Subject: [PATCH 3/7] Use literal syntax instead of function calls to create data structure Signed-off-by: shubhendra --- tf_quant_finance/models/hull_white/swaption.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tf_quant_finance/models/hull_white/swaption.py b/tf_quant_finance/models/hull_white/swaption.py index 227134da9..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 From ce17bd5e40f7f2b743ef8cb01e990ae12110b806 Mon Sep 17 00:00:00 2001 From: shubhendra Date: Thu, 25 Mar 2021 23:28:39 +0530 Subject: [PATCH 4/7] Use `callable()` to check if the object is calllable Signed-off-by: shubhendra --- tf_quant_finance/math/pde/fd_solvers.py | 2 +- tf_quant_finance/models/legacy/brownian_motion_utils.py | 2 +- tf_quant_finance/models/sabr/sabr_model.py | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) 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/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/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) From 6cbb6a7e113abe67bbbcf49425913140a67021c6 Mon Sep 17 00:00:00 2001 From: shubhendra Date: Thu, 25 Mar 2021 23:28:40 +0530 Subject: [PATCH 5/7] Refactor unnecessary `else` / `elif` when `if` block has a `break` statement Signed-off-by: shubhendra --- tf_quant_finance/models/hjm/gaussian_hjm_test.py | 11 +++++------ .../models/hjm/quasi_gaussian_hjm_test.py | 11 +++++------ tf_quant_finance/models/hull_white/hull_white_test.py | 11 +++++------ 3 files changed, 15 insertions(+), 18 deletions(-) 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) From 813be322465c37f2edc1e1df8333a1c17803cddb Mon Sep 17 00:00:00 2001 From: shubhendra Date: Thu, 25 Mar 2021 23:28:41 +0530 Subject: [PATCH 6/7] Remove implicit `object` from the base class Signed-off-by: shubhendra --- tf_quant_finance/math/optimizer/conjugate_gradient.py | 2 +- tf_quant_finance/math/piecewise.py | 2 +- tf_quant_finance/models/ito_process.py | 2 +- tf_quant_finance/models/legacy/ito_process.py | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) 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/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/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/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: From 53cff5edb6c090356f4890233f4f1768c60c23bf Mon Sep 17 00:00:00 2001 From: Shubhendra Singh Chauhan Date: Tue, 30 Mar 2021 01:17:33 +0530 Subject: [PATCH 7/7] Delete .deepsource.toml --- .deepsource.toml | 15 --------------- 1 file changed, 15 deletions(-) delete mode 100644 .deepsource.toml diff --git a/.deepsource.toml b/.deepsource.toml deleted file mode 100644 index 96121e293..000000000 --- a/.deepsource.toml +++ /dev/null @@ -1,15 +0,0 @@ -version = 1 - -test_patterns = ["tf_quant_finance/**/*_test.py"] - -exclude_patterns = [ - "api_docs/**", - "third_party/sobol_data/**" -] - -[[analyzers]] -name = "python" -enabled = true - - [analyzers.meta] - runtime_version = "3.x.x"