Hint: Import Apple, Google, Netflix, Microsoft and Tesla from “2015-01-01” to “2018-12-31”.
library(tidyquant)
library(tidyverse)
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
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
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.1, 0.1, 0.1, 0.1, 0.6,
0.1, 0.1, 0.1, 0.6, 0.1,
0.1, 0.1, 0.6, 0.1, 0.1)
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.1
## 2 1 GOOG 0.1
## 3 1 NFLX 0.1
## 4 1 MSFT 0.1
## 5 1 TSLA 0.6
## 6 2 AAPL 0.1
## 7 2 GOOG 0.1
## 8 2 NFLX 0.1
## 9 2 MSFT 0.6
## 10 2 TSLA 0.1
## 11 3 AAPL 0.1
## 12 3 GOOG 0.1
## 13 3 NFLX 0.6
## 14 3 MSFT 0.1
## 15 3 TSLA 0.1
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 + options(scipen=999)
## portfolio date Ra
## 1 1 2015-01-30 -0.02095757371
## 2 1 2015-02-27 0.03286115821
## 3 1 2015-03-31 -0.06833277382
## 4 1 2015-04-30 0.16817509085
## 5 1 2015-05-29 0.08005444043
## 6 1 2015-06-30 0.03753922284
## 7 1 2015-07-31 0.04632401185
## 8 1 2015-08-31 -0.04680448784
## 9 1 2015-09-30 -0.02273504870
## 10 1 2015-10-30 -0.04721906772
## 11 1 2015-11-30 0.09062963057
## 12 1 2015-12-31 0.00175434247
## 13 1 2016-01-29 -0.15186876638
## 14 1 2016-02-29 -0.01137535110
## 15 1 2016-03-31 0.14429066305
## 16 1 2016-04-29 -0.02538903636
## 17 1 2016-05-31 -0.00049751073
## 18 1 2016-06-30 -0.05869873183
## 19 1 2016-07-29 0.08738174432
## 20 1 2016-08-31 -0.03789601861
## 21 1 2016-09-30 -0.00935526658
## 22 1 2016-10-31 0.03735436870
## 23 1 2016-11-30 -0.03743061457
## 24 1 2016-12-30 0.08077291623
## 25 1 2017-01-31 0.12527949958
## 26 1 2017-02-28 0.01256142814
## 27 1 2017-03-31 0.07178160076
## 28 1 2017-04-28 0.08459712908
## 29 1 2017-05-31 0.07366949286
## 30 1 2017-06-30 0.00442126466
## 31 1 2017-07-31 -0.01270934595
## 32 1 2017-08-31 0.05607923412
## 33 1 2017-09-29 -0.01828844446
## 34 1 2017-10-31 0.02744645431
## 35 1 2017-11-30 -0.03911451872
## 36 1 2017-12-29 0.01184082629
## 37 1 2018-01-31 0.17709754263
## 38 1 2018-02-28 0.00387474054
## 39 1 2018-03-29 -0.10932377003
## 40 1 2018-04-30 0.05827807738
## 41 1 2018-05-31 0.05061786401
## 42 1 2018-06-29 0.11538526973
## 43 1 2018-07-31 -0.08051352458
## 44 1 2018-08-31 0.05706458559
## 45 1 2018-09-28 -0.03936406590
## 46 1 2018-10-31 0.00300861304
## 47 1 2018-11-30 -0.00914862489
## 48 1 2018-12-28 -0.07496838889
## 49 2 2015-01-30 -0.05314753028
## 50 2 2015-02-27 0.07708966459
## 51 2 2015-03-31 -0.06886772044
## 52 2 2015-04-30 0.16784056614
## 53 2 2015-05-29 0.01423653527
## 54 2 2015-06-30 -0.02183446695
## 55 2 2015-07-31 0.08142043055
## 56 2 2015-08-31 -0.04436983196
## 57 2 2015-09-30 -0.01617048273
## 58 2 2015-10-30 0.11551360088
## 59 2 2015-11-30 0.05871052931
## 60 2 2015-12-31 -0.00577202996
## 61 2 2016-01-29 -0.06321632034
## 62 2 2016-02-29 -0.04548473358
## 63 2 2016-03-31 0.09641675621
## 64 2 2016-04-29 -0.08917637009
## 65 2 2016-05-31 0.06659817667
## 66 2 2016-06-30 -0.05121531100
## 67 2 2016-07-29 0.08952289523
## 68 2 2016-08-31 0.01488681288
## 69 2 2016-09-30 0.00684870209
## 70 2 2016-10-31 0.06280170527
## 71 2 2016-11-30 -0.01205787687
## 72 2 2016-12-30 0.04169943446
## 73 2 2017-01-31 0.06600123099
## 74 2 2017-02-28 0.01210004857
## 75 2 2017-03-31 0.03687649893
## 76 2 2017-04-28 0.04649042083
## 77 2 2017-05-31 0.04665005252
## 78 2 2017-06-30 -0.02801578019
## 79 2 2017-07-31 0.06253882459
## 80 2 2017-08-31 0.02822385638
## 81 2 2017-09-29 -0.00159924974
## 82 2 2017-10-31 0.09106467874
## 83 2 2017-11-30 -0.00215764357
## 84 2 2017-12-29 0.01523842961
## 85 2 2018-01-31 0.15890654024
## 86 2 2018-02-28 0.01023903913
## 87 2 2018-03-29 -0.03535140285
## 88 2 2018-04-30 0.03117431819
## 89 2 2018-05-31 0.07816703512
## 90 2 2018-06-29 0.04121337173
## 91 2 2018-07-31 0.00005523581
## 92 2 2018-08-31 0.07110017906
## 93 2 2018-09-28 0.00624285069
## 94 2 2018-10-31 -0.08451552263
## 95 2 2018-11-30 0.00118297587
## 96 2 2018-12-28 -0.09276413599
## 97 3 2015-01-30 0.14791939912
## 98 3 2015-02-27 0.06987331617
## 99 3 2015-03-31 -0.09714407571
## 100 3 2015-04-30 0.24691216279
## 101 3 2015-05-29 0.09292959150
## 102 3 2015-06-30 0.03436516021
## 103 3 2015-07-31 0.17093653584
## 104 3 2015-08-31 -0.00776535283
## 105 3 2015-09-30 -0.07958070859
## 106 3 2015-10-30 0.05333179509
## 107 3 2015-11-30 0.11391627960
## 108 3 2015-12-31 -0.05599177295
## 109 3 2016-01-29 -0.16465412449
## 110 3 2016-02-29 0.00130058191
## 111 3 2016-03-31 0.09917988321
## 112 3 2016-04-29 -0.10452108950
## 113 3 2016-05-31 0.10836078983
## 114 3 2016-06-30 -0.09170255550
## 115 3 2016-07-29 0.02774095568
## 116 3 2016-08-31 0.04360143328
## 117 3 2016-09-30 0.01121201241
## 118 3 2016-10-31 0.19286929990
## 119 3 2016-11-30 -0.05251198940
## 120 3 2016-12-30 0.05574087133
## 121 3 2017-01-31 0.11918559463
## 122 3 2017-02-28 0.01622940241
## 123 3 2017-03-31 0.04123472999
## 124 3 2017-04-28 0.03813513546
## 125 3 2017-05-31 0.06871132658
## 126 3 2017-06-30 -0.06754691546
## 127 3 2017-07-31 0.15951460087
## 128 3 2017-08-31 -0.01640400000
## 129 3 2017-09-29 0.02407762319
## 130 3 2017-10-31 0.07829799721
## 131 3 2017-11-30 -0.03570711223
## 132 3 2017-12-29 0.02006278616
## 133 3 2018-01-31 0.33389205435
## 134 3 2018-02-28 0.06101503057
## 135 3 2018-03-29 -0.00400793416
## 136 3 2018-04-30 0.05145666330
## 137 3 2018-05-31 0.11526369714
## 138 3 2018-06-29 0.10228786240
## 139 3 2018-07-31 -0.11440611210
## 140 3 2018-08-31 0.08602331763
## 141 3 2018-09-28 0.01117083311
## 142 3 2018-10-31 -0.16562203786
## 143 3 2018-11-30 -0.04647378074
## 144 3 2018-12-28 -0.10045458679
Portfolio 1 had a -2.09% return for the month of January 2015. Portfolio 2 had a -5.31% return for the month of January 2015. Portfolio 3 had a 14.79% return for the month of January 2015.
These returns are calculated based upon their given weights within each portfolio. For example, Portfolio 1 is calculated by (.1 * Apple) + (.1 * Google) + (.1 * Netlfix) + (.1 * Microsoft) + (.6 * Tesla). The return for this portfolio will give Apple, Google, Netflix, and Microsoft a 10% weight within the portfolio, while it gives Tesla a 60% weight within the portfolio. Portfolio 2 is calculated the same, but with Microsoft having 60% of the weight with all other stocks having 10% of the weight. Portfolio 3 is calculated the same way as well, but Netflix has 60% of the weight with all other stocks having 10% of the weight.
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 -0.0210 -0.0307
## 2 1 2015-02-27 0.0329 0.0549
## 3 1 2015-03-31 -0.0683 -0.0174
## 4 1 2015-04-30 0.168 0.00852
## 5 1 2015-05-29 0.0801 0.0105
## 6 1 2015-06-30 0.0375 -0.0210
## 7 1 2015-07-31 0.0463 0.0197
## 8 1 2015-08-31 -0.0468 -0.0626
## 9 1 2015-09-30 -0.0227 -0.0264
## 10 1 2015-10-30 -0.0472 0.0830
## # ... with 134 more rows
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.1431 0.2005 0.3499
## Alpha 0.0132 0.0149 0.0275
## AnnualizedAlpha 0.1709 0.1937 0.3856
## Beta 0.8465 1.2389 1.2484
## Beta- 0.8137 1.0441 1.6631
## Beta+ 0.0575 1.2787 0.9734
## Correlation 0.4093 0.6901 0.4150
## Correlationp-value 0.0039 0.0000 0.0034
## InformationRatio 0.6457 1.2965 1.0878
## R-squared 0.1675 0.4763 0.1722
## TrackingError 0.2216 0.1546 0.3217
## TreynorRatio 0.2261 0.2008 0.3190
Beta is the volatility of a stock, or how much a stock’s return would be expected to change. Beta is way to measure the risk of a stock in terms of its expected returns. The higher the Beta, the more risky the stock is.
Portfolio 3 has the highest Beta at 1.2484, meaning that it is a risky portfolio that could provide higher returns.
Portfolio 2 is also a risky portfolio with a Beta of 1.2389.
Portfolio 1 is not a very risky portfolio with a Beta of .8465, meaning that there is not that much risk involved with the stock, which would bring lower expected returns.
When looking at Alpha, you can see how much better the portfolio did than the market benchmark. Portfolio 3 has the highest Alpha of 2.75%, which means that it outperformed the benchmark by 2.75%.
Portfolio 2 had the second highest Alpha at 1.49%, which means that it outperformed the benchmark by 1.49%. Portfolio 1 had the lowest Alpha at 1.32%, which means that it outperformed the benchmark by 1.32%. All Portfolios outperformed their respective benchmarks, but Portfolio 3 outperformed its benchmark by the greatest amount.
Annualized Alpha is the total of all Alphas over a year’s period. The higher the annualized Alpha is, the better the stock did during that year, in comparison to a baseline stock.
Portfolio 3 has the highest annualized Alpha of 38.56%.
Portfolio 2 had the second highest annualized Alpha at 19.37%.
Portfolio 1 had the lowest annualized Alpha at 17.09%. All of the portfolios outperformed the baseline stock over the year, while Portfolio 3 outperformed the baseline by the greatest amount.