lundi 2 mai 2016

Conducting a Monte Carlo simulation in Python?

I am trying to run a stock simulation in Python, whereby I graph a random walk for a stock price given a set level of return and volatility.

However, I am having a problem in that I am not sure how to iterate so that 250 different random numbers are generated to plug into the s1 formula. e.g. I have defined a random number x, but since it is only one random number, Python will then generate a graph of a straight line since the formula is only taking mu(return) into account. The below is my code, with the hashtag indicating where I have inputted new code (this is a modification on a previous program which would only graph a straight-line return without randomness):

from __future__ import division
from random import random
import numpy as np
import matplotlib.pyplot as plt

#ITERATIONS = 250
#datapoints = 0

#for i in range (1, ITERATIONS):
#datapoints += 1
#x = random()
#x

mu = 0.1
v = 0.06
s0 = 500
k = mu-(v**2)/2

t=np.arange(0,250)

s1=s0*np.exp(k*t/250) #+(v*datapoints)*np.sqrt(t/250)
s1

plt.figure(figsize=(10, 6), dpi=80)

plt.plot(t,s1)

plt.xlim(0.0, 250.0)
plt.xticks(np.linspace(0, 250, 11, endpoint=True))
plt.xlabel(‘Trading Days’)
plt.ylabel(‘Price’)

plt.title(r’Stock Returns (Linear) – 250 Trading Days’)

plt.show()

The codes with the hashtag symbol is where I have made edits to the code in an attempt to get the program working. For 250 data points, the program needs 250 random numbers between 0 and 1 generated to be able to run a random process on the data. However, I am not sure where to go from here and would appreciate any advice.




Aucun commentaire:

Enregistrer un commentaire