### Install a specific dependency Source: https://github.com/py-why/dowhy/blob/main/README.rst Manually install a specific dependency with a given version. ```shell pip install '==' ``` -------------------------------- ### Import DoWhy and Setup Notebook Rendering Source: https://github.com/py-why/dowhy/blob/main/docs/source/example_notebooks/dowhy_causal_discovery_example.ipynb Imports necessary DoWhy components and enables notebook rendering for visualizations. This is a standard setup for DoWhy notebooks. ```python import dowhy dowhy.enable_notebook_rendering() from dowhy import CausalModel import numpy as np import pandas as pd import graphviz import networkx as nx np.set_printoptions(precision=3, suppress=True) np.random.seed(0) ``` -------------------------------- ### Install DoWhy using poetry Source: https://github.com/py-why/dowhy/blob/main/README.rst Install the latest release of DoWhy using poetry. ```shell poetry add dowhy ``` -------------------------------- ### Setup DoWhy Notebook Rendering and Logging Source: https://github.com/py-why/dowhy/blob/main/docs/source/example_notebooks/dowhy_twins_example.ipynb Initializes DoWhy for notebook rendering and configures logging levels. This setup is essential for interactive analysis and managing output verbosity. ```python import pandas as pd import numpy as np import dowhy dowhy.enable_notebook_rendering() from dowhy import CausalModel from dowhy import causal_estimators # Config dict to set the logging level import logging.config DEFAULT_LOGGING = { 'version': 1, 'disable_existing_loggers': False, 'loggers': { '': { 'level': 'WARN', }, } } logging.config.dictConfig(DEFAULT_LOGGING) ``` -------------------------------- ### Install DoWhy and EconML Source: https://github.com/py-why/dowhy/blob/main/docs/source/example_notebooks/tutorial-causalinference-machinelearning-using-dowhy-econml.ipynb Installs the necessary DoWhy and EconML libraries. Ensure you have a compatible Python environment. ```bash pip install "dowhy[all]" econml ``` -------------------------------- ### Install DoWhy Dependencies with Pip Source: https://github.com/py-why/dowhy/blob/main/docs/source/getting_started/install.rst Install all project dependencies listed in the requirements.txt file. ```shell pip install -r requirements.txt ``` -------------------------------- ### Install DoWhy with EconML Extra Source: https://github.com/py-why/dowhy/blob/main/AGENTS.md Installs DoWhy with the EconML extra, which provides access to EconML CATE estimators. ```bash poetry install -E "econml" # EconML CATE estimators ``` -------------------------------- ### Install DoWhy with Plotting Extra Source: https://github.com/py-why/dowhy/blob/main/AGENTS.md Installs DoWhy with the plotting extra for development. Ensure pip is up-to-date before installation. ```bash pip install --upgrade pip poetry install -E "plotting" # Standard dev install ``` -------------------------------- ### Test DoWhy Installation Source: https://github.com/py-why/dowhy/blob/main/docs/source/getting_started/install.rst Verify the DoWhy installation by importing the library in a Python environment. ```python import dowhy ``` -------------------------------- ### Install PyGraphviz on Linux Source: https://github.com/py-why/dowhy/blob/main/docs/source/contributing/contributing-code.rst Install pygraphviz on Linux systems. This involves first installing graphviz and its development headers, then installing pygraphviz with specific build options. ```shell sudo apt install graphviz libgraphviz-dev graphviz-dev pkg-config pip install --global-option=build_ext \ --global-option="-I/usr/local/include/graphviz/" \ --global-option="-L/usr/local/lib/graphviz" pygraphviz ``` -------------------------------- ### Load Sample Data and Create Causal Model Source: https://github.com/py-why/dowhy/blob/main/docs/source/user_guide/causal_tasks/estimating_causal_effects/effect_estimation_with_estimators.rst This snippet demonstrates loading a sample dataset and initializing a CausalModel in DoWhy. Ensure you have the necessary libraries installed. ```python from dowhy import CausalModel import dowhy.datasets # Load some sample data data = dowhy.datasets.linear_dataset( beta=10, num_common_causes=5, num_instruments=2, num_samples=10000, treatment_is_binary=True) model = CausalModel( data=data["df"], treatment=data["treatment_name"], outcome=data["outcome_name"], graph=data["gml_graph"]) ``` -------------------------------- ### Install DoWhy using pip Source: https://github.com/py-why/dowhy/blob/main/README.rst Install the latest release of DoWhy using pip. ```shell pip install dowhy ``` -------------------------------- ### Install DoWhy development version Source: https://github.com/py-why/dowhy/blob/main/README.rst Install the latest development version of DoWhy from GitHub. ```shell pip install git+https://github.com/py-why/dowhy@main ``` -------------------------------- ### Install DoWhy Development Version in Azure ML Notebook Source: https://github.com/py-why/dowhy/blob/main/docs/source/getting_started/install.rst Install the development version of DoWhy in an Azure ML notebook by specifying the local path after cloning the repository. ```python %pip install -e ``` -------------------------------- ### Install Tigramite library Source: https://github.com/py-why/dowhy/blob/main/docs/source/example_notebooks/timeseries/effect_inference_timeseries_data.ipynb Installs the Tigramite library, which is used for temporal causal discovery and can be integrated with DoWhy. ```bash !pip install tigramite ``` -------------------------------- ### Initialize DoWhy and Set Signup Month Source: https://github.com/py-why/dowhy/blob/main/docs/source/example_notebooks/dowhy_example_effect_of_memberrewards_program.ipynb Initializes DoWhy for notebook rendering and sets a variable `i` to represent the signup month for subsequent analysis. This is a setup step for causal modeling. ```python import dowhy dowhy.enable_notebook_rendering() # Setting the signup month (for ease of analysis) i = 3 ``` -------------------------------- ### Install DoWhy with Plotting Source: https://github.com/py-why/dowhy/blob/main/docs/source/contributing/contributing-code.rst Install DoWhy and its requirements using Poetry. The '-E "plotting"' flag includes optional plotting dependencies. Poetry automatically creates a virtual environment. ```shell cd dowhy pip install --upgrade pip poetry install -E "plotting" ``` -------------------------------- ### Install DoWhy with Pygraphviz Extra Source: https://github.com/py-why/dowhy/blob/main/AGENTS.md Installs DoWhy with the pygraphviz extra for graph visualization. Note that on Linux, graphviz and its developer package may need to be installed separately. ```bash poetry install -E "pygraphviz" # Graph visualization via graphviz ``` -------------------------------- ### Import Libraries for Causal Inference Source: https://github.com/py-why/dowhy/blob/main/docs/source/example_notebooks/tutorial-causalinference-machinelearning-using-dowhy-econml.ipynb Imports DoWhy, enables notebook rendering, and sets up logging and warnings for cleaner output. This is a standard setup for DoWhy tutorials. ```python # Required libraries import dowhy dowhy.enable_notebook_rendering() from dowhy import CausalModel import dowhy.datasets # Avoiding unnecessary log messges and warnings import logging logging.getLogger("dowhy").setLevel(logging.WARNING) import warnings from sklearn.exceptions import DataConversionWarning warnings.filterwarnings(action='ignore', category=DataConversionWarning) ``` -------------------------------- ### Import DoWhy and Enable Notebook Rendering Source: https://github.com/py-why/dowhy/blob/main/docs/source/example_notebooks/dowhy_generalized_covariate_adjustment_example.ipynb Imports the necessary DoWhy library and enables rendering for notebook environments. This is a standard setup for DoWhy notebooks. ```python import dowhy dowhy.enable_notebook_rendering() from dowhy import CausalModel import pandas as pd from IPython.display import Image, display ``` -------------------------------- ### Graphical Causal Model (GCM) Setup and Fitting Source: https://github.com/py-why/dowhy/blob/main/README.rst Sets up and fits a Structural Causal Model (SCM) to data for causal inference. This involves defining the causal graph and assigning causal mechanisms. ```python import networkx as nx, numpy as np, pandas as pd from dowhy import gcm # Let's generate some "normal" data we assume we're given from our problem domain: X = np.random.normal(loc=0, scale=1, size=1000) Y = 2 * X + np.random.normal(loc=0, scale=1, size=1000) Z = 3 * Y + np.random.normal(loc=0, scale=1, size=1000) data = pd.DataFrame(dict(X=X, Y=Y, Z=Z)) # 1. Modeling cause-effect relationships as a structural causal model # (causal graph + functional causal models): causal_model = gcm.StructuralCausalModel(nx.DiGraph([('X', 'Y'), ('Y', 'Z')])) # X -> Y -> Z gcm.auto.assign_causal_mechanisms(causal_model, data) # 2. Fitting the SCM to the data: gcm.fit(causal_model, data) # Optional: Evaluate causal model print(gcm.evaluate_causal_model(causal_model, data)) ``` -------------------------------- ### Setting up Experiment for Unobserved Confounding Source: https://github.com/py-why/dowhy/blob/main/docs/source/example_notebooks/dowhy_ranking_methods.ipynb Initializes an Experiment object to simulate unobserved confounding. This setup includes defining experiment parameters, data generation processes (DGPs), estimators, and refuters. ```python unobserved_confounding_error = Experiment( experiment_name='Test', experiment_id='2', num_experiments=10, # 10 sample_sizes=sample_size, dgps=dgp_list, estimators=estimator_tuples, refuters=refuter_tuples, simulate_unobserved_confounding = True ) # Run the experiment res = pd.read_csv(unobserved_confounding_error.experiment()) ``` -------------------------------- ### Initialize Causal Model for Example 8 Source: https://github.com/py-why/dowhy/blob/main/docs/source/example_notebooks/dowhy_generalized_covariate_adjustment_example.ipynb Initializes a CausalModel for a more complex graph from Example 8 of Perković et al. (2018). This graph is designed to show limitations of the backdoor criterion. ```python # Random data treatment = ["X1", "X2"] outcome = "Y" variables = ["V1", "V2", "V3", "V4", "V5"] hidden_variables = ["L"] causal_graph = """graph[directed 1 node[id \"X1\" label \"X1\"] node[id \"X2\" label \"X2\"] node[id \"Y\" label \"Y\"] node[id \"V1\" label \"V1\"] node[id \"V2\" label \"V2\"] node[id \"V3\" label \"V3\"] node[id \"V4\" label \"V4\"] node[id \"V5\" label \"V5\"] node[id \"L\" label \"L\"] edge[source \"V5\" target \"X1\"] edge[source \"V4\" target \"X1\"] edge[source \"X1\" target \"V1\"] edge[source \"V1\" target \"V2\"] edge[source \"V2\" target \"X2\"] edge[source \"X2\" target \"Y\"] edge[source \"X1\" target \"V3\"] edge[source \"V3\" target \"Y\"] edge[source \"L\" target \"V3\"] edge[source \"L\" target \"V2\"] " columns = list(treatment) + list(outcome) + list(variables) df = pd.DataFrame(columns=columns) # Causal Model Initialization causal_model = CausalModel(df, treatment, outcome, graph=causal_graph) # View graph causal_model.view_model() from IPython.display import Image, display print("Graph with no backdoor set:") display(Image(filename="causal_model.png")) ``` -------------------------------- ### Setup and Plot Causal Graph Source: https://github.com/py-why/dowhy/blob/main/docs/source/example_notebooks/gcm_supply_chain_dist_change.ipynb Initializes a directed graph representing the causal relationships between supply chain variables and plots it. This requires the networkx and dowhy libraries. ```python import dowhy dowhy.enable_notebook_rendering() import networkx as nx import dowhy.gcm as gcm from dowhy.utils import plot gcm.util.general.set_random_seed(0) causal_graph = nx.DiGraph([('demand', 'submitted'), ('constraint', 'submitted'), ('submitted', 'confirmed'), ('confirmed', 'received')]) plot(causal_graph) ``` -------------------------------- ### Generate Example Data Source: https://github.com/py-why/dowhy/blob/main/docs/source/user_guide/causal_tasks/root_causing_and_explaining/anomaly_attribution.rst Generates sample data for a simple chain X -> Y -> Z -> W using numpy and pandas. This data is used to model cause-effect relationships. ```python import numpy as np import pandas as pd from dowhy import gcm X = np.random.uniform(low=-5, high=5, size=1000) Y = 0.5 * X + np.random.normal(loc=0, scale=1, size=1000) Z = 2 * Y + np.random.normal(loc=0, scale=1, size=1000) W = 3 * Z + np.random.normal(loc=0, scale=1, size=1000) data = pd.DataFrame(data=dict(X=X, Y=Y, Z=Z, W=W)) ``` -------------------------------- ### Install Latest DoWhy Release in Azure ML Notebook Source: https://github.com/py-why/dowhy/blob/main/docs/source/getting_started/install.rst Use the %pip magic command within an Azure Machine Learning notebook to install the latest DoWhy release. ```python %pip install dowhy ``` -------------------------------- ### Load Lalonde Dataset Source: https://github.com/py-why/dowhy/blob/main/docs/source/example_notebooks/dowhy_lalonde_example.ipynb Loads the Lalonde dataset using dowhy.datasets.lalonde_dataset(). Requires dowhy to be installed and notebook rendering enabled. ```python import dowhy dowhy.enable_notebook_rendering() import dowhy.datasets lalonde = dowhy.datasets.lalonde_dataset() ``` -------------------------------- ### Generate Example Data for Causal Model Source: https://github.com/py-why/dowhy/blob/main/docs/source/user_guide/causal_tasks/root_causing_and_explaining/feature_relevance.rst Generates synthetic data for demonstrating causal GCM feature relevance. Requires numpy, pandas, and networkx. ```python import numpy as np import pandas as pd import networkx as nx from dowhy import gcm X = np.random.normal(loc=0, scale=1, size=1000) Z = np.random.normal(loc=0, scale=1, size=1000) Y = X + 3 * Z + np.random.normal(loc=0, scale=1, size=1000) data = pd.DataFrame(data=dict(X=X, Y=Y, Z=Z)) ``` -------------------------------- ### Building Graph with Costs for Variables Source: https://github.com/py-why/dowhy/blob/main/docs/source/example_notebooks/dowhy_efficient_backdoor_example.ipynb Defines a complex graph structure and associated costs for observable variables. This setup is used to demonstrate minimum cost backdoor adjustment. ```python graph_str = """graph[directed 1 node[id "L" label "L"] node[id "X" label "X"] node[id "K" label "K"] node[id "B" label "B"] node[id "Q" label "Q"] node[id "R" label "R"] node[id "T" label "T"] node[id "M" label "M"] node[id "Y" label "Y"] node[id "U" label "U"] node[id "F" label "F"] edge[source "L" target "X"] edge[source "X" target "M"] edge[source "K" target "X"] edge[source "B" target "K"] edge[source "B" target "R"] edge[source "Q" target "K"] edge[source "Q" target "T"] edge[source "R" target "Y"] edge[source "T" target "Y"] edge[source "M" target "Y"] edge[source "U" target "Y"] edge[source "U" target "F"] ] """ observed_node_names = ["L", "X", "B", "K", "Q", "R", "M", "T", "Y", "F"] conditional_node_names = ["L"] costs = [ ("L", {"cost": 1}), ("B", {"cost": 1}), ("K", {"cost": 4}), ("Q", {"cost": 1}), ("R", {"cost": 2}), ("T", {"cost": 1}), ] G = build_graph_from_str(graph_str) ``` -------------------------------- ### Simulate Resource Shifting Intervention Source: https://github.com/py-why/dowhy/blob/main/docs/source/example_notebooks/gcm_rca_microservice_architecture.ipynb Simulates the effect of interventions on service latencies by adjusting causal mechanisms. This example models reducing 'Caching Service' latency and increasing 'Shipping Cost Service' latency. ```python median_mean_latencies, uncertainty_mean_latencies = gcm.confidence_intervals( lambda : gcm.fit_and_compute(gcm.interventional_samples, causal_model, outlier_data, interventions = { "Caching Service": lambda x: x-1, "Shipping Cost Service": lambda x: x+2 }, observed_data=outlier_data)().mean().to_dict(), num_bootstrap_resamples=10) ``` -------------------------------- ### Initialize Causal Model for Figure 1(b) Source: https://github.com/py-why/dowhy/blob/main/docs/source/example_notebooks/dowhy_generalized_covariate_adjustment_example.ipynb Initializes a CausalModel for a simple graph representing Figure 1(b) from Shpitser et al. (2010). This setup is used to illustrate cases where the backdoor criterion is insufficient. ```python # Random data treatment = "X" outcome = "Y" variables = ["Z"] causal_graph = "digraph{Z;X;Y; X->Z;X->Y}" columns = list(treatment) + list(outcome) + list(variables) df = pd.DataFrame(columns=columns) # Causal Model Initialization causal_model = CausalModel(df, treatment, outcome, graph=causal_graph) # View graph causal_model.view_model() print("Figure 1(b):") display(Image(filename="causal_model.png")) ``` -------------------------------- ### Estimate Conditional Treatment Effects using EconML's DML Source: https://github.com/py-why/dowhy/blob/main/docs/source/user_guide/causal_tasks/estimating_causal_effects/effect_estimation_with_estimators.rst This example shows how to estimate conditional average treatment effects (CATE) by integrating with EconML's Double Machine Learning (DML) estimator. It requires specifying models for outcome and treatment, and a featurizer. ```python from sklearn.preprocessing import PolynomialFeatures from sklearn.linear_model import LassoCV from sklearn.ensemble import GradientBoostingRegressor dml_estimate = model.estimate_effect(identified_estimand, method_name="backdoor.econml.dml.DML", control_value = 0, treatment_value = 1, target_units = lambda df: df["X0"]>1, confidence_intervals=False, method_params={ "init_params":{'model_y':GradientBoostingRegressor(), 'model_t': GradientBoostingRegressor(), 'model_final':LassoCV(), 'featurizer':PolynomialFeatures(degree=1, include_bias=True)} }) ``` -------------------------------- ### Import Libraries and Configure Logging Source: https://github.com/py-why/dowhy/blob/main/docs/source/example_notebooks/dowhy_demo_dummy_outcome_refuter.ipynb Imports necessary libraries for DoWhy and data manipulation, and sets up basic logging configuration. ```python import dowhy dowhy.enable_notebook_rendering() from dowhy import CausalModel import dowhy.datasets import pandas as pd import numpy as np # Config dict to set the logging level import logging.config DEFAULT_LOGGING = { 'version': 1, 'disable_existing_loggers': False, 'loggers': { '': { 'level': 'WARN', }, } } logging.config.dictConfig(DEFAULT_LOGGING) ``` -------------------------------- ### Set up Python Path Source: https://github.com/py-why/dowhy/blob/main/docs/source/example_notebooks/do_sampler_demo.ipynb Appends the parent directory to the Python path to allow importing modules from the project root. ```python import os, sys sys.path.append(os.path.abspath("../../../\\")) ``` -------------------------------- ### Initialize a Causal Graph Source: https://github.com/py-why/dowhy/wiki/API-proposal-for-v1 Demonstrates the initialization of a directed graph for causal modeling. This is a foundational step before defining relationships between variables. ```python causal_graph = networkx.DiGraph(...) ``` -------------------------------- ### Install DoWhy using conda Source: https://github.com/py-why/dowhy/blob/main/README.rst Install the latest release of DoWhy using conda. ```shell conda install -c conda-forge dowhy ``` -------------------------------- ### Initialize DoWhy and Enable Notebook Rendering Source: https://github.com/py-why/dowhy/blob/main/docs/source/example_notebooks/gcm_401k_analysis.ipynb Sets up the DoWhy library for causal inference and enables rendering of causal graphs within a notebook environment. Imports necessary libraries for GCMs and network visualization. ```python import dowhy dowhy.enable_notebook_rendering() import networkx as nx import dowhy.gcm as gcm ``` -------------------------------- ### Enable Notebook Rendering and Import Libraries Source: https://github.com/py-why/dowhy/blob/main/docs/source/example_notebooks/sales_attribution_intervention.ipynb Enables notebook rendering for DoWhy and imports necessary libraries including plotting, data manipulation, and statistical functions. Sets a random seed for reproducibility. ```python import dowhy dowhy.enable_notebook_rendering() import matplotlib.pyplot as plt import networkx as nx import numpy as np import pandas as pd from functools import partial from dowhy import gcm from dowhy.utils.plotting import plot from scipy import stats from statsmodels.stats.multitest import multipletests gcm.util.general.set_random_seed(0) %matplotlib inline ``` -------------------------------- ### Advanced Algorithm Usage with fit() Method Source: https://github.com/py-why/dowhy/wiki/API-for-Global-Causal-Discovery Illustrates using discovery algorithms directly via a scikit-learn-like API. Algorithms like PC and BIC are instantiated, fitted to the context, and their results (graph and separating sets) are accessed. ```Python discoverer1= PC(test="chi-squared", alpha=.05) discoverer1.fit(context) cpdag = discoverer1.graph_ separating_sets = discoverer1.sep_sets_ discoverer2 = BIC() discoverer2.fit(context) ``` -------------------------------- ### Data Preparation for Causal Analysis Source: https://github.com/py-why/dowhy/blob/main/docs/source/example_notebooks/dowhy_example_effect_of_memberrewards_program.ipynb Prepares customer data by calculating average monthly spend before and after a signup month for causal analysis. This involves grouping by user and signup month, then applying a function to compute pre and post spend means. ```python df_i_signupmonth = ( df[df.signup_month.isin([0, i])] .groupby(["user_id", "signup_month", "treatment"]) .apply( lambda x: pd.Series( { "pre_spends": x.loc[x.month < i, "spend"].mean(), "post_spends": x.loc[x.month > i, "spend"].mean(), } ) ) .reset_index() ) print(df_i_signupmonth) ``` -------------------------------- ### Enable Notebook Rendering and Import Libraries Source: https://github.com/py-why/dowhy/blob/main/docs/source/example_notebooks/gcm_icc.ipynb Enables notebook rendering for DoWhy and imports necessary libraries including pandas, networkx, numpy, and DoWhy's GCM module. Sets a random seed for reproducibility. ```python import dowhy dowhy.enable_notebook_rendering() import pandas as pd import networkx as nx import numpy as np from dowhy import gcm gcm.util.general.set_random_seed(0) ``` -------------------------------- ### Initialize Causal Model and View Graph Source: https://github.com/py-why/dowhy/blob/main/docs/source/example_notebooks/identifying_effects_using_id_algorithm.ipynb Initializes a CausalModel with specified treatment, outcome, and graph, then visualizes the model's graph. Requires pandas and IPython.display. ```python import pandas as pd from dowhy import CausalModel from IPython.display import Image, display # Random data treatment = "T" outcome = "Y" variables = ["X1"] causal_graph = "digraph{T;X1->Y;}" columns = list(treatment) + list(outcome) + list(variables) df = pd.DataFrame(columns=columns) # Causal Model Initialization causal_model = CausalModel(df, treatment, outcome, graph=causal_graph) # View graph causal_model.view_model() print("Graph:") display(Image(filename="causal_model.png")) ``` -------------------------------- ### Initialize and Visualize Causal Model Source: https://github.com/py-why/dowhy/blob/main/docs/source/example_notebooks/sensitivity_analysis_testing.ipynb Initializes a CausalModel using the generated dataset and visualizes the causal graph. Displays the first few rows of the dataset. ```python model = CausalModel( data=data["df"], treatment=data["treatment_name"], outcome=data["outcome_name"], graph=data["gml_graph"], test_significance=None, ) model.view_model() from IPython.display import Image, display display(Image(filename="causal_model.png")) data['df'].head() ``` -------------------------------- ### Generate Data for ICC Example Source: https://github.com/py-why/dowhy/blob/main/docs/source/user_guide/causal_tasks/quantify_causal_influence/icc.rst Generates synthetic data for the train delay example (X, Y, Z) following a linear causal structure with additive noise. Note the larger standard deviation for the noise in X. ```python import numpy as np import pandas as pd from dowhy import gcm import networkx as nx X = abs(np.random.normal(loc=0, scale=5, size=1000)) Y = X + abs(np.random.normal(loc=0, scale=1, size=1000)) Z = Y + abs(np.random.normal(loc=0, scale=1, size=1000)) data = pd.DataFrame(data=dict(X=X, Y=Y, Z=Z)) ``` -------------------------------- ### Create and Visualize Causal Model Source: https://github.com/py-why/dowhy/blob/main/docs/source/example_notebooks/sensitivity_analysis_nonparametric_estimators.ipynb Initializes a CausalModel using the prepared user data, treatment, outcome, and causal graph, then displays the model's graphical representation. ```python model = CausalModel( data=user_data, treatment=data["treatment_name"], outcome=data["outcome_name"], graph=user_graph, test_significance=None, ) model.view_model() from IPython.display import Image, display display(Image(filename="causal_model.png")) ``` -------------------------------- ### Estimate the Causal Effect Source: https://github.com/py-why/dowhy/blob/main/docs/source/example_notebooks/tutorial-causalinference-machinelearning-using-dowhy-econml.ipynb Estimates the identified causal effect using a specified method. This example uses the 'backdoor.propensity_score_matching' method. ```python # Estimate the causal effect causal_estimate = cause_model.estimate_effect(causal_estimate, method_name="backdoor.propensity_score_matching") print(causal_estimate) ``` -------------------------------- ### Import DoWhy Libraries Source: https://github.com/py-why/dowhy/blob/main/docs/source/example_notebooks/dowhy_efficient_backdoor_example.ipynb Imports necessary libraries from the DoWhy package for causal graph manipulation and analysis. This is a standard setup for DoWhy applications. ```python import dowhy dowhy.enable_notebook_rendering() from dowhy.causal_graph import CausalGraph from dowhy.causal_identifier import AutoIdentifier, BackdoorAdjustment, EstimandType from dowhy.graph import build_graph_from_str from dowhy.utils.plotting import plot ``` -------------------------------- ### Print Default SupportConfig Source: https://github.com/py-why/dowhy/blob/main/docs/source/example_notebooks/dowhy_refuter_assess_overlap.ipynb Prints the default configuration of SupportConfig. This is useful for understanding the initial settings before customization. ```python support_config = SupportConfig() print(support_config) ``` -------------------------------- ### Visualize Causal Graph using pydot Source: https://github.com/py-why/dowhy/blob/main/docs/source/example_notebooks/dowhy_causal_discovery_example.ipynb Visualizes a causal graph using the pydot library and matplotlib. Requires the 'pydot' and 'matplotlib' libraries to be installed. ```python from causallearn.utils.GraphUtils import GraphUtils import matplotlib.image as mpimg import matplotlib.pyplot as plt import io pyd = GraphUtils.to_pydot(Record['G'], labels=labels) tmp_png = pyd.create_png(f="png") fp = io.BytesIO(tmp_png) img = mpimg.imread(fp, format='png') plt.axis('off') plt.imshow(img) plt.show() ``` -------------------------------- ### Configure and Analyze Counterfactual Fairness (Aware Model) Source: https://github.com/py-why/dowhy/blob/main/docs/source/example_notebooks/counterfactual_fairness_dowhy.ipynb Sets up configuration parameters for analyzing counterfactual fairness with an 'aware' linear regression model. This includes specifying the dataset, estimator, protected attributes, DAG, features, target variable, and disadvantage group. It then calls the `analyse_counterfactual_fairness` function to perform the analysis. ```python config = { "df": df_sample, "estimator": LinearRegression, "protected_attrs": ["Race"], "dag": dag, "X": ["GPA", "LSAT", "Race", "Gender"], "target": "avg_grade", "return_cache": True, "disadvantage_group": disadvantage_group, } counterfactual_fairness_aware, df_obs_aware, df_cf_aware = ( analyse_counterfactual_fairness(**config) ) counterfactual_fairness_aware ``` -------------------------------- ### Define Graph and Observed Nodes Source: https://github.com/py-why/dowhy/blob/main/docs/source/example_notebooks/dowhy_efficient_backdoor_example.ipynb Defines the graph structure and lists the observed nodes for the causal inference problem. This setup is crucial for subsequent analysis. ```python graph_str = """graph[directed 1 node[id "X" label "X"] node[id "Y" label "Y"] node[id "Z1" label "Z1"] node[id "Z2" label "Z2"] node[id "U" label "U"] edge[source "X" target "Y"] edge[source "Z1" target "X"] edge[source "Z1" target "Z2"] edge[source "U" target "Z2"] edge[source "U" target "Y"] ] """ observed_node_names = ["X", "Y", "Z1", "Z2"] treatment_name = "X" outcome_name = "Y" G = build_graph_from_str(graph_str) ``` -------------------------------- ### Generate Linear Dataset Source: https://github.com/py-why/dowhy/blob/main/docs/source/example_notebooks/sensitivity_analysis_testing.ipynb Generates a synthetic linear dataset for causal inference analysis. This is useful for creating reproducible examples and testing causal models. ```python import numpy as np import dowhy np.random.seed(100) data = dowhy.datasets.linear_dataset( beta = 10, num_common_causes = 7, num_samples = 500, num_treatments = 1, stddev_treatment_noise=10, stddev_outcome_noise = 1 ) ``` -------------------------------- ### Run Specific Tests with Pytest Source: https://github.com/py-why/dowhy/blob/main/docs/source/contributing/contributing-code.rst To speed up development, you can run specific tests using pytest. This example shows how to run tests only for the 'causal_refuters' module. ```shell poetry run pytest -v tests/causal_refuters ``` -------------------------------- ### Visualize Causal Model Source: https://github.com/py-why/dowhy/blob/main/docs/source/example_notebooks/tutorial-causalinference-machinelearning-using-dowhy-econml.ipynb Visualizes the defined causal model using the 'dot' layout. This helps in understanding the assumed causal relationships between variables. Requires graphviz to be installed. ```python model.view_model(layout="dot") from IPython.display import Image, display display(Image(filename="causal_model.png")) ``` -------------------------------- ### Force Reinstall DoWhy in Azure ML Notebook Source: https://github.com/py-why/dowhy/blob/main/docs/source/getting_started/install.rst Use the %pip magic command with force-reinstall and no-cache-dir options to ensure a clean installation of DoWhy in Azure ML. ```python %pip install --force-reinstall --no-cache-dir dowhy ``` -------------------------------- ### Display Do Method Docstring Source: https://github.com/py-why/dowhy/blob/main/docs/source/example_notebooks/lalonde_pandas_api.ipynb Prints the docstring for the 'do' method, providing detailed information on its usage and parameters. ```python help(lalonde.causal.do) ``` -------------------------------- ### Ensemble Discovery via Bootstrapping and Mapping Source: https://github.com/py-why/dowhy/wiki/API-for-Global-Causal-Discovery Demonstrates an ensemble discovery workflow using bootstrapping. Datasets are bootstrapped, contexts are created for each, and `learn_graph` is applied to each context using `map` for parallel processing. ```Python get_context = partial(BayesianContext, prior={ "pseudocounts": 10, "edgewise": prior_df } ) contexts = [get_context(boot_df) for boot_df in bootstrap_dfs] def get_pdags(context): return learn_graph( context, method=score_discoverer, ) pdags = map(contexts, get_pdags) ``` -------------------------------- ### Import necessary libraries for DoWhy and data manipulation Source: https://github.com/py-why/dowhy/blob/main/docs/source/example_notebooks/dowhy_refuter_assess_overlap.ipynb Imports DoWhy, NumPy, Pandas, Matplotlib, and specific classes for OverRule refutation and configuration. This setup is required before applying OverRule. ```python import dowhy dowhy.enable_notebook_rendering() import numpy as np import pandas as pd import dowhy.datasets from dowhy import CausalModel # Functional API from dowhy.causal_refuters.assess_overlap import assess_support_and_overlap_overrule # Data classes to configure ruleset optimization from dowhy.causal_refuters.assess_overlap_overrule import SupportConfig, OverlapConfig import matplotlib.pyplot as plt ``` -------------------------------- ### Basic Context Initialization for Causal Discovery Source: https://github.com/py-why/dowhy/wiki/API-for-Global-Causal-Discovery Initialize an empty Context object for assumption-free causal discovery. This context is then passed along with data to the learn_graph function. ```Python context = Context() model = learn_graph(context, data) ``` -------------------------------- ### Use Textual Effect Interpreter in DoWhy Source: https://github.com/py-why/dowhy/blob/main/docs/source/example_notebooks/dowhy_interpreter.ipynb Demonstrates how to use the textual effect interpreter to get a textual explanation of the causal effect. This method provides a summary of the estimated effect. ```python interpretation = causal_estimate_ipw.interpret(method_name="textual_effect_interpreter") ``` -------------------------------- ### Load Datasets and Configure Refuter Source: https://github.com/py-why/dowhy/blob/main/docs/source/example_notebooks/dowhy_refuter_assess_overlap.ipynb Loads the Lalonde experimental dataset and the PSID observational dataset. It then merges them and configures the SupportConfig and OverlapConfig for the OverRule refuter. ```python import pandas as pd import dowhy.datasets from dowhy.causal_refuters.assess_overlap import assess_support_and_overlap_overrule from dowhy.causal_refuters.assess_overlap_overrule import OverlapConfig, SupportConfig # Experimental sample from Lalonde experiment_data = dowhy.datasets.lalonde_dataset() # Observational sample observational_controls = dowhy.datasets.psid_dataset() experiment_data["exp_samp"] = True observational_controls["exp_samp"] = False data = pd.concat([experiment_data, observational_controls], axis=0, ignore_index=True) support_config = SupportConfig(seed=0, lambda0=0.01, lambda1=0.01) overlap_config = OverlapConfig(lambda0=0.01, lambda1=0.01) ``` -------------------------------- ### Manually Assign Causal Mechanisms Source: https://github.com/py-why/dowhy/blob/main/docs/source/example_notebooks/gcm_basic_example.ipynb Manually assigns specific causal mechanisms to nodes in the structural causal model. This example assigns an EmpiricalDistribution to 'X' and AdditiveNoiseModel with linear regressors to 'Y' and 'Z'. ```python causal_model.set_causal_mechanism('X', gcm.EmpiricalDistribution()) causal_model.set_causal_mechanism('Y', gcm.AdditiveNoiseModel(gcm.ml.create_linear_regressor())) causal_model.set_causal_mechanism('Z', gcm.AdditiveNoiseModel(gcm.ml.create_linear_regressor())) ``` -------------------------------- ### Experiment Class Initialization Source: https://github.com/py-why/dowhy/blob/main/docs/source/example_notebooks/dowhy_ranking_methods.ipynb Initializes the Experiment class with various parameters including experiment details, data generation processes (DGPs), estimators, refuters, and simulation settings. Includes input validation for sample sizes, DGPs, estimators, and refuters. ```python class Experiment(): ''' Class to define the experiment setup to compare a list of estimators across a list of refuters for the given dataset. ''' def __init__(self, **kwargs): self.experiment_name = kwargs['experiment_name'] self.experiment_id = kwargs['experiment_id'] self.num_experiments = kwargs['num_experiments'] self.sample_sizes = kwargs['sample_sizes'] self.dgps = kwargs['dgps'] self.estimators = kwargs['estimators'] self.refuters = kwargs['refuters'] self.results = [] self.simulate_unobserved_confounding = kwargs["simulate_unobserved_confounding"] # Handle input errors in sample_sizes if isinstance(self.sample_sizes, list) == False: if type(self.sample_sizes) != int: raise ValueError('The input to "sample_sizes" should be an int or a list') else: self.sample_sizes = [self.sample_sizes] # Handle input errors in DGPs if isinstance(self.dgps, list) == False: if isinstance(self.dgps, DataGeneratingProcess) == False: raise ValueError('The input to "dgps" should be a list or a subclass of "DataGeneratingProcess"') else: self.dgps = [self.dgps] # Handle inputs errors in estimators if isinstance(self.estimators, list) == False: if isinstance(self.estimators, Estimator) == False: raise ValueError('The input to "estimators" should be a list or an Estimator namedtuple') else: self.estimators = [self.estimators] # Handle input errors in refuters if isinstance(self.refuters, list) == False: if isinstance(self.refuters, Refuter) == False: raise ValueError('The input to "refuters" should be a list of a Refuter namedtuple') else: self.refuters = [self.refuters] ``` -------------------------------- ### Define Causal Graph Edges Source: https://github.com/py-why/dowhy/blob/main/docs/source/example_notebooks/gcm_401k_analysis.ipynb Constructs the edges for a causal graph representing the relationships between 401(k) eligibility, net financial assets, and covariates. This setup is crucial for causal inference modeling. ```python treatment_var = "e401" outcome_var = "net_tfa" covariates = ["age","inc","fsize","educ","male","db", "marr","twoearn","pira","hown","hval", "hequity","hmort","nohs","hs","smcol"] edges = [(treatment_var, outcome_var)] edges.extend([(covariate, treatment_var) for covariate in covariates]) edges.extend([(covariate, outcome_var) for covariate in covariates]) ``` -------------------------------- ### Load DoWhy and Dependencies Source: https://github.com/py-why/dowhy/blob/main/docs/source/example_notebooks/sensitivity_analysis_testing.ipynb Imports necessary libraries for DoWhy, data manipulation, and configuration. Sets up logging and warning filters. ```python import os, sys sys.path.append(os.path.abspath("../../../\n")) import dowhy dowhy.enable_notebook_rendering() from dowhy import CausalModel import pandas as pd import numpy as np import dowhy.datasets # Config dict to set the logging level import logging.config DEFAULT_LOGGING = { 'version': 1, 'disable_existing_loggers': False, 'loggers': { '': { 'level': 'ERROR', }, } } logging.config.dictConfig(DEFAULT_LOGGING) # Disabling warnings output import warnings from sklearn.exceptions import DataConversionWarning #warnings.filterwarnings(action='ignore', category=DataConversionWarning) ``` -------------------------------- ### Configure Counterfactual Fairness Analysis Source: https://github.com/py-why/dowhy/blob/main/docs/source/example_notebooks/counterfactual_fairness_dowhy.ipynb Sets up the configuration dictionary for the analyse_counterfactual_fairness function, specifying the dataset, causal DAG, estimator, protected attributes, features, target variable, and disadvantaged group. ```python target = "avg_grade" disadvantage_group = {"Race": 1} protected_attrs = ["Race"] features = ["GPA", "LSAT"] ``` -------------------------------- ### Example Usage of Intervention Analysis Source: https://github.com/py-why/dowhy/blob/main/docs/source/example_notebooks/sales_attribution_intervention.ipynb Demonstrates how to call the intervention_influence function with specific parameters, including the causal model, target variable, and non-interveneable nodes. This is used to analyze the impact of interventions on sales. ```python interv_result = intervention_influence(causal_model=causal_model, target='sale', non_interveneable_nodes=['dpv', 'sale', 'special_shopping_event'], prints=True) interv_result ``` -------------------------------- ### Display Causal Graph Source: https://github.com/py-why/dowhy/blob/main/docs/source/example_notebooks/gcm_online_shop.ipynb Displays the causal graph for the online shop scenario using IPython.Image. Ensure the image file 'online-shop-graph.png' is accessible. ```python from IPython.display import Image Image('online-shop-graph.png') ``` -------------------------------- ### Import Libraries and Configure Logging Source: https://github.com/py-why/dowhy/blob/main/docs/source/example_notebooks/dowhy_refutation_testing.ipynb Imports necessary libraries for DoWhy, pandas, and numpy. Configures logging level and suppresses specific warnings. ```python import dowhy dowhy.enable_notebook_rendering() from dowhy import CausalModel import pandas as pd import numpy as np # Config dict to set the logging level import logging.config DEFAULT_LOGGING = { 'version': 1, 'disable_existing_loggers': False, 'loggers': { '': { 'level': 'WARN', }, } } logging.config.dictConfig(DEFAULT_LOGGING) # Disabling warnings output import warnings from sklearn.exceptions import DataConversionWarning warnings.filterwarnings(action='ignore', category=DataConversionWarning) ``` -------------------------------- ### Building Graph with No Observable Adjustment Sets Source: https://github.com/py-why/dowhy/blob/main/docs/source/example_notebooks/dowhy_efficient_backdoor_example.ipynb Defines a graph structure and observed variables for a scenario where no observable adjustment sets exist. This setup is used to test DoWhy's error handling for such cases. ```python graph_str = """graph[directed 1 node[id "X" label "X"] node[id "Y" label "Y"] node[id "U" label "U"] edge[source "X" target "Y"] edge[source "U" target "X"] edge[source "U" target "Y"] ]""" observed_node_names = ["X", "Y"] treatment_name = "X" outcome_name = "Y" G = build_graph_from_str(graph_str) ``` -------------------------------- ### Perform Intervention and Distribution Change Source: https://github.com/py-why/dowhy/wiki/API-proposal-for-v1 Demonstrates how to perform soft or hard interventions on a target node and calculate changes in attribute distributions using DoWhy. ```Python intervention_samples = dowhy.intervene(scm, target_node="X", intervention_func) # Attribute distribution changes change_attributions = dowhy.distribution_change(scm, data, new_data) ``` -------------------------------- ### Estimate Effect with EconML DML Source: https://github.com/py-why/dowhy/blob/main/docs/source/example_notebooks/dowhy_functional_api.ipynb Example of using the `Econml` estimator with DoWhy, integrating with EconML's `DML` estimator. Requires specifying models for outcome, treatment, and final estimation, along with a featurizer. ```python # EconML estimator example from econml.dml import DML from sklearn.linear_model import LassoCV from sklearn.preprocessing import PolynomialFeatures from sklearn.ensemble import GradientBoostingRegressor estimator = Econml( identified_estimand=identified_estimand, econml_estimator=DML( model_y=GradientBoostingRegressor(), model_t=GradientBoostingRegressor(), model_final=LassoCV(fit_intercept=False), featurizer=PolynomialFeatures(degree=1, include_bias=True), ), ).fit( data=data["df"], effect_modifier_names=data["effect_modifier_names"], ) estimate_econml = estimator.estimate_effect( data=data["df"], control_value=0, treatment_value=1, target_units="ate", ) print(estimate) ``` -------------------------------- ### Compute Pi using Monte Carlo Simulation Source: https://github.com/py-why/dowhy/blob/main/docs/source/user_guide/modeling_gcm/estimating_confidence_intervals.rst A Python function to compute Pi using the Monte Carlo method with a specified number of trials. This serves as a basic example for demonstrating confidence interval estimation. ```python import numpy as np def compute_pi_monte_carlo(): trials = 1000 return 4*(np.random.default_rng().uniform(-1, 1, (trials,))**2+ np.random.default_rng().uniform(-1, 1, (trials,))**2 <= 1).sum() / trials ``` -------------------------------- ### Initialize Causal Models Source: https://github.com/py-why/dowhy/blob/main/docs/source/example_notebooks/dowhy_doubly_robust_estimator.ipynb Initializes CausalModel objects for both linear and non-linear datasets. This step requires specifying the data, treatment, outcome, graph, and instruments. ```python model_linear=CausalModel( data = df_linear, treatment=data["treatment_name"], outcome=data["outcome_name"], graph=data["gml_graph"], instruments=data["instrument_names"] ) model_nonlinear=CausalModel( data = df_nonlinear, treatment=data["treatment_name"], outcome=data["outcome_name"], graph=data["gml_graph"], instruments=data["instrument_names"] ) ``` -------------------------------- ### Estimating Conditional Treatment Effect with T-Learner Source: https://github.com/py-why/dowhy/blob/main/docs/source/example_notebooks/dowhy-conditional-treatment-effects.ipynb Estimates the conditional treatment effect using EconML's T-Learner metalearner. Requires scikit-learn's RandomForestRegressor for the underlying models. `confidence_intervals=False` is set for simplicity in this example. ```python from sklearn.ensemble import RandomForestRegressor metalearner_estimate = model_experiment.estimate_effect(identified_estimand_experiment, method_name="backdoor.econml.metalearners.TLearner", confidence_intervals=False, method_params={"init_params":{ 'models': RandomForestRegressor() }, "fit_params":{} }) print(metalearner_estimate) print("True causal estimate is", data_experiment["ate"]) ``` -------------------------------- ### Load Graph from DOT File Source: https://github.com/py-why/dowhy/blob/main/docs/source/example_notebooks/load_graph_example.ipynb Initializes a CausalModel using a graph from a DOT file. The graph structure is visualized and displayed. ```python # With DOT file model=CausalModel( data = df, treatment='X', outcome='Y', graph="../example_graphs/simple_graph_example.dot" ) model.view_model() display(Image(filename="causal_model.png")) ```