# Load packages
library(tidyverse)
library(tidyquant)

1 Get stock prices and convert to returns

Ra <- c("GM", "GOOG", "TSLA") %>%
    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: 30 × 3
## # Groups:   symbol [3]
##    symbol date            Ra
##    <chr>  <date>       <dbl>
##  1 GM     2022-01-31 -0.138 
##  2 GM     2022-02-28 -0.114 
##  3 GM     2022-03-31 -0.0638
##  4 GM     2022-04-29 -0.133 
##  5 GM     2022-05-31  0.0203
##  6 GM     2022-06-30 -0.179 
##  7 GM     2022-07-29  0.142 
##  8 GM     2022-08-31  0.0562
##  9 GM     2022-09-30 -0.160 
## 10 GM     2022-10-18  0.0673
## # … with 20 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: 10 × 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-18  0.0186

3 Join the two tables

RaRb <- left_join(Ra, Rb, by = c("date" = "date"))
RaRb
## # A tibble: 30 × 4
## # Groups:   symbol [3]
##    symbol date            Ra      Rb
##    <chr>  <date>       <dbl>   <dbl>
##  1 GM     2022-01-31 -0.138  -0.101 
##  2 GM     2022-02-28 -0.114  -0.0343
##  3 GM     2022-03-31 -0.0638  0.0341
##  4 GM     2022-04-29 -0.133  -0.133 
##  5 GM     2022-05-31  0.0203 -0.0205
##  6 GM     2022-06-30 -0.179  -0.0871
##  7 GM     2022-07-29  0.142   0.123 
##  8 GM     2022-08-31  0.0562 -0.0464
##  9 GM     2022-09-30 -0.160  -0.105 
## 10 GM     2022-10-18  0.0673  0.0186
## # … with 20 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 GM       -0.13   -0.0093 -0.106  1.17    1.54    1.25    0.808  0.0047  -0.554
## 2 GOOG      0.0206 -0.0012 -0.0146 0.888   1.27    0.195   0.895  0.0005   0.168
## 3 TSLA     -0.141   0.0195  0.261  1.82    0.667   3.42    0.779  0.0079  -0.313
## # … 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 sweked distribution of returns?

Tesla is the stock that has positive returns. As the same with General Motors, Tesla sees a decrease in returns to start but both end up with positive returns.It took a few months for both to gain returns but by the end of the year you can see a positive outcome.