# Load packages
# Core
library(tidyverse)
library(tidyquant)
Visualize and compare skewness of your portfolio and its assets.
Choose your stocks.
from 2012-12-31 to 2017-12-31
symbols <- c("PLTR", "IBM", "BLK", "TSM", "SLB")
prices <- tq_get(x = symbols,
get = "stock.prices",
fro = "2020-12-31",
to = "2024-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 <- asset_returns_tbl %>% distinct(asset) %>% pull()
symbols
## [1] "BLK" "IBM" "PLTR" "SLB" "TSM"
weights <- c(0.4, 0.2, 0.2, 0.1, 0.1)
weights
## [1] 0.4 0.2 0.2 0.1 0.1
w_tbl <- tibble(symbols, weights)
portfolio_returns_tbl <- asset_returns_tbl %>%
tq_portfolio(assets_col = asset,
returns_col = returns,
weights = w_tbl,
rebalance_on = "months")
asset_skewness_tbl2 <- asset_returns_tbl %>%
group_by(asset) %>%
summarise(skew = skewness(returns)) %>%
ungroup() %>%
add_row(tibble(asset = "Portfolio",
skew = skewness(portfolio_returns_tbl$portfolio.returns)))
asset_skewness_tbl2
## # A tibble: 6 × 2
## asset skew
## <chr> <dbl>
## 1 BLK -0.321
## 2 IBM -0.164
## 3 PLTR 0.596
## 4 SLB 0.756
## 5 TSM 0.434
## 6 Portfolio -0.0969
asset_skewness_tbl2 %>%
ggplot(aes(x = asset, y = skew)) +
geom_point() +
ggrepel::geom_text_repel(aes(label = asset),
data = asset_skewness_tbl2 %>%
filter(asset == "Portfolio"))
Is any asset in your portfolio more likely to return extreme positive returns than your portfolio collectively? Discuss in terms of skewness. You may also refer to the distribution of returns you plotted in Code along 4.
There a few assets that are more likely to return extreme positive returns in comparison to my portfolio. To start PLTR and TSM have a skewness of .6 and .45 respectively. In addition, SLB has the highest skewness of the portfolio holdings, sitting at over .75. This means that through the holding period, these three stocks have produced right skewed curves with tails on the right side of the curve. However, since BLK and IBM have had negatively skewed distributions, the portfolio nearly has a symmetrical bell curve shape.