Random Walk Part 2
Solution
First, create our data.
walk = [0]
for _ in range(200):
walk.append(randint(-1, 1)+walk[-1])
print(walk)
And now to display the random walk….
import matplotlib.pyplot as plt
plt.plot(walk)
plt.xlabel("Time")
plt.ylabel("Place")
plt.title("Random Walk")
plt.show()
Another type of random we can use is a random normal. What it does is give us a random normal z-score, this is going to be covered a little later so for now trust me, and run the below code.
import numpy as np
scores = [400+np.random.normal()*100 for _ in range(2000)]
plt.hist(scores,bins=20)
plt.show()
What we just plotted was a normal distribution. Let’s try and get the mean and standard deviation of our new plot! But first let’s define mean and standard deviation. The mean is the average value for the data set, the standard deviation is a statistical measure meant to measure the deviation of the data from the mean of the data. High standard deviation would mean a very wide graph, low standard deviation would mean a very close graph.
scores = np.array(scores)
print(scores.mean())
print(scores.std())
You will notice that the mean is very close to 400, the first number we put and the std is very close to 100, the second number we put. This is because the random.normal() function returns a random z-score with a mean z-score of 0 and a standard deviation of 1 (as it should be). When we add in 400, we adjust the mean upwards, and when we multiply the random number by 100 we expand the standard deviation.