def rchisq(n,df,ncp=0): """ Generates random variables from the chi-square distribution """ from scipy.stats import chi2,ncx2 if ncp==0: result=chi2.rvs(size=n,df=df,loc=0,scale=1) else: result=ncx2.rvs(size=n,df=df,nc=ncp,loc=0,scale=1) return result
def direct_simulation(k, lamda, theta, X_0, T, N): dt = T / N c = (2 * k) / ((1 - np.exp(-k * dt)) * theta**2) df = 4 * k * lamda / theta**2 X_temp = X_0 X_sol = np.zeros(N + 1) X_sol[0] = X_0 for j in range(1, N + 1): nc = 2 * c * X_temp * np.exp(-k * dt) Y = ncx2.rvs(df, nc, size=1) X_temp = Y / (2 * c) X_sol[j] = X_temp return np.linspace(0, T, N + 1), X_sol
def draw_signalData(Nsamp=1, alpha=__alpha, beta=__beta, **kwargs): """ draw an SNR from the signal distribution """ return np.array([ncx2.rvs(__noise_df, nc) for nc in __draw_truncatedPareto(Nsamp, alpha=alpha, beta=beta)])
def Gen_var_t(delta, kappa, theta, xi, var_s): return c(delta, kappa, xi) * ncx2.rvs(d(kappa, theta, xi), lbd(delta, kappa, xi, var_s))
fig = plt.figure(1, dpi=150) ax = fig.add_subplot(111) ax.set_xlim((0, 42)) ax.set_title('Analysis of PE') ax.set_xlabel('TS') ax.set_ylabel('pdf, normalized') # parameters of non central chi squared distribution df, nc = 10.09, 2.51 # plot the pdf x = np.linspace(0, 43, num=1000) y = ncx2.pdf(x, df, nc) ax.plot(x, y, label=f'pdf, $df={df}, nc={nc}$', color='black', linewidth=0.8) # generate some random numbers, do some histogram things random = ncx2.rvs(df, nc, size=1000) n, bins, patches = ax.hist(random, bins=nbins, label='random numbers', density=True, color='grey') cum_bins = bins.copy() cum_bins[-1] = 50 # do fit from random numbers # get x places: dx = (bins[1] - bins[0]) / 2 x_r = np.linspace(bins[0] + dx / 2, bins[-1] - dx / 2, num=len(bins)) #ax.plot(bins+dx, np.zeros(bins.shape[0]), 'o') def fit_func(x, df, nc):
# Display the probability density function (``pdf``): x = np.linspace(ncx2.ppf(0.01, df, nc), ncx2.ppf(0.99, df, nc), 100) ax.plot(x, ncx2.pdf(x, df, nc), 'r-', lw=5, alpha=0.6, label='ncx2 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 = ncx2(df, nc) ax.plot(x, rv.pdf(x), 'k-', lw=2, label='frozen pdf') # Check accuracy of ``cdf`` and ``ppf``: vals = ncx2.ppf([0.001, 0.5, 0.999], df, nc) np.allclose([0.001, 0.5, 0.999], ncx2.cdf(vals, df, nc)) # True # Generate random numbers: r = ncx2.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()