Get Stock Prices and Convert to Returns
Ra <- c("JPM", "BAC", "WFC") |>
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: 171 × 3
## # Groups: symbol [3]
## symbol date Ra
## <chr> <date> <dbl>
## 1 JPM 2022-01-31 -0.0755
## 2 JPM 2022-02-28 -0.0458
## 3 JPM 2022-03-31 -0.0386
## 4 JPM 2022-04-29 -0.118
## 5 JPM 2022-05-31 0.108
## 6 JPM 2022-06-30 -0.148
## 7 JPM 2022-07-29 0.0335
## 8 JPM 2022-08-31 -0.0141
## 9 JPM 2022-09-30 -0.0812
## 10 JPM 2022-10-31 0.215
## # ℹ 161 more rows
Get Baseline and Convert to Returns
Rb <- "^IXIC" |>
tq_get(get = "stock.prices",
from = "2022-01-01") |>
group_by(symbol) |>
tq_transmute(select = adjusted,
mutate_fun = periodReturn,
period = "monthly",
col_rename = "Rb")
Rb
## # A tibble: 57 × 3
## # Groups: symbol [1]
## symbol date Rb
## <chr> <date> <dbl>
## 1 ^IXIC 2022-01-31 -0.101
## 2 ^IXIC 2022-02-28 -0.0343
## 3 ^IXIC 2022-03-31 0.0341
## 4 ^IXIC 2022-04-29 -0.133
## 5 ^IXIC 2022-05-31 -0.0205
## 6 ^IXIC 2022-06-30 -0.0871
## 7 ^IXIC 2022-07-29 0.123
## 8 ^IXIC 2022-08-31 -0.0464
## 9 ^IXIC 2022-09-30 -0.105
## 10 ^IXIC 2022-10-31 0.0390
## # ℹ 47 more rows
Join the Two Tables
RaRb <- left_join(Ra, Rb, by = c("date" = "date"))
RaRb
## # A tibble: 171 × 5
## symbol.x date Ra symbol.y Rb
## <chr> <date> <dbl> <chr> <dbl>
## 1 JPM 2022-01-31 -0.0755 ^IXIC -0.101
## 2 JPM 2022-02-28 -0.0458 ^IXIC -0.0343
## 3 JPM 2022-03-31 -0.0386 ^IXIC 0.0341
## 4 JPM 2022-04-29 -0.118 ^IXIC -0.133
## 5 JPM 2022-05-31 0.108 ^IXIC -0.0205
## 6 JPM 2022-06-30 -0.148 ^IXIC -0.0871
## 7 JPM 2022-07-29 0.0335 ^IXIC 0.123
## 8 JPM 2022-08-31 -0.0141 ^IXIC -0.0464
## 9 JPM 2022-09-30 -0.0812 ^IXIC -0.105
## 10 JPM 2022-10-31 0.215 ^IXIC 0.0390
## # ℹ 161 more rows
Calculate CAPM
RaRb_capm <- RaRb |>
tq_performance(Ra = Ra,
Rb = Rb,
performance_fun = table.CAPM,
scale = 12)
RaRb_capm
## # A tibble: 1 × 17
## ActivePremium Alpha AlphaRobust AnnualizedAlpha Beta `Beta-` `Beta-Robust`
## <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 0.0374 0.0074 0.0082 0.0929 0.658 1.23 1.31
## # ℹ 10 more variables: `Beta+` <dbl>, `Beta+Robust` <dbl>, BetaRobust <dbl>,
## # Correlation <dbl>, `Correlationp-value` <dbl>, InformationRatio <dbl>,
## # `R-squared` <dbl>, `R-squaredRobust` <dbl>, TrackingError <dbl>,
## # TreynorRatio <dbl>
Which Stock has a Positive Skewed Distribution of Returns
Ra |>
group_by(symbol) |>
summarise(Skewness = PerformanceAnalytics::skewness(Ra))
## # A tibble: 3 × 2
## symbol Skewness
## <chr> <dbl>
## 1 BAC -0.0254
## 2 JPM 0.0710
## 3 WFC -0.321
JP Morgan is the only company out of the three that has a positive
skewed distribution of returns.