# load packages
library(tidyverse)
library(tidyquant)

1 get stock prices and convert to returns

Ra <- c("AAPL", "GOOG", "NFLX") %>%
    tq_get(get = "stock.prices",
           from = "2026-01-01") %>%
    group_by(symbol) %>%
    tq_transmute(select = adjusted,
                 mutate_fun = periodReturn,
                 period = "daily",
                 col_rename = "Ra")
Ra
## # A tibble: 543 × 3
## # Groups:   symbol [3]
##    symbol date             Ra
##    <chr>  <date>        <dbl>
##  1 AAPL   2026-01-02  0      
##  2 AAPL   2026-01-05 -0.0138 
##  3 AAPL   2026-01-06 -0.0183 
##  4 AAPL   2026-01-07 -0.00774
##  5 AAPL   2026-01-08 -0.00496
##  6 AAPL   2026-01-09  0.00127
##  7 AAPL   2026-01-12  0.00339
##  8 AAPL   2026-01-13  0.00307
##  9 AAPL   2026-01-14 -0.00418
## 10 AAPL   2026-01-15 -0.00673
## # ℹ 533 more rows

2 get baseline and convert to returns

Rb <- c("^IXIC") %>%
    tq_get(get = "stock.prices",
           from = "2026-01-01",) %>%
    group_by(symbol) %>%
    tq_transmute(select = adjusted,
                 mutate_fun = periodReturn,
                 period = "monthly",
                 col_rename = "Rb")
Rb
## # A tibble: 9 × 3
## # Groups:   symbol [1]
##   symbol date             Rb
##   <chr>  <date>        <dbl>
## 1 ^IXIC  2026-01-30  0.00973
## 2 ^IXIC  2026-02-27 -0.0338 
## 3 ^IXIC  2026-03-31 -0.0475 
## 4 ^IXIC  2026-04-30  0.153  
## 5 ^IXIC  2026-05-29  0.0836 
## 6 ^IXIC  2026-06-30 -0.0281 
## 7 ^IXIC  2026-07-31 -0.0320 
## 8 ^IXIC  2026-08-31  0.0393 
## 9 ^IXIC  2026-09-21  0.0285

3 join the two tables

# RaRb <- left_join(Ra, Rb, by = c("date" = "date"))
#RaRb


# trying to fix an error 
RaRb <- left_join(Ra, 
                   Rb %>% ungroup() %>% select(-symbol), 
                   by = "date")
RaRb
## # A tibble: 543 × 4
## # Groups:   symbol [3]
##    symbol date             Ra    Rb
##    <chr>  <date>        <dbl> <dbl>
##  1 AAPL   2026-01-02  0          NA
##  2 AAPL   2026-01-05 -0.0138     NA
##  3 AAPL   2026-01-06 -0.0183     NA
##  4 AAPL   2026-01-07 -0.00774    NA
##  5 AAPL   2026-01-08 -0.00496    NA
##  6 AAPL   2026-01-09  0.00127    NA
##  7 AAPL   2026-01-12  0.00339    NA
##  8 AAPL   2026-01-13  0.00307    NA
##  9 AAPL   2026-01-14 -0.00418    NA
## 10 AAPL   2026-01-15 -0.00673    NA
## # ℹ 533 more rows

4 Calculate CAPM

RaRb_capm <- RaRb %>%
    group_by(symbol) %>%
    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 AAPL           -75.7 -0.0062      0.0053          -0.789  0.0748   -2.12
## 2 GOOG           202.   0.0214      0.0151         205.     0.103    -1.48
## 3 NFLX           -26.7  0.0194     -0.0003         127.    -0.147    -2.35
## # ℹ 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>

Google and Netflix beat the market ## Which stock has a positively skewed distribution of returns?

Ra_skew <- Ra %>%
    group_by(symbol) %>%
    tq_performance(Ra = Ra,
                   
                   performance_fun = table.Distributions)
Ra_skew %>%
    select(symbol, Sampleskewness)
## # A tibble: 3 × 2
## # Groups:   symbol [3]
##   symbol Sampleskewness
##   <chr>           <dbl>
## 1 AAPL           -0.579
## 2 GOOG            0.718
## 3 NFLX            0.688

Google and Netflix had positive skews