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 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 findyield. 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
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 findyield function on the run you created in Part A printing the result. If everything is correct, you should get
as the result. Note that this is quite a bit different from the expected 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 simulated runs.
In a new cell, use a loop to create simulated runs for a stock with a current price of an expected annual yield of
and an annual volatility of over a period of days. Plot all runs in the same line plot. For each run, use findyield
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 pltplot.
Create a markdown cell to explain that the next cell will display the yields for the 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
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
