I try to sample a 1D random walk with 'n= 1000 steps' in time 't=1' in the x-y axis. For each step, the position of walker adds or decreases '(1/1000)**0.5'. I have a simple code but I do not know how let it be random adding or reducing '(1/1000)**0.5'. Thanks for your help.
import numpy as np
import matplotlib.pyplot as plt
# Generate 500 random steps with mean=0 and standard deviation=1
steps = np.random.normal(loc=0, scale=1.0, size=500)
# Set first element to 0 so that the first price will be the starting stock price
steps[0]=0
# Simulate stock prices, P with a starting price of 100
P = 100 + np.cumsum(steps)
# Plot the simulated stock prices
plt.plot(P)
plt.title("Simulated Random Walk")
plt.show()
Aucun commentaire:
Enregistrer un commentaire