def test_expand_xd_errors(): """ One of several _expand_?D tests. """ dct = dict() with pytest.raises(ValueError): ParametersBase._expand_1D(dct, inflate=False, inflation_rates=[], num_years=10) with pytest.raises(ValueError): ParametersBase._expand_2D(dct, inflate=False, inflation_rates=[], num_years=10)
def test_expand_2d_accept_none(): # pylint doesn't like caps in var name, so pylint: disable=invalid-name _II_brk2 = [[36000, 72250, 36500, 48600, 72500], [38000, 74000, 36900, 49400, 73800], [40000, 74900, 37450, 50200, 74900], [41000, None, None, None, None]] exp1 = 74900 * 1.02 exp2 = 37450 * 1.02 exp3 = 50200 * 1.02 exp4 = 74900 * 1.02 exp = [[36000, 72250, 36500, 48600, 72500], [38000, 74000, 36900, 49400, 73800], [40000, 74900, 37450, 50200, 74900], [41000, exp1, exp2, exp3, exp4]] exp = np.array(exp).astype('i4', casting='unsafe') res = ParametersBase._expand_array(_II_brk2, inflate=True, inflation_rates=[0.02] * 5, num_years=4) assert_equal(res, exp) syr = 2013 pol = Policy(start_year=syr) irates = pol.inflation_rates() reform = {2016: {u'_II_brk2': _II_brk2}} pol.implement_reform(reform) pol.set_year(2019) # The 2019 policy should be the combination of the user-defined # value and CPI-inflated values from 2018 exp_2019 = [41000.] + [(1.0 + irates[2018 - syr]) * i for i in _II_brk2[2][1:]] exp_2019 = np.array(exp_2019) assert_equal(pol.II_brk2, exp_2019)
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 = ParametersBase._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_1d_scalar(): val = 10.0 exp = np.array([val * math.pow(1.02, i) for i in range(0, 10)]) res = ParametersBase._expand_1D(val, inflate=True, inflation_rates=[0.02] * 10, num_years=10) assert np.allclose(exp, res)
def test_expand_1d_variable_rates(): irates = [0.02, 0.02, 0.03, 0.035] ary = np.array([4, 5, 9], dtype='f4') res = ParametersBase._expand_1D(ary, inflate=True, inflation_rates=irates, num_years=5) exp = np.array([4, 5, 9, 9 * 1.03, 9 * 1.03 * 1.035]) assert np.allclose(exp.astype('f4', casting='unsafe'), res)
def test_expand_1d_variable_rates(): """ One of several _expand_?D tests. """ irates = [0.02, 0.02, 0.03, 0.035] ary = np.array([4, 5, 9], dtype='f4') res = ParametersBase._expand_1D(ary, inflate=True, inflation_rates=irates, num_years=5) exp = np.array([4, 5, 9, 9 * 1.03, 9 * 1.03 * 1.035]) assert np.allclose(exp, res, atol=0.01, rtol=0.0)
def test_expand_1d_scalar(): """ One of several _expand_?D tests. """ val = 10.0 exp = np.array([val * math.pow(1.02, i) for i in range(0, 10)]) res = ParametersBase._expand_1D(np.array([val]), inflate=True, inflation_rates=[0.02] * 10, num_years=10) assert np.allclose(exp, res, atol=0.01, rtol=0.0)
def test_expand_2d_already_filled(): # 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 = ParametersBase._expand_2D(_II_brk2, inflate=True, inflation_rates=[0.02] * 5, num_years=3) assert_equal(res, np.array(_II_brk2))
def test_expand_1d_short_array(): ary = np.array([4, 5, 9], dtype='i4') exp2 = np.array([9.0 * math.pow(1.02, i) for i in range(1, 8)]) exp1 = np.array([4, 5, 9]) exp = np.zeros(10) exp[:3] = exp1 exp[3:] = exp2 res = ParametersBase._expand_1D(ary, inflate=True, inflation_rates=[0.02] * 10, num_years=10) assert np.allclose(exp, res, atol=0.0, rtol=1.0E-7)
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 = ParametersBase._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(): ary = np.array([[1, 2, 3]], dtype=np.float64) val = np.array([1, 2, 3], dtype=np.float64) exp2 = np.array([val * math.pow(1.02, i) for i in range(1, 5)]) exp1 = np.array([1, 2, 3], dtype=np.float64) exp = np.zeros((5, 3)) exp[:1] = exp1 exp[1:] = exp2 res = ParametersBase._expand_2D(ary, inflate=True, inflation_rates=[0.02] * 5, num_years=5) assert np.allclose(exp, res)
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 = ParametersBase._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_1d_accept_none(): lst = [4., 5., None] irates = [0.02, 0.02, 0.03, 0.035] exp = [] cur = 5.0 * 1.02 exp = [4., 5., cur] cur *= 1.03 exp.append(cur) cur *= 1.035 exp.append(cur) exp = np.array(exp) res = ParametersBase._expand_array(lst, inflate=True, inflation_rates=irates, num_years=5) assert np.allclose(exp.astype('f4', casting='unsafe'), res)
def test_expand_2d_variable_rates(): ary = np.array([[1, 2, 3]], dtype=np.float64) cur = np.array([1, 2, 3], dtype=np.float64) 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], dtype=np.float64) exp = np.zeros((5, 3), dtype=np.float64) exp[:1] = exp1 exp[1:] = exp2 res = ParametersBase._expand_2D(ary, inflate=True, inflation_rates=irates, num_years=5) assert np.allclose(exp, res)
def test_expand_2d_accept_none_add_row(): # pylint doesn't like caps in var name, so pylint: disable=invalid-name _II_brk2 = [[36000, 72250, 36500, 48600, 72500], [38000, 74000, 36900, 49400, 73800], [40000, 74900, 37450, 50200, 74900], [41000, None, None, None, None], [43000, None, None, None, None]] exx = [0.0] exx.append(74900 * 1.02) # exx[1] exx.append(37450 * 1.02) # exx[2] exx.append(50200 * 1.02) # exx[3] exx.append(74900 * 1.02) # exx[4] exx.append(0.0) exx.append(exx[1] * 1.03) # exx[6] exx.append(exx[2] * 1.03) # exx[7] exx.append(exx[3] * 1.03) # exx[8] exx.append(exx[4] * 1.03) # exx[9] exp = [[36000, 72250, 36500, 48600, 72500], [38000, 74000, 36900, 49400, 73800], [40000, 74900, 37450, 50200, 74900], [41000, exx[1], exx[2], exx[3], exx[4]], [43000, exx[6], exx[7], exx[8], exx[9]]] inflation_rates = [0.015, 0.02, 0.02, 0.03] res = ParametersBase._expand_array(_II_brk2, inflate=True, inflation_rates=inflation_rates, num_years=5) assert_equal(res, exp) user_mods = {2016: {'_II_brk2': _II_brk2}} syr = 2013 pol = Policy(start_year=syr) irates = pol.inflation_rates() pol.implement_reform(user_mods) pol.set_year(2020) irates = pol.inflation_rates() # The 2020 policy should be the combination of the user-defined # value and CPI-inflated values from 2018 exp_2020 = [43000.] + [(1 + irates[2019 - syr]) * (1 + irates[2018 - syr]) * i for i in _II_brk2[2][1:]] exp_2020 = np.array(exp_2020) assert np.allclose(pol.II_brk2, exp_2020)
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 = ParametersBase._expand_2D(ary, inflate=True, inflation_rates=irates, num_years=5) assert np.allclose(exp, res, atol=0.01, rtol=0.0)
def test_expand_2d_partial_expand(): # 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]] # 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, 72250, 36500, 48600, 72500, 36250], [38000, 74000, 36900, 49400, 73800, 36900], [40000, 74900, 37450, 50200, 74900, 37450], [exp1, exp2, exp3, exp4, exp5, exp6]] res = ParametersBase._expand_2D(_II_brk2, inflate=True, inflation_rates=inf_rates, num_years=4) assert_equal(res, exp)
def test_ParametersBase_instantiation_and_usage(): pbase = ParametersBase() assert pbase assert pbase.inflation_rates() is None assert pbase.wage_growth_rates() is None pbase.initialize(start_year=2000, num_years=10) with pytest.raises(ValueError): pbase.set_year(1999) with pytest.raises(NotImplementedError): pbase._params_dict_from_json_file() with pytest.raises(ValueError): pbase._update([]) with pytest.raises(ValueError): pbase._update({}) with pytest.raises(ValueError): pbase._update({2099: {}}) with pytest.raises(ValueError): pbase._update({2013: []}) with pytest.raises(ValueError): ParametersBase.expand_array({}, True, [0.02], 1) threedarray = np.array([[[1, 1]], [[1, 1]], [[1, 1]]]) with pytest.raises(ValueError): ParametersBase.expand_array(threedarray, True, [0.02, 0.02], 2)
def test_instantiation_and_usage(): """ Test ParametersBase instantiation and usage. """ pbase = ParametersBase() assert pbase assert pbase.inflation_rates() is None assert pbase.wage_growth_rates() is None syr = 2010 nyrs = 10 pbase.initialize(start_year=syr, num_years=nyrs) # pylint: disable=protected-access with pytest.raises(ValueError): pbase.set_year(syr - 1) with pytest.raises(NotImplementedError): pbase._params_dict_from_json_file() with pytest.raises(ValueError): pbase._update([]) with pytest.raises(ValueError): pbase._update({}) with pytest.raises(ValueError): pbase._update({(syr + nyrs): {}}) with pytest.raises(ValueError): pbase._update({syr: []}) # pylint: disable=no-member with pytest.raises(ValueError): ParametersBase._expand_array({}, True, True, [0.02], 1) threedarray = np.array([[[1, 1]], [[1, 1]], [[1, 1]]]) with pytest.raises(ValueError): ParametersBase._expand_array(threedarray, True, True, [0.02, 0.02], 2)