Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

params #183

Closed
github-actions bot opened this issue Nov 29, 2022 · 2 comments
Closed

params #183

github-actions bot opened this issue Nov 29, 2022 · 2 comments
Assignees
Labels

Comments

@github-actions
Copy link

    graph = constructPAG(fields, causalModel)
    print('treat:', treatment)
    results = []
    
    def testModel(results, model):
        # model.view_model()
        estimand = model.identify_effect(proceed_when_unidentifiable=True)
        methods = {
            'psm': 'backdoor.propensity_score_matching',
            'pss': 'backdoor.propensity_score_stratification',
            'psw': 'backdoor.propensity_score_weighting',
            'lr': 'backdoor.linear_regression',
            'glm': 'backdoor.generalized_linear_model',
            'iv': 'iv.instrumental_variable',
            'iv/rd': 'iv.regression_discontinuity'
        }
        tmp = lambda df: satisfy(df, groups.current)
        satCurrent, satOther = satisfy(dataSource, groups.current), satisfy(dataSource, groups.other)
        method = 'lr'
        if methods[method].startswith('backdoor.propensity_score_'):
            for treat in model._treatment:
                filters = [f for f in groups.current.predicates if f.fid == treat]
                tmp = IDoWhy.IRInsightExplainSubspace(predicates=filters)
                model._data = model._data.assign(**{treat: satisfy(transData, tmp) })
        estimate = model.estimate_effect(
            estimand,
            methods[method],
            target_units=lambda df: inferDiff(satCurrent, satOther), # satisfy(self.dataSource, groups.current), satisfy(df, groups.other)),
            # evaluate_effect_strength=True,
            )
        results.append(IDoWhy.LinkInfo(
            src=f.fid,
            tar=measures[0].fid,
            src_type=2,
            tar_type=1,
            description=IDoWhy.LinkInfoDescription(key='', data={'estimate': str(estimate)}),
            responsibility=significance_value(estimate.value, var=1.)
        ))
        # TODO: params
        if estimate.value > 0:
            print("f===========", f.fid)
            print("target_units=\n", dataSource[tmp(transData)])
            print('unobserved f = ', f, '\n', estimate)
    
    for e in adj[f_ind[measures[0].fid]]:
        if e['src_type'] in [-1, 2]:
            # TODO:
            pass
    
    # General: use origin graph 
    # Fallback: without graph, any variable can be used as common_cause
    for f in fields:
        if f.fid not in dimensions and f.fid not in [f.fid for f in measures]:
            # common_causes = [f.fid]
            # effect_modifiers = [f.fid]
            effect_modifiers = [f.fid]
            # TODO: if edges in graph
            model = dowhy.CausalModel(
                data=transData,
                # treatment=[d for d in dimensions if flipped or not compare(current.get(d, None), other.get(d, None))],
                common_causes=[f.fid],
                treatment=treatment,
                outcome=[measures[0].fid],
                # instruments=[], # Z, causes of treatment, no confounding for the effect of Z on outcome
                # effect_modifiers=effect_modifiers, # causes of outcome other than treatment
                # graph=graph,
                identify_vars=True
            )
            testModel(results, model)
    
    return results

def significance_value(x: float, var: float=1.):
    import scipy.stats as st
    """
        x (float): X - EX
        var (float): σ(X)
    """
    print("x = ", x)
    print("norm cdf =", st.norm.cdf(abs(x)))
    return 2 * st.norm.cdf(abs(x), scale=var) - 1

def ExplainData(props: IDoWhy.IRInsightExplainProps) -> tp.List[IDoWhy.IRInsightExplainResult]:
    session = ExplainDataSession(props.data, props.fields)
    session.g_gml = constructPAG(props.fields, props.causalModel)
    session.updateModel(props.view.dimensions, props.view.measures, props.groups)
    session.identitifyEstimand()
    session.estimateEffect(props.groups)
    results = []
    try:
        descrip_data = {
            'data': inferInfo(session),
            'target estimand': session.estimate.target_estimand.__str__(),
            'realized estimand': session.estimate.realized_estimand_expr,
            'target units': session.estimate.estimator.target_units_tostr() if hasattr(session.estimate, "estimator") else None,
            'mean value of estimation': session.estimate.value,
            'effect estimates': session.estimate.cate_estimates if hasattr(session.estimate, "cate_estimates") else None,
        }
        if hasattr(session.estimate, "estimator"):
            if session.estimate.estimator._significance_test:
                descrip_data['p-value'] = session.estimate.test_stat_significance()
                # session.estimate.estimator.signif_results_tostr(session.estimate.test_stat_significance())
            if session.estimate.estimator._confidence_intervals:
                descrip_data['confidence interval'], [session.estimate.estimator.confidence_level,
                                                                session.estimate.get_confidence_intervals()]
        if session.estimate.conditional_estimates is not None:
            descrip_data['conditional estimates'] = str(session.estimate.conditional_estimates)
        if session.estimate.effect_strength is not None:
            descrip_data['change in outcome attributable to treatment'] = session.estimate.effect_strength["fraction-effect"]
        print("descrip_data=", descrip_data)
        descrip_data['desc_by'] = 'ExplainData'
        results.append(IDoWhy.LinkInfo(
            src=props.view.dimensions[0], tar=props.view.measures[0].fid, src_type=-1, tar_type=1,
            description=IDoWhy.LinkInfoDescription(key='', data=descrip_data),
            responsibility=significance_value(session.estimate.value, var=1.)
        ))
    except Exception as e:
        print(str(e), file=sys.stderr)
    
    results.extend(explainData(props))
    # print("results =", results)
    
    return IDoWhy.IRInsightExplainResult(
        causalEffects=results
@github-actions
Copy link
Author

Closed in fe41774

1 similar comment
@github-actions
Copy link
Author

Closed in fe41774

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant