# Load Packages
library(tidyverse)
library(tidyquant)

1 Get stock prices and convert to returns

Ra <- c("AMZN", "NVDA", "CAT") %>% 
    tq_get(get = "stock.prices",
           from = "2025-01-01") %>%
    group_by(symbol) %>%
    tq_transmute(select   = adjusted,
               mutate_fun = periodReturn,
                 period   = "monthly",
               col_rename = "Ra")
Ra
## # A tibble: 63 × 3
## # Groups:   symbol [3]
##    symbol date            Ra
##    <chr>  <date>       <dbl>
##  1 AMZN   2025-01-31  0.0793
##  2 AMZN   2025-02-28 -0.107 
##  3 AMZN   2025-03-31 -0.104 
##  4 AMZN   2025-04-30 -0.0307
##  5 AMZN   2025-05-30  0.112 
##  6 AMZN   2025-06-30  0.0701
##  7 AMZN   2025-07-31  0.0671
##  8 AMZN   2025-08-29 -0.0218
##  9 AMZN   2025-09-30 -0.0412
## 10 AMZN   2025-10-31  0.112 
## # ℹ 53 more rows

2 Get baseline and convert to returns

Rb <- "^IXIC" %>% 
    tq_get(get = "stock.prices",
           from = "2025-01-01") %>%
    tq_transmute(select   = adjusted,
               mutate_fun = periodReturn,
                 period   = "monthly",
               col_rename = "Rb")
Rb
## # A tibble: 21 × 2
##    date             Rb
##    <date>        <dbl>
##  1 2025-01-31  0.0180 
##  2 2025-02-28 -0.0397 
##  3 2025-03-31 -0.0821 
##  4 2025-04-30  0.00850
##  5 2025-05-30  0.0956 
##  6 2025-06-30  0.0657 
##  7 2025-07-31  0.0370 
##  8 2025-08-29  0.0158 
##  9 2025-09-30  0.0561 
## 10 2025-10-31  0.0470 
## # ℹ 11 more rows

3 Join the two tables

RaRb <- left_join(Ra, Rb, by = c("date" = "date"))
RaRb
## # A tibble: 63 × 4
## # Groups:   symbol [3]
##    symbol date            Ra       Rb
##    <chr>  <date>       <dbl>    <dbl>
##  1 AMZN   2025-01-31  0.0793  0.0180 
##  2 AMZN   2025-02-28 -0.107  -0.0397 
##  3 AMZN   2025-03-31 -0.104  -0.0821 
##  4 AMZN   2025-04-30 -0.0307  0.00850
##  5 AMZN   2025-05-30  0.112   0.0956 
##  6 AMZN   2025-06-30  0.0701  0.0657 
##  7 AMZN   2025-07-31  0.0671  0.0370 
##  8 AMZN   2025-08-29 -0.0218  0.0158 
##  9 AMZN   2025-09-30 -0.0412  0.0561 
## 10 AMZN   2025-10-31  0.112   0.0470 
## # ℹ 53 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 AMZN          -0.137 -0.0115     -0.0176         -0.130   1.25   0.956
## 2 NVDA           0.114  0.005       0.0065          0.0622  1.33   1.28 
## 3 CAT            0.401  0.0274      0.0277          0.384   1.14   0.960
## # ℹ 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>

NVDA and CAT beat the market in 2025 with Alpha’s of .005 and .0274 respectively

Which stock has a positively skewed distribution of returns

RaRb_skewness <- RaRb %>%
    tq_performance(Ra = Ra,
                   Rb = Rb,
                   performance_fun = VolatilitySkewness)
RaRb_skewness
## # A tibble: 3 × 2
## # Groups:   symbol [3]
##   symbol VolatilitySkewness.1
##   <chr>                 <dbl>
## 1 AMZN                   2.64
## 2 NVDA                   4.31
## 3 CAT                    4.03

All chosen stocks have positively skeweded distribution of returns