def test_param_dict_for_year(): """ Check logic of param_dict_for_year staticmethod. """ param_info = { 'pname1': { 'default_value': 0.0, 'minimum_value': 0.0, 'maximum_value': 9e99 }, 'pname2': { 'default_value': 0.0, 'minimum_value': -9e99, 'maximum_value': 0.0 } } param_dict = { 2019: {'pname1': 0.05}, 2021: {'pname2': -0.5}, 2023: {'pname2': 0.5} } ydict = Parameters.param_dict_for_year(2018, param_dict, param_info) assert ydict['pname1'] == 0.0 assert ydict['pname2'] == 0.0 ydict = Parameters.param_dict_for_year(2020, param_dict, param_info) assert ydict['pname1'] == 0.05 assert ydict['pname2'] == 0.0 ydict = Parameters.param_dict_for_year(2022, param_dict, param_info) assert ydict['pname1'] == 0.05 assert ydict['pname2'] == -0.5 with pytest.raises(AssertionError): Parameters.param_dict_for_year(2024, param_dict, param_info)
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_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_1d_scalar(): """ One of several _expand_?D tests. """ yrs = 12 val = 10.0 exp = np.array([val * math.pow(1.02, i) for i in range(0, yrs)]) res = Parameters._expand_1d(np.array([val]), inflate=True, inflation_rates=[0.02] * yrs, num_years=yrs) assert np.allclose(exp, res, atol=0.01, rtol=0.0) res = Parameters._expand_1d(np.array([val]), inflate=True, inflation_rates=[0.02] * yrs, num_years=1) assert np.allclose(np.array([val]), res, atol=0.01, rtol=0.0)
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_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_read_json_revision_foramts(params, is_paramtools): """ Check _read_json_revision for ParamTools and Tax-Calculator styled parameters. """ result = Parameters._read_json_revision(params, "consumption") assert is_paramtools_format(result) is is_paramtools if is_paramtools: assert result == json.loads(params)["consumption"]
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_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_param_dict_for_year(): """ Check logic of param_dict_for_year staticmethod. """ param_info = { 'pname1': { 'default_value': 0.0, 'minimum_value': 0.0, 'maximum_value': 9e99 }, 'pname2': { 'default_value': 0.0, 'minimum_value': -9e99, 'maximum_value': 0.0 } } param_dict = { 2019: { 'pname1': 0.05 }, 2021: { 'pname2': -0.5 }, 2023: { 'pname2': 0.5 } } ydict = Parameters.param_dict_for_year(2018, param_dict, param_info) assert ydict['pname1'] == 0.0 assert ydict['pname2'] == 0.0 ydict = Parameters.param_dict_for_year(2020, param_dict, param_info) assert ydict['pname1'] == 0.05 assert ydict['pname2'] == 0.0 ydict = Parameters.param_dict_for_year(2022, param_dict, param_info) assert ydict['pname1'] == 0.05 assert ydict['pname2'] == -0.5 with pytest.raises(AssertionError): Parameters.param_dict_for_year(2024, param_dict, param_info)
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_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_read_json_revision(good_revision): """ Check _read_json_revision logic. """ # pllint: disable=private-method with pytest.raises(TypeError): # error because first obj argument is neither None nor a string Parameters._read_json_revision(list(), '') with pytest.raises(ValueError): # error because second topkey argument must be a string Parameters._read_json_revision(good_revision, 999) with pytest.raises(ValueError): # error because second topkey argument is not in good_revision Parameters._read_json_revision(good_revision, 'unknown_topkey')
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)
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)
def test_read_json_revision(): """ Check _read_json_revision logic. """ good_revision = """ { "consumption": {"BEN_mcaid_value": {"2013": 0.9}} } """ # pllint: disable=private-method with pytest.raises(ValueError): # error because first obj argument is neither None nor a string Parameters._read_json_revision(list(), '') with pytest.raises(ValueError): # error because second topkey argument must be a string Parameters._read_json_revision(good_revision, 999) with pytest.raises(ValueError): # error because second topkey argument is not in good_revision Parameters._read_json_revision(good_revision, 'unknown_topkey')
def test_instantiation_and_usage(): """ Test Parameters instantiation and usage. """ pbase = Parameters() 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): Parameters._expand_array({}, False, False, True, [0.02], 1) arr3d = np.array([[[1, 1]], [[1, 1]], [[1, 1]]]) with pytest.raises(ValueError): Parameters._expand_array(arr3d, False, False, True, [0.02], 1)