def test_score(self): N = 100000 z1 = np.random.normal(size=N) z2 = np.random.choice([0, 1], size=N) z3 = np.random.choice(['a', 'b', 'c'], size=N) numeric_mapping = {'a': 3, 'b': 4, 'c': 5} z3_numeric = [numeric_mapping[z3i] for z3i in z3] p_assign = np.exp(z1 + z2 + z3_numeric) / (1. + np.exp(z1 + z2 + z3_numeric)) assignment = np.random.binomial(1, p_assign) outcome = np.random.normal(assignment) matcher = PropensityScoreMatching() X = pd.DataFrame({ 'z1': z1, 'z2': z2, 'z3': z3, 'assignment': assignment, 'outcome': outcome }) confounder_types = {'z1': 'c', 'z2': 'o', 'z3': 'o'} matcher.score(X, confounder_types, store_model_fit=True) assert 0.9 <= matcher.model_fit.params['z1'] <= 1.1 assert 0.9 <= matcher.model_fit.params['z2'] <= 1.1 assert 0.0 <= matcher.model_fit.params['z3_b'] <= 2.0 assert 1.0 <= matcher.model_fit.params['z3_c'] <= 3.0 assert 2.0 <= matcher.model_fit.params['intercept'] <= 4.0
def test_score(self): N = 5000 z1 = np.random.normal(size=N) z2 = np.random.choice(['a','b','c'], size=N) numeric_mapping = {'a' :3, 'b' :4, 'c' :5} z2_numeric = [numeric_mapping[z2i] for z2i in z2] p_assign = np.exp(z1 + z2_numeric) / (1. + np.exp(z1 + z2_numeric)) assignment = np.random.binomial(1, p_assign) outcome = np.random.normal(assignment) matcher = PropensityScoreMatching() X = pd.DataFrame({'z1': z1, 'z2': z2, 'assignment': assignment, 'outcome': outcome}) confounder_types = {'z1': 'c', 'z2':'o'} matcher.score(X, confounder_types, store_model_fit=True) assert 0.7 <= matcher.propensity_score_model.params['z1'] <= 1.3 assert 0.0 <= matcher.propensity_score_model.params['z2_b'] <= 2.0 assert 1.0 <= matcher.propensity_score_model.params['z2_c'] <= 3.0 assert 2.0 <= matcher.propensity_score_model.params['intercept'] <= 4.0
def test_score(self): N = 100000 z1 = np.random.normal(size=N) z2 = np.random.choice([0, 1], size=N) z3 = np.random.choice(["a", "b", "c"], size=N) numeric_mapping = {"a": 3, "b": 4, "c": 5} z3_numeric = [numeric_mapping[z3i] for z3i in z3] p_assign = np.exp(z1 + z2 + z3_numeric) / (1.0 + np.exp(z1 + z2 + z3_numeric)) assignment = np.random.binomial(1, p_assign) outcome = np.random.normal(assignment) matcher = PropensityScoreMatching() X = pd.DataFrame({"z1": z1, "z2": z2, "z3": z3, "assignment": assignment, "outcome": outcome}) confounder_types = {"z1": "c", "z2": "o", "z3": "o"} matcher.score(X, confounder_types, store_model_fit=True) assert 0.9 <= matcher.model_fit.params["z1"] <= 1.1 assert 0.9 <= matcher.model_fit.params["z2"] <= 1.1 assert 0.0 <= matcher.model_fit.params["z3_b"] <= 2.0 assert 1.0 <= matcher.model_fit.params["z3_c"] <= 3.0 assert 2.0 <= matcher.model_fit.params["intercept"] <= 4.0
def spx_wmOnBoard_tobin(): ''' Test the effect of having women on the board of directors on the tobins Q score M&M: For the American companies inside the S&P 500 index, we found a positive correlation between the percentage higher than 20 % of women in the board and the Tobin’s Q ratio Latest Results: (-0.094388682158789469, -0.04962212239013655, -0.0068850052276448973) Comments: So no real causal influence here, wrong sign ''' data, types = getData("spx_fboard","corp_gov_causal") controlFor = stageAlgo(types) treatment = 'X..Women.on.Bd' target = 'Tobins.Q' matcher = PropensityScoreMatching() ATE_results = matcher.estimate_ATE(data, treatment, target, {'P.EBITDA': 'c', 'P.B': 'c', 'Asset':'c', 'Tax':'c', 'P.E':'c'}, bootstrap=True) matcher.check_support(data, 'X..Women.on.Bd', {'P.EBITDA': 'c', 'P.B': 'c','Asset':'c', 'Tax':'c', 'P.E':'c'}) print("") print("Balance before matching") print(matcher.assess_balance(data, 'X..Women.on.Bd', {'P.EBITDA': 'c', 'P.B': 'c','Asset':'c', 'Tax':'c', 'P.E':'c', 'propensity score': 'c'})) print ("") data = matcher.score(data, assignment='X..Women.on.Bd', confounder_types={'P.EBITDA': 'c', 'P.B': 'c','Asset':'c', 'Tax':'c', 'P.E':'c'}) treated, control = matcher.match(data, assignment='X..Women.on.Bd') print("") print("Balance after matching") print(matcher.assess_balance(treated.append(control), 'X..Women.on.Bd', {'P.EBITDA': 'c', 'P.B': 'c','Asset':'c', 'Tax':'c', 'P.E':'c', 'propensity score': 'c'})) print ("") #now write results to mysql conn = MySQLdb.connect(host="localhost", user="******", passwd="", db="causal_results") cur = conn.cursor() query = """ insert into akelleh_results values ('%s','%s','%s','%s','%s','%s'); """ % (now,"spx",treatment,target,str(ATE_results),"For the American companies inside the S and P 500 index, we found a positive correlation between the percentage higher than 20pct of women in the board and the Tobins Q ratio") cur.execute(query) conn.commit() conn.close() print("Done")