# Load Packages
library(tidyverse) # core functions
library(tidyquant) # for financial analysis

1 Get stock prices and convert to returns

Ra <- c("HLIT", "ABR", "RYLD", "GM", "COPX", "AMLP", "XYLD", "FDRR", "DBP", "PRFT", "IUSV", "BSV", "FRC", "META", "JPM", "JNJ", "NXST", "SPY") %>%
    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: 252 × 3
## # Groups:   symbol [18]
##    symbol date             Ra
##    <chr>  <date>        <dbl>
##  1 HLIT   2022-01-31 -0.105  
##  2 HLIT   2022-02-28 -0.135  
##  3 HLIT   2022-03-31 -0.00215
##  4 HLIT   2022-04-29 -0.107  
##  5 HLIT   2022-05-31  0.160  
##  6 HLIT   2022-06-30 -0.0997 
##  7 HLIT   2022-07-29  0.260  
##  8 HLIT   2022-08-31  0.0311 
##  9 HLIT   2022-09-30  0.161  
## 10 HLIT   2022-10-31  0.182  
## # … with 242 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: 14 × 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-31  0.0390
## 11 2022-11-30  0.0437
## 12 2022-12-30 -0.0873
## 13 2023-01-31  0.107 
## 14 2023-02-10  0.0115

3 Join the two tables

RaRb <- left_join(Ra, Rb, by = c("date" = "date"))
RaRb
## # A tibble: 252 × 4
## # Groups:   symbol [18]
##    symbol date             Ra      Rb
##    <chr>  <date>        <dbl>   <dbl>
##  1 HLIT   2022-01-31 -0.105   -0.101 
##  2 HLIT   2022-02-28 -0.135   -0.0343
##  3 HLIT   2022-03-31 -0.00215  0.0341
##  4 HLIT   2022-04-29 -0.107   -0.133 
##  5 HLIT   2022-05-31  0.160   -0.0205
##  6 HLIT   2022-06-30 -0.0997  -0.0871
##  7 HLIT   2022-07-29  0.260    0.123 
##  8 HLIT   2022-08-31  0.0311  -0.0464
##  9 HLIT   2022-09-30  0.161   -0.105 
## 10 HLIT   2022-10-31  0.182    0.0390
## # … with 242 more rows

4 Calculate CAPM

RaRb_capm <- RaRb %>%
    tq_performance(Ra = Ra, 
                   Rb = Rb, 
                   performance_fun = table.CAPM)

