library(tidyverse)
library(tidyquant)

Get stock prices and convert to returns

Ra <- c("FDX", "ADBE", "BA") %>%
    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: 27 × 3
## # Groups:   symbol [3]
##    symbol date               Ra
##    <chr>  <date>          <dbl>
##  1 FDX    2024-01-31 -0.0434   
##  2 FDX    2024-02-29  0.0318   
##  3 FDX    2024-03-28  0.170    
##  4 FDX    2024-04-30 -0.0965   
##  5 FDX    2024-05-31 -0.0299   
##  6 FDX    2024-06-28  0.187    
##  7 FDX    2024-07-31  0.00804  
##  8 FDX    2024-08-30 -0.0115   
##  9 FDX    2024-09-17  0.0000898
## 10 ADBE   2024-01-31  0.0650   
## # ℹ 17 more rows

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: 9 × 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-17 -0.00483

Join the two tables

RaRb <- left_join(Ra, Rb, by = c("date" = "date"))
RaRb
## # A tibble: 27 × 4
## # Groups:   symbol [3]
##    symbol date               Ra       Rb
##    <chr>  <date>          <dbl>    <dbl>
##  1 FDX    2024-01-31 -0.0434     0.0270 
##  2 FDX    2024-02-29  0.0318     0.0612 
##  3 FDX    2024-03-28  0.170      0.0179 
##  4 FDX    2024-04-30 -0.0965    -0.0441 
##  5 FDX    2024-05-31 -0.0299     0.0688 
##  6 FDX    2024-06-28  0.187      0.0596 
##  7 FDX    2024-07-31  0.00804   -0.00751
##  8 FDX    2024-08-30 -0.0115     0.00649
##  9 FDX    2024-09-17  0.0000898 -0.00483
## 10 ADBE   2024-01-31  0.0650     0.0270 
## # ℹ 17 more rows

Calculate CAPM

RaRb_capm <- RaRb %>%
    tq_performance(Ra = Ra, 
                   Rb = Rb, 
                   performance_fun = table.CAPM)
RaRb_capm
## # A tibble: 3 × 13
## # Groups:   symbol [3]
##   symbol ActivePremium   Alpha AnnualizedAlpha  Beta `Beta-` `Beta+` Correlation
##   <chr>          <dbl>   <dbl>           <dbl> <dbl>   <dbl>   <dbl>       <dbl>
## 1 FDX           0.0046  0.0012          0.0142  1.11   2.63    0.184       0.441
## 2 ADBE         -0.413  -0.03           -0.306   1.09   0.591   0.504       0.357
## 3 BA           -0.736  -0.0709         -0.586   1.09   2.53    2.24        0.518
## # ℹ 5 more variables: `Correlationp-value` <dbl>, InformationRatio <dbl>,
## #   `R-squared` <dbl>, TrackingError <dbl>, TreynorRatio <dbl>

Which Stock has a positively skewed distribution of returns?

All stocks have a positively skewed distribution of returns. This means that the tail is on the right side.

RaRb_skewness <- RaRb %>%
    tq_performance(Ra = Ra, 
                   Rb = NULL, 
                   performance_fun = skewness)
RaRb_skewness
## # A tibble: 3 × 2
## # Groups:   symbol [3]
##   symbol skewness.1
##   <chr>       <dbl>
## 1 FDX        0.804 
## 2 ADBE       1.30  
## 3 BA         0.0824