def opt_n(n): stderr_n=sigma/n**.5 lam_n=(mean_h1-mean_h0)/stderr_n alpha_xval_n=t.isf(alphas,df=n-1,scale=stderr_n) betah_n=nct.cdf(alpha_xval_n,df=n-1,nc=lam_n,scale=stderr_n) if sided==2: betal_n=nct.cdf(-alpha_xval_n,df=n-1,nc=lam_n) betacalc_n=betah_n-betal_n else: betacalc_n=betah_n return betacalc_n
def t_test_power(alpha, mu_1, mu_2, n_1, n_2, sigma_1, sigma_2): nu = ((sigma_1**2 / n_1 + sigma_2**2 / n_2)**2) / (( (sigma_1**2 / n_1)**2) / (n_1 - 1) + ((sigma_2**2 / n_2)**2) / (n_2 - 1)) theta = (mu_1 - mu_2) / ((sigma_1**2 / n_1 + sigma_2**2 / n_2)**0.5) t_cut = t.ppf(1 - alpha / 2, nu) power = 1 - (nct.cdf(t_cut, nu, theta) - nct.cdf(-t_cut, nu, theta)) return power
def pt(q,df,ncp=0): """ Calculates the cumulative of the t-distribution """ from scipy.stats import t,nct if ncp==0: result=t.cdf(x=q,df=df,loc=0,scale=1) else: result=nct.cdf(x=q,df=df,nc=ncp,loc=0,scale=1) return result
def g_(p, n, K): from scipy.stats import nct return 1 - nct.cdf(sqrt(n) * K, n-1, sqrt(n) * x_scipy(p))
# Display the probability density function (``pdf``): x = np.linspace(nct.ppf(0.01, df, nc), nct.ppf(0.99, df, nc), 100) ax.plot(x, nct.pdf(x, df, nc), 'r-', lw=5, alpha=0.6, label='nct pdf') # Alternatively, the distribution object can be called (as a function) # to fix the shape, location and scale parameters. This returns a "frozen" # RV object holding the given parameters fixed. # Freeze the distribution and display the frozen ``pdf``: rv = nct(df, nc) ax.plot(x, rv.pdf(x), 'k-', lw=2, label='frozen pdf') # Check accuracy of ``cdf`` and ``ppf``: vals = nct.ppf([0.001, 0.5, 0.999], df, nc) np.allclose([0.001, 0.5, 0.999], nct.cdf(vals, df, nc)) # True # Generate random numbers: r = nct.rvs(df, nc, size=1000) # And compare the histogram: ax.hist(r, density=True, histtype='stepfilled', alpha=0.2) ax.legend(loc='best', frameon=False) plt.show()
lam=(mean_h1-mean_h0)/stderr dsr=mean_h1/sigma #set up x axis for plotting xmin=mean_h0-3*stderr xmax=mean_h1+3*stderr xs=np.linspace(xmin,xmax,200) #define H0 distribution h0pdf=t.pdf(xs,df=n-1,loc=mean_h0,scale=stderr) alpha_xval=t.isf(alphas,df=n-1,loc=mean_h0,scale=stderr) ymax=h0pdf.max() #define H1 distribution h1pdf=nct.pdf(xs,df=n-1,nc=lam,scale=stderr) betah=nct.cdf(alpha_xval,df=n-1,nc=lam,scale=stderr) if sided==1.2: betal=nct.cdf(-alpha_xval,df=n-1,nc=lam) betacalc=betah-betal else: betacalc=betah #plot results plt.plot(xs,h0pdf,label="H0") plt.plot(xs,h1pdf,label="H1") plt.axvline(alpha_xval,ls="--",color="blue") if sided==2: plt.fill_between(xs,0,h0pdf,where=(xs>alpha_xval)|(xs<-alpha_xval),color="blue",alpha=.2, label="alpha") plt.fill_between(xs,0,h1pdf,where=(xs<alpha_xval)&(xs>-alpha_xval),color="orange",alpha=.2, label="beta")
def g_(p, n, K): from scipy.stats import nct return 1 - nct.cdf(sqrt(n) * K, n - 1, sqrt(n) * x_scipy(p))