def test_yields_from_pvs_no_group(self): dtypes = [np.float32, np.float64] for dtype in dtypes: coupon_rate = 0.04 # Fifteen year bond with semi-annual coupons. cashflows = np.array( [coupon_rate * 500] * 29 + [1000 + coupon_rate * 500], dtype=dtype) pv = 995.50315587 times = np.linspace(0.5, 15, num=30).astype(dtype) expected_yield_rate = 0.04 actual_yield_rate = self.evaluate( cashflows_lib.yields_from_pv(cashflows, times, [pv], dtype=dtype)) np.testing.assert_allclose(expected_yield_rate, actual_yield_rate)
def test_yield_saturated_pv(self): dtypes = [np.float32, np.float64] for dtype in dtypes: # 2 and 3 year bonds with 1000 face value and 4%, 6% semi-annual coupons. cashflows = np.array([20, 20, 20, 1020, 30, 30, 30, 30, 30, 1030], dtype=dtype) times = np.array([0.5, 1, 1.5, 2, 0.5, 1, 1.50, 2, 2.5, 3], dtype=dtype) groups = np.array([0] * 4 + [1] * 6) pvs = np.array([1080., 1180.]) expected_yields = [0., 0.] actual_yields = self.evaluate( cashflows_lib.yields_from_pv( cashflows, times, pvs, groups=groups, dtype=dtype)) np.testing.assert_allclose(expected_yields, actual_yields, atol=1e-9)
def test_yields_from_pv_grouped(self): dtypes = [np.float32, np.float64] for dtype in dtypes: # 2 and 3 year bonds with 1000 face value and 4%, 6% semi-annual coupons. cashflows = np.array([20, 20, 20, 1020, 30, 30, 30, 30, 30, 1030], dtype=dtype) times = np.array([0.5, 1, 1.5, 2, 0.5, 1, 1.50, 2, 2.5, 3], dtype=dtype) groups = np.array([0] * 4 + [1] * 6) pvs = np.array([942.71187528177757, 1025.7777300221542]) expected_yield_rates = [0.07, 0.05] actual_yield_rates = self.evaluate( cashflows_lib.yields_from_pv( cashflows, times, pvs, groups=groups, dtype=dtype)) np.testing.assert_allclose( expected_yield_rates, actual_yield_rates, atol=1e-7)
def _initial_discount_rates(bond_cashflows, bond_cashflow_times, present_values, name='initial_discount_rates'): """Constructs a guess for the initial rates as the yields to maturity.""" n = len(bond_cashflows) groups = [] for i in range(n): groups.append(tf.fill(tf.shape(bond_cashflows[i]), i)) bond_cashflows = tf.concat(bond_cashflows, axis=0) bond_cashflow_times = tf.concat(bond_cashflow_times, axis=0) groups = tf.concat(groups, axis=0) return cashflows.yields_from_pv(bond_cashflows, bond_cashflow_times, present_values, groups=groups, name=name)
def test_yield_small_pv(self): """Tests in the limit where implied yields are high.""" dtypes = [np.float32, np.float64] for dtype in dtypes: # 2 and 3 year bonds with 1000 face value and 4%, 6% semi-annual coupons. cashflows = np.array([20, 20, 20, 1020, 30, 30, 30, 30, 30, 1030], dtype=dtype) times = np.array([0.5, 1, 1.5, 2, 0.5, 1, 1.50, 2, 2.5, 3], dtype=dtype) groups = np.array([0] * 4 + [1] * 6) pvs = np.array([7.45333412e-05, 2.27476813e-08]) expected_yields = [25.0, 42.0] actual_yields = self.evaluate( cashflows_lib.yields_from_pv( cashflows, times, pvs, groups=groups, dtype=dtype, max_iterations=30)) np.testing.assert_allclose(expected_yields, actual_yields, atol=1e-9)