# Load packages
# Core
library(tidyverse)
library(tidyquant)
Visualize and compare skewness of your portfolio and its assets.
Choose your stocks.
from 2017-12-31 to 2022-12-31
# Choose stocks
symbols <- c("MSFT", "JPM", "GM", "TMUS", "IRVRF")
prices <- tq_get(x = symbols,
get = "stock.prices",
from = "2017-12-31",
to = "2022-12-31")
asset_returns_tbl <- prices %>%
group_by(symbol) %>%
tq_transmute(select = adjusted,
mutate_fun = periodReturn,
period = "monthly",
type = "log") %>%
slice(-1) %>%
ungroup() %>%
set_names(c("asset", "date", "returns"))
# symbols
symbols <- asset_returns_tbl %>% distinct(asset) %>% pull()
symbols
## [1] "GM" "IRVRF" "JPM" "MSFT" "TMUS"
# weights
weights <- c(0.25, 0.2, 0.2, 0.1, 0.25)
weights
## [1] 0.25 0.20 0.20 0.10 0.25
w_tbl <- tibble(symbols, weights)
w_tbl
## # A tibble: 5 × 2
## symbols weights
## <chr> <dbl>
## 1 GM 0.25
## 2 IRVRF 0.2
## 3 JPM 0.2
## 4 MSFT 0.1
## 5 TMUS 0.25
portfolio_returns_tbl <- asset_returns_tbl %>%
tq_portfolio(assets_col = asset,
returns_col = returns,
weights = w_tbl,
rebalance_on = "months",
col_rename = "returns")
portfolio_returns_tbl
## # A tibble: 59 × 2
## date returns
## <date> <dbl>
## 1 2018-02-28 0.00823
## 2 2018-03-29 -0.0253
## 3 2018-04-30 0.0341
## 4 2018-05-31 -0.00672
## 5 2018-06-29 -0.0144
## 6 2018-07-31 -0.00515
## 7 2018-08-31 0.0271
## 8 2018-09-28 -0.0164
## 9 2018-10-31 0.0523
## 10 2018-11-30 0.0644
## # … with 49 more rows
# Data transformation: calculate skewness
asset_skewness_tbl <- asset_returns_tbl %>%
group_by(asset) %>%
summarise(skew = skewness(returns)) %>%
ungroup() %>%
# Add portfolio skewness
add_row(tibble(asset = "Portfolio",
skew = skewness(portfolio_returns_tbl$returns)))
asset_skewness_tbl
## # A tibble: 6 × 2
## asset skew
## <chr> <dbl>
## 1 GM -0.303
## 2 IRVRF -0.376
## 3 JPM -0.339
## 4 MSFT -0.129
## 5 TMUS 0.363
## 6 Portfolio -0.742
# Plot skewness
asset_skewness_tbl %>%
ggplot(aes(x = asset, y = skew, color = asset)) +
geom_point() +
ggrepel::geom_text_repel(aes(label = asset),
data = asset_skewness_tbl %>%
filter(asset == "Portfolio")) +
labs(y = "skewness")
# Plot distribution of returns
asset_returns_tbl %>%
ggplot(aes(x = returns)) +
geom_density(aes(color = asset), show.legend = FALSE, alpha = 1) +
geom_histogram(aes(fill = asset), show.legend = FALSE, alpha = 0.3, binwidth = 0.01) +
facet_wrap(~asset, ncol = 1, scales = "free_y") +
# Labeling
labs(title = "Distribution of Monthly Returns, 2017-2022",
y = "Frequency",
x = "Rate of Returns")
All but one of the assets in the portfolio are moderately left-skewed distributions. TMUS is the only asset that is right-skewed, indicating that this asset has a long tail on the right side of its monthly returns distribution. When interpreting the skewness along with the data in the distribution plot, TMUS is more likely to yield large positive gains than the collective portfolio. The other assets are more evenly distributed with slight negative skew, meaning they have small tails on the left side of the center of their distributions. With these assets we can more likely expect frequent small gains and less frequent large losses.