Loading Packages

library(tidyverse)
library(tidyquant)

1 Get stock prices and convet to returns

Ra <- c("SNDK", "NVDA", "DELL") %>%
    tq_get(get = "stock.prices",
           from = "2026-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 SNDK   2026-01-30  1.09     
##  2 SNDK   2026-02-27  0.103    
##  3 SNDK   2026-03-31 -0.0000314
##  4 SNDK   2026-04-30  0.726    
##  5 SNDK   2026-05-29  0.546    
##  6 SNDK   2026-06-30  0.341    
##  7 SNDK   2026-07-31 -0.466    
##  8 SNDK   2026-08-31  0.290    
##  9 SNDK   2026-09-18  0.144    
## 10 NVDA   2026-01-30  0.0121   
## # ℹ 17 more rows

2 Get baseline and convert to retunrs

Rb <- "^IXIC" %>%
    tq_get(get = "stock.prices",
           from = "2026-01-01") %>%
    tq_transmute(select = adjusted,
                 mutate_fun = periodReturn,
                 period = "monthly",
                 col_rename = "Rb")

3 Join the 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 SNDK   2026-01-30  1.09       0.00973
##  2 SNDK   2026-02-27  0.103     -0.0338 
##  3 SNDK   2026-03-31 -0.0000314 -0.0475 
##  4 SNDK   2026-04-30  0.726      0.153  
##  5 SNDK   2026-05-29  0.546      0.0836 
##  6 SNDK   2026-06-30  0.341     -0.0281 
##  7 SNDK   2026-07-31 -0.466     -0.0320 
##  8 SNDK   2026-08-31  0.290      0.0393 
##  9 SNDK   2026-09-18  0.144      0.00575
## 10 NVDA   2026-01-30  0.0121     0.00973
## # ℹ 17 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 SNDK         11.0    0.244        0.227         12.7    3.89     5.04
## 2 NVDA          0.0536 0.0051       0.005          0.0626 0.934   -1.32
## 3 DELL          6.20   0.173        0.107          5.79   2.49    -4.09
## # ℹ 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>
#Alpha means they beat the market/ benchmark

Which Stock has a positivley skewed distributio of return?

5 Calculate Skewednes of Distribution

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 SNDK       0.0926
## 2 NVDA       0.471 
## 3 DELL       1.64

All of the three tech stock have a positive skewness, but DELL is an outlier with a positive value of 1.64: Meaning they have a big tail.

Visualizing the distribution of returns

Ra %>%
  ggplot(aes(x = Ra, color = symbol)) +
  geom_density(linewidth = 1.2) +
  labs(
    title = "Distribution of Monthly Stock Returns",
    x = "Monthly Return",
    y = "Density",
    color = "Stock"
  ) +
  theme_minimal()