I am not sure how to even proceed or where I went wrong, I am getting a large array
This is the instruction
Create a markdown cell that displays a level 2 header that reads: "Part D: Monte Carlo Simulation". Also add some text briefly describing the purpose of your code in this part. Write a function named monte_carlo. The function should accept five parameters: start, rate, vol, days, and num_runs. The function should use a loop to generate a number of simulated stock runs equal to num_runs. The characteristics of the runs are provided by the parameters start, rate, vol, and days. A detailed description of this function is provided below. Each time you loop executes, the following steps should be performed:
Create a markdown cell to explain that you are about to test the function by running a Monte Carlo simulation with a specific seed. Set a seed of 1, and run a Monte Carlo simulation consisting of 10,000 simulated runs for a stock with a current price of 200, an expected annual return of 10%, and a volatility of 0.4. Each run should be over a period of 500 days. Create a histogram of the final prices. Use bins=np.arange(0, 1600, 50), and set the edgecolor to black. Set the size of the figure to be [10,5]. If your code is correct, your histogram should have a peak around 200 and should have a long tail trailing off to the right. This shows that the majority of the simulated final prices are near 200, but there are some very large outliers.
Create a markdown cell to explain that you are about to display the 10th, 25th, 50th, 75th, and 90th percentiles of the simulated final prices. Use np.percentile to calculate the 10th, 25th, 50th, 75th, and 90th percentiles for the final prices in the simulated runs generated for the stock. Display the results by creating five lines of output, with each line using the following format: __th percentile: _____ Round the display percentiles to 2 decimal places. If done correctly, you should get a 10th percentile of 118.05 and a 90th percentile of 505.91.
def monte_carlo(start, rate, vol, days, num_runs):
final_prices =[]
annual_yields =[]
for runs in range(0, num_runs):
runs = simulate_stock(start, rate, vol, days)
y = runs[-1]
final_prices.append(y)
z = find_yield(runs)
annual_yields.append(z)
np.asarray(final_prices)
np.asarray(annual_yields)
return final_prices, annual_yields
np.random.seed(1)
final_prices, annual_yields = monte_carlo(200, 0.1, 0.4, 500, 10000)
plt.figure(figsize=[10, 5])
plt.hist(final_prices, bins=np.arange(0, 1600, 50), edgecolor='black')
np.asarray
doesn't change the value in place, but returns a result. You are converting the two values into a numpy array and then throwing away the result. So you want something like
return np.asarray(final_prices), np.asarray(annual_yields)
As a second minor issue, you're using runs
both as the count of which run this is and as the return value from simulate_stock
. Much more Pythonic would be to write:
for _ in range(0, num_runs)
since you don't really care which run number it is in your calculations. If you think you will need this value later:
for run_number in range(0, num_runs)