import math
import numpy as np
import pandas as pd
from matplotlib import pyplot as plt
from scipy.special import chdtri
from collections import defaultdict
%matplotlib inline
defpchisq(q,df,ncp=0):"""
Calculates the cumulative of the chi-square distribution
"""from scipy.stats import chi2,ncx2
if ncp==0:
result=chi2.cdf(x=q,df=df,loc=0,scale=1)else:
result=ncx2.cdf(x=q,df=df,nc=ncp,loc=0,scale=1)return result
defqchisq(p,df,ncp=0):"""
Calculates the quantile function of the chi-square distribution
"""from scipy.stats import chi2,ncx2
if ncp==0:
result=chi2.ppf(q=p,df=df,loc=0,scale=1)else:
result=ncx2.ppf(q=p,df=df,nc=ncp,loc=0,scale=1)return result
defget_power(MAF, beta_alt, count, pvalue=5e-8):# MAF = 0.5# beta_alt = 0.2# count = 2000
sigma = math.sqrt(1-2*MAF*(1-MAF)*beta_alt**2)# error sd after SNP effect is accounted for (see next part for explanation)
ses = sigma/math.sqrt(count*2*MAF*(1-MAF))# q_thresh = scipy.stats.chi2.ppf(q= 5e-8, df = 1)
q_thresh = qchisq(1-pvalue,1)
pwr =1- pchisq(q_thresh,1,(beta_alt/ses)**2)return pwr