The Sharpe Ratio Shiny app structure should feel familiar but have a quick look at the final app in Figure $7.6$ and notice a few differences from our usual:
Because the Sharpe Ratio is best understood by comparison we chart the rolling Sharpe Ratio of our portfolio alongside that of the $S \& P 500$, plus we have added two blue value boxes. That means we need to calculate the rolling and overall Sharpe for the S\&P500 based on whatever starting date the user selects.
There are several calculations for this app and I divide them into market calculations and portfolio calculations.
In the chunks below, we run our market Sharpe Ratio equations, relying on the user-selected RFR, rolling window and starting date. The code flow runs through three reactives: market_returns (), which is used to find the market_sharpe() and the market_rolling_sharpe().
First, we get the RFR, rolling window and market returns.
统计代写|r语言作业代写代考|CAPM
By way of extraordinarily brief background, the Capital Asset Pricing Model (CAPM) is a model, created by William Sharpe, that estimates the return of an asset based on the return of the market and the asset’s linear relationship to the return of the market. That linear relationship is the stock’s beta coefficient. Beta can be thought of as the stock’s sensitivity to the market, or its riskiness with respect to the market.
CAPM was introduced back in 1964, garnered a Nobel for its creator and, like many epoch-altering theories, has been widely used, updated, criticized, debunked, revived, re-debunked, etc. Fama and French have written that CAPM “is the centerpiece of MBA investment courses. Indeed, it is often the only asset pricing model taught in these courses… [u]nfortunately, the empirical record of the model is poor.”1
Nevertheless, we will forge ahead with our analysis because calculating CAPM betas can serve as a nice template for more complex models. Plus, CAPM is still an iconic model. We will focus on one particular aspect of CAPM: beta. Beta, as we noted above, is the beta coefficient of an asset that results from regressing the returns of that asset on market returns. It captures the linear relationship between the asset and the market. For our purposes, it’s a good vehicle for exploring a reproducible flow for modeling or regressing our portfolio returns on the market returns. Even if your team prefers more nuanced models, this workflow can serve as a good base.
统计代写|r语言作业代写代考|CAPM and Market Returns
Our first step is to make a choice about which asset to use as a proxy for the market return and we will go with the SPY ETF, effectively treating the S\&P500 as the market. That makes our calculations substantively uninteresting because (1) SPY is $25 \%$ of our portfolio and (2) we have chosen assets and
a time period $(2013-2017)$ in which correlations with SPY have been high. With those caveats in mind, feel free to choose a different asset for the market return and try to reproduce this work, or construct a different portfolio that does not include SPY.
We first import prices for SPY, calculate monthly returns and save the object as market_returns_xts.We also want a tibble object of market returns for when we use the tidyverse.Since we will be regressing portfolio returns on market returns, let’s ensure that the number of portfolio returns observations is equal to the number of market returns observations.
Portfolio beta is equal to the covariance of the portfolio returns and market returns, divided by the variance of market returns. Here is the equation:
$$
\beta_{\text {portfolio }}=\operatorname{cov}\left(R_{p}, R_{m}\right) / \sigma_{m}
$$
We calculate covariance of portfolio and market returns with cov(), and the variance of market returns with var().
Our portfolio beta is equal to:
We can also calculate portfolio beta by finding the beta of each of our assets and then multiplying by asset weights. That is, another equation for portfolio beta is the weighted sum of the asset betas:
$$
\beta_{\text {portfolio }}=\sum_{i=1}^{n} W_{i} \beta_{i}
$$
We first find the beta for each of our assets and this affords an opportunity to introduce a code flow for regression analysis.
We need to regress each of our individual asset returns on the market return and use the $\operatorname{lm}$ () function for that purpose. We could do that for asset 1 with
Im(asset_return_1 market_returns_tidy\$returns), and then again for asset 2 with $1 \mathrm{~m}$ (asset_return_2 market_returns_tidy $\$$ returns) etc. for all 5 of our assets. But if we had a 50 -asset portfolio, that would be impractical. Instead we write a code flow and use map () to regress each of our asset returns on market returns with one call.
We will start with our asset_returns_long tidy data frame and will then run nest (-asset).