def test_interpolation(self): # test default interpolation scheme for x in self.x_test: f = lambda t: max(.0, min(t, .09)) self.assertAlmostEqual(f(x), self.curve(x)) ccc = dyn_scheme(constant, constant, constant) curve = Curve(self.x_list, self.y_list, ccc) constant_curve = Curve(self.x_list, self.y_list, constant) for x in self.x_test: f = lambda t: max(.0, min(floor(t / .01) * .01, .09)) self.assertAlmostEqual(f(x), curve(x)) self.assertAlmostEqual(constant_curve(x), curve(x)) lll = dyn_scheme(linear, linear, linear) curve = Curve(self.x_list, self.y_list, lll) linear_curve = Curve(self.x_list, self.y_list, linear) for x in self.x_test: f = lambda t: t self.assertAlmostEqual(f(x), curve(x)) self.assertAlmostEqual(linear_curve(x), curve(x)) dcf_curve = Curve(self.x_list, self.y_list, dyn_scheme(constant, linear, constant)) scipy_linear = lambda x, y: interp1d(x, y, kind="linear") scipy_curve = Curve(self.x_list, self.y_list, dyn_scheme(constant, scipy_linear, constant)) for x in self.x_test: self.assertAlmostEqual(scipy_curve(x), dcf_curve(x)) dcf_curve = Curve(self.x_list, self.y_list, dyn_scheme(linear, linear, linear)) scipy_scheme = lambda x,y : \ interp1d(x, y, kind="linear", fill_value="extrapolate") scipy_curve = Curve(self.x_list, self.y_list, scipy_scheme) for x in self.x_test: self.assertAlmostEqual(scipy_curve(x), dcf_curve(x)) dcf_curve = Curve(self.x_list, self.y_list, dyn_scheme(constant, linear, constant)) scipy_scheme = lambda x,y : \ interp1d(x, y, kind="linear", bounds_error=False, fill_value=(self.y_list[0], self.y_list[-1])) scipy_curve = Curve(self.x_list, self.y_list, scipy_scheme) for x in self.x_test: self.assertAlmostEqual(scipy_curve(x), dcf_curve(x))
def test_algebra(self): other = Curve(self.x_list, self.y_list) new = self.curve + other for x in new.domain: self.assertAlmostEqual(new(x), self.curve(x) * 2.) new = self.curve - other for x in new.domain: self.assertAlmostEqual(new(x), 0.) new = self.curve * other for x in new.domain: self.assertAlmostEqual(new(x), self.curve(x) ** 2) self.assertRaises(ZeroDivisionError, self.curve.__div__, other) new = self.curve / Curve(self.x_list, [0.1] * len(self.x_list)) for x in new.domain: self.assertAlmostEqual(new(x), self.curve(x) / 0.1)
def test_init(self): self.assertEqual(str(Curve()), 'Curve()') self.assertEqual(str(DateCurve()), 'DateCurve()') self.assertEqual(str(RateCurve()), 'RateCurve()')
def setUp(self): self.x_list = [float(x) * 0.01 for x in range(10)] self.y_list = list(self.x_list) self.interpolation = dyn_scheme(constant, linear, constant) self.curve = Curve(self.x_list, self.y_list, self.interpolation) self.x_test = [float(x) * 0.005 for x in range(-10, 30)]
def test_cast(self): date_curve = DateCurve(self.dates, self.values) curve = Curve(date_curve) for x, d in zip(curve.domain, date_curve.domain): self.assertAlmostEqual(curve(x), date_curve(d))
def test_init(self): self.assertEqual(str(Curve()), str(list())) self.assertEqual(str(DateCurve()), str(list())) self.assertEqual(str(RateCurve()), str(list()))