# Load packages
# Core
library(tidyverse)
library(tidyquant)
Visualize and compare skewness of your portfolio and its assets.
Choose your stocks.
from 2020-12-31 to 2024-12-31
symbols <- c("UAL", "AAL", "LUV", "DAL")
prices <- tq_get(x = symbols,
get = "stock.prices",
from = "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
symbols <- asset_returns_tbl %>% distinct(asset) %>% pull()
symbols
## [1] "AAL" "DAL" "LUV" "UAL"
# weights
weights <- c(0.2, 0.1, 0.4, 0.3)
weights
## [1] 0.2 0.1 0.4 0.3
w_tbl <- tibble(symbols, weights)
w_tbl
## # A tibble: 4 × 2
## symbols weights
## <chr> <dbl>
## 1 AAL 0.2
## 2 DAL 0.1
## 3 LUV 0.4
## 4 UAL 0.3
# ?tq_portfolio
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: 48 × 2
## date returns
## <date> <dbl>
## 1 2021-01-29 -0.0359
## 2 2021-02-26 0.258
## 3 2021-03-31 0.0733
## 4 2021-04-30 -0.0277
## 5 2021-05-28 0.0361
## 6 2021-06-30 -0.128
## 7 2021-07-30 -0.0697
## 8 2021-08-31 -0.0103
## 9 2021-09-30 0.0308
## 10 2021-10-29 -0.0646
## # ℹ 38 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: 5 × 2
## asset skew
## <chr> <dbl>
## 1 AAL -0.170
## 2 DAL -0.236
## 3 LUV 0.132
## 4 UAL 0.355
## 5 Portfolio -0.0165
# 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")
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.
Based off of the skewness comparison plot and distribution of quarterly returns from Apply 4, UAL has the highest likelihood to return extreme positive returns compared to my portfolio. Skewness shown for UAL is 0.355. LUV is also more likely to as well, with 0.132 skewness. Both of these values are above that of the portfolio, which is shown at -0.0165. Below that are AAL and DAL showing -0.17 and -0.236, respectively. Looking at the distribution of quarterly returns, this matches up. However, it is also important to consider that UAL also has the potential for extreme loss as well according to Apply 4.