ymax_prev = 1 # for axis in plot, prev. step ymin = -0.1 # min limit for y axis for step in range(ns): for p in range(np): coin = random.randint(1, 2) # flip coin if coin == HEAD: positions[p] += 1 # one unit length to the right elif coin == TAIL: positions[p] -= 1 # one unit length to the left # Statistics mean_pos = numpy.mean(positions) stdev_pos = numpy.std(positions) pos, freq = compute_histogram(positions, nbins, piecewise_constant=True) # Extend x axis limits? if min(positions) < xmin: xmin -= 2 * numpy.sqrt(ns) if max(positions) > xmax: xmax += 2 * numpy.sqrt(ns) # "Intelligent" choice of y axis limits: # estimate ymax on the y axis from 1.1*max(freq) # (1.1 to get some space), do not change ymax # unless it deviates more than 0.1 from the previous # value, and let ymin = -0.1*ymax. ymax = 1.1 * max(freq) # axis for freq if abs(ymax - ymax_prev) < 0.1: # Do not change axis ymax = ymax_prev
import sys N = int(sys.argv[1]) m = float(sys.argv[2]) s = float(sys.argv[3]) import numpy as np np.random.seed(12) samples = np.random.normal(m, s, N) print np.mean(samples), np.std(samples) import scitools.std as st x, y = st.compute_histogram(samples, 20, piecewise_constant=True) st.plot(x, y, hardcopy='tmp.eps', title ='%d samples of Gaussian/normal numbers on (0,1)' % N)
try: np = int(sys.argv[1]) # number of particles ns = int(sys.argv[2]) # number of steps except IndexError: np = 2000 ns = 200 # Draw from 1, 2 moves = numpy.random.random_integers(1, 2, size=np*ns) # Transform 1 to -1 and 2 to 1 moves = 2*moves - 3 moves.shape = (ns, np) positions = numpy.zeros(np) for step in range(ns): positions += moves[step, :] mean_pos = numpy.mean(positions) stdev_pos = numpy.std(positions) print mean_pos, stdev_pos nbins = int(3*numpy.sqrt(ns)) # no of intervals in histogram pos, freq = compute_histogram(positions, nbins, piecewise_constant=True) plot(positions, numpy.zeros(np), 'ko3', pos, freq, 'r', axis=[min(positions), max(positions), -0.01, 1.1*max(freq)], hardcopy='tmp.eps')
import sys N = int(sys.argv[1]) m = float(sys.argv[2]) s = float(sys.argv[3]) import numpy as np np.random.seed(12) samples = np.random.normal(m, s, N) print np.mean(samples), np.std(samples) import scitools.std as st x, y = st.compute_histogram(samples, 20, piecewise_constant=True) st.plot(x, y, hardcopy="tmp.eps", title="%d samples of Gaussian/normal numbers on (0,1)" % N)
""" As uniform_numbers1.py but the histogram is plotted both as a piecewise constant curve and a piecewise linear curve. The number of bins is read from the command line. """ import sys N = int(sys.argv[1]) nbins = int(sys.argv[2]) import numpy as np np.random.seed(12) # Vectorized generation of random numbers samples = np.random.random(size=N) import scitools.std as st x1, y1 = st.compute_histogram(samples, nbins, piecewise_constant=True) x2, y2 = st.compute_histogram(samples, nbins, piecewise_constant=False) st.plot(x1, y1, 'r', x2, y2, 'b') st.title('%d samples of uniform numbers on (0,1)' % N) st.hardcopy('tmp.eps')
import sys N = int(sys.argv[1]) import numpy as np np.random.seed(12) # Vectorized generation of random numbers samples = np.random.random(size=N) # Plot histogram import scitools.std as st x, y = st.compute_histogram(samples, nbins=20) st.plot(x, y, title='%d samples of uniform numbers on [0,1)' % N) st.hardcopy('tmp.eps')
# Exercise 8.20 # Author: Noah Waterfield Price from time import time from scitools.std import plot, compute_histogram def diff_eq(x, a, c, m): xn = (a * x + c) % m yn = float(xn) / m return xn, yn def rand_gen(a, c, m, N=10, x=time()): r = [] for i in range(N): x, y = diff_eq(x, a, c, m) r.append(y) return r a = 8121 c = 28411 m = 134456 N = 1000000 random_nos = rand_gen(a, c, m, N) x, y = compute_histogram(random_nos, nbins=50) plot(x, y, title='%d samples of psuedo-random numbers generated on (0, 1)' % N) raw_input()