# Load Packages
library(tidyverse)
library(tidyquant)

1 Get stock prices and convert to returns

Ra <- c("TSLA", "SOFI", "MSFT") %>%
    tq_get(get  = "stock.prices",
           from = "2010-01-01") %>%
    group_by(symbol) %>%
    tq_transmute(select     = adjusted, 
                 mutate_fun = periodReturn, 
                 period     = "monthly", 
                 col_rename = "Ra")
Ra
## # A tibble: 394 × 3
## # Groups:   symbol [3]
##    symbol date             Ra
##    <chr>  <date>        <dbl>
##  1 TSLA   2010-06-30 -0.00251
##  2 TSLA   2010-07-30 -0.163  
##  3 TSLA   2010-08-31 -0.0231 
##  4 TSLA   2010-09-30  0.0477 
##  5 TSLA   2010-10-29  0.0701 
##  6 TSLA   2010-11-30  0.618  
##  7 TSLA   2010-12-31 -0.246  
##  8 TSLA   2011-01-31 -0.0950 
##  9 TSLA   2011-02-28 -0.00871
## 10 TSLA   2011-03-31  0.162  
## # ℹ 384 more rows

2 get baseline and convert to returns

Rb <- "XLK" %>%
    tq_get(get  = "stock.prices",
           from = "2010-01-01") %>%
    tq_transmute(select     = adjusted, 
                 mutate_fun = periodReturn, 
                 period     = "monthly", 
                 col_rename = "Rb")
Rb
## # A tibble: 177 × 2
##    date            Rb
##    <date>       <dbl>
##  1 2010-01-29 -0.0993
##  2 2010-02-26  0.0348
##  3 2010-03-31  0.0684
##  4 2010-04-30  0.0126
##  5 2010-05-28 -0.0748
##  6 2010-06-30 -0.0540
##  7 2010-07-30  0.0745
##  8 2010-08-31 -0.0561
##  9 2010-09-30  0.117 
## 10 2010-10-29  0.0578
## # ℹ 167 more rows

3 Join the two tables

RaRb <- left_join(Ra, Rb, by = c("date" = "date"))
RaRb
## # A tibble: 394 × 4
## # Groups:   symbol [3]
##    symbol date             Ra      Rb
##    <chr>  <date>        <dbl>   <dbl>
##  1 TSLA   2010-06-30 -0.00251 -0.0540
##  2 TSLA   2010-07-30 -0.163    0.0745
##  3 TSLA   2010-08-31 -0.0231  -0.0561
##  4 TSLA   2010-09-30  0.0477   0.117 
##  5 TSLA   2010-10-29  0.0701   0.0578
##  6 TSLA   2010-11-30  0.618   -0.0164
##  7 TSLA   2010-12-31 -0.246    0.0556
##  8 TSLA   2011-01-31 -0.0950   0.0318
##  9 TSLA   2011-02-28 -0.00871  0.0219
## 10 TSLA   2011-03-31  0.162   -0.0155
## # ℹ 384 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 TSLA          0.225   0.0215          0.291  1.39     1.36   1.90        0.388
## 2 SOFI         -0.268  -0.0012         -0.0147 1.10     5.16   1.78        0.280
## 3 MSFT          0.0377  0.004           0.0491 0.952    1.03   0.843       0.781
## # ℹ 5 more variables: `Correlationp-value` <dbl>, InformationRatio <dbl>,
## #   `R-squared` <dbl>, TrackingError <dbl>, TreynorRatio <dbl>

Which stock has a positively skewed distrabution of returns?

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 TSLA       1.22  
## 2 SOFI       1.92  
## 3 MSFT       0.0712