Reputation: 105
I'm plotting Stock prices that evolve according to geometric Brownian motion. You don't need to understand the mathematics, I've taken care of it all. But, my plots are not what I want them to be. They are too bunched up together
and for some reason it's adding these straight lines which I think might be lines of best fit, but I can't see at all where it comes from my code.
Here is my python code. Any suggestions to help me distinguish the paths better, and get rid of those straight lines?
from scipy.stats import norm
from scipy import sqrt
import matplotlib.pyplot as plt
def Euler_disc(S0, mu, sigma, T, n):
times = [0]
stocks = [S0]
dt = ((float(T)/n))
for i in range(0, 10):
for x in range(0, n):
times.append(times[x] + dt)
stocks.append(stocks[x] + mu * stocks[x] * dt \
+ sigma * stocks[x] * sqrt(dt) * norm.rvs(0, 1, 1))
plt.plot(times, stocks)
Upvotes: 1
Views: 120
Reputation: 1675
You are reusing the times
and stocks
variable in each inner loop. So everytime you reach plt.plot(times, stocks)
, you will replot all your calculated data.
Here is the fixed version:
from scipy.stats import norm
from scipy import sqrt
import matplotlib.pyplot as plt
def Euler_disc(S0, mu, sigma, T, n):
dt = ((float(T)/n))
for i in range(0, 10):
times = [0]
stocks = [S0]
for x in range(0, n):
times.append(times[x] + dt)
stocks.append(stocks[x] + mu * stocks[x] * dt \
+ sigma * stocks[x] * sqrt(dt) * norm.rvs(0, 1, 1))
plt.plot(times, stocks)
Upvotes: 1