# Load Packages
library(tidyverse)
library(tidyquant)

1 Get stock prices and returns

Ra <- c("MSFT", "AAPL", "NVDA") %>%
    tq_get(get  = "stock.prices",
           from = "2022-01-01") %>%
    group_by(symbol) %>%
    tq_transmute(select     = adjusted, 
                 mutate_fun = periodReturn, 
                 period     = "monthly", 
                 col_rename = "Ra")
Ra
## # A tibble: 141 × 3
## # Groups:   symbol [3]
##    symbol date             Ra
##    <chr>  <date>        <dbl>
##  1 MSFT   2022-01-31 -0.0710 
##  2 MSFT   2022-02-28 -0.0372 
##  3 MSFT   2022-03-31  0.0319 
##  4 MSFT   2022-04-29 -0.0999 
##  5 MSFT   2022-05-31 -0.0181 
##  6 MSFT   2022-06-30 -0.0553 
##  7 MSFT   2022-07-29  0.0931 
##  8 MSFT   2022-08-31 -0.0667 
##  9 MSFT   2022-09-30 -0.109  
## 10 MSFT   2022-10-31 -0.00331
## # ℹ 131 more rows

2 get baseline and convert to returns

Rb <- "^IXIC" %>%
    tq_get(get  = "stock.prices",
           from = "2022-01-01") %>%
    tq_transmute(select     = adjusted, 
                 mutate_fun = periodReturn, 
                 period     = "monthly", 
                 col_rename = "Rb")
Rb
## # A tibble: 47 × 2
##    date            Rb
##    <date>       <dbl>
##  1 2022-01-31 -0.101 
##  2 2022-02-28 -0.0343
##  3 2022-03-31  0.0341
##  4 2022-04-29 -0.133 
##  5 2022-05-31 -0.0205
##  6 2022-06-30 -0.0871
##  7 2022-07-29  0.123 
##  8 2022-08-31 -0.0464
##  9 2022-09-30 -0.105 
## 10 2022-10-31  0.0390
## # ℹ 37 more rows

3 Join the two tables

RaRb <- left_join(Ra, Rb, by = c("date" = "date"))
RaRb
## # A tibble: 141 × 4
## # Groups:   symbol [3]
##    symbol date             Ra      Rb
##    <chr>  <date>        <dbl>   <dbl>
##  1 MSFT   2022-01-31 -0.0710  -0.101 
##  2 MSFT   2022-02-28 -0.0372  -0.0343
##  3 MSFT   2022-03-31  0.0319   0.0341
##  4 MSFT   2022-04-29 -0.0999  -0.133 
##  5 MSFT   2022-05-31 -0.0181  -0.0205
##  6 MSFT   2022-06-30 -0.0553  -0.0871
##  7 MSFT   2022-07-29  0.0931   0.123 
##  8 MSFT   2022-08-31 -0.0667  -0.0464
##  9 MSFT   2022-09-30 -0.109   -0.105 
## 10 MSFT   2022-10-31 -0.00331  0.0390
## # ℹ 131 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 MSFT          0.0062 0.0021      0.0011          0.0251 0.869   0.593
## 2 AAPL          0.017  0.0031      0.0043          0.0378 0.899   1.02 
## 3 NVDA          0.477  0.0289      0.0235          0.408  2.14    2.71 
## # ℹ 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?

RaRb_skew <- RaRb %>%
    tq_performance(Ra = Ra, 
                   Rb = NULL, 
                   performance_fun = skewness)
RaRb_skew
## # A tibble: 3 × 2
## # Groups:   symbol [3]
##   symbol skewness.1
##   <chr>       <dbl>
## 1 MSFT        0.324
## 2 AAPL        0.206
## 3 NVDA       -0.112

Microsoft and Uber show positively skewed return distributions, while Nvidia’s return distribution is negatively skewed. Even with these differences in skewness, all three stocks still exhibit a positive alpha