docstr: Demonstrates the nonconvexity of Hagan West for extreme cases.
docstr_tok: ["Demonstrates","the","nonconvexity","of","Hagan","West","for","extreme","cases","."]
code: def test_non_convex(self, dtype):
      """Demonstrates the nonconvexity of Hagan West for extreme cases."""
      # This is the same example as the previous one but with different starting
      # point.
      cashflows = [
          # 1 year bond with 5% three monthly coupon.
          np.array([12.5, 12.5, 12.5, 1012.5], dtype=dtype),
          # 2 year bond with 6% semi-annual coupon.
          np.array([30, 30, 30, 1030], dtype=dtype),
          # 3 year bond with 8% semi-annual coupon.
          np.array([40, 40, 40, 40, 40, 1040], dtype=dtype),
          # 4 year bond with 3% semi-annual coupon.
          np.array([15, 15, 15, 15, 15, 15, 15, 1015], dtype=dtype),
      ]
      cashflow_times = [
          np.array([0.25, 0.5, 0.75, 1.0], dtype=dtype),
          np.array([0.5, 1.0, 1.5, 2.0], dtype=dtype),
          np.array([0.5, 1.0, 1.5, 2.0, 2.5, 3.0], dtype=dtype),
          np.array(
              [0.5, 1.0, 1.5, 2.0, 2.5, 3.0, 3.5, 4.0], dtype=dtype
          ),
      ]
      # Computed with discount rates of [5.0, 4.75, 4.53333333, 4.775]
      # which are 100 times the values in the previous test case.
      pvs = np.array(
          [
              11.561316110080888,
              2.6491572753698067,
              3.4340789041846866,
              1.28732090544209,
          ],
          dtype=dtype,
      )
      true_discount_rates = np.array(
          [5.0, 4.75, 4.53333333, 4.775], dtype=dtype
      )
      initial_rates = true_discount_rates * 1.01
      # Check failure with default initial rates.
      results = self.evaluate(
          bond_curve.bond_curve(
              cashflows,
              cashflow_times,
              pvs,
              initial_discount_rates=initial_rates,
              maximum_iterations=100,
              validate_args=True,
              dtype=dtype,
          )
      )
      self.assertTrue(results.converged)
      self.assertFalse(results.failed)
      # It converges to a different set of rates.
      np.testing.assert_allclose(
          results.discount_rates,
          [4.96098643, 4.17592063, 2.83970042, 2.38685078],
          atol=1e-6,
      )
  
      # However, the actual bond prices with the returned rates are indeed
      # correct.
      implied_pvs = self.evaluate(
          _compute_pv(
              cashflows,
              cashflow_times,
              results.discount_rates,
              np.array([1.0, 2.0, 3.0, 4.0], dtype=dtype),
          )
      )
  
      np.testing.assert_allclose(implied_pvs, pvs, rtol=1e-5)
  
imports: import math
  from absl.testing import parameterized
  import numpy
  import tensorflow.compat.v2
  from tensorflow.python.framework import test_util
  from tf_quant_finance.rates.hagan_west import bond_curve
  from tf_quant_finance.rates.hagan_west import monotone_convex
label_desc: Use image node_id 6 for calling the BondCurveTest obj's underlying member method code with example usage: obj.test_non_convex(dtype) without return types