代写 C++ python MFE C++ – Assignment 3

MFE C++ – Assignment 3
Due Jan 7, 2020 @ 6pm PST
Please include a single file (MS Word, pdf, etc.) with your answers to the questions. Screenshots are OK as long as they show ALL of the output requested. For each question, please include your code as part of the Word/PDF file. We recommend creating 2 separate projects, one for each question.
This assignment is worth 30 points or 15% of points available in the course.
The derivative in Q1 has been corrected.
MFE C++ 2020 – Assignment 3 1
This document is authorized for use by Conghui Wei, from 12/20/2019 to 4/30/2020, in the course: MFE: C++ Preprogram – Klein (Spring 2020), University of California, Berkeley.
Any unauthorized use or reproduction of this document is strictly prohibited*.

Q1: Root-finding methods
For this question, it is fine to use the code provided in class as your starting point. You will need make changes to the functions.
For the following exercise, we are going to use the polynomial:
𝑓(𝑥)=(𝑥+3)(𝑥−3)(𝑥−5)(𝑥+5)=𝑥4 −34𝑥2 +225 𝑓′(𝑥) = 4𝑥3 − 68𝑥
Clearly, the function has 4 roots: -5, -3, 3 and 5.
a. Implement a function double f(double x)that returns the polynomial value and a
function double fPrime(x) that returns the derivative of the polynomial.
b. Implement double findRootNewton(double x, int iterations)that implements the Newton Raphson root finding method for starting point x and iterating iterations times or until |f(x)| < 1e-10. That is, we searching for a root but will stop if the max number of iterations is reached or we hit a precision of 1e-10. c. Implement double findRootSecant(double x, int iterations)that implements the secant root finding method for starting point x and iterating iterations times For your second starting point, use x + 0.25. Note: your code should check whether f(x_prev) – f(x) is essentially zero. For our purposes, you can use the fabs() function for the difference and check whether it is less than 1e-10. Hint: look at the lecture notes and the IsZero() function ;-) d. Implement double findRootBisection(double x, int iterations)that implements the bisection root finding method for starting point x For your second starting point, use -6 if f(x) < 0 and 4 if f(x) > 0. Note: the bisection function in the lecture notes uses two parameters, a & b, to define starting points. For the homework, only one starting point is passed in as a parameter.
MFE C++ 2020 – Assignment 3 2
This document is authorized for use by Conghui Wei, from 12/20/2019 to 4/30/2020, in the course: MFE: C++ Preprogram – Klein (Spring 2020), University of California, Berkeley.
Any unauthorized use or reproduction of this document is strictly prohibited*.

e. Create a program whose main() function calls each root finding function with starting points: -12, 4.3, -2.2 and 8 and iterations=5.
Within each root-finding function, print out:
1. The estimated root r
2. The value of f(r)
Include these values in your homework text file. Hint: you may want to create a function that takes starting point x and iterations as parameters and then calls the root- finding methods and prints out results.
f. Repeat step e but use iterations=20
g. Repeat step e but use iterations=50
MFE C++ 2020 – Assignment 3 3
This document is authorized for use by Conghui Wei, from 12/20/2019 to 4/30/2020, in the course: MFE: C++ Preprogram – Klein (Spring 2020), University of California, Berkeley.
Any unauthorized use or reproduction of this document is strictly prohibited*.

