# Load packages
library(tidyverse)
library(tidyquant)

1 Get stock prices and convert to returns

Ra <- c("META", "MSFT", "AMZN") %>%
    tq_get(get = "stock.prices",
           from = "2024-01-01") %>%
    group_by(symbol) %>%
    tq_transmute(select = adjusted,
                 mutate_fun = periodReturn,
                 period = "monthly",
                 col_rename = "Ra")
Ra
## # A tibble: 63 × 3
## # Groups:   symbol [3]
##    symbol date             Ra
##    <chr>  <date>        <dbl>
##  1 META   2024-01-31  0.127  
##  2 META   2024-02-29  0.258  
##  3 META   2024-03-28 -0.00928
##  4 META   2024-04-30 -0.114  
##  5 META   2024-05-31  0.0852 
##  6 META   2024-06-28  0.0812 
##  7 META   2024-07-31 -0.0583 
##  8 META   2024-08-30  0.0979 
##  9 META   2024-09-30  0.0991 
## 10 META   2024-10-31 -0.00849
## # ℹ 53 more rows

2 Get baseline and convert to returns

Rb <- "^IXIC" %>%
    tq_get(get = "stock.prices",
           from = "2024-01-01") %>%
    tq_transmute(select = adjusted,
                 mutate_fun = periodReturn,
                 period = "monthly",
                 col_rename = "Rb")
Rb
## # A tibble: 21 × 2
##    date             Rb
##    <date>        <dbl>
##  1 2024-01-31  0.0270 
##  2 2024-02-29  0.0612 
##  3 2024-03-28  0.0179 
##  4 2024-04-30 -0.0441 
##  5 2024-05-31  0.0688 
##  6 2024-06-28  0.0596 
##  7 2024-07-31 -0.00751
##  8 2024-08-30  0.00649
##  9 2024-09-30  0.0268 
## 10 2024-10-31 -0.00517
## # ℹ 11 more rows

3 Join the two tables

RaRb <- left_join(Ra, Rb, by = c("date" = "date"))
RaRb
## # A tibble: 63 × 4
## # Groups:   symbol [3]
##    symbol date             Ra       Rb
##    <chr>  <date>        <dbl>    <dbl>
##  1 META   2024-01-31  0.127    0.0270 
##  2 META   2024-02-29  0.258    0.0612 
##  3 META   2024-03-28 -0.00928  0.0179 
##  4 META   2024-04-30 -0.114   -0.0441 
##  5 META   2024-05-31  0.0852   0.0688 
##  6 META   2024-06-28  0.0812   0.0596 
##  7 META   2024-07-31 -0.0583  -0.00751
##  8 META   2024-08-30  0.0979   0.00649
##  9 META   2024-09-30  0.0991   0.0268 
## 10 META   2024-10-31 -0.00849 -0.00517
## # ℹ 53 more rows

4 Calculate CAPM

RaRb_capm <- RaRb %>%
    tq_performance(Ra = Ra,
                   Rb = Rb,
                   performance_fun = table.CAPM)
RaRb_capm
## # A tibble: 3 × 18
## # Groups:   symbol [3]
##   symbol ActivePremium   Alpha AlphaRobust AnnualizedAlpha  Beta `Beta-`
##   <chr>          <dbl>   <dbl>       <dbl>           <dbl> <dbl>   <dbl>
## 1 META          0.306   0.0063      0.0049          0.0785  1.79  1.39  
## 2 MSFT         -0.0521 -0.0072     -0.0072         -0.0831  1.22 -0.0483
## 3 AMZN          0.0106 -0.0046     -0.0046         -0.0533  1.33  1.16  
## # ℹ 11 more variables: `Beta-Robust` <dbl>, `Beta+` <dbl>, `Beta+Robust` <dbl>,
## #   BetaRobust <dbl>, Correlation <dbl>, `Correlationp-value` <dbl>,
## #   InformationRatio <dbl>, `R-squared` <dbl>, `R-squaredRobust` <dbl>,
## #   TrackingError <dbl>, TreynorRatio <dbl>

Which stock beat the market in 2024?

Only META managed to beat the market in 2024 with an Alpha of 0.0063. Both MSFT and AMZN under-performed with Alphas of -0.0072 and -0.0046 respectively.

RaRb_capm <- RaRb %>%
    tq_performance(Ra = Ra,
                   Rb = Rb,
                   performance_fun = table.CAPM)
RaRb_capm
## # A tibble: 3 × 18
## # Groups:   symbol [3]
##   symbol ActivePremium   Alpha AlphaRobust AnnualizedAlpha  Beta `Beta-`
##   <chr>          <dbl>   <dbl>       <dbl>           <dbl> <dbl>   <dbl>
## 1 META          0.306   0.0063      0.0049          0.0785  1.79  1.39  
## 2 MSFT         -0.0521 -0.0072     -0.0072         -0.0831  1.22 -0.0483
## 3 AMZN          0.0106 -0.0046     -0.0046         -0.0533  1.33  1.16  
## # ℹ 11 more variables: `Beta-Robust` <dbl>, `Beta+` <dbl>, `Beta+Robust` <dbl>,
## #   BetaRobust <dbl>, Correlation <dbl>, `Correlationp-value` <dbl>,
## #   InformationRatio <dbl>, `R-squared` <dbl>, `R-squaredRobust` <dbl>,
## #   TrackingError <dbl>, TreynorRatio <dbl>

Which stock has a positively skewed distribution of returns?

Both META and MSFT had positively-skewed distribution of returns (0.203 and 0.356) for 2024; AMZN was the only stock that had a negatively-skewed distribution of returns with a skewness of -0.175 during that same year.

RaRb_skew <- RaRb %>%
    tq_performance(Ra = Ra,
                   performance_fun = skewness)
RaRb_skew
## # A tibble: 3 × 2
## # Groups:   symbol [3]
##   symbol skewness.1
##   <chr>       <dbl>
## 1 META        0.203
## 2 MSFT        0.356
## 3 AMZN       -0.175