library(tidyquant)
library(tidyverse)

Q1 Import stock prices and calculate monthly returns.

Hint: Import Apple, Google, Netflix, Microsoft and Tesla from “2015-01-01” to “2018-12-31”.

stock_returns_monthly <- c("AAPL", "GOOG", "NFLX", "MSFT", "TSLA") %>%
    tq_get(get  = "stock.prices",
           from = "2015-01-01",
           to   = "2018-12-31") %>%
    group_by(symbol) %>%
    tq_transmute(select     = adjusted, 
                 mutate_fun = periodReturn, 
                 period     = "monthly", 
                 col_rename = "Ra")
stock_returns_monthly 
## # A tibble: 240 x 3
## # Groups:   symbol [5]
##    symbol date             Ra
##    <chr>  <date>        <dbl>
##  1 AAPL   2015-01-30  0.0716 
##  2 AAPL   2015-02-27  0.101  
##  3 AAPL   2015-03-31 -0.0314 
##  4 AAPL   2015-04-30  0.00579
##  5 AAPL   2015-05-29  0.0453 
##  6 AAPL   2015-06-30 -0.0372 
##  7 AAPL   2015-07-31 -0.0329 
##  8 AAPL   2015-08-31 -0.0662 
##  9 AAPL   2015-09-30 -0.0218 
## 10 AAPL   2015-10-30  0.0834 
## # ... with 230 more rows

Q2 Import S&P500 as a baseline index fund and calculate monthly returns.

baseline_returns_monthly <- "^GSPC" %>%
    tq_get(get  = "stock.prices",
           from = "2015-01-01",
           to   = "2018-12-31") %>%
    tq_transmute(select     = adjusted, 
                 mutate_fun = periodReturn, 
                 period     = "monthly", 
                 col_rename = "Rb")
baseline_returns_monthly
## # A tibble: 48 x 2
##    date             Rb
##    <date>        <dbl>
##  1 2015-01-30 -0.0307 
##  2 2015-02-27  0.0549 
##  3 2015-03-31 -0.0174 
##  4 2015-04-30  0.00852
##  5 2015-05-29  0.0105 
##  6 2015-06-30 -0.0210 
##  7 2015-07-31  0.0197 
##  8 2015-08-31 -0.0626 
##  9 2015-09-30 -0.0264 
## 10 2015-10-30  0.0830 
## # ... with 38 more rows

Q3 Aggregate three Portfolios with the following weighting schemes.

stock_returns_monthly_multi <- stock_returns_monthly %>%
    tq_repeat_df(n = 3)
stock_returns_monthly_multi
## # A tibble: 720 x 4
## # Groups:   portfolio [3]
##    portfolio symbol date             Ra
##        <int> <chr>  <date>        <dbl>
##  1         1 AAPL   2015-01-30  0.0716 
##  2         1 AAPL   2015-02-27  0.101  
##  3         1 AAPL   2015-03-31 -0.0314 
##  4         1 AAPL   2015-04-30  0.00579
##  5         1 AAPL   2015-05-29  0.0453 
##  6         1 AAPL   2015-06-30 -0.0372 
##  7         1 AAPL   2015-07-31 -0.0329 
##  8         1 AAPL   2015-08-31 -0.0662 
##  9         1 AAPL   2015-09-30 -0.0218 
## 10         1 AAPL   2015-10-30  0.0834 
## # ... with 710 more rows
weights <- c(
    0.40, 0.40, 0.40,0.40, 0.60,
    0.40, 0.40, 0.40, 0.60, 0.40,
    0.40, 0.40, 0.60, 0.40, 0.40
)
stocks <- c("AAPL", "GOOG", "NFLX","MSFT", "TSLA")
weights_table <-  tibble(stocks) %>%
    tq_repeat_df(n = 3) %>%
    bind_cols(tibble(weights)) %>%
    group_by(portfolio)
weights_table
## # A tibble: 15 x 3
## # Groups:   portfolio [3]
##    portfolio stocks weights
##        <int> <chr>    <dbl>
##  1         1 AAPL       0.4
##  2         1 GOOG       0.4
##  3         1 NFLX       0.4
##  4         1 MSFT       0.4
##  5         1 TSLA       0.6
##  6         2 AAPL       0.4
##  7         2 GOOG       0.4
##  8         2 NFLX       0.4
##  9         2 MSFT       0.6
## 10         2 TSLA       0.4
## 11         3 AAPL       0.4
## 12         3 GOOG       0.4
## 13         3 NFLX       0.6
## 14         3 MSFT       0.4
## 15         3 TSLA       0.4

Q4 What is the portfolio’s return on the first period? Explain how it’s computed.

portfolio_returns_monthly_multi  <-
  stock_returns_monthly_multi %>%
    tq_portfolio(assets_col  = symbol, 
                 returns_col = Ra, 
                 weights     = weights_table, 
                 col_rename  = "Ra")
portfolio_returns_monthly_multi 
## # A tibble: 144 x 3
## # Groups:   portfolio [3]
##    portfolio date            Ra
##        <int> <date>       <dbl>
##  1         1 2015-01-30  1.25  
##  2         1 2015-02-27  0.0583
##  3         1 2015-03-31 -0.0660
##  4         1 2015-04-30  0.148 
##  5         1 2015-05-29  0.0586
##  6         1 2015-06-30  0.0130
##  7         1 2015-07-31  0.0900
##  8         1 2015-08-31 -0.0340
##  9         1 2015-09-30 -0.0370
## 10         1 2015-10-30  0.0409
## # ... with 134 more rows

