Exemplo n.º 1
0
def test_get_data():
    '''
    Test of get_micro_data.get_data() function
    '''
    expected_data = utils.safe_read_pickle(
        os.path.join(CUR_PATH, 'test_io_data',
                     'micro_data_dict_for_tests.pkl'))
    test_data, _ = get_micro_data.get_data(
        baseline=True, start_year=2028, reform={}, data='cps',
        client=None, num_workers=1)
    for k, v in test_data.items():
        assert_frame_equal(expected_data[k], v)
Exemplo n.º 2
0
def test_get_data(baseline, dask_client):
    '''
    Test of get_micro_data.get_data() function
    '''
    expected_data = utils.safe_read_pickle(
        os.path.join(CUR_PATH, 'test_io_data',
                     'micro_data_dict_for_tests.pkl'))
    test_data, _ = get_micro_data.get_data(
        baseline=baseline, start_year=2030, reform={}, data='cps',
        client=dask_client, num_workers=NUM_WORKERS)
    for k, v in test_data.items():
        try:
            assert_frame_equal(
                expected_data[k], v)
        except KeyError:
            pass
Exemplo n.º 3
0
def test_get_data(baseline, dask_client):
    """
    Test of get_micro_data.get_data() function

    Note that this test may fail if the Tax-Calculator is not v 3.2.1
    """
    expected_data = utils.safe_read_pickle(
        os.path.join(CUR_PATH, "test_io_data", "micro_data_dict_for_tests.pkl")
    )
    test_data, _ = get_micro_data.get_data(
        baseline=baseline,
        start_year=2030,
        reform={},
        data="cps",
        client=dask_client,
        num_workers=NUM_WORKERS,
    )
    for k, v in test_data.items():
        try:
            assert_frame_equal(expected_data[k], v)
        except KeyError:
            pass
