Beispiel #1
0
def test5():
    """The test checks if the estimation process works properly when maxiter is set to zero."""
    for _ in range(10):
        constr = dict()
        constr['DETERMINISTIC'], constr['MAXITER'] = False, 0
        generate_random_dict(constr)
        simulate('test.grmpy.ini')
        estimate('test.grmpy.ini')
Beispiel #2
0
def test5():
    """The test checks if the estimation process works properly when maxiter is set to
    zero.
    """
    for _ in range(10):
        constr = constraints(probability=0.0, maxiter=0)
        generate_random_dict(constr)
        simulate('test.grmpy.ini')
        estimate('test.grmpy.ini')
Beispiel #3
0
def test4():
    """The test checks if the estimation process works if the Powell algorithm is specified as
    the optimizer option.
    """
    for _ in range(5):
        constr = constraints(probability=0.0,
                             agents=10000,
                             start='init',
                             optimizer='SCIPY-POWELL')
        generate_random_dict(constr)

        simulate('test.grmpy.ini')
        estimate('test.grmpy.ini')
Beispiel #4
0
def test4():
    """The test checks if the estimation process works if the Powell algorithm is specified as
    the optimizer option.
    """
    for _ in range(5):
        constr = dict()
        constr['DETERMINISTIC'], constr['AGENTS'], constr[
            'start'] = False, 10000, 'init'
        constr['optimizer'] = 'SCIPY-Powell'
        generate_random_dict(constr)

        simulate('test.grmpy.ini')
        estimate('test.grmpy.ini')
Beispiel #5
0
def test9():
    """This test ensures that the random initialization file generating process, the read in process
    and the simulation process works if the constraints function allows for different number of co-
    variates for each treatment state and the occurence of cost-benefit shifters."""
    for i in range(5):
        constr = dict()
        constr['DETERMINISTIC'], constr['AGENT'], constr[
            'STATE_DIFF'] = False, 1000, True
        constr['OVERLAP'] = True
        generate_random_dict(constr)
        read('test.grmpy.ini')
        simulate('test.grmpy.ini')
        estimate('test.grmpy.ini')

    cleanup()
Beispiel #6
0
def test6():
    """Additionally to test5 this test checks if the descriptives file provides the expected
    output when maxiter is set to zero and the estimation process uses the initialization file
    values as start values.
    """
    for _ in range(5):
        constr = constraints(probability=0.0,
                             maxiter=0,
                             agents=1000,
                             start='init')
        generate_random_dict(constr)
        simulate('test.grmpy.ini')
        estimate('test.grmpy.ini')
        dict_ = read_desc('descriptives.grmpy.txt')
        for key_ in ['All', 'Treated', 'Untreated']:
            np.testing.assert_equal(len(set(dict_[key_]['Number'])), 1)
            np.testing.assert_array_equal(
                dict_[key_]['Observed Sample'],
                dict_[key_]['Simulated Sample (finish)'])
            np.testing.assert_array_equal(
                dict_[key_]['Simulated Sample (finish)'],
                dict_[key_]['Simulated Sample (start)'])
    cleanup()
Beispiel #7
0
def test6():
    """Additionally to test5 this test checks if the comparison file provides the expected
    output when maxiter is set to zero and the estimation process uses the initialization file
    values as start values.
    """
    for _ in range(5):
        constr = dict()
        constr['DETERMINISTIC'], constr['MAXITER'], constr[
            'AGENTS'] = False, 0, 10000
        constr['START'], constr['SAME_SIZE'] = 'init', True
        dict_ = generate_random_dict(constr)
        dict_['DIST']['all'][1], dict_['DIST']['all'][5] = 0.0, 1.0
        print_dict(dict_)
        simulate('test.grmpy.ini')
        estimate('test.grmpy.ini')
        dict_ = read_desc('comparison.grmpy.txt')
        for key_ in ['All', 'Treated', 'Untreated']:
            np.testing.assert_equal(len(set(dict_[key_]['Number'])), 1)
            np.testing.assert_almost_equal(
                dict_[key_]['Observed Sample'],
                dict_[key_]['Simulated Sample (finish)'], 0.001)
            np.testing.assert_array_almost_equal(
                dict_[key_]['Simulated Sample (finish)'],
                dict_[key_]['Simulated Sample (start)'], 0.001)
Beispiel #8
0
def test11():
    """This test checks if the refactor auxiliary function returns an unchanged init file if the
    maximum number of iterations is set to zero.
    """

    for _ in range(10):
        constr = dict()
        constr['DETERMINISTIC'], constr['AGENTS'] = False, 1000
        constr['MAXITER'], constr['START'] = 0, 'init'
        generate_random_dict(constr)
        init_dict = read('test.grmpy.ini')
        df = simulate('test.grmpy.ini')
        start = start_values(init_dict, df, 'init')
        start = backward_transformation(start)
        rslt = estimate('test.grmpy.ini')

        np.testing.assert_equal(start, rslt['AUX']['x_internal'])
