def test_expand_xd_errors(): """ One of several _expand_?D tests. """ dct = dict() with pytest.raises(ValueError): Parameters._expand_1D(dct, inflate=False, inflation_rates=[], num_years=10) with pytest.raises(ValueError): Parameters._expand_2D(dct, inflate=False, inflation_rates=[], num_years=10)
def test_expand_2d_partial_expand(): """ One of several _expand_?D tests. """ # pylint doesn't like caps in var name, so pylint: disable=invalid-name _II_brk2 = [[36000.0, 72250.0, 36500.0, 48600.0, 72500.0, 36250.0], [38000.0, 74000.0, 36900.0, 49400.0, 73800.0, 36900.0], [40000.0, 74900.0, 37450.0, 50200.0, 74900.0, 37450.0]] # We have three years worth of data, need 4 years worth, # but we only need the inflation rate for year 3 to go # from year 3 -> year 4 inf_rates = [0.02, 0.02, 0.03] exp1 = 40000. * 1.03 exp2 = 74900. * 1.03 exp3 = 37450. * 1.03 exp4 = 50200. * 1.03 exp5 = 74900. * 1.03 exp6 = 37450. * 1.03 exp = [[36000.0, 72250.0, 36500.0, 48600.0, 72500.0, 36250.0], [38000.0, 74000.0, 36900.0, 49400.0, 73800.0, 36900.0], [40000.0, 74900.0, 37450.0, 50200.0, 74900.0, 37450.0], [exp1, exp2, exp3, exp4, exp5, exp6]] res = Parameters._expand_2D(np.array(_II_brk2), inflate=True, inflation_rates=inf_rates, num_years=4) assert np.allclose(res, exp, atol=0.01, rtol=0.0)
def test_expand_2d_already_filled(): """ One of several _expand_?D tests. """ # pylint doesn't like caps in var name, so pylint: disable=invalid-name _II_brk2 = [[36000., 72250., 36500., 48600., 72500., 36250.], [38000., 74000., 36900., 49400., 73800., 36900.], [40000., 74900., 37450., 50200., 74900., 37450.]] res = Parameters._expand_2D(np.array(_II_brk2), inflate=True, inflation_rates=[0.02] * 5, num_years=3) np.allclose(res, np.array(_II_brk2), atol=0.01, rtol=0.0)
def test_expand_2d_short_array(): """ One of several _expand_?D tests. """ ary = np.array([[1., 2., 3.]]) val = np.array([1., 2., 3.]) exp2 = np.array([val * math.pow(1.02, i) for i in range(1, 5)]) exp1 = np.array([1., 2., 3.]) exp = np.zeros((5, 3)) exp[:1] = exp1 exp[1:] = exp2 res = Parameters._expand_2D(ary, inflate=True, inflation_rates=[0.02] * 5, num_years=5) assert np.allclose(exp, res, atol=0.01, rtol=0.0)
def test_expand_2d_variable_rates(): """ One of several _expand_?D tests. """ ary = np.array([[1., 2., 3.]]) cur = np.array([1., 2., 3.]) irates = [0.02, 0.02, 0.02, 0.03, 0.035] exp2 = [] for i in range(0, 4): idx = i + len(ary) - 1 cur = np.array(cur * (1.0 + irates[idx])) print('cur is ', cur) exp2.append(cur) exp1 = np.array([1., 2., 3.]) exp = np.zeros((5, 3)) exp[:1] = exp1 exp[1:] = exp2 res = Parameters._expand_2D(ary, inflate=True, inflation_rates=irates, num_years=5) assert np.allclose(exp, res, atol=0.01, rtol=0.0)