def plot(p2D,pens,p2De,para,params_exp,thickness,tempres): #print tempres plt.subplot(511) plt.hist(p2D[:,5], bins=20, normed=True) x = np.linspace(0, 5, 80) if para !=None : plt.plot(x, maxwell.pdf(x, *para),'r',x,maxwell.cdf(x, *para), 'g') plt.subplot(512) plt.hist(pens, bins=20, normed=True) z=np.linspace(0,500,200) plt.xlim(0,2*thickness) plt.plot(z, expon.pdf(z, *params_exp),'r')#plt.plot(z, expon.pdf(z, *params_exp),'r',z,expon.cdf(z, *params_exp), 'g') plt.subplot(513) plt.xlim(0,thickness+1) plt.plot(p2D[:,0],p2D[:,1],'b.') plt.subplot(514) plt.ylim(-1*10**6,1*10**6) plt.xlim(0,thickness+1) plt.plot(p2De[:,0],p2De[:,1],'b.') plt.subplot(515) plt.plot(tempres[:,0],tempres[:,1],'r-') plt.savefig() plt.show() return
def plot(p2D, pens, p2De, para, params_exp, thickness): plt.subplot(411) plt.hist(p2D[:, 5], bins=10, normed=True) x = np.linspace(0, 5, 80) plt.plot(x, maxwell.pdf(x, *para), 'r', x, maxwell.cdf(x, *para), 'g') plt.subplot(412) plt.hist(pens, bins=20, normed=True) z = np.linspace(0, 500, 200) plt.xlim(0, 2 * thickness) plt.plot( z, expon.pdf(z, *params_exp), 'r' ) #plt.plot(z, expon.pdf(z, *params_exp),'r',z,expon.cdf(z, *params_exp), 'g') plt.subplot(413) plt.xlim(0, thickness + 1) plt.plot(p2D[:, 0], p2D[:, 1], 'b.') plt.subplot(414) plt.ylim(-1 * 10**6, 1 * 10**6) plt.xlim(0, thickness + 1) plt.plot(p2De[:, 0], p2De[:, 1], 'b.') plt.show() return
def plot(p2D, pens, p2De, para, params_exp, thickness, tempres, petest, electron_affinity, finame): #print tempres plt.subplot(511) plt.xlabel('thickness[nm]') plt.ylabel('P') plt.hist(p2D[:, 5], bins=20, normed=True) x = np.linspace(0, 5, 80) if para != None: plt.plot(x, maxwell.pdf(x, *para), 'r', x, maxwell.cdf(x, *para), 'g') plt.subplot(512) plt.hist(pens, bins=20, normed=True) z = np.linspace(0, 500, 200) plt.xlabel('thickness[nm]') plt.ylabel('P') plt.xlim(0, 2 * thickness) plt.plot( z, expon.pdf(z, *params_exp), 'r' ) #plt.plot(z, expon.pdf(z, *params_exp),'r',z,expon.cdf(z, *params_exp), 'g') plt.subplot(513) plt.xlim(0, thickness + 1) plt.xlabel('thickness[nm]') plt.ylabel('size[nm]') plt.plot(p2D[:, 0], p2D[:, 1], 'b,') plt.subplot(514) plt.ylim(-1 * 10**6, 1 * 10**6) plt.xlim(0, thickness + 1) plt.xlabel('thickness[nm]') plt.ylabel('size[nm]') plt.plot(p2De[:, 0], p2De[:, 1], 'b,') plt.subplot(515) plt.xlabel('Time[s]') plt.ylabel('count[1]') smooth = 80 for i in np.arange(0, len(tempres)): tempres[i, 1] = (np.sum(tempres[i:i + smooth, 1]) / smooth).astype(float) plt.ylim(0, tempres[1, 1] * 1.5) plt.plot(tempres[:, 0], tempres[:, 1], 'r-') plt.savefig(finame + 'full_tk_%dpe_%.3fea_%.3f.pdf' % (thickness, petest, electron_affinity)) plt.show() return
def gen_velocities(v_0, v_Esc, v_E, num_object): """ Generates a list of velocities """ cdf_v_Esc = maxwell.cdf(v_Esc, scale=v_0 / np.sqrt(2)) velocity_r = maxwell.ppf(np.random.rand(num_object) * cdf_v_Esc, scale=v_0 / np.sqrt(2)) # kpc/yr velocity_theta = np.arccos(1 - 2 * np.random.rand(num_object)) velocity_phi = 2 * np.pi * np.random.rand(num_object) velocity = np.zeros((3, num_object)) velocity[0] = velocity_r * np.sin(velocity_theta) * np.cos(velocity_phi) velocity[1] = velocity_r * np.sin(velocity_theta) * np.sin(velocity_phi) velocity[2] = velocity_r * np.cos(velocity_theta) return velocity.T # (N, 3)
x = np.linspace(maxwell.ppf(0.01), maxwell.ppf(0.99), 100) ax.plot(x, maxwell.pdf(x), 'r-', lw=5, alpha=0.6, label='maxwell 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 = maxwell() ax.plot(x, rv.pdf(x), 'k-', lw=2, label='frozen pdf') # Check accuracy of ``cdf`` and ``ppf``: vals = maxwell.ppf([0.001, 0.5, 0.999]) np.allclose([0.001, 0.5, 0.999], maxwell.cdf(vals)) # True # Generate random numbers: r = maxwell.rvs(size=1000) # And compare the histogram: ax.hist(r, density=True, histtype='stepfilled', alpha=0.2) ax.legend(loc='best', frameon=False) plt.show()
def test_rand_velocity(sigma, num_sample=10000, nbins=20, tolerance=1e-3, seed="Dimitris", plot=False, save=True): """Test that the velocity output is sampled as a maxwellian Arguments: - sigma: argument needed to run rand_velocity - num_sample: number of random theta generated - nbins: random sampled numbers will be binned to compute probabilities and compare to expected values. This variable specifies the number of bins used. - tolerance: tolerance for the test - seed: the seed used for the random number generator - plot: if true, plot results - save: if true, saves the plot Returns: True if the test is succesful, False otherwise """ rd.seed(seed) velocity_array = np.zeros(num_sample) for k in range(0, len(velocity_array)): velocity_array[k] = kicks.rand_velocity(sigma) #do a histogram vals_velocity, bins_velocity = np.histogram(velocity_array, bins=np.linspace( 0, 3 * sigma, nbins)) prob_test = np.zeros(len(vals_velocity)) for k in range(0, len(vals_velocity)): prob_test[k] = maxwell.cdf(bins_velocity[k + 1], 0, sigma) - maxwell.cdf( bins_velocity[k], 0, sigma) test_array = [] for j in range(0, len(prob_test)): test_array = np.append( test_array, np.ones(int(round(prob_test[j] * num_sample))) * bins_velocity[j]) if plot: plt.hist(velocity_array, bins=np.linspace(0, 3 * sigma, nbins), alpha=0.5, label="function output") plt.hist(test_array, bins=np.linspace(0, 3 * sigma, nbins), alpha=0.5, label="expected value") plt.title("velocity distribution") plt.xlabel("velocity value") plt.ylabel("distribution") plt.legend(loc='upper right') plt.show() plt.close() if save: plt.hist(velocity_array, bins=np.linspace(0, 3 * sigma, nbins), alpha=0.5, label="function output") plt.hist(test_array, bins=np.linspace(0, 3 * sigma, nbins), alpha=0.5, label="expected value") plt.title("velocity distribution") plt.xlabel("velocity value") plt.ylabel("distribution") plt.legend(loc='upper right') plt.savefig("velocity_distribution.png") plt.close() #check if the probability computed from each bin is within the tolerance success = True tolerance = max(vals_velocity) * tolerance for k in range(0, len(vals_velocity)): prob_hist = vals_velocity[k] / (sum(vals_velocity)) if abs(prob_test[k] - prob_hist) > tolerance: success = False break #re-seed the random number generator rd.seed() return success
sys.exit(0) temperature = float(sys.argv[1]) * kAuPerKelvin atom = sys.argv[2] # 'H' or 'C' n = int(sys.argv[3]) # The number of generated random numbers. if atom == 'H': mass = mass_hydrogen elif atom == 'C': mass = mass_carbon else: assert(False) scale = numpy.sqrt(kBoltzmann * temperature / mass) speeds = [0.0] * n cumulations = [0.0] * n speeds[0] = maxwell.rvs(scale=scale) cumulations[0] = maxwell.cdf(speeds[0], scale=scale) for i in range(1, n): # bernoulli.rvs(p) = 1 in probability p, 0 in probability (1 - p). accelerate = (bernoulli.rvs(cumulations[i - 1]) == 0) if accelerate: speeds[i] = speeds[i - 1] * (1.0 + kDeltaTime * kAccelRatio) else: speeds[i] = max(0.0, speeds[i - 1] * (1.0 - kDeltaTime * kAccelRatio)) # Cumulative probability P(x < speeds[i]) where x is a random variable # from the Maxwell distribution. cumulations[i] = maxwell.cdf(speeds[i], scale=scale) energies = map(lambda s: 0.5 * mass * (s ** 2.0), speeds) print 'speed [a.u.] cumulation energy [a.u.]' for i in range(n): print speeds[i], cumulations[i], energies[i] print 'temperature [a.u.]:', temperature