RaRb_capm
## # A tibble: 18 × 13
## # Groups:   symbol [18]
##    symbol ActivePremium   Alpha Annual…¹    Beta `Beta-` `Beta+` Corre…² Corre…³
##    <chr>          <dbl>   <dbl>    <dbl>   <dbl>   <dbl>   <dbl>   <dbl>   <dbl>
##  1 HLIT          0.352   0.0334   0.483   0.870    1.01   1.10    0.536   0.0483
##  2 ABR           0.113   0.0247   0.341   1.40     0.851  1.92    0.800   0.0006
##  3 RYLD          0.137   0.0019   0.023   0.483    0.529  0.158   0.850   0.0001
##  4 GM           -0.055   0.007    0.0871  1.41     1.60   1.10    0.834   0.0002
##  5 COPX          0.286   0.0268   0.373   0.845    1.40   0.508   0.566   0.0347
##  6 AMLP          0.488   0.0323   0.464   0.540    0.981  0.734   0.549   0.0421
##  7 XYLD          0.149   0.002    0.024   0.439    0.180  0.172   0.863   0.0001
##  8 FDRR          0.164   0.008    0.100   0.642    0.635  0.215   0.852   0.0001
##  9 DBP           0.227   0.0034   0.0417  0.140    0.142  0.104   0.252   0.385 
## 10 PRFT         -0.109  -0.0071  -0.0821  1.10     0.472  0.654   0.781   0.001 
## 11 IUSV          0.233   0.0138   0.179   0.640    0.632  0.34    0.860   0.0001
## 12 BSV           0.186  -0.002   -0.0232  0.0822   0.106  0.188   0.604   0.022 
## 13 FRC          -0.0427 -0.0063  -0.0735  0.874    1.17   2.00    0.789   0.0008
## 14 META         -0.207  -0.0127  -0.142   0.961   -0.14   0.302   0.428   0.127 
## 15 JPM           0.147   0.0119   0.153   0.801    1.53  -0.0878  0.661   0.0101
## 16 JNJ           0.202  -0.0018  -0.0208 -0.0244  -0.468 -0.806  -0.0458  0.876 
## 17 NXST          0.489   0.0425   0.649   0.972    2.31   1.70    0.736   0.0027
## 18 SPY           0.113   0.006    0.0743  0.772    0.756  0.512   0.958   0     
## # … with 4 more variables: InformationRatio <dbl>, `R-squared` <dbl>,
## #   TrackingError <dbl>, TreynorRatio <dbl>, and abbreviated variable names
## #   ¹​AnnualizedAlpha, ²​Correlation, ³​`Correlationp-value`
# Stocks that beat ^IXIC
filter(RaRb_capm, Alpha > 0)
## # A tibble: 13 × 13
## # Groups:   symbol [13]
##    symbol ActiveP…¹  Alpha Annua…²  Beta `Beta-` `Beta+` Corre…³ Corre…⁴ Infor…⁵
##    <chr>      <dbl>  <dbl>   <dbl> <dbl>   <dbl>   <dbl>   <dbl>   <dbl>   <dbl>
##  1 HLIT       0.352 0.0334  0.483  0.870   1.01   1.10     0.536  0.0483   0.916
##  2 ABR        0.113 0.0247  0.341  1.40    0.851  1.92     0.800  0.0006   0.359
##  3 RYLD       0.137 0.0019  0.023  0.483   0.529  0.158    0.850  0.0001   0.819
##  4 GM        -0.055 0.007   0.0871 1.41    1.60   1.10     0.834  0.0002  -0.194
##  5 COPX       0.286 0.0268  0.373  0.845   1.40   0.508    0.566  0.0347   0.828
##  6 AMLP       0.488 0.0323  0.464  0.540   0.981  0.734    0.549  0.0421   1.85 
##  7 XYLD       0.149 0.002   0.024  0.439   0.180  0.172    0.863  0.0001   0.864
##  8 FDRR       0.164 0.008   0.100  0.642   0.635  0.215    0.852  0.0001   1.10 
##  9 DBP        0.227 0.0034  0.0417 0.140   0.142  0.104    0.252  0.385    0.801
## 10 IUSV       0.233 0.0138  0.179  0.640   0.632  0.34     0.860  0.0001   1.59 
## 11 JPM        0.147 0.0119  0.153  0.801   1.53  -0.0878   0.661  0.0101   0.564
## 12 NXST       0.489 0.0425  0.649  0.972   2.31   1.70     0.736  0.0027   1.96 
## 13 SPY        0.113 0.006   0.0743 0.772   0.756  0.512    0.958  0        1.25 
## # … 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 skewed distribution of returns

RaRb_skewness <- RaRb %>%
    tq_performance(Ra = Ra,
                   Rb = NULL,
                   performance_fun = skewness)

RaRb_skewness
## # A tibble: 18 × 2
## # Groups:   symbol [18]
##    symbol skewness.1
##    <chr>       <dbl>
##  1 HLIT      0.400  
##  2 ABR       0.236  
##  3 RYLD      0.168  
##  4 GM        0.359  
##  5 COPX      0.324  
##  6 AMLP     -0.417  
##  7 XYLD      0.208  
##  8 FDRR      0.131  
##  9 DBP       0.850  
## 10 PRFT     -0.518  
## 11 IUSV      0.230  
## 12 BSV       0.268  
## 13 FRC       0.796  
## 14 META      0.00420
## 15 JPM       0.624  
## 16 JNJ       0.0402 
## 17 NXST     -0.275  
## 18 SPY       0.181
# Stocks that have a positive skew
filter(RaRb_skewness, skewness.1 > 0)
## # A tibble: 15 × 2
## # Groups:   symbol [15]
##    symbol skewness.1
##    <chr>       <dbl>
##  1 HLIT      0.400  
##  2 ABR       0.236  
##  3 RYLD      0.168  
##  4 GM        0.359  
##  5 COPX      0.324  
##  6 XYLD      0.208  
##  7 FDRR      0.131  
##  8 DBP       0.850  
##  9 IUSV      0.230  
## 10 BSV       0.268  
## 11 FRC       0.796  
## 12 META      0.00420
## 13 JPM       0.624  
## 14 JNJ       0.0402 
## 15 SPY       0.181