# Load packages
library(tidyverse)
library(tidyquant)

1 Get stock prices and convert to returns

Ra <- c("AAPL", "MSFT", "META") %>%
    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: 27 × 3
## # Groups:   symbol [3]
##    symbol date            Ra
##    <chr>  <date>       <dbl>
##  1 AAPL   2022-01-31 -0.0397
##  2 AAPL   2022-02-28 -0.0541
##  3 AAPL   2022-03-31  0.0575
##  4 AAPL   2022-04-29 -0.0971
##  5 AAPL   2022-05-31 -0.0545
##  6 AAPL   2022-06-30 -0.0814
##  7 AAPL   2022-07-29  0.189 
##  8 AAPL   2022-08-31 -0.0312
##  9 AAPL   2022-09-21 -0.0223
## 10 MSFT   2022-01-31 -0.0710
## # … with 17 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: 9 × 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-21 -0.0504

3 Join the two data 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 AAPL   2022-01-31 -0.0397 -0.101 
##  2 AAPL   2022-02-28 -0.0541 -0.0343
##  3 AAPL   2022-03-31  0.0575  0.0341
##  4 AAPL   2022-04-29 -0.0971 -0.133 
##  5 AAPL   2022-05-31 -0.0545 -0.0205
##  6 AAPL   2022-06-30 -0.0814 -0.0871
##  7 AAPL   2022-07-29  0.189   0.123 
##  8 AAPL   2022-08-31 -0.0312 -0.0464
##  9 AAPL   2022-09-21 -0.0223 -0.0504
## 10 MSFT   2022-01-31 -0.0710 -0.101 
## # … with 17 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 AAPL      0.171   0.0223  0.303  1.06    0.396   1.47    0.928  0.0003   1.49 
## 2 MSFT      0.0117 -0.0076 -0.0872 0.767   0.513   0.685   0.949  0.0001   0.128
## 3 META     -0.318  -0.0657 -0.558  0.550  -0.192  -0.750   0.366  0.332   -0.814
## # … 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 positive skewd distribution of returns?

RaRb_capm <- RaRb %>%
    tq_performance(Ra = Ra,
                   Rb = Rb,
                   performance_fun = BetaCoSkewness)
RaRb_capm
## # A tibble: 3 × 2
## # Groups:   symbol [3]
##   symbol BetaCoSkewness.1
##   <chr>             <dbl>
## 1 AAPL              1.48 
## 2 MSFT              0.904
## 3 META              0.734

Between my chosen stocks, APPL, MSFT & META. Only Apple has a positive alpha of 0.0223 which indicates it has beaten the market up to this point.

Does any of your stocks has positively skewed distribution? When I inserted “BetaCoSkewness” from the $moment.funs section, I could identify that all my stockes turned out positive. APPL= 1.48, MSFT= 0.904, META= 0.734