# Load the Packages
library(tidyverse)
library(tidyquant)

1 Get Stock Prices and convert to Returns

Ra <- c("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: 28 × 3
## # Groups:   symbol [2]
##    symbol date            Ra
##    <chr>  <date>       <dbl>
##  1 AAPL   2022-01-31 -0.0397
##  2 AAPL   2022-02-28 -0.0541
##  3 AAPL   2022-03-31  0.0575
##  4 AAPL   2022-04-29 -0.0971
##  5 AAPL   2022-05-31 -0.0545
##  6 AAPL   2022-06-30 -0.0814
##  7 AAPL   2022-07-29  0.189 
##  8 AAPL   2022-08-31 -0.0312
##  9 AAPL   2022-09-30 -0.121 
## 10 AAPL   2022-10-31  0.110 
## # … with 18 more rows

2 Get baseline and convert to return

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: 14 × 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
## 11 2022-11-30  0.0437
## 12 2022-12-30 -0.0873
## 13 2023-01-31  0.107 
## 14 2023-02-10  0.0115

3 Join the Two tables

RaRb <- left_join(Ra, Rb, by = c("date" = "date"))
RaRb
## # A tibble: 28 × 4
## # Groups:   symbol [2]
##    symbol date            Ra      Rb
##    <chr>  <date>       <dbl>   <dbl>
##  1 AAPL   2022-01-31 -0.0397 -0.101 
##  2 AAPL   2022-02-28 -0.0541 -0.0343
##  3 AAPL   2022-03-31  0.0575  0.0341
##  4 AAPL   2022-04-29 -0.0971 -0.133 
##  5 AAPL   2022-05-31 -0.0545 -0.0205
##  6 AAPL   2022-06-30 -0.0814 -0.0871
##  7 AAPL   2022-07-29  0.189   0.123 
##  8 AAPL   2022-08-31 -0.0312 -0.0464
##  9 AAPL   2022-09-30 -0.121  -0.105 
## 10 AAPL   2022-10-31  0.110   0.0390
## # … with 18 more rows

4 Calculate CAPM

RaRb_capm <- RaRb %>%
    tq_performance(Ra = Ra, 
                   Rb = Rb, 
                   performance_fun = table.CAPM)
RaRb_capm
## # A tibble: 2 × 13
## # Groups:   symbol [2]
##   symbol ActivePr…¹  Alpha Annua…²  Beta `Beta-` `Beta+` Corre…³ Corre…⁴ Infor…⁵
##   <chr>       <dbl>  <dbl>   <dbl> <dbl>   <dbl>   <dbl>   <dbl>   <dbl>   <dbl>
## 1 AAPL       0.0848 0.0109   0.138  1.07   0.532    1.17   0.896       0   0.571
## 2 NVDA      -0.03   0.0366   0.539  2.34   2.49     1.48   0.952       0  -0.07 
## # … with 3 more variables: `R-squared` <dbl>, TrackingError <dbl>,
## #   TreynorRatio <dbl>, and abbreviated variable names ¹​ActivePremium,
## #   ²​AnnualizedAlpha, ³​Correlation, ⁴​`Correlationp-value`, ⁵​InformationRatio

Which Stock Has a positively scewed distribution of returns

# AAPL and NVDA are Positively Skewed
RaRb_capm <- RaRb %>%
    tq_performance(Ra = Ra, 
                   Rb = NULL, 
                   performance_fun = skewness)
RaRb_capm
## # A tibble: 2 × 2
## # Groups:   symbol [2]
##   symbol skewness.1
##   <chr>       <dbl>
## 1 AAPL        0.657
## 2 NVDA        0.150