def dt(x,df,ncp=0): """ Calculates the density/point estimate of the t-distribution """ from scipy.stats import t,nct if ncp==0: result=t.pdf(x=x,df=df,loc=0,scale=1) else: result=nct.pdf(x=x,df=df,nc=ncp,loc=0,scale=1) return result
from scipy.stats import nct import matplotlib.pyplot as plt fig, ax = plt.subplots(1, 1) # Calculate a few first moments: df, nc = 14, 0.24 mean, var, skew, kurt = nct.stats(df, nc, moments='mvsk') # 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:
alphas=alpha/sided 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,