"""Multiplicity check for the rare-pair discovery procedure, not a causal test."""
from common import *
from collections import defaultdict
from itertools import combinations
from scipy.stats import binomtest
g,t=load();p=g[g.valid&~g.free&~g.explicit&g.year.between(2023,2025)&g.price.notna()]
excluded={'Indie','Casual','Singleplayer','Early Access','Free to Play','Great Soundtrack','Controller','Family Friendly','Kickstarter','Crowdfunded','Software','Utilities','Benchmark','Documentary','Gaming','Masterpiece','Cult Classic','Classic','Remake','Epic','Short','Long','Beautiful','Relaxing','Colorful','Stylized','Realistic','Cinematic','Emotional','Funny','Comedy','Dark','Cute','Difficult','Replay Value','Content Rich','Immersive'}
tags=t[t.appid.isin(p.appid)&t['rank'].le(7)].groupby('appid').tag.agg(list).to_dict()
single=defaultdict(lambda:np.zeros(3,dtype=int));pairs=defaultdict(lambda:np.zeros(3,dtype=int))
for row in p[['appid','r100','h50']].itertuples(index=False):
    v=np.array([1,int(row.r100),int(row.h50)]);ns=sorted(tags.get(row.appid,[]))
    for name in ns:single[name]+=v
    for pair in combinations(ns,2):pairs[pair]+=v
rows=[]
for (a,b),v in pairs.items():
    if not 4<=v[0]<=40:continue
    av,bv=single[a],single[b]
    if min(av[0],bv[0])<80 or a in excluded or b in excluded:continue
    row=dict(a=a,b=b,n=v[0])
    for j,outcome in [(1,'r100'),(2,'h50')]:
        null=max(av[j]/av[0],bv[j]/bv[0],float(p[outcome].mean()));row[outcome+'_count']=v[j];row[outcome+'_reference']=null
        row[outcome+'_p']=binomtest(int(v[j]),int(v[0]),null,alternative='greater').pvalue
    rows.append(row)
d=pd.DataFrame(rows)
for c in ['r100','h50']:
    order=d.sort_values(c+'_p').index;raw=d.loc[order,c+'_p'].to_numpy();q=np.minimum.accumulate((raw*len(d)/np.arange(1,len(d)+1))[::-1])[::-1].clip(0,1);d.loc[order,c+'_q']=q
d.to_csv(R/'rare_pair_multiple_testing.csv',index=False)
print('Eligible tests',len(d),'nominal h50 p<.05',int(d.h50_p.lt(.05).sum()),'BH q<.05',int(d.h50_q.lt(.05).sum()))
print(d[((d.a=='Base Building')&(d.b=='Card Game'))|((d.a=='Idler')&(d.b=='Retro'))|((d.a=='Mystery')&(d.b=='Roguelite'))].to_string(index=False))
