def test3(): """The fourth test checks whether the simulation process works if there are only treated or untreated Agents by setting the number of agents to one. """ constr = constraints(probability=0.0, agents=1) for _ in range(10): generate_random_dict(constr) simulate('test.grmpy.ini')
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')
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')
def test2(): """The third test checks whether the relationships hold if the coefficients are zero in different setups. """ for _ in range(10): for i in ['ALL', 'TREATED', 'UNTREATED', 'COST', 'TREATED & UNTREATED']: constr = constraints(probability=0.0) dict_ = generate_random_dict(constr) if i == 'ALL': for key_ in ['TREATED', 'UNTREATED', 'COST']: dict_[key_]['coeff'] = np.array([0.] * len(dict_[key_]['coeff'])) elif i == 'TREATED & UNTREATED': for key_ in ['TREATED', 'UNTREATED']: dict_[key_]['coeff'] = np.array([0.] * len(dict_[key_]['coeff'])) else: dict_[i]['coeff'] = np.array([0.] * len(dict_[i]['coeff'])) print_dict(dict_) dict_ = read('test.grmpy.ini') df = simulate('test.grmpy.ini') x = df.filter(regex=r'^X\_', axis=1) if i == 'ALL': np.testing.assert_array_equal(df.Y1, df.U1) np.testing.assert_array_equal(df.Y0, df.U0) elif i == 'TREATED & UNTREATED': np.testing.assert_array_equal(df.Y1, df.U1) np.testing.assert_array_equal(df.Y0, df.U0) np.testing.assert_array_equal(df.Y[df.D == 1], df.U1[df.D == 1]) np.testing.assert_array_equal(df.Y[df.D == 0], df.U0[df.D == 0]) elif i == 'TREATED': y_untreated = pd.DataFrame.sum(dict_['UNTREATED']['all'] * x, axis=1) + df.U0 np.testing.assert_array_almost_equal(df.Y0, y_untreated, decimal=5) np.testing.assert_array_equal(df.Y1, df.U1) elif i == 'UNTREATED': y_treated = pd.DataFrame.sum(dict_['TREATED']['all'] * x, axis=1) + df.U1 np.testing.assert_array_almost_equal(df.Y1, y_treated, decimal=5) np.testing.assert_array_equal(df.Y0, df.U0) else: y_treated = pd.DataFrame.sum(dict_['TREATED']['all'] * x, axis=1) + df.U1 y_untreated = pd.DataFrame.sum(dict_['UNTREATED']['all'] * x, axis=1) + df.U0 np.testing.assert_array_almost_equal(df.Y1, y_treated, decimal=5) np.testing.assert_array_almost_equal(df.Y0, y_untreated, decimal=5) np.testing.assert_array_equal(df.Y[df.D == 1], df.Y1[df.D == 1]) np.testing.assert_array_equal(df.Y[df.D == 0], df.Y0[df.D == 0]) np.testing.assert_array_almost_equal(df.V, (df.UC - df.U1 + df.U0))
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)
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()
def test1(): """The first test tests whether the relationships in the simulated datasets are appropriate in a deterministic and an undeterministic setting. """ for case in ['deterministic', 'undeterministic']: if case == 'deterministic': prob = 1.0 else: prob = 0.0 constr = constraints(probability=prob) for _ in range(10): generate_random_dict(constr) df = simulate('test.grmpy.ini') dict_ = read('test.grmpy.ini') x = df.filter(regex=r'^X\_', axis=1) y_treated = pd.DataFrame.sum(dict_['TREATED']['all'] * x, axis=1) + df.U1 y_untreated = pd.DataFrame.sum(dict_['UNTREATED']['all'] * x, axis=1) + df.U0 np.testing.assert_array_almost_equal(df.Y1, y_treated, decimal=5) np.testing.assert_array_almost_equal(df.Y0, y_untreated, decimal=5) np.testing.assert_array_equal(df.Y[df.D == 1], df.Y1[df.D == 1]) np.testing.assert_array_equal(df.Y[df.D == 0], df.Y0[df.D == 0]) np.testing.assert_array_almost_equal(df.V, (df.UC - df.U1 + df.U0), decimal=7)
from grmpy.simulate.simulate import simulate from grmpy.test.auxiliary import cleanup from grmpy.read.read import read NUM_TESTS = 100 np.random.seed(1234235) seeds = np.random.randint(0, 1000, size=NUM_TESTS) directory = os.path.dirname(__file__) file_dir = os.path.join(directory, 'regression_vault.grmpy.json') if True: tests = [] for seed in seeds: np.random.seed(seed) constr = constraints(0.0) dict_ = generate_random_dict(constr) df = simulate('test.grmpy.ini') stat = np.sum(df.sum()) init_dict = read('test.grmpy.ini') start = start_values(init_dict, df, 'init') criteria = calculate_criteria(init_dict, df, start) tests += [(stat, dict_, criteria)] json.dump(tests, open(file_dir, 'w')) if True: tests = json.load(open(file_dir, 'r')) for test in tests: stat, dict_, criteria = test print_dict(dict_)