# Load Packages 
library(tidyquant)
library(tidyverse)

1. Get Stock Prices and Convert to Returns

Ra <- c("NVDA", "DELL", "DIS") %>%
    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: 42 × 3
## # Groups:   symbol [3]
##    symbol date             Ra
##    <chr>  <date>        <dbl>
##  1 NVDA   2022-01-31 -0.187  
##  2 NVDA   2022-02-28 -0.00412
##  3 NVDA   2022-03-31  0.119  
##  4 NVDA   2022-04-29 -0.320  
##  5 NVDA   2022-05-31  0.00674
##  6 NVDA   2022-06-30 -0.188  
##  7 NVDA   2022-07-29  0.198  
##  8 NVDA   2022-08-31 -0.169  
##  9 NVDA   2022-09-30 -0.196  
## 10 NVDA   2022-10-31  0.112  
## # … with 32 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: 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: 42 × 4
## # Groups:   symbol [3]
##    symbol date             Ra      Rb
##    <chr>  <date>        <dbl>   <dbl>
##  1 NVDA   2022-01-31 -0.187   -0.101 
##  2 NVDA   2022-02-28 -0.00412 -0.0343
##  3 NVDA   2022-03-31  0.119    0.0341
##  4 NVDA   2022-04-29 -0.320   -0.133 
##  5 NVDA   2022-05-31  0.00674 -0.0205
##  6 NVDA   2022-06-30 -0.188   -0.0871
##  7 NVDA   2022-07-29  0.198    0.123 
##  8 NVDA   2022-08-31 -0.169   -0.0464
##  9 NVDA   2022-09-30 -0.196   -0.105 
## 10 NVDA   2022-10-31  0.112    0.0390
## # … with 32 more rows

4. 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 ActiveP…¹   Alpha Annua…²  Beta `Beta-` `Beta+` Corre…³ Corre…⁴ Infor…⁵
##   <chr>      <dbl>   <dbl>   <dbl> <dbl>   <dbl>   <dbl>   <dbl>   <dbl>   <dbl>
## 1 NVDA     -0.03    0.0366  0.539  2.34    2.49    1.48    0.952  0       -0.07 
## 2 DELL      0.0306 -0.0027 -0.0317 0.623   0.256  -0.787   0.535  0.0488   0.104
## 3 DIS      -0.0457  0.0037  0.0451 1.24    2.12    2.05    0.792  0.0007  -0.166
## # … 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 Skewed Distribution of Returns?

All 3 of my stocks are positively skewed. The stocks are Nvidia, Dell, and Disney

RaRb_capm <- RaRb %>%
    tq_performance(Ra = Ra, 
                   Rb = NULL, 
                   performance_fun = skewness)
RaRb_capm
## # A tibble: 3 × 2
## # Groups:   symbol [3]
##   symbol skewness.1
##   <chr>       <dbl>
## 1 NVDA        0.150
## 2 DELL        0.473
## 3 DIS         0.595