### SDGB 7844 HW 5: Portfolio Optimization

代写homework | R代写 – 这道题目是利用R语言相关的代写任务

#### Instructor: Prof. Nagaraja

#### Due: 12/

Submit two files through Blackboard: (a) .Rmd R Markdown file with answers and code and (b) Word document of knitted R Markdown file. Your file should be named as follows: HW5-[Full Name]-[Class Time] and include those details in the body of your file.

Complete your work individually and comment your code for full credit. For an example of how to format your homework see the files related to the Lecture 1 Exercises and the RMarkdown examples on Blackboard. Show all of your code in the knitted Word document.

Given the uncertainty about the future performance of financial markets, investors typically diversify their portfol ios to improve the quality of their returns. In this assignment, you will be constructing a portfolio composed of two ETFs (exchange traded funds) that tracks US equity and fixed income markets. The equity ETF tracks the widely followed S&P 500 while the fixed income ETF tracks long-term US Treasury bonds. You will use optimization techniques to determine what fraction of your money should be allotted to each asset (i.e., portfolio weights).

The Sharpe ratio is a widely used metric to gauge the quality of portfolio returns. It was developed by William F. Sharpe (a Nobel prize winner) and provides one way to construct an optimal portfolio. This computation allows investors to determine expected returns while taking into account a measure of how risky that investment is. Heuristically, it is computed by taking the expected return of an asset/portfolio, subtracting the risk free rate (i.e., how much you would make if you simply kept your money in the bank and let it collect interest) and dividing by the standard deviation of the portfolio returns. The higher the Sharpe ratio value, the better the investment is considered to be. This formula assumes that returns are normally distributed (not always the best assumption to make with financial data).

##### 1

The data for the two assets were downloaded from Google Finance (ticker symbols: SPY, TLT) and the federal funds interest rate, representing the risk free rate, was taken from the U.S. Federal Reserve Bank website. We will be looking at weekly returns, as opposed to daily returns, as they are less correlated with each other; monthly returns would be even less correlated, however, we would need a much longer time period of data to have enough observations to do a robust analysis. Note that if we used a different time period to compute our optimal rates, or we used daily instead of weekly returns, we may get very different results.

- Upload the data in assetdata.txt into R and call the tibbledata.x. The columns are defined as follows:date,close.spyis the closing price for the S&P500 ETF,close.tlt is the closing price for the long term treasury bond ETF, andfed.rateis the federal funds interest rate in percentform. Look at the data type for the columndate; just like the data types numeric, logical, etc., R has one for date/time data.Extract only the observations where the federal funds rate is available (so you are left with weekly data); this is the data you will use for the rest of the analysis.What is the start date and end date of this reduced data set? Graph the federal funds interest rate as a time series. Describe what you see in the plot and relate it briefly to the most recent financial crisis.
- Now we will split the data into training and test sets. The training data will be used to compute our portfolio weights and our test set will be used to evaluate our portfolio. Make two separate tibbles: (a) the training set should contain all observations before 2014 and (b) the test set should contain all observations in 2014. (Install and load the R packagelubridateand use the functionyearto extract the year from thedatecolumn of your data setdata.x.) How many observations are in each subset?
- The federal funds interest rate is in percent form so convert it to decimal (i.e., fractional) form. Then, for the S&P 500 and long term treasury bonds ETF assets, compute the returns using the following formula:

```
rt =
```

```
ptpt 1
pt 1
```

```
wherertis the return at timet,ptis the asset price at timet, andpt 1 is the asset price
at timet1 (i.e., the previous period). Add both sets of returns to your training set
tibble. These returns are also called total returns. Construct a single time series plot
with the returns for both assets plotted. Add a dotted, horizontal line aty= 0 to the
plot. Compare the two returns series. What do you see?
```

- The Sharpe ratio calculation assumes that returns are normally distributed^1. Construct

(^1) We will continue with our analysis regardless of whether this assumption is satisfied.

```
two normal quantile plots, one for training setreturns of each asset. Is this assumption
satisfied? Justify your answer.
```