Beispiel #9
0
def test3():
    """The test checks if the criteria function value of the simulated and the 'estimated'
    sample is equal if both samples include an identical number of individuals.
    """
    for _ in range(5):
        constr = dict()
        constr['DETERMINISTIC'], constr['AGENTS'], constr[
            'START'] = False, 1000, 'init'
        constr['OPTIMIZER'], constr['SAME_SIZE'] = 'SCIPY-BFGS', True
        generate_random_dict(constr)
        df1 = simulate('test.grmpy.ini')
        rslt = estimate('test.grmpy.ini')
        init_dict = read('test.grmpy.ini')
        df2 = simulate_estimation(init_dict, rslt)
        start = start_values(init_dict, df1, 'init')

        criteria = []
        for data in [df1, df2]:
            criteria += [calculate_criteria(init_dict, data, start)]
        np.testing.assert_allclose(criteria[1], criteria[0], rtol=0.1)
Beispiel #10
0
def monte_carlo(file, grid_points):
    """This function estimates the ATE for a sample with different correlation structures between U1
     and V. Two different strategies for (OLS,LATE) are implemented.
     """

    # Define a dictionary with a key for each estimation strategy
    effects = {}
    for key_ in ['grmpy', 'ols', 'true']:
        effects[key_] = []

    # Loop over different correlations between V and U_1
    for rho in np.linspace(0.00, 0.99, grid_points):

        # Readjust the initialization file values to add correlation
        model_spec = read(file)
        sim_spec = read('reliability.grmpy.ini')
        X = [sim_spec['varnames'][j - 1] for j in sim_spec['TREATED']['order']]
        update_correlation_structure(model_spec, rho)

        # Simulate a Data set and specify exogeneous and endogeneous variables
        df_mc = create_data()
        endog, exog, exog_ols = df_mc['wage'], df_mc[X], df_mc[['state'] + X]

        # Calculate true average treatment effect
        ATE = np.mean(df_mc['wage1'] - df_mc['wage0'])
        effects['true'] += [ATE]

        # Estimate  via grmpy
        rslt = estimate('reliability.grmpy.ini')
        beta_diff = rslt['TREATED']['all'] - rslt['UNTREATED']['all']
        stat = np.dot(np.mean(exog), beta_diff)

        effects['grmpy'] += [stat]

        # Estimate via OLS
        ols = sm.OLS(endog, exog_ols).fit()
        stat = ols.params[0]

        effects['ols'] += [stat]

    return effects
Beispiel #11
0
def test3():
    """The test checks if the criteria function value of the simulated and the 'estimated'
    sample is equal if both samples include an identical number of individuals.
    """
    for _ in range(5):
        constr = constraints(probability=0.0,
                             agents=10000,
                             start='init',
                             optimizer='SCIPY-BFGS')
        dict_ = generate_random_dict(constr)
        print_dict(dict_)

        df1 = simulate('test.grmpy.ini')
        rslt = estimate('test.grmpy.ini')
        init_dict = read('test.grmpy.ini')
        df2 = simulate_estimation(init_dict, rslt, df1)
        start = start_values(init_dict, df1, 'init')

        criteria = []
        for data in [df1, df2]:
            criteria += [calculate_criteria(init_dict, data, start)]
        np.testing.assert_allclose(criteria[1], criteria[0], rtol=0.1)
Beispiel #12
0
def test12():
    """This test ensures that the tutorial configuration works as intended."""
    fname = TEST_RESOURCES_DIR + '/tutorial.grmpy.ini'
    simulate(fname)
    estimate(fname)
Beispiel #13
0
    x_neg = [-i for i in x]
    x += x_neg
    x = np.array(x)

    # Create auxiliary parameters
    part1 = np.dot(x, np.dot(param_cov, x))
    part2 = np.dot(dist_gradients, np.dot(dist_cov, dist_gradients))
    # Prepare two lists for storing the values
    mte_up = []
    mte_d = []

    # Combine all auxiliary parameters and calculate the confidence intervals
    for counter, i in enumerate(quantiles):
        value = part2 * (norm.ppf(i))**2
        aux = np.sqrt(part1 + value) / 4
        mte_up += [mte[counter] + norm.ppf(0.95) * aux]
        mte_d += [mte[counter] - norm.ppf(0.95) * aux]

    return mte_up, mte_d


if __name__ == '__main__':

    init_dict = read('replication.grmpy.ini')
    # Estimate the coefficients
    rslt = estimate('replication.grmpy.ini')

    # Calculate and plot the marginal treatment effect
    data = pd.read_pickle('aer-replication-mock.pkl')
    mte = plot_est_mte(rslt, init_dict, data)
Beispiel #14
0
#!/usr/bin/env python
""" This module contains a first stab at a reliability test."""
import grmpy
import sys

from grmpy.simulate.simulate import simulate
from grmpy.estimate.estimate import estimate

simulate('test.grmpy.ini')

option = 'true_values'
estimate('test.grmpy.ini', option)
Beispiel #15
0
"""This module contains a tutorial illustrating the basic capabilities of the grmpy package."""
import os

from grmpy.simulate.simulate import simulate
from grmpy.estimate.estimate import estimate

f = os.path.dirname(__file__) + '/tutorial.grmpy.ini'
simulate(f)
rslt = estimate(f)