<jupyter_start><jupyter_text>Experimental and Nonexperimental Data used to illustrate the estimation of PN, the probability that drug $x$ was responsible for a person’s death ($y$).<jupyter_code>data = pd.DataFrame({'do(TakeDrug)':[16, 984],
                     "do(NotTakeDrug)": [14,986],
                     "TakeDrug": [2, 998],
                     "NotTakeDrug": [28, 972]},index=['Deaths', 'Survival'])
data<jupyter_output><empty_output><jupyter_text>Model generating the experimental and observational data of equations(16 and 17).Zrepresents an unobserved confounder affecting both treatment (X) and outcome (Y).<jupyter_code>P =     {'Syndrome': 28/2000,
         "Death|Syndrome": 1,
         "Death|TakeDrug,NoSyndrome": 28/1000,
         "Death|NotTakeDrug,NoSyndrome": 2/1000,
         "TakeDrug|NoSyndrome": 1000/(1000 + 972),
         "TakeDrug|Syndrome": 0}
def legal_liability(bvar):
    # bvar contains a dictionary of bound variables. 
    if 'Z' not in bvar:
        return prevalence_of_syndrome( P['Syndrome'] )
    if 'X' not in bvar:
        return take_drug_based_on_syndrome( bvar['Z'], P["TakeDrug|NoSyndrome"], P["TakeDrug|Syndrome"] )
    if 'Y' not in bvar:
        return patient_dies_if_syndrome_or_takes_drug( bvar['X'], 
                                                       bvar['Z'],
                                                       P["Death|NotTakeDrug,NoSyndrome"],
                                                       P["Death|TakeDrug,NoSyndrome"] )
    return None

def prevalence_of_syndrome( r ):
    return [(1-r, 'Z=NoSyndrome'),
            (r,   'Z=Syndrome')]
def take_drug_based_on_syndrome( Z, q1, q2 ):
    if Z == 'NoSyndrome':
        return [(1-q1,'X=NotTakeDrug'),
                (q1,  'X=TakeDrug')]
    else:
        return [(1-q2,'X=NotTakeDrug'),
                (q2,  'X=TakeDrug')]   
    
def patient_dies_if_syndrome_or_takes_drug( X, Z, p1, p2 ):
    if Z == 'Syndrome':
        return [(0, 'Y=Survival'),
                (1, 'Y=Death')]
    elif X== 'TakeDrug':
        return [(1-p2, 'Y=Survival'),
                (p2,   'Y=Death')]
    else:
        return [(1-p1, 'Y=Survival'),
                (p1,   'Y=Death')]

scm = PTree.fromFunc(legal_liability, 'Root=1')

display(scm.show(show_prob=True))

do_x1 = scm.do(scm.prop('X=TakeDrug'))
y1 = scm.prop('Y=Death')
P["Death|do(TakeDrug)"] = do_x1.prob(y1)
P
x1 = scm.prop('X=TakeDrug')
y1 = scm.prop('Y=Death')
y0 = scm.prop('Y=Survive')
given_x1_and_y1 = scm.see(x1 & y1)
what_if_x0 = scm.prop("X=NotTakeDrug")
PN = scm.cf(given_x1_and_y1, what_if_x0)
P["TakeDrug is Necessary Cause of Death"] = PN.prob(y0)
P<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.149063670411985, 'fraction_numerical': 0.03333333333333333, 'mean_word_length': 2.59005376344086, 'pattern_counts': {'":': 8, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '282467', 'license': 'Apache-2.0', 'n_tokens_mistral': 1076, 'n_tokens_neox': 986, 'n_words': 194, 'path': 'notebooks/ActualCausality.ipynb', 'repo_name': 'COVID-19-Causal-Reasoning/probability_trees', 'size': '2670'}