Question: Part B: Annual Yield Function In this section, we will create and test a function that takes a simulated run as its input, and calculates

Part B: Annual Yield Function
In this section, we will create and test a function that takes a simulated run as its input, and calculates the annual yield
during for that particular run.
Create a markdown cell that displays a level 2 header that reads: "Part B: Annual Yield Function". Also add some text
briefly describing the purpose of your code in this part.
Write a function named find_yield. This function should accept a single parameter called run, which is expected to be
an array of simulated daily prices for a stock. The function should return the annual yield for the stock over the simulated
period, rounded to four decimal places.
The formula for calculating the annual yield is as follows: annual yield = ln (
Final Price
Initial Price)
252
Days in Run
. You can use the
function math.log in the math package to calculate the natural logarithm.
Recall that the number of days in a run is equal to one less than the length of the run since the run contains the starting
price.
Create a markdown cell that explains you are about to test your function by running it on the previously simulated run.
Call the find_yield function on the run you created in Part A, printing the result. If everything is correct, you should get
0.1196 as the result. Note that this is quite a bit different from the expected 8% annual yield that we used to simulate this
run. This is a result of the randomness involved in our simulation.
Create a markdown cell to explain that the next cell will explore the potential variability in our simulated runs by creating
and plotting 25 simulated runs.
In a new cell, use a loop to create 25 simulated runs for a stock with a current price of 100, an expected annual yield of 6%,
and an annual volatility of 0.4 over a period of 200 days. Plot all 25 runs in the same line plot. For each run, use find_yield
to calculate the annual yield for the run, and store the value in a list. You should see a wide range of simulated results.
Hint: Each iteration of your loop should do three things: Simulate a run, calculate the yield for that run and add the result to
a list, and add a line plot of the run to a figure using plt.plot.
Create a markdown cell to explain that the next cell will display the yields for the 25 simulated runs.
Print the list of annual yields created in the previous code cell. Again, you will likely see a wide range of results.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!