Example #1
0
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)
Example #2
0
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)
Example #3
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)
Example #4
0
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)