Exemplo n.º 4
0
    def get_tax_function_parameters(
        self,
        p,
        iit_reform={},
        guid="",
        data="",
        client=None,
        num_workers=1,
        run_micro=False,
        tax_func_path=None,
    ):
        """
        Reads pickle file of tax function parameters or estimates the
        parameters from microsimulation model output.

        Args:
            client (Dask client object): client
            run_micro (bool): whether to estimate parameters from
                microsimulation model
            tax_func_path (string): path where find or save tax
                function parameter estimates

        Returns:
            None

        """
        # set paths if none given
        if tax_func_path is None:
            if p.baseline:
                pckl = "TxFuncEst_baseline{}.pkl".format(guid)
                tax_func_path = os.path.join(p.output_base, pckl)
                print("Using baseline tax parameters from ", tax_func_path)
            else:
                pckl = "TxFuncEst_policy{}.pkl".format(guid)
                tax_func_path = os.path.join(p.output_base, pckl)
                print(
                    "Using reform policy tax parameters from ", tax_func_path
                )
        # create directory for tax function pickles to be saved to
        mkdirs(os.path.split(tax_func_path)[0])
        # If run_micro is false, check to see if parameters file exists
        # and if it is consistent with Specifications instance
        if not run_micro:
            dict_params, run_micro = self.read_tax_func_estimate(tax_func_path)
        if run_micro:
            micro_data, taxcalc_version = get_micro_data.get_data(
                baseline=p.baseline,
                start_year=p.start_year,
                reform=iit_reform,
                data=data,
                path=p.output_base,
                client=client,
                num_workers=num_workers,
            )
            p.BW = len(micro_data)
            dict_params = txfunc.tax_func_estimate(  # pragma: no cover
                micro_data,
                p.BW,
                p.S,
                p.starting_age,
                p.ending_age,
                start_year=p.start_year,
                baseline=p.baseline,
                analytical_mtrs=p.analytical_mtrs,
                tax_func_type=p.tax_func_type,
                age_specific=p.age_specific,
                reform=iit_reform,
                data=data,
                client=client,
                num_workers=num_workers,
                tax_func_path=tax_func_path,
            )
        mean_income_data = dict_params["tfunc_avginc"][0]
        frac_tax_payroll = np.append(
            dict_params["tfunc_frac_tax_payroll"],
            np.ones(p.T + p.S - p.BW)
            * dict_params["tfunc_frac_tax_payroll"][-1],
        )

        # Reorder indices of tax function and tile for all years after
        # budget window ends
        num_etr_params = dict_params["tfunc_etr_params_S"].shape[2]
        num_mtrx_params = dict_params["tfunc_mtrx_params_S"].shape[2]
        num_mtry_params = dict_params["tfunc_mtry_params_S"].shape[2]
        # First check to see if tax parameters that are used were
        # estimated with a budget window and ages that are as long as
        # the those implied based on the start year and model age.
        # N.B. the tax parameters dictionary does not save the years
        # that correspond to the parameter estimates, so the start year
        # used there may name match what is used in a run that reads in
        # some cached tax function parameters.  Likewise for age.
        params_list = ["etr", "mtrx", "mtry"]
        BW_in_tax_params = dict_params["tfunc_etr_params_S"].shape[1]
        S_in_tax_params = dict_params["tfunc_etr_params_S"].shape[0]
        if p.BW != BW_in_tax_params:
            print(
                "Warning: There is a discrepency between the start"
                + " year of the model and that of the tax functions!!"
            )
        # After printing warning, make it work by tiling
        if p.BW > BW_in_tax_params:
            for item in params_list:
                dict_params["tfunc_" + item + "_params_S"] = np.concatenate(
                    (
                        dict_params["tfunc_" + item + "_params_S"],
                        np.tile(
                            dict_params["tfunc_" + item + "_params_S"][
                                :, -1, :
                            ].reshape(S_in_tax_params, 1, num_etr_params),
                            (1, p.BW - BW_in_tax_params, 1),
                        ),
                    ),
                    axis=1,
                )
                dict_params["tfunc_avg_" + item] = np.append(
                    dict_params["tfunc_avg_" + item],
                    np.tile(
                        dict_params["tfunc_avg_" + item][-1],
                        (p.BW - BW_in_tax_params),
                    ),
                )
        if p.S != S_in_tax_params:
            print(
                "Warning: There is a discrepency between the ages"
                + " used in the model and those in the tax functions!!"
            )
        # After printing warning, make it work by tiling
        if p.S > S_in_tax_params:
            for item in params_list:
                dict_params["tfunc_" + item + "_params_S"] = np.concatenate(
                    (
                        dict_params["tfunc_" + item + "_params_S"],
                        np.tile(
                            dict_params["tfunc_" + item + "_params_S"][
                                -1, :, :
                            ].reshape(1, p.BW, num_etr_params),
                            (p.S - S_in_tax_params, 1, 1),
                        ),
                    ),
                    axis=0,
                )
        etr_params = np.empty((p.T, p.S, num_etr_params))
        mtrx_params = np.empty((p.T, p.S, num_mtrx_params))
        mtry_params = np.empty((p.T, p.S, num_mtry_params))
        etr_params[: p.BW, :, :] = np.transpose(
            dict_params["tfunc_etr_params_S"][: p.S, : p.BW, :], axes=[1, 0, 2]
        )
        etr_params[p.BW :, :, :] = np.tile(
            np.transpose(
                dict_params["tfunc_etr_params_S"][: p.S, -1, :].reshape(
                    p.S, 1, num_etr_params
                ),
                axes=[1, 0, 2],
            ),
            (p.T - p.BW, 1, 1),
        )
        mtrx_params[: p.BW, :, :] = np.transpose(
            dict_params["tfunc_mtrx_params_S"][: p.S, : p.BW, :],
            axes=[1, 0, 2],
        )
        mtrx_params[p.BW :, :, :] = np.transpose(
            dict_params["tfunc_mtrx_params_S"][: p.S, -1, :].reshape(
                p.S, 1, num_mtrx_params
            ),
            axes=[1, 0, 2],
        )
        mtry_params[: p.BW, :, :] = np.transpose(
            dict_params["tfunc_mtry_params_S"][: p.S, : p.BW, :],
            axes=[1, 0, 2],
        )
        mtry_params[p.BW :, :, :] = np.transpose(
            dict_params["tfunc_mtry_params_S"][: p.S, -1, :].reshape(
                p.S, 1, num_mtry_params
            ),
            axes=[1, 0, 2],
        )

        if p.constant_rates:
            print("Using constant rates!")
            # Make all ETRs equal the average
            etr_params = np.zeros(etr_params.shape)
            # set shift to average rate
            etr_params[: p.BW, :, 10] = np.tile(
                dict_params["tfunc_avg_etr"].reshape(p.BW, 1), (1, p.S)
            )
            etr_params[p.BW :, :, 10] = dict_params["tfunc_avg_etr"][-1]

            # # Make all MTRx equal the average
            mtrx_params = np.zeros(mtrx_params.shape)
            # set shift to average rate
            mtrx_params[: p.BW, :, 10] = np.tile(
                dict_params["tfunc_avg_mtrx"].reshape(p.BW, 1), (1, p.S)
            )
            mtrx_params[p.BW :, :, 10] = dict_params["tfunc_avg_mtrx"][-1]

            # # Make all MTRy equal the average
            mtry_params = np.zeros(mtry_params.shape)
            # set shift to average rate
            mtry_params[: p.BW, :, 10] = np.tile(
                dict_params["tfunc_avg_mtry"].reshape(p.BW, 1), (1, p.S)
            )
            mtry_params[p.BW :, :, 10] = dict_params["tfunc_avg_mtry"][-1]
        if p.zero_taxes:
            print("Zero taxes!")
            etr_params = np.zeros(etr_params.shape)
            mtrx_params = np.zeros(mtrx_params.shape)
            mtry_params = np.zeros(mtry_params.shape)
        tax_param_dict = {
            "etr_params": etr_params,
            "mtrx_params": mtrx_params,
            "mtry_params": mtry_params,
            "taxcalc_version": taxcalc_version,
            "mean_income_data": mean_income_data,
            "frac_tax_payroll": frac_tax_payroll,
        }

        return tax_param_dict