# Load packages
library(tidyverse)
library(tidyquant)

1 Get stock prices and convert to returns

Ra <- c("NVDA", "SHOP", "TTD") %>%
    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 NVDA   2024-01-31  0.277 
##  2 NVDA   2024-02-29  0.286 
##  3 NVDA   2024-03-28  0.142 
##  4 NVDA   2024-04-30 -0.0438
##  5 NVDA   2024-05-31  0.269 
##  6 NVDA   2024-06-28  0.127 
##  7 NVDA   2024-07-31 -0.0528
##  8 NVDA   2024-08-30  0.0201
##  9 NVDA   2024-09-24  0.0127
## 10 SHOP   2024-01-31  0.0845
## # ℹ 17 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: 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-24  0.0204

3 Join the two data 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 NVDA   2024-01-31  0.277   0.0270 
##  2 NVDA   2024-02-29  0.286   0.0612 
##  3 NVDA   2024-03-28  0.142   0.0179 
##  4 NVDA   2024-04-30 -0.0438 -0.0441 
##  5 NVDA   2024-05-31  0.269   0.0688 
##  6 NVDA   2024-06-28  0.127   0.0596 
##  7 NVDA   2024-07-31 -0.0528 -0.00751
##  8 NVDA   2024-08-30  0.0201  0.00649
##  9 NVDA   2024-09-24  0.0127  0.0204 
## 10 SHOP   2024-01-31  0.0845  0.0270 
## # ℹ 17 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 ActivePremium  Alpha AnnualizedAlpha   Beta `Beta-` `Beta+` Correlation
##   <chr>          <dbl>  <dbl>           <dbl>  <dbl>   <dbl>   <dbl>       <dbl>
## 1 NVDA           2.10  0.045            0.695 3.02    -0.247    3.08      0.796 
## 2 SHOP          -0.190 0.0151           0.198 0.0131   0.462   -3.29      0.0041
## 3 TTD            0.514 0.0131           0.170 1.84    -0.752    1.24      0.629 
## # ℹ 5 more variables: `Correlationp-value` <dbl>, InformationRatio <dbl>,
## #   `R-squared` <dbl>, TrackingError <dbl>, TreynorRatio <dbl>

All three of my stocks beat the market, with NVIDIA being the best.

Which stock has a positively skewed distrobution of returns?

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 NVDA        0.108
## 2 SHOP        0.147
## 3 TTD         0.450

All three of my stocks have a positive skewness with TTD being the highest return this year.