(Solved) : 4 Monte Carlo Simulation Gain Insight Nature Bernoulli Distribution Monte Carlo Simulation Q44108500 . . .
4. Monte Carlo simulation. We can gain more insight into the nature of the Bernoulli distribution by doing a Monte Carlo simulation, that is, by using a computer to “flip coins” and average over many measurements. In the context of random walks, we can implement a N-step walk with the following python code import random import numpy as np def simulate_random_walk_length(n, p): # N is the number of steps # p is the probability that it moves to the right # (so 1-p is the probability that it moves to the left) # x holds the position of the walker at a given time x = 0 for i in range (N): rnd = random.random() if rnd <= p: x = x + 1 else: x = x – 1 return x def get_distribution (nsims, N, p): # nsims is the number of times you want to simulate a random walk simulations = [] for sim in range (nsims): simulations.append(simulate_random_walk_length(N=N, p=p)) return np.array(simulations) distributions = get distribution (nsims=10, N=4, p=0.5) print(‘simulate random walks lengths:’, distributions) print(‘average’, np.mean( distributions ) ) random.random() generates a random number between zero and one. The quantity x is the net displacement assuming that the steps are of unit length. If you run the code verbatim (but be careful with the tabs and spaces, these are important in python) it will print the individual results of as many simulations (nsims) of the lengths of random walk as you specify. Compute P(x). Essentially this is a histogram created using the values printed by the program. First choose N = 4 and p = and simulate the results 20 times. This is a small enough value that you can count and generate the histogram manually, i.e. draw it. Use the program to check the behavior as the number of steps and the number of simulations increases. If you can write some code to count the values and generate the histogram automatically, please compare the shape of the distribution with 1000 simulations and with 1000000 simulations. You can look at the code for Problem 7 for ideas. (Based on GT 3.36) Show transcribed image text 4. Monte Carlo simulation. We can gain more insight into the nature of the Bernoulli distribution by doing a Monte Carlo simulation, that is, by using a computer to “flip coins” and average over many measurements. In the context of random walks, we can implement a N-step walk with the following python code import random import numpy as np def simulate_random_walk_length(n, p): # N is the number of steps # p is the probability that it moves to the right # (so 1-p is the probability that it moves to the left) # x holds the position of the walker at a given time x = 0 for i in range (N): rnd = random.random() if rnd
Expert Answer
Answer to 4. Monte Carlo simulation. We can gain more insight into the nature of the Bernoulli distribution by doing a Monte Carlo…
OR