Q2: Simulation – Comparing Trading Strategies
For this question we are going to compare three simple trading strategies:
1. Buy and Hold
2. Long Call Options and Treasuries
3. Put-Write
We are going to simulate 1 year equity returns and analyze the difference in performance for each strategy. Below, we refer to spot0 as the current price of the stock and spot1 as the price of the stock after 1 year.
We ask you to store the portfolio value for each of the strategies.
Buy and Hold:
We buy 1 share of equity and hold it for one year. Our profit or loss is exactly the return of the equity. For example, assume we hold 1 share of a stock that has a value of $100 at time 0. If, after one year, the stock price is $110, then our portfolio value is $110.
Long Call Options
We buy 1 at-the money European call option and hold the rest of our cash as a risk-free Treasury note, both for one year. Assume the call option has strike price K == spot0. Also the premium (price) of the call option is P and the interest rate for cash holdings (Treasuries) is r.
Then, the value of our portfolio after 1 year is: cash + interest earned on cash + call option exercise value.
𝑝𝑜𝑟𝑡𝑓𝑜𝑙𝑖𝑜 𝑣𝑎𝑙𝑢𝑒 = (1 + 𝑟) ∗ (𝑠𝑝𝑜𝑡0 − 𝑃) + max(0, 𝑠𝑝𝑜𝑡1 − 𝑠𝑝𝑜𝑡0)
Put-Write
We hold spot0 dollars and sell 1 put option with premium P and with strike K = spot0 (note: in practice, a put-write strategy would generally have K < spot). The value of our portfolio after one year is: premium + dollars + interest earned – put option exercise value. 𝑝𝑜𝑟𝑡𝑓𝑜𝑙𝑖𝑜 𝑣𝑎𝑙𝑢𝑒 = (1 + 𝑟) ∗ (𝑃 + 𝑠𝑝𝑜𝑡0) − max(0, 𝑠𝑝𝑜𝑡0 − 𝑠𝑝𝑜𝑡1) Notice that we don’t hold any of the stock and that our profit is capped at (1 + 𝑟) ∗ (𝑃 + 𝑠𝑝𝑜𝑡0) MFE C++ 2020 – Assignment 3 4 This document is authorized for use by Conghui Wei, from 12/20/2019 to 4/30/2020, in the course: MFE: C++ Preprogram - Klein (Spring 2020), University of California, Berkeley. Any unauthorized use or reproduction of this document is strictly prohibited*. Simulation Assume spot0 is $100. We will use simulation to generate a distribution of potential spot1’s. We simulate the stock’s stochastic process as: 𝑑𝑆 = 𝜇𝑑𝑡 + 𝜎𝑑𝑍 𝑆 After solving using Ito’s Lemma and with our horizon as 1 year, we want to generate log returns. In our case dt = 1 and sqrt(dt) = sqrt(1) = 1 We will do the following: 1. Simulate a normal return 2. Take the exponential of that return 3. Multiply the exponential by the original spot to calculate spot1 We’ll label the equity normal return as s 𝑠 = (𝜇 − 1 𝜎2) + 𝜎𝑍 2 where Z is a standard normal variable. Refer to Lecture 6 for sample code to generate lognormal equity price paths. We then can estimate our stock price as 𝑠𝑝𝑜𝑡1 = 𝑠𝑝𝑜𝑡0𝑒𝑠 = 100 ∗ 𝑒𝑠 To calculate the arithmetic returns, use the following formula: 𝑎=100∗𝑒𝑠 −100=𝑒𝑠 −1 100 MFE C++ 2020 – Assignment 3 5 This document is authorized for use by Conghui Wei, from 12/20/2019 to 4/30/2020, in the course: MFE: C++ Preprogram - Klein (Spring 2020), University of California, Berkeley. Any unauthorized use or reproduction of this document is strictly prohibited*. Assignment Tasks i) Create a class StrategyAnalyzer that will hold our simulation results and perform analysis on them. ii) Add a public constructor StrategyAnalyzer(int numSimulations, double mu, double sigma, double r, double P_call, double P_put) where: a. numSimulations is the number of simulations to perform b. mu is the expected return of the stock c. sigma is the stock’s annualized volatility d. r is the risk-free rate e. P_call is the premium of the call f. P_put is the premium received for the put The constructor should store each of these values in a property. Also, it should dynamically create three arrays of type double with lengths num_simulations to hold the buy and hold, long call and long put strategy arithmetic returns (not portfolio values!). We’ll cover dynamic array construction in the next lecture. To create the arrays, you can use code similar to: my_array = new double[num_simulations]; Finally, the constructor should calculate s (as shown above) and run the simulation num_simulation times, recording strategy portfolio arithmetic returns in the arrays. Refer to Lecture 5&6 slides for how to create normal pseudorandom numbers. Seed your random number generator with the current time (time(0)). Remember, spot0 is $100 and the strike prices for both options is also $100. iii) In StrategyAnalyzer, create a public method: void writeResults(const std::string& fileName) which writes the simulation results as a CSV file with the following header: BuyAndHold, LongCall, PutWrite For example, assume our numSimulations is 100, then writeResults() will create a file with 101 lines: 1 line for the header and 100 lines of simulation results. Refer to the final slides of Lecture 6 for an example of writing a CSV file (with header X,Y,Z). MFE C++ 2020 – Assignment 3 6 This document is authorized for use by Conghui Wei, from 12/20/2019 to 4/30/2020, in the course: MFE: C++ Preprogram - Klein (Spring 2020), University of California, Berkeley. Any unauthorized use or reproduction of this document is strictly prohibited*. iv) In StrategyAnalyzer, create a method double calcStdDev(double* arr, int arrLen) which calculates the standard deviation of the array arr up to length arrLen Use the following formula for standard deviation: 1𝑁 𝑠𝑑 = √𝑁 − 1 ∑(𝑥 − 𝑥̅)2 𝑖=1 Check out https://en.wikipedia.org/wiki/Standard_deviation for more information. v) In StrategyAnalyzer, create a public method analyzeReturns() which prints out the following information for each strategy to the console (cout): a. Expected return : the average (mean) of the arithmetic returns for the strategy b. Volatility : the standard deviation of the strategy c. Sharpe Ratio : 𝐸𝑥𝑝𝑒𝑐𝑡𝑒𝑑 𝑅𝑒𝑡𝑢𝑟𝑛 𝑉𝑜𝑙𝑎𝑡𝑖𝑙𝑖𝑡𝑦 vi) Use the following main() function to test your class. You may want to add other lines of code to the main function to debug your work. Please make sure the following lines are in there. int main() { } StrategyAnalyzer sa1(1000000, 0.08, 0.16, 0.02, 7.34, 5.38); sa1.analyzeReturns(); StrategyAnalyzer sa2(1000, 0.08, 0.16, 0.02, 7.34, 5.38); sa2.writeResults("strategy.csv"); return 0; MFE C++ 2020 – Assignment 3 7 This document is authorized for use by Conghui Wei, from 12/20/2019 to 4/30/2020, in the course: MFE: C++ Preprogram - Klein (Spring 2020), University of California, Berkeley. Any unauthorized use or reproduction of this document is strictly prohibited*. vii) In Excel, R, Python or some other tool (not C++), create two scatter plots using the strategy.csv file created by running your program. The X-axis should be BuyAndHold and the Y-axes should be BuyCall and PutWrite respectively. You’ll need to take a screen shot or use some other method to submit these charts with your assignment. What do you notice about the two charts? viii) The Sharpe ratio indicates “return” for one unit of “risk” (volatility). Given the trade-off between risk and return, which strategy (or strategies) is preferable for risk-averse investors (investors who do not like risk)? Which strategy (or strategies) is preferable for risk-seeking investors (investors who care more about returns than volatility)? ix) Submit your code and an answer file with your charts and what analyzeReturns prints to the console. It is ok to do this in a single Word or PDF document. DO NOT SUBMIT YOUR CSV FILE. MFE C++ 2020 – Assignment 3 8 This document is authorized for use by Conghui Wei, from 12/20/2019 to 4/30/2020, in the course: MFE: C++ Preprogram - Klein (Spring 2020), University of California, Berkeley. Any unauthorized use or reproduction of this document is strictly prohibited*.