0

I wrote a program in Python that calculates a random walk for an array given certain parameters.

I presume the "random" function is working, since I get the output " (I am using iPython). However, I cannot plot the function on a graph, because the float() argument must be a string or number, not a function. Nevertheless, given the recursive nature of the variables in question, it needs to be defined as a function for the program to run.

How can I graph this?

1

1 Answer 1

1

How about this?

randomnumbers = np.random.normal((mu*(price/10)), (v*(price/10)), days) #This variable generates a series of random numbers based on the normal distribution and in accordance with the return and volatility specified.
randomwalk    = [simAssetPrice[i-1] + randomnumbers[i] + k for i in range(1,days)]

plt.plot(randomwalk)
plt.ylabel('Price')
plt.show()

I had to use one more random number then you specified.

In place of your randomwalk() function, I've used a list comprehension.

Using the same names for variables as for functions is bound to mess things up. Also, since you wrote:

randomwalk
plt.plot(randomwalk)

You were referring to a "pointer" to the function. You need to write randomwalk(simAssetPrice, randomnumbers) in order to call the function.

You may want to review how functions work in Python.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks Richard. It worked in graphing the plot. However, the reason I defined it as a function was because it wasn't generating a random walk. In other words, the array was scattered around the straight-line return in a zig-zag fashion, because the calculation is not iterating from the previous variable (for price), and that still seems to be an issue. Could you assist in any way?
I'm afraid I don't follow. What are the axes you are expecting on your graph?
Essentially, I am looking to generate a random walk for the stock price (with the x axis being number of days and y being the price). However, instead of a random walk, what is happening is that the stock price is simply oscillating up and down in a zig-zag fashion (imgur.com/nGaAcq2). This is because the array generated is not iteratively calculating the next day's price because we are simply adding randomness to the entire array.
Never mind, I solved it by redefining the price variable to include the random element right off the bat, thus saving the need for a recursive function.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.