def simulate_n_paths_maxstorage(n, N, x0, p0, M, m): self.simulations = zeros((n, N+1, 2)) for i in range(n): x, p = simulate_one_path(N, x0, p0, M, m) self.simulations[i,:,0] = x self.simulations[i,:,1] = p # show 5 random sample paths: for i in range(0, n, n/5): self.plot_path(x, 'sample paths of investment', 1) self.plot_path(p, 'sample paths of interest rate', 2) figure(1); hardcopy('tmp_sample_paths_investment.eps') figure(2); hardcopy('tmp_sample_paths_interestrate.eps') # compute average: xm = mean(self.simulations[:,:,0], axis=1) pm = mean(self.simulations[:,:,1], axis=1) # compute standard deviation: xs = std(self.simulations[:,:,0], axis=1) ps = std(self.simulations[:,:,1], axis=1) return xm, xs, pm, ps
def simulate_n_paths_maxstorage(n, N, x0, p0, M, m): self.simulations = zeros((n, N + 1, 2)) for i in range(n): x, p = simulate_one_path(N, x0, p0, M, m) self.simulations[i, :, 0] = x self.simulations[i, :, 1] = p # show 5 random sample paths: for i in range(0, n, n / 5): self.plot_path(x, 'sample paths of investment', 1) self.plot_path(p, 'sample paths of interest rate', 2) figure(1) hardcopy('tmp_sample_paths_investment.eps') figure(2) hardcopy('tmp_sample_paths_interestrate.eps') # compute average: xm = mean(self.simulations[:, :, 0], axis=1) pm = mean(self.simulations[:, :, 1], axis=1) # compute standard deviation: xs = std(self.simulations[:, :, 0], axis=1) ps = std(self.simulations[:, :, 1], axis=1) return xm, xs, pm, ps
from scitools.std import * N = int(sys.argv[1]) m = float(sys.argv[2]) s = float(sys.argv[3]) random.seed(12) samples = random.normal(m, s, N) x, y = compute_histogram(samples, 20, piecewise_constant=True) print mean(samples), std(samples) plot(x, y) title('%d samples of Gaussian/normal numbers on (0,1)' % N) hardcopy('tmp.eps')
from scitools.std import * N = int(sys.argv[1]); m = float(sys.argv[2]); s = float(sys.argv[3]) random.seed(12) samples = random.normal(m, s, N) x, y = compute_histogram(samples, 20, piecewise_constant=True) print mean(samples), std(samples) plot(x, y) title('%d samples of Gaussian/normal numbers on (0,1)' % N) hardcopy('tmp.eps')
y = positions.copy() # y position is always 0 nbins = int(3 * xmax) # no of intervals in histogram 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_number.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 = mean(positions) stdev_pos = std(positions) pos, freq = compute_histogram(positions, nbins, piecewise_constant=True) # extend x axis limits? if min(positions) < xmin: xmin -= 2 * sqrt(ns) if max(positions) > xmax: xmax += 2 * 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 math.abs(ymax - ymax_prev) < 0.1:
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 = 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 = zeros(np) for step in range(ns): positions += moves[step, :] mean_pos, stdev_pos = mean(positions), std(positions) print mean_pos, stdev_pos nbins = int(3*sqrt(ns)) # no of intervals in histogram pos, freq = compute_histogram(positions, nbins, piecewise_constant=True) plot(positions, zeros(np), 'ko3', pos, freq, 'r', axis=[min(positions), max(positions), -0.05, 1.1*max(freq)], hardcopy='tmp.eps')
y = positions.copy() # y position is always 0 nbins = int(3*xmax) # no of intervals in histogram 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_number.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 = mean(positions) stdev_pos = std(positions) pos, freq = compute_histogram(positions, nbins, piecewise_constant=True) # extend x axis limits? if min(positions) < xmin: xmin -= 2*sqrt(ns) if max(positions) > xmax: xmax += 2*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
ns = int(sys.argv[2]) # number of steps except IndexError: np = 2000 ns = 200 # draw from 1, 2: moves = 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 = zeros(np) for step in range(ns): positions += moves[step, :] mean_pos, stdev_pos = mean(positions), std(positions) print mean_pos, stdev_pos nbins = int(3 * sqrt(ns)) # no of intervals in histogram pos, freq = compute_histogram(positions, nbins, piecewise_constant=True) plot(positions, zeros(np), 'ko3', pos, freq, 'r', axis=[min(positions), max(positions), -0.05, 1.1 * max(freq)], hardcopy='tmp.eps')