"""Authored observed outcomes, two grouped mean models and a treatment-effect contrast; no causal experiment."""
from fractions import Fraction as F
import json
SPEC=[('A',1,18),('A',0,19),('B',1,8),('B',0,2)]
def demo():
    rows=[]
    for group,t,successes in SPEC:
        for i in range(20):rows.append({'id':group+'-'+str(t)+'-'+str(i+1),'group':group,'treatment':t,'observed_y':int(i<successes)})
    assert len({r['id'] for r in rows})==80
    models={}
    for t in [0,1]:
        models[t]={g:sum((F(r['observed_y']) for r in rows if r['group']==g and r['treatment']==t),F(0))/sum(r['group']==g and r['treatment']==t for r in rows) for g in ['A','B']}
    contrasts=[{'group':g,'fitted_mean_treated':str(models[1][g]),'fitted_mean_control':str(models[0][g]),'observed_conditional_contrast':str(models[1][g]-models[0][g])} for g in ['A','B']]
    response_order=sorted(['A','B'],key=lambda g:(-models[1][g],g));contrast_order=sorted(['A','B'],key=lambda g:(-(models[1][g]-models[0][g]),g))
    assert response_order==['A','B'] and contrast_order==['B','A']
    assert contrasts[0]['observed_conditional_contrast']=='-1/20' and contrasts[1]['observed_conditional_contrast']=='3/10'
    return {'authored_observed_rows':rows,'fitted_two_models':{str(t):{g:str(p) for g,p in m.items()} for t,m in models.items()},'contrasts':contrasts,'ranking_by_treated_response':response_order,'ranking_by_conditional_contrast':contrast_order,'unseen_group_estimate':None,'both_potential_outcomes_of_an_individual_observed':False,'actual_random_assignment_performed':False,'causal_identification_assumptions_not_verified':['conditional exchangeability or valid randomized design','positivity within group','consistency and well-defined treatment','appropriate independence/interference assumptions'],'causalml_training_or_customer_sales_or_effect_experiment':False}
if __name__=='__main__':print(json.dumps(demo(),ensure_ascii=False,indent=2))
