### Suggest Domain Expertises using ModelSuggester Source: https://github.com/py-why/pywhyllm/blob/main/docs/notebooks/examples.ipynb Illustrates how to use the ModelSuggester to identify relevant domain expertises for a given set of variables. This is crucial for guiding causal inference. ```python domain_expertises = m.suggest_domain_expertises(sea_ice_variables) ``` -------------------------------- ### Suggest Negative Controls using PyWhy LLM Source: https://github.com/py-why/pywhyllm/blob/main/docs/notebooks/examples.ipynb This example shows how to suggest negative control variables using the `suggest_negative_controls` method in PyWhy LLM. This is useful for validating causal relationships by checking for expected null effects. ```python from pywhyllm.suggesters.validation_suggester import ValidationSuggester v = ValidationSuggester('gpt-4') # Assuming 'treatment', 'outcome', 'sea_ice_variables', and 'domain_expertises' are defined negative_controls = v.suggest_negative_controls(treatment, outcome, sea_ice_variables, domain_expertises) ``` -------------------------------- ### Install Guidance Library Source: https://github.com/py-why/pywhyllm/blob/main/docs/notebooks/tuebingen_causality_pairs/tuebingen_pairs_notebook_v2.ipynb Installs the 'guidance' Python library. This library is likely used for interacting with language models in a structured way, potentially for prompt engineering or controlling LLM output. ```bash pip install guidance ``` -------------------------------- ### Critique Causal Graph using PyWhy LLM Source: https://github.com/py-why/pywhyllm/blob/main/docs/notebooks/examples.ipynb This example shows how to critique an entire causal graph using PyWhy LLM's `ValidationSuggester`. It takes a list of variables, model edges, domain expertise, and a relationship strategy (e.g., pairwise) as input. ```python from pywhyllm.suggesters.validation_suggester import ValidationSuggester from pywhyllm.suggesters.relationship_strategy import RelationshipStrategy v = ValidationSuggester('gpt-4') # Define relationship strategy pairwise = RelationshipStrategy.Pairwise # Assuming 'sea_ice_variables', 'model_edges', and 'domain_expertises' are defined critique = v.critique_graph(sea_ice_variables, model_edges, domain_expertises, pairwise) ``` -------------------------------- ### Install PyWhyLLM Package Source: https://github.com/py-why/pywhyllm/blob/main/docs/notebooks/tuebingen_causality_pairs/tuebingen_pairs_notebook_v2.ipynb Installs the pywhyllm package from a local path in Google Drive. This command is used when the package is not available via standard package managers and needs to be installed from a specific file location. ```bash pip install '/content/drive/MyDrive/pywhy-llm' ``` -------------------------------- ### Suggest Relationships (Pairwise Strategy) using ModelSuggester Source: https://github.com/py-why/pywhyllm/blob/main/docs/notebooks/examples.ipynb Illustrates how to suggest relationships between a treatment and outcome variable using the pairwise strategy. This function aggregates relationship suggestions. ```python model_edges = m.suggest_relationships(treatment, outcome, sea_ice_variables, domain_expertises, RelationshipStrategy.Pairwise) ``` -------------------------------- ### Install PyWhy-LLM using pip Source: https://github.com/py-why/pywhyllm/blob/main/README.md This command installs the PyWhy-LLM library using pip, making it available for use in Python projects. Ensure you have pip installed and configured. ```bash pip install pywhyllm ``` -------------------------------- ### Suggest Domain Experts using ModelSuggester Source: https://github.com/py-why/pywhyllm/blob/main/docs/notebooks/examples.ipynb Demonstrates the use of ModelSuggester to suggest specific domain experts based on a list of variables. This helps in finding relevant knowledge sources for the analysis. ```python domain_experts = m.suggest_domain_experts(sea_ice_variables) ``` -------------------------------- ### Initialize IdentificationSuggester for Causal Discovery Source: https://github.com/py-why/pywhyllm/blob/main/docs/notebooks/examples.ipynb Shows the instantiation of the IdentificationSuggester class, which is used for identifying causal relationships and performing adjustments like backdoor adjustments. ```python from pywhyllm.suggesters.identification_suggester import IdentificationSuggester i = IdentificationSuggester('gpt-4') ``` -------------------------------- ### Initialize ModelSuggester for Causal Relationship Suggestions Source: https://github.com/py-why/pywhyllm/blob/main/docs/notebooks/examples.ipynb Shows how to instantiate the ModelSuggester class, which is used for suggesting various causal relationships between variables. It takes a model name as an argument. ```python from pywhyllm.suggesters.model_suggester import ModelSuggester m = ModelSuggester('gpt-4') ``` -------------------------------- ### Suggest Child Nodes in Causal Graph using ModelSuggester Source: https://github.com/py-why/pywhyllm/blob/main/docs/notebooks/examples.ipynb Illustrates how to suggest child nodes for a given variable in a causal graph using ModelSuggester. This complements the parent suggestion for graph construction. ```python children = m.suggest_children(domain_expertises[0], "relative_humidity", sea_ice_variables) ``` -------------------------------- ### Install dotenv for environment variable management Source: https://github.com/py-why/pywhyllm/blob/main/docs/notebooks/augmented_model_suggester_examples.ipynb Installs the 'dotenv' Python package, which is used to load environment variables from a .env file. This is typically used to manage sensitive information like API keys. ```bash pip install dotenv ``` -------------------------------- ### Suggest Confounders for Causal Inference using ModelSuggester Source: https://github.com/py-why/pywhyllm/blob/main/docs/notebooks/examples.ipynb Shows how to suggest confounders for a given treatment and outcome variable. This is essential for controlling for spurious correlations and estimating true causal effects. ```python confounders = m.suggest_confounders(treatment, outcome, sea_ice_variables, domain_expertises) ``` -------------------------------- ### Suggest Parent Nodes in Causal Graph using ModelSuggester Source: https://github.com/py-why/pywhyllm/blob/main/docs/notebooks/examples.ipynb Shows how to suggest parent nodes for a given variable in a causal graph using ModelSuggester. This is useful for building a directed acyclic graph (DAG) of relationships. ```python parents = m.suggest_parents(domain_expertises[0], "relative_humidity", sea_ice_variables) ``` -------------------------------- ### Install python-dotenv Source: https://github.com/py-why/pywhyllm/blob/main/docs/notebooks/tuebingen_causality_pairs/tuebingen_pairs_notebook_v2.ipynb Installs the 'python-dotenv' library. This library is commonly used to load environment variables from a .env file into the script's environment, often for managing sensitive information like API keys. ```bash pip install python-dotenv ``` -------------------------------- ### Suggest Instrumental Variables using PyWhy LLM Source: https://github.com/py-why/pywhyllm/blob/main/docs/notebooks/examples.ipynb This snippet demonstrates how to use the `suggest_ivs` method from PyWhy LLM to identify potential instrumental variables for a given treatment and outcome. It requires a list of potential variables and domain expertise. ```python from pywhyllm.suggesters.suggestion_suggester import SuggestionSuggester # Assuming 'i' is an instance of SuggestionSuggester # Assuming 'treatment', 'outcome', 'sea_ice_variables', and 'domain_expertises' are defined ivs = i.suggest_ivs(treatment, outcome, sea_ice_variables, domain_expertises) ``` -------------------------------- ### Suggest Backdoor Adjustment Set using IdentificationSuggester Source: https://github.com/py-why/pywhyllm/blob/main/docs/notebooks/examples.ipynb Demonstrates how to suggest a backdoor adjustment set for identifying the causal effect of a treatment on an outcome. This is a core component of causal inference. ```python backdoor = i.suggest_backdoor(treatment, outcome, sea_ice_variables, domain_expertises, "causal mechanisms") ``` -------------------------------- ### Install Python Dotenv Source: https://github.com/py-why/pywhyllm/blob/main/docs/notebooks/walkthrough.ipynb Installs the python-dotenv library, which is used to load environment variables from a .env file. ```python !pip install python-dotenv ``` -------------------------------- ### Suggest Pairwise Causal Relationship using ModelSuggester Source: https://github.com/py-why/pywhyllm/blob/main/docs/notebooks/examples.ipynb Demonstrates suggesting a pairwise causal relationship between two variables. This function helps determine the nature of the direct influence between specific variables. ```python pairwise_relationship = m.suggest_pairwise_relationship(domain_expertises[0], "total_precipitation", "relative_humidity") ``` -------------------------------- ### Define Causal Inference Parameters in Python Source: https://github.com/py-why/pywhyllm/blob/main/docs/notebooks/examples.ipynb Demonstrates the initialization of ModelType and RelationshipStrategy enums for causal inference. These are fundamental for configuring the behavior of pywhyllm functions. ```python from pywhyllm import ModelType, RelationshipStrategy model_type = ModelType.Completion relationship_strategy = RelationshipStrategy.Parent ``` -------------------------------- ### Suggest pairwise relationship (flooding, rain) Source: https://github.com/py-why/pywhyllm/blob/main/docs/notebooks/augmented_model_suggester_examples.ipynb Queries the AugmentedModelSuggester for the causal relationship between 'flooding' and 'rain'. This example checks if the model can identify a common causal link using RAG. ```python result = model.suggest_pairwise_relationship("flooding", "rain") ``` -------------------------------- ### Suggest Latent Confounders using PyWhy LLM Source: https://github.com/py-why/pywhyllm/blob/main/docs/notebooks/examples.ipynb This code illustrates the use of `suggest_latent_confounders` from PyWhy LLM's `ValidationSuggester` to identify potential unobserved confounding variables. It takes treatment, outcome, and domain expertise as input. ```python from pywhyllm.suggesters.validation_suggester import ValidationSuggester v = ValidationSuggester('gpt-4') # Assuming 'treatment', 'outcome', and 'domain_expertises' are defined latent_confounders = v.suggest_latent_confounders(treatment, outcome, domain_expertises) ``` -------------------------------- ### Load Environment Variables and Configure API Key Source: https://github.com/py-why/pywhyllm/blob/main/docs/notebooks/tuebingen_causality_pairs/tuebingen_pairs_notebook_v2.ipynb Loads environment variables from a .env file and sets the OpenAI API key. The code first imports necessary modules, loads variables using load_dotenv, and then explicitly sets the OPENAI_API_KEY environment variable. Note: The API key is intentionally left empty in this example. ```python from dotenv import load_dotenv from typing import Dict, List, Tuple import guidance import os load_dotenv() os.environ["OPENAI_API_KEY"] = '' ``` -------------------------------- ### Request Pairwise Critique of Causal Relationships with PyWhy LLM Source: https://github.com/py-why/pywhyllm/blob/main/docs/notebooks/examples.ipynb This snippet demonstrates how to request a pairwise critique of a potential causal relationship between two variables using PyWhy LLM's `ValidationSuggester`. It utilizes domain expertise to evaluate the relationship. ```python from pywhyllm.suggesters.validation_suggester import ValidationSuggester from pywhyllm.suggesters.relationship_strategy import RelationshipStrategy v = ValidationSuggester('gpt-4') # Assuming 'domain_expertises' is a list and 'total_precipitation' and 'relative_humidity' are variables critique = v.request_pairwise_critique(domain_expertises[0], "total_precipitation", "relative_humidity") ``` -------------------------------- ### Define Sea Ice Variables and Relationships in Python Source: https://github.com/py-why/pywhyllm/blob/main/docs/notebooks/examples.ipynb Defines a list of variables related to sea ice and a list of tuples representing potential causal relationships between these variables. This data serves as input for causal inference functions. ```python sea_ice_confounders = ["total_precipitation"] sea_ice_relationships: List[Tuple[str, str]] = [ ("surface_net_longwave_flux", "northern_hemisphere_sea_ice_extent"), ("geopotential_heights", "surface_net_longwave_flux"), ("geopotential_heights", "relative_humidity"), ("geopotential_heights", "sea_level_pressure"), ("relative_humidity", "total_cloud_cover"), ("relative_humidity", "total_cloud_water_path"), ("relative_humidity", "total_precipitation"), ("relative_humidity", "surface_net_longwave_flux"), ("sea_level_pressure", "relative_humidity"), ("sea_level_pressure", "geopotential_heights"), ("sea_level_pressure", "zonal_wind_at_10_meters"), ("sea_level_pressure", "northern_hemisphere_sea_ice_extent"), ("sea_level_pressure", "sensible_plus_latent_heat_flux"), ("sea_level_pressure", "meridional_wind_at_10_meters"), ("zonal_wind_at_10_meters", "northern_hemisphere_sea_ice_extent"), ("zonal_wind_at_10_meters", "sensible_plus_latent_heat_flux"), ("meridional_wind_at_10_meters", "northern_hemisphere_sea_ice_extent"), ("meridional_wind_at_10_meters", "sensible_plus_latent_heat_flux"), ("sensible_plus_latent_heat_flux", "northern_hemisphere_sea_ice_extent"), ("sensible_plus_latent_heat_flux", "sea_level_pressure"), ("sensible_plus_latent_heat_flux", "zonal_wind_at_10_meters"), ("sensible_plus_latent_heat_flux", "meridional_wind_at_10_meters"), ("sensible_plus_latent_heat_flux", "total_precipitation"), ("sensible_plus_latent_heat_flux", "total_cloud_cover"), ("sensible_plus_latent_heat_flux", "total_cloud_water_path"), ("total_precipitation", "northern_hemisphere_sea_ice_extent"), ("total_precipitation", "relative_humidity"), ("total_precipitation", "sensible_plus_latent_heat_flux"), ("total_precipitation", "surface_net_longwave_flux"), ("total_precipitation", "total_cloud_cover"), ("total_precipitation", "total_cloud_water_path"), ("total_cloud_water_path", "total_precipitation"), ("total_cloud_water_path", "sensible_plus_latent_heat_flux"), ("total_cloud_water_path", "relative_humidity"), ("total_cloud_water_path", "surface_net_longwave_flux"), ("total_cloud_water_path", "surface_net_shortwave_flux"), ("total_cloud_cover", "total_precipitation"), ("total_cloud_cover", "sensible_plus_latent_heat_flux"), ("total_cloud_cover", "relative_humidity"), ("total_cloud_cover", "surface_net_longwave_flux"), ("total_cloud_cover", "surface_net_shortwave_flux"), ("surface_net_shortwave_flux", "northern_hemisphere_sea_ice_extent"), ("northern_hemisphere_sea_ice_extent", "sea_level_pressure"), ("northern_hemisphere_sea_ice_extent", "zonal_wind_at_10_meters"), ("northern_hemisphere_sea_ice_extent", "meridional_wind_at_10_meters"), ("northern_hemisphere_sea_ice_extent", "sensible_plus_latent_heat_flux"), ("northern_hemisphere_sea_ice_extent", "surface_net_shortwave_flux"), ("northern_hemisphere_sea_ice_extent", "surface_net_longwave_flux") ] ``` -------------------------------- ### Define Sea Ice Variables for LLM Analysis Source: https://github.com/py-why/pywhyllm/blob/main/docs/notebooks/examples.ipynb Defines a list of sea ice related variables and specifies treatment and outcome variables. This is likely used for causal inference or time-series analysis within the LLM project, focusing on the impact of longwave flux on sea ice extent. ```python from typing import Dict, Tuple, List sea_ice_variables = [ "geopotential_heights", "relative_humidity", "sea_level_pressure", "zonal_wind_at_10_meters", "meridional_wind_at_10_meters", "sensible_plus_latent_heat_flux", "total_precipitation", "total_cloud_cover", "total_cloud_water_path", "surface_net_shortwave_flux", "surface_net_longwave_flux", "northern_hemisphere_sea_ice_extent", ] treatment = "surface_net_longwave_flux" outcome = "northern_hemisphere_sea_ice_extent" ``` -------------------------------- ### Load Environment Variables and Initialize Guidance Source: https://github.com/py-why/pywhyllm/blob/main/docs/notebooks/walkthrough.ipynb Loads environment variables using dotenv and initializes the Guidance library with an OpenAI API key. Ensure your API key is correctly specified. ```python from dotenv import load_dotenv import os import guidance from guidance import models load_dotenv() os.environ["OPENAI_API_KEY"] = '' # specify your key here ``` -------------------------------- ### Suggest Domain Expertise and Experts in Python Source: https://context7.com/py-why/pywhyllm/llms.txt Identifies relevant domain expertises and specific expert roles for causal analysis context. Takes a list of factors, number of experts, and analysis context as input. Returns lists of suggested expertises or specific expert titles. ```python from pywhyllm.suggesters.model_suggester import ModelSuggester modeler = ModelSuggester('gpt-4') medical_factors = ["drug dosage", "blood pressure", "age", "diet", "exercise"] # Suggest domain expertises domain_expertises = modeler.suggest_domain_expertises( all_factors=medical_factors, n_experts=3, analysis_context="medical treatment effects" ) # Suggest specific domain experts domain_experts = modeler.suggest_domain_experts( all_factors=medical_factors, n_experts=3, analysis_context="medical treatment effects" ) ``` -------------------------------- ### Mount Google Drive Source: https://github.com/py-why/pywhyllm/blob/main/docs/notebooks/tuebingen_causality_pairs/tuebingen_pairs_notebook_v2.ipynb Mounts the Google Drive to the Colab environment, allowing access to files stored in Drive. This is typically the first step when working with data or custom libraries stored in Google Drive. ```python from google.colab import drive drive.mount('/content/drive') ``` -------------------------------- ### Load environment variables and set API key Source: https://github.com/py-why/pywhyllm/blob/main/docs/notebooks/augmented_model_suggester_examples.ipynb Loads environment variables using 'dotenv' and specifically sets the 'OPENAI_API_KEY'. Ensure you replace the placeholder with your actual OpenAI API key. ```python from dotenv import load_dotenv import os load_dotenv() os.environ["OPENAI_API_KEY"] = '' # specify your key here ``` -------------------------------- ### Initialize Simple Model Suggester Source: https://github.com/py-why/pywhyllm/blob/main/docs/notebooks/walkthrough.ipynb Initializes the SimpleModelSuggester from PyWhylLM with a specified language model (e.g., 'gpt-4'). This suggester is used for proposing pairwise causal relationships. ```python from pywhyllm.suggesters.simple_model_suggester import SimpleModelSuggester modeler = SimpleModelSuggester('gpt-4') ``` -------------------------------- ### Initialize AugmentedModelSuggester for RAG Source: https://github.com/py-why/pywhyllm/blob/main/docs/notebooks/augmented_model_suggester_examples.ipynb Demonstrates how to create an instance of the AugmentedModelSuggester class. This class enables Retrieval Augmented Generation (RAG) by integrating the CauseNet dataset to find causal relationships. ```python from pywhyllm.suggesters.augmented_model_suggester import AugmentedModelSuggester model = AugmentedModelSuggester('gpt-4') ``` -------------------------------- ### Suggest Stakeholders for Causal Analysis (Python) Source: https://context7.com/py-why/pywhyllm/llms.txt This snippet demonstrates how to use the ModelSuggester to suggest relevant stakeholders for a given causal analysis context. It takes a list of factors and the analysis context as input and returns a list of suggested stakeholder roles. ```python from pywhyllm.suggesters.model_suggester import ModelSuggester # Assuming 'modeler' and 'medical_factors' are defined elsewhere # modeler = ModelSuggester('gpt-4') # or a custom LLM # medical_factors = [...] # list of medical factors stakeholders = modeler.suggest_stakeholders( all_factors=medical_factors, n_stakeholders=4, analysis_context="medical treatment effects" ) # Example Output: ['Patients', 'Healthcare Providers', 'Insurance Companies', 'Regulatory Agencies'] ``` -------------------------------- ### Suggest causal analysis elements using Modeler Source: https://github.com/py-why/pywhyllm/blob/main/README.md This Python code demonstrates how to use the `ModelSuggester` class to suggest domain expertises, potential confounders, and relationships between variables for causal analysis. It requires an LLM model name (e.g., 'gpt-4') and a list of factors. ```python # Create instance of Modeler modeler = ModelSuggester('gpt-4') all_factors = ["smoking", "lung cancer", "exercise habits", "air pollution exposure"] treatment = "smoking" outcome = "lung cancer" # Suggest a list of domain expertises domain_expertises = modeler.suggest_domain_expertises(all_factors) # Suggest a set of potential confounders suggested_confounders = modeler.suggest_confounders(treatment, outcome, all_factors, domain_expertises) # Suggest pair-wise relationships between variables suggested_dag = modeler.suggest_relationships(treatment, outcome, all_factors, domain_expertises, RelationshipStrategy.Pairwise) ``` -------------------------------- ### Initialize Simple Identification Suggester Source: https://github.com/py-why/pywhyllm/blob/main/docs/notebooks/walkthrough.ipynb Initializes the SimpleIdentificationSuggester from PyWhylLM with a specified language model (e.g., 'gpt-4'). This suggester is used for identifying causal structures like IVs, backdoors, and frontdoors. ```python from pywhyllm.suggesters.simple_identification_suggester import SimpleIdentificationSuggester identifier = SimpleIdentificationSuggester('gpt-4') ``` -------------------------------- ### Suggest Causal Identification Strategies (Python) Source: https://context7.com/py-why/pywhyllm/llms.txt Employs IdentificationSuggester to propose causal identification strategies, including backdoor sets, mediators, and instrumental variables. This function requires the pywhyllm library and an LLM backend. It outputs suggested backdoor adjustment sets based on provided factors, treatment, outcome, and domain expertise. ```python from pywhyllm.suggesters.identification_suggester import IdentificationSuggester # Initialize identifier identifier = IdentificationSuggester('gpt-4') all_factors = ["smoking", "lung cancer", "exercise habits", "air pollution exposure", "genetic predisposition"] treatment = "smoking" outcome = "lung cancer" domain_expertises = ["Epidemiology", "Genetics"] # Suggest backdoor set for adjustment backdoor_edges, backdoor_set = identifier.suggest_backdoor( treatment=treatment, outcome=outcome, all_factors=all_factors, expertise_list=domain_expertises ) # Returns backdoor adjustment set # backdoor_set = ['air pollution exposure', 'genetic predisposition'] ``` -------------------------------- ### Suggest causal identification sets using Identifier Source: https://github.com/py-why/pywhyllm/blob/main/README.md This Python code shows how to use the `IdentificationSuggester` to propose backdoor sets, mediator sets, and instrumental variable (IV) sets for causal inference. It requires an LLM model name, treatment, outcome, all factors, and suggested domain expertises. ```python # Create instance of Identifier identifier = IdentificationSuggester('gpt-4') # Suggest a backdoor set, mediator set, and iv set suggested_backdoor = identifier.suggest_backdoor(treatment, outcome, all_factors, domain_expertises) suggested_mediators = identifier.suggest_mediators(treatment, outcome, all_factors, domain_expertises) suggested_iv = identifier.suggest_ivs(treatment, outcome, all_factors, domain_expertises) ``` -------------------------------- ### Initialize TuebingenModelSuggester Source: https://github.com/py-why/pywhyllm/blob/main/docs/notebooks/tuebingen_causality_pairs/tuebingen_pairs_notebook_v2.ipynb Initializes the TuebingenModelSuggester from the pywhyllm library, specifying 'gpt-4' as the language model. This object will be used to suggest relationships between variables based on their descriptions. ```python from pywhyllm.suggesters.tuebingen_model_suggester import TuebingenModelSuggester, Strategy modeler = TuebingenModelSuggester('gpt-4') ``` -------------------------------- ### Suggest pairwise relationship (income, exercise level) Source: https://github.com/py-why/pywhyllm/blob/main/docs/notebooks/augmented_model_suggester_examples.ipynb Suggests the pairwise relationship between 'income' and 'exercise level' using the AugmentedModelSuggester. The model will leverage CauseNet data if available for this pair. ```python result = model.suggest_pairwise_relationship("income", "exercise level") ``` -------------------------------- ### Suggest Causal Relationships and Confounders (Python) Source: https://context7.com/py-why/pywhyllm/llms.txt Uses ModelSuggester to suggest domain expertises, confounders, and pairwise causal relationships for DAG construction. Requires the pywhyllm library and specifies an LLM backend like 'gpt-4'. Outputs include expertises, confounder edges, and suggested DAG edges. ```python from pywhyllm.suggesters.model_suggester import ModelSuggester from pywhyllm import RelationshipStrategy # Initialize the modeler with GPT-4 modeler = ModelSuggester('gpt-4') # Define variables for analysis all_factors = ["smoking", "lung cancer", "exercise habits", "air pollution exposure"] treatment = "smoking" outcome = "lung cancer" # Step 1: Suggest domain expertises relevant to the causal analysis domain_expertises = modeler.suggest_domain_expertises(all_factors, n_experts=2) # Returns: ['Epidemiology', 'Public Health'] # Step 2: Suggest confounders between treatment and outcome confounders_edges, suggested_confounders = modeler.suggest_confounders( treatment=treatment, outcome=outcome, all_factors=all_factors, expertise_list=domain_expertises ) # Returns: # confounders_edges = {('smoking', 'lung cancer'): 1, ('air pollution exposure', 'smoking'): 2, ('air pollution exposure', 'lung cancer'): 2} # suggested_confounders = ['air pollution exposure'] # Step 3: Suggest pairwise causal relationships to construct a DAG suggested_dag = modeler.suggest_relationships( treatment=treatment, outcome=outcome, all_factors=all_factors, expertise_list=domain_expertises, relationship_strategy=RelationshipStrategy.Pairwise ) # Returns: Dictionary of edges with vote counts # {('smoking', 'lung cancer'): 2, ('air pollution exposure', 'lung cancer'): 2, ('exercise habits', 'lung cancer'): 1} ``` -------------------------------- ### Configure Custom LLM Backends for PyWhy-LLM (Python) Source: https://context7.com/py-why/pywhyllm/llms.txt This snippet illustrates how to configure PyWhy-LLM to use custom Large Language Models (LLMs) via the guidance library. It shows options for using built-in configurations, custom OpenAI models, and models with identifiers, along with error handling for invalid model names. ```python import guidance from pywhyllm.suggesters.model_suggester import ModelSuggester from pywhyllm.suggesters.identification_suggester import IdentificationSuggester # Option 1: Use built-in GPT-4 configuration modeler_builtin = ModelSuggester('gpt-4') # Option 2: Use custom guidance model (e.g., GPT-4 Turbo) custom_llm = guidance.models.OpenAI('gpt-4-turbo') modeler_custom = ModelSuggester(llm=custom_llm) # Option 3: Use custom model with an identifier suggester identifier = IdentificationSuggester(llm=custom_llm) # Example of handling invalid model input try: invalid_modeler = ModelSuggester('invalid-model') except ValueError as e: print(e) # Expected output: "llm must be either 'gpt-4' or a guidance model instance." ``` -------------------------------- ### Display DataFrame Source: https://github.com/py-why/pywhyllm/blob/main/docs/notebooks/tuebingen_causality_pairs/tuebingen_pairs_notebook_v2.ipynb Displays the contents of the pandas DataFrame 'df'. This is a common step for inspecting the loaded data. ```python df ``` -------------------------------- ### Suggest relationships with RAG using AugmentedModelSuggester Source: https://github.com/py-why/pywhyllm/blob/main/README.md This Python code utilizes the `AugmentedModelSuggester` for suggesting relationships between variables, incorporating Retrieval Augmented Generation (RAG) through CauseNet. It requires an LLM model name and specific treatment and outcome variables. ```python # Create instance of Modeler modeler = AugmentedModelSuggester('gpt-4') treatment = "smoking" outcome = "lung cancer" # Suggest pair-wise relationship between two given variables, utilizing CauseNet for RAGing the LLM suggested_relationship = modeler.suggest_relationships(treatment, outcome) ``` -------------------------------- ### Print LLM Outputs for Saved Pairs Source: https://github.com/py-why/pywhyllm/blob/main/docs/notebooks/tuebingen_causality_pairs/tuebingen_pairs_notebook_v2.ipynb Iterates through a list of 'saved_pairs_info' and prints the 'llm_ab' output from the 'llm_output' dictionary for each pair across multiple runs. This snippet seems to be a partial loop, as only 'llm_ab' is printed and the inner loop is not fully defined. ```python for id in saved_pairs_info: av_correct_ab = 0 av_correct_ba = 0 for i in range(5): print(llm_output[(id, 0.3, i+1)]['llm_ab']) ``` -------------------------------- ### Suggest Relationships for Variable Pairs (Straight Strategy) Source: https://github.com/py-why/pywhyllm/blob/main/docs/notebooks/tuebingen_causality_pairs/tuebingen_pairs_notebook_v2.ipynb Iterates through each pair of variables in the DataFrame and uses the TuebingenModelSuggester to suggest causal relationships in both directions (A causes B, B causes A) using the 'Straight' strategy. The results are stored in the llm_output dictionary. ```python llm_output : Dict[str, dict] = {} for pair_number, values in df.iterrows(): temp_dict = {} temp_dict['llm_ab'] = modeler.suggest_relationship(variable_a=values['var1'], variable_b=values['var2'], description_a=values['var1_desc'], description_b=values['var2_desc'], strategy=Strategy.Straight) temp_dict['llm_ba'] = modeler.suggest_relationship(variable_a=values['var2'], variable_b=values['var1'], description_a=values['var2_desc'], description_b=values['var1_desc'], strategy=Strategy.Straight) llm_output[(pair_number, temperature, n)] = temp_dict ``` -------------------------------- ### ModelSuggester - Causal Model Construction Source: https://context7.com/py-why/pywhyllm/llms.txt Suggests causal relationships, confounders, and directed acyclic graphs (DAGs) between variables using LLM-based domain expertise simulation. ```APIDOC ## ModelSuggester - Causal Model Construction ### Description Suggests causal relationships, confounders, and directed acyclic graphs (DAGs) between variables using LLM-based domain expertise simulation. ### Method N/A (This is a class with methods, not a direct API endpoint) ### Endpoint N/A ### Parameters N/A ### Request Example ```python from pywhyllm.suggesters.model_suggester import ModelSuggester from pywhyllm import RelationshipStrategy # Initialize the modeler with GPT-4 modeler = ModelSuggester('gpt-4') # Define variables for analysis all_factors = ["smoking", "lung cancer", "exercise habits", "air pollution exposure"] treatment = "smoking" outcome = "lung cancer" # Step 1: Suggest domain expertises relevant to the causal analysis domain_expertises = modeler.suggest_domain_expertises(all_factors, n_experts=2) # Returns: ['Epidemiology', 'Public Health'] # Step 2: Suggest confounders between treatment and outcome confounders_edges, suggested_confounders = modeler.suggest_confounders( treatment=treatment, outcome=outcome, all_factors=all_factors, expertise_list=domain_expertises ) # Returns: # confounders_edges = {('smoking', 'lung cancer'): 1, ('air pollution exposure', 'smoking'): 2, ('air pollution exposure', 'lung cancer'): 2} # suggested_confounders = ['air pollution exposure'] # Step 3: Suggest pairwise causal relationships to construct a DAG suggested_dag = modeler.suggest_relationships( treatment=treatment, outcome=outcome, all_factors=all_factors, expertise_list=domain_expertises, relationship_strategy=RelationshipStrategy.Pairwise ) # Returns: Dictionary of edges with vote counts # {('smoking', 'lung cancer'): 2, ('air pollution exposure', 'lung cancer'): 2, ('exercise habits', 'lung cancer'): 1} ``` ### Response N/A (Output depends on the specific method called and input data) ### Response Example N/A ``` -------------------------------- ### Suggest Mediators and Instrumental Variables in Python Source: https://context7.com/py-why/pywhyllm/llms.txt Identifies mediating and instrumental variables for causal inference. Requires treatment, outcome, all factors, and domain expertises as input. Returns identified variables and their associated edges. ```python from pywhyllm.suggesters.identifier import Identifier identifier = Identifier('gpt-4') treatment = ... outcome = ... all_factors = [...] domain_expertises = [...] # Suggest mediators mediator_edges, suggested_mediators = identifier.suggest_mediators( treatment=treatment, outcome=outcome, all_factors=all_factors, expertise_list=domain_expertises ) # Suggest instrumental variables iv_edges, suggested_ivs = identifier.suggest_ivs( treatment=treatment, outcome=outcome, all_factors=all_factors, expertise_list=domain_expertises ) ``` -------------------------------- ### Construct Causal DAGs using Relationship Strategies in Python Source: https://context7.com/py-why/pywhyllm/llms.txt Constructs Directed Acyclic Graphs (DAGs) using different strategies to elicit causal relationships from LLMs. Supports Pairwise, Parent, Child, and Confounder relationship strategies. Requires LLM modeler, factors, treatment, outcome, and expertises. ```python from pywhyllm import RelationshipStrategy from pywhyllm.suggesters.model_suggester import ModelSuggester modeler = ModelSuggester('gpt-4') all_factors = ["A", "B", "C", "D"] treatment = "A" outcome = "D" domain_expertises = ["Statistics"] # Suggest relationships using Pairwise strategy pairwise_dag = modeler.suggest_relationships( treatment=treatment, outcome=outcome, all_factors=all_factors, expertise_list=domain_expertises, relationship_strategy=RelationshipStrategy.Pairwise ) # Suggest relationships using Parent strategy parent_dag = modeler.suggest_relationships( treatment=treatment, outcome=outcome, all_factors=all_factors, expertise_list=domain_expertises, relationship_strategy=RelationshipStrategy.Parent ) # Suggest relationships using Child strategy child_dag = modeler.suggest_relationships( treatment=treatment, outcome=outcome, all_factors=all_factors, expertise_list=domain_expertises, relationship_strategy=RelationshipStrategy.Child ) # Suggest relationships focusing on Confounders confounder_edges, confounders = modeler.suggest_relationships( treatment=treatment, outcome=outcome, all_factors=all_factors, expertise_list=domain_expertises, relationship_strategy=RelationshipStrategy.Confounder ) ``` -------------------------------- ### RAG-Based Causal Relationship Suggestion (Python) Source: https://context7.com/py-why/pywhyllm/llms.txt Utilizes AugmentedModelSuggester for causal modeling with Retrieval Augmented Generation (RAG) from the CauseNet knowledge base. It requires the pywhyllm library, an LLM backend, and a path to the CauseNet dataset. It returns evidence-backed causal relationships or indicates when none are found. ```python from pywhyllm.suggesters.augmented_model_suggester import AugmentedModelSuggester # Initialize with GPT-4 and download CauseNet dataset modeler = AugmentedModelSuggester('gpt-4', file_path='data/causenet-precision.jsonl.bz2') # Downloads CauseNet dataset automatically on initialization # Suggest pairwise relationship with RAG augmentation treatment = "smoking" outcome = "lung cancer" relationship = modeler.suggest_pairwise_relationship(treatment, outcome) # Returns: [cause, effect, reasoning] # Example: ['smoking', 'lung cancer', 'Based on extensive epidemiological evidence from CauseNet sources...'] # If no causal relationship exists relationship_none = modeler.suggest_pairwise_relationship("eye color", "lung cancer") # Returns: [None, None, 'No direct causal relationship found...'] ``` -------------------------------- ### IdentificationSuggester - Causal Identification Strategy Source: https://context7.com/py-why/pywhyllm/llms.txt Suggests identification strategies including backdoor sets, mediators, and instrumental variables for causal effect estimation. ```APIDOC ## IdentificationSuggester - Causal Identification Strategy ### Description Suggests identification strategies including backdoor sets, mediators, and instrumental variables for causal effect estimation. ### Method N/A (This is a class with methods, not a direct API endpoint) ### Endpoint N/A ### Parameters N/A ### Request Example ```python from pywhyllm.suggesters.identification_suggester import IdentificationSuggester # Initialize identifier identifier = IdentificationSuggester('gpt-4') all_factors = ["smoking", "lung cancer", "exercise habits", "air pollution exposure", "genetic predisposition"] treatment = "smoking" outcome = "lung cancer" domain_expertises = ["Epidemiology", "Genetics"] # Suggest backdoor set for adjustment backdoor_edges, backdoor_set = identifier.suggest_backdoor( treatment=treatment, outcome=outcome, all_factors=all_factors, expertise_list=domain_expertises ) # Returns backdoor adjustment set # backdoor_set = ['air pollution exposure', 'genetic predisposition'] ``` ### Response N/A (Output depends on the specific method called and input data) ### Response Example N/A ``` -------------------------------- ### Validate causal graph using Validator Source: https://github.com/py-why/pywhyllm/blob/main/README.md This Python code demonstrates the use of the `ValidationSuggester` to critique a given causal graph (DAG), suggest latent confounders, and identify negative controls. It requires an LLM model name, factors, the DAG, domain expertises, and a relationship strategy. ```python # Create instance of Validator validator = ValidationSuggester('gpt-4') # Suggest a critique of the edges in provided DAG suggested_critiques_dag = validator.critique_graph(all_factors, suggested_dag, domain_expertises, RelationshipStrategy.Pairwise) # Suggest latent confounders suggested_latent_confounders = validator.suggest_latent_confounders(treatment, outcome, all_factors, domain_expertises) # Suggest negative controls suggested_negative_controls = validator.suggest_negative_controls(treatment, outcome, all_factors, domain_expertises) ``` -------------------------------- ### Import necessary classes from PyWhy-LLM Source: https://github.com/py-why/pywhyllm/blob/main/README.md This Python code imports essential classes from the PyWhy-LLM library for performing various causal analysis tasks. These include suggesters for models, identification, validation, and relationship strategies. ```python from pywhyllm.suggesters.model_suggester import ModelSuggester from pywhyllm.suggesters.identification_suggester import IdentificationSuggester from pywhyllm.suggesters.validation_suggester import ValidationSuggester from pywhyllm.suggesters.augmented_model_suggester import AugmentedModelSuggester from pywhyllm import RelationshipStrategy ``` -------------------------------- ### Load Tübingen Dataset Source: https://github.com/py-why/pywhyllm/blob/main/docs/notebooks/tuebingen_causality_pairs/tuebingen_pairs_notebook_v2.ipynb Loads the Tübingen dataset from a CSV file located in Google Drive into a pandas DataFrame. The dataset appears to contain pairs of variables and their descriptions. ```python df = pd.read_csv('/content/drive/MyDrive/pywhy-llm/pywhyllm/tuebingen_pairs.csv') ``` -------------------------------- ### AugmentedModelSuggester - RAG-Based Causal Modeling Source: https://context7.com/py-why/pywhyllm/llms.txt Extends ModelSuggester with Retrieval Augmented Generation using the CauseNet knowledge base for evidence-backed causal relationship suggestions. ```APIDOC ## AugmentedModelSuggester - RAG-Based Causal Modeling ### Description Extends ModelSuggester with Retrieval Augmented Generation using the CauseNet knowledge base for evidence-backed causal relationship suggestions. ### Method N/A (This is a class with methods, not a direct API endpoint) ### Endpoint N/A ### Parameters N/A ### Request Example ```python from pywhyllm.suggesters.augmented_model_suggester import AugmentedModelSuggester # Initialize with GPT-4 and download CauseNet dataset modeler = AugmentedModelSuggester('gpt-4', file_path='data/causenet-precision.jsonl.bz2') # Downloads CauseNet dataset automatically on initialization # Suggest pairwise relationship with RAG augmentation treatment = "smoking" outcome = "lung cancer" relationship = modeler.suggest_pairwise_relationship(treatment, outcome) # Returns: [cause, effect, reasoning] # Example: ['smoking', 'lung cancer', 'Based on extensive epidemiological evidence from CauseNet sources...'] # If no causal relationship exists relationship_none = modeler.suggest_pairwise_relationship("eye color", "lung cancer") # Returns: [None, None, 'No direct causal relationship found...'] ``` ### Response N/A (Output depends on the specific method called and input data) ### Response Example N/A ``` -------------------------------- ### Import Pandas Library Source: https://github.com/py-why/pywhyllm/blob/main/docs/notebooks/tuebingen_causality_pairs/tuebingen_pairs_notebook_v2.ipynb Imports the pandas library, a fundamental tool for data manipulation and analysis in Python. It is commonly aliased as 'pd'. ```python import pandas as pd ``` -------------------------------- ### Suggest pairwise relationship (smoking, lung cancer) Source: https://github.com/py-why/pywhyllm/blob/main/docs/notebooks/augmented_model_suggester_examples.ipynb Uses the AugmentedModelSuggester to suggest the pairwise relationship between 'smoking' and 'lung cancer'. If a causal pair exists in CauseNet, the LLM will be augmented with that evidence. ```python result = model.suggest_pairwise_relationship("smoking", "lung cancer") ``` -------------------------------- ### Display relationship suggestion Source: https://github.com/py-why/pywhyllm/blob/main/docs/notebooks/augmented_model_suggester_examples.ipynb Prints the result of the previous pairwise relationship suggestion. This will show the suggested causal link or indicate if no direct causal link was found in the augmented dataset. ```python result ``` -------------------------------- ### Calculate Average LLM Output for a Specific Pair Source: https://github.com/py-why/pywhyllm/blob/main/docs/notebooks/tuebingen_causality_pairs/tuebingen_pairs_notebook_v2.ipynb Calculates and prints the average 'llm_ab' and 'llm_ba' values from the 'llm_output' dictionary for a specific pair ('pair0087') across multiple runs (i+1, from 1 to 5). It also prints the individual outputs for each run. ```python av_ab = 0 av_ba = 0 for i in range(5): av_ab += llm_output[('pair0087', 0.3, i+1)]['llm_ab'] av_ba += llm_output[('pair0087', 0.3, i+1)]['llm_ba'] print(llm_output[('pair0087', 0.3, i+1)]['llm_ab']) print(llm_output[('pair0087', 0.3, i+1)]['llm_ba']) av_ab = av_ab/5.0 av_ba = av_ba/5.0 print(av_ab) print(av_ba) ``` -------------------------------- ### Validate Causal Model Assumptions in Python Source: https://context7.com/py-why/pywhyllm/llms.txt Validates causal assumptions by suggesting latent confounders, negative controls, and critiquing causal graphs. It takes treatment, outcome, all factors, and domain expertises as input. Outputs include counters and lists of suggested variables or critiqued graph structures. ```python from pywhyllm.suggesters.validation_suggester import ValidationSuggester from pywhyllm import RelationshipStrategy validator = ValidationSuggester('gpt-4') treatment = "smoking" outcome = "lung cancer" all_factors = ["smoking", "lung cancer", "exercise habits", "air pollution exposure"] domain_expertises = ["Epidemiology", "Oncology"] # Suggest latent confounders latent_counter, latent_confounders = validator.suggest_latent_confounders( treatment=treatment, outcome=outcome, expertise_list=domain_expertises ) # Suggest negative controls negative_controls_counter, negative_controls = validator.suggest_negative_controls( treatment=treatment, outcome=outcome, all_factors=all_factors, expertise_list=domain_expertises ) # Critique an existing DAG suggested_dag = { ('smoking', 'lung cancer'): 2, ('exercise habits', 'smoking'): 1, ('air pollution exposure', 'lung cancer'): 2 } original_edges, critiqued_edges = validator.critique_graph( all_factors=all_factors, edges=suggested_dag, experts=domain_expertises, relationship_strategy=RelationshipStrategy.Pairwise ) ``` -------------------------------- ### Save Analysis Results to CSV Source: https://github.com/py-why/pywhyllm/blob/main/docs/notebooks/tuebingen_causality_pairs/tuebingen_pairs_notebook_v2.ipynb Saves the calculated accuracy results to a CSV file named 'gpt-4_results_straight_prompt_w_descriptions.csv'. The CSV includes columns for accuracy metrics, PairID, variable names, and ground truth. It uses Python's built-in csv module for writing. ```python import csv import copy # CSV file name csv_file = "gpt-4_results_straight_prompt_w_descriptions.csv" # Define the CSV file's header (column names) header = ["CorrectACauseB", "CorrectBCauseA", "PairID", "VarA", "VarB", "GroundTruth"] # Write the data to the CSV file with open(csv_file, mode="w", newline="", encoding="utf-8") as file: writer = csv.DictWriter(file, fieldnames=header) writer.writeheader() for pair_id, values in results.items(): writer.writerow(values) print(f"CSV file '{csv_file}' has been created.") ``` -------------------------------- ### Suggest Frontdoor Paths for Causal Inference Source: https://github.com/py-why/pywhyllm/blob/main/docs/notebooks/walkthrough.ipynb Identifies potential frontdoor paths between a treatment and an outcome from a list of variables. Frontdoor paths can be used to estimate causal effects when backdoor adjustment is not possible. ```python frontdoors = identifier.suggest_frontdoor(variables, treatment="semaglutide treatment", outcome = "cardiovascular health") print(frontdoors) ``` -------------------------------- ### Suggest Pairwise Causal Relationship Source: https://github.com/py-why/pywhyllm/blob/main/docs/notebooks/walkthrough.ipynb Uses the SimpleModelSuggester to suggest a pairwise causal relationship between two variables. The result is a list where the first element indicates causation and the second is the cause. ```python result = modeler.suggest_pairwise_relationship("ice cream sales", "shark attacks") ``` -------------------------------- ### Suggest Relationships Among Multiple Variables Source: https://github.com/py-why/pywhyllm/blob/main/docs/notebooks/walkthrough.ipynb Suggests causal relationships among a list of variables using the SimpleModelSuggester. The output provides potential causal links within the given set of variables. ```python variables = ["ice cream sales", "temperature", "cavities"] results = modeler.suggest_relationships(variables) results ``` -------------------------------- ### Suggest Backdoor Paths for Causal Inference Source: https://github.com/py-why/pywhyllm/blob/main/docs/notebooks/walkthrough.ipynb Identifies potential backdoor paths between a treatment and an outcome from a list of variables. Backdoor paths represent confounding pathways that need to be adjusted for. ```python variables = ["Age", "Sex", "HbA1c", "LDL", "eGFR", "Prior MI", "Prior Stroke or TIA", "Prior Heart Failure", "Cardiovascular medication", "T2DM medication", "Insulin", "Morbid obesity", "First occurrence of Nonfatal myocardial infarction, nonfatal stroke, death from all cause", "semaglutide treatment", "Semaglutide medication", "income", "musical taste"] backdoors = identifier.suggest_backdoor(variables, treatment="semaglutide treatment", outcome = "cardiovascular health") print(backdoors) ``` -------------------------------- ### Suggest Confounders for Causal Inference Source: https://github.com/py-why/pywhyllm/blob/main/docs/notebooks/walkthrough.ipynb Identifies potential confounders for a given pair of variables (treatment and outcome) from a list of variables. This helps in controlling for spurious correlations. ```python variables = ["ice cream sales", "temperature", "cavities"] latents = modeler.suggest_confounders(variables, "ice cream sales", "shark attacks") print(latents) ``` ```python latents = modeler.suggest_confounders(["weight", "diet", "age"], "vitamin c", "cardiovascular health") print(latents) ```