- Compute the correlation between the S&P500 and long term treasury bond returns in the training setand interpret it^2. Now, we will compute a rolling-window correlation as follows: compute the correlation between the two asset returns only using the first 24 weeks of data (i.e., weeks 2 to 25), next compute the correlation between the two asset returns for data from week 3 through 26, then week 4 through 27, and so forth. Once you compute the rolling-window correlations, make a time series plot of the rolling- window correlation with each point plotted on the last day of the window. Add a horizontal, dotted, gray line at 0 to your plot. Is the correlation or rolling-window correlation a better way to describe the relationship between these two assets? Justify your answer.
- Compute the Sharpe ratios^3 for each asset on the training setas follows:

```
Step 0. Letrtbe the return andytbe the federal funds interest rate for weekt= 1,...,T.
Step 1. Compute the excess returns,et, for each week in the data set^4 :
```

```
et = rt
```

```
yt 1
52
Excess returns are returns that you earn in excess to the risk free rate.
Step 2. Convert the excess returns into an excess returns index,gt:
```

```
g 1 = 100
gt = gt 1 (1 +et)
```

```
Step 3. Compute the number of years of data,n, by taking the number of weeks for which
you havereturns (i.e., number of observations in your training set minus 1) and
dividing by 52 (since there are 52 weeks in a year); therefore the number of years
of data can be a fractional amount.
Step 4. Compute the compounded annual growth rate, CAGR:
```

##### CAGR =

##### (

```
gT
g 1
```

```
) 1 /n
1
```

```
Step 5. Compute the annualized volatility,:
```

```
=
```

```
52 SD[et]
```

```
whereSD[et] is the standard deviation of the excess returns.
```

(^2) If you construct a scatterplot of these two particular assets, you will get a linear relationship. (^3) The Sharpe ratio can be positive or negative. (^4) We divide the federal funds interest rate by 52, the number of weeks in a year, to turn it into a weekly rate from an annual rate.

```
Step 6. Compute the Sharpe Ratio, SR, which is the ratio of the compounded annual
growth rate and the annualized volatility:
```

```
SR =
```

##### CAGR

```
Which asset is a better investment? Justify your answer.
```

- Write a function which takes the following inputs: (a) a vector of portfolio weights (call this argumentx; weights are between 0 and 1), (b) a vector of returns for asset 1, (c) a vector of returns for asset 2, and (d) a vector of the corresponding weekly federal funds interest rates. The function will then do the following: for each weight value in your vectorx, you will compute the Sharpe ratio for the corresponding portfolio. To obtain the returns for the portfolio, use the following equation^5 :

```
rt,portf olio = xrt,S&P 500 + (1x)rt,treasury
```

```
That is,xproportion of the funds will be invested in the S&P500 ETF and (1x)
proportion of the funds will be invested into the treasury bond ETF. After you compute
the returns for the portfolio, apply the steps in question 6 to get the Sharpe ratio for that
portfolio. Your function should output a vector of Sharpe ratios, one for each portfolio
weight inx.
```

```
Usestatfunction()to plot the function you just wrote. Weights between 0 and 1
should be on thex-axis and the Sharpe ration should be on they-axis. The training set
data should be used as the input for (b), (c), and (d) above. Do you see a portfolio
weight that produces the maximum Sharpe ratio?
```

- Using the training set, useoptimize()to determine the optimum weight for each asset using the function you wrote in question 7; how much of the funds should be allocated to each asset? What is the Sharpe ratio of the overall portfolio? According to your analysis, is it better to invest in S&P500 only, long term treasury bonds only, or your combined portfolio? Justify your answer.
- For the remainder of this assignment, we will be evaluating our portfolio using the test set data. We will be comparing three strategies: investing only in the S&P500, investing only in long term treasury bonds, and investing in the combined portfolio (computed in question 8).

```
In your test set, convert the federal funds interest rate from percent to decimal form and
compute the returns series for each of the three assets (see question 3 for more details).
Next, compute the excess returns index for each asset in the test set (as outlined in
question 6). Plot the excess returns index for each asset on the same time series plot.
Add a dotted, horizontal line aty= 100. Describe what you see.
```

(^5) Note to people with a finance background: these computations correspond to a setting where you are rebalancing the portfolio daily.

- The excess returns index can be interpreted as follows: if you invested in $100 in at time t= 1, the index value at timeT represents how much you have earned in addition to (i.e., in excess of) the risk free interest rate. If you invested $100 in each asset (portfolio, all in long term treasury bonds, or all in S&P500) in the first week of January, 2014 , how much would you have at the end of the test setperiod for each asset in addition to the risk-free interest rate? Did your portfolio perform well in the test set? Justify your answer.