DoWhyClassExercise (Oct10)

py

School

Boston University *

*We aren’t endorsed by this school

Course

BA472

Subject

Computer Science

Date

Jan 9, 2024

Type

py

Pages

2

Uploaded by MagistrateHyena2457

Report
#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Created on Wed Nov 16 09:16:37 2022 This document was created by Dr. N. Orkun Baycik and cannot be shared with anyone without permission @author: nobaycik """ import pandas as pd import numpy as np IncomeData = pd.read_csv("/Users/shunnosuke/Desktop/BA472/Income.txt", delimiter = "\t") print(IncomeData) import dowhy from dowhy import CausalModel #test if education level impacts annual income. #Create a causal diagram. This is the only place where we are involved. causal_graph = """ digraph { Age; PostGradDegree; Age -> PostGradDegree; Age -> AnnualIncome; PostGradDegree -> AnnualIncome; } """ #Build the model in DoWhy model= CausalModel( data = IncomeData, graph=causal_graph.replace("\n", " "), treatment='PostGradDegree', outcome='AnnualIncome') model.view_model() #Identify the causal effect. Do calculus calculations in the background estimands = model.identify_effect() print(estimands) #----------------------------------------------BACKDOOR LINEAR REGRESSION---------------------------------------------- #Causal Effect Estimation. There are few methods to Estimate the Causal Effect causal_estimate_reg = model.estimate_effect(estimands, method_name="backdoor.linear_regression", test_significance=True) #Validation of assumptions. Add a placebo treatment. Replace the true treatment variable with an independent random variable; #If the assumption was correct, the estimate should go close to zero. placebo_refute_blr = model.refute_estimate(estimands, causal_estimate_reg, method_name="placebo_treatment_refuter", num_simulations=20)
#Validation of assumptions. Data Subset Refuter — replace the given dataset with a randomly selected subset; #If the assumption was correct, the estimation should not change that much. subset_refute_blr = model.refute_estimate(estimands,causal_estimate_reg, method_name = "data_subset_refuter", num_simulations=20) print("BACKDOOR LINEAR REGRESSION RESULTS") print("Results with placebo treatment refuter:") print(placebo_refute_blr) print("Results with data subset refuter:") print(subset_refute_blr)
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help