Portfolio 1: 1.2451104095 Apple: 01/30/15: 7.161809e-02 Google:01/30/15: 1.850194e-02 Netflix:01/30/15: 0.2661202211 Microsoft: 01/30/15: -0.1360136377 Tesla: 01/30/15: -0.071633725 Equation: 1.2451104095= 7.161809e-02(.40) + 1.850194e-02(.40)+ 0.2661202211(.40) + -0.1360136377 (.40)+ -0.071633725* (.60) Portfolio 2: 1.2322344268 Apple:01/30/15: 7.161809e-02 Google:01/30/15: 1.850194e-02 Netflix:01/30/15: 0.2661202211 Microsoft:01/30/15: -0.1360136377 Tesla:01/30/15: -0.071633725 Equation: 1.2322344268= 7.161809e-02(.40) + 1.850194e-02(.40)+ 0.2661202211(.40) + -0.1360136377 (.60)+ -0.071633725* (.40) Portfolio 3:1.3126611986 Apple:01/30/15: 7.161809e-02 Google:01/30/15: 1.850194e-02 Netflix:01/30/15: 0.2661202211 Microsoft:01/30/15: -0.1360136377 Tesla:01/30/15: -0.071633725 Equation: 1.3126611986=7.161809e-02(.40) + 1.850194e-02(.40)+ 0.2661202211(.60) + -0.1360136377 (.40)+ -0.071633725* (.40)

Q5 Merge Ra and Rb

RaRb_single_portfolio <- left_join(portfolio_returns_monthly_multi , 
                                   baseline_returns_monthly,
                                   by = "date")
RaRb_single_portfolio
## # A tibble: 144 x 4
## # Groups:   portfolio [?]
##    portfolio date            Ra       Rb
##        <int> <date>       <dbl>    <dbl>
##  1         1 2015-01-30  1.25   -0.0307 
##  2         1 2015-02-27  0.0583  0.0549 
##  3         1 2015-03-31 -0.0660 -0.0174 
##  4         1 2015-04-30  0.148   0.00852
##  5         1 2015-05-29  0.0586  0.0105 
##  6         1 2015-06-30  0.0130 -0.0210 
##  7         1 2015-07-31  0.0900  0.0197 
##  8         1 2015-08-31 -0.0340 -0.0626 
##  9         1 2015-09-30 -0.0370 -0.0264 
## 10         1 2015-10-30  0.0409  0.0830 
## # ... with 134 more rows

Q6 Compute the CAPM Table

RaRb_single_portfolio %>%
    tq_performance(Ra = Ra, Rb = Rb, performance_fun = table.CAPM) %>%
  t()
##                      [,1]   [,2]   [,3]
## portfolio          1.0000 2.0000 3.0000
## ActivePremium      0.4703 0.4821 0.5195
## Alpha              0.0448 0.0450 0.0486
## AnnualizedAlpha    0.6922 0.6956 0.7671
## Beta               0.2903 0.3566 0.3421
## Beta-              1.1252 1.1487 1.2885
## Beta+              0.8219 1.0242 0.9765
## Correlation        0.0521 0.0647 0.0578
## Correlationp-value 0.7252 0.6624 0.6965
## InformationRatio   0.7158 0.7432 0.7463
## R-squared          0.0027 0.0042 0.0033
## TrackingError      0.6571 0.6486 0.6961
## TreynorRatio       1.7862 1.4873 1.6597

Q7 Interpret Beta.

For Beta: The portfolios have a 0.2829, 0.3492, 0.3342 beta. All the beta’s in the portfolios are less than 1. With them being less than one the security of the portfolios are less volatile than the market. The overall monthly returns dont fluctuate as much as the market value in S&P 500. There is less risk involved in the average S&P 500 stock given.

Q8 Interpret Alpha.

For Alpha: It shows that each portfolio is above 0. The alpha for the portfolio’s are 0.0448 0.0450 0.0486. With this being said the portfolio’s alphas are also positive. Every stock and portfolio given falls between 0 and 1 which means that the portfolios are less risky. In general these portfolios based on the alpha will be a better investment overall. Also, the alpha didnt underperform or outperform based on the stocks and their weights for this year.

Q9 Interpret AnnualizedAlpha.

AnnualizedAlpha for the portfolios are 0.6929 0.6963 0.7679.These numbers represent the benchmark for the stocks and the overall funds together. By adding these funds together this allows to show what each portfolio differences in from the benchmark given in the stock. For annualized alpha you take the alpha and multiple it by 12 for each portfolio to get a ending outcome. By look at the annualized alpha you can see that the first portfolio will recieve 69.29% back, the second gives 69.63% back, and the last portfolio gives 76.79% back. These percentages are the returns on investment for each portfolio.

In conclusion: Portfolio 3 performs the best because the rfa is the widest, and it beats the market by 4.8% creating a wider margin. The weights for each portolio help determine which one will do the best therefore there is a bigger weight on Netflix. This has the heaviest weight on the best performer stock Netflix causing it to be the best performing portfolio. At the same time though portfolio 3 will be riskier because Netflix is known to have more fluctuations and a higher volatilty which makes it riskier.

Q10.a Display both code and the results of the code on the webpage.

Q10.b Display the title and your name correctly at the top of the webpage.

Q10.c Use the correct slug.