# 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("WMT", "AAPL", "NFLX", "GOOGL")
prices <- tq_get(x = symbols,
get = "stock,prices",
from = "2012-12-31",
to = "2017-12-31")
prices
## # A tibble: 5,040 × 8
## symbol date open high low close volume adjusted
## <chr> <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
## 1 WMT 2012-12-31 22.5 22.8 22.5 22.7 21037500 17.4
## 2 WMT 2013-01-02 23.0 23.1 22.8 23.1 31172400 17.7
## 3 WMT 2013-01-03 23.1 23.1 22.8 22.9 26730300 17.6
## 4 WMT 2013-01-04 22.9 23.1 22.8 23.0 19314000 17.7
## 5 WMT 2013-01-07 22.9 23.0 22.7 22.8 18604200 17.5
## 6 WMT 2013-01-08 22.8 23.0 22.7 22.9 17600700 17.5
## 7 WMT 2013-01-09 22.9 22.9 22.7 22.9 15165600 17.5
## 8 WMT 2013-01-10 22.9 23.0 22.6 22.8 34361400 17.5
## 9 WMT 2013-01-11 22.9 22.9 22.7 22.9 18673500 17.6
## 10 WMT 2013-01-14 22.8 22.9 22.7 22.8 16471200 17.5
## # ℹ 5,030 more rows
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"))
asset_returns_tbl
## # A tibble: 240 × 3
## asset date returns
## <chr> <date> <dbl>
## 1 AAPL 2013-01-31 -0.156
## 2 AAPL 2013-02-28 -0.0256
## 3 AAPL 2013-03-28 0.00285
## 4 AAPL 2013-04-30 0.000271
## 5 AAPL 2013-05-31 0.0222
## 6 AAPL 2013-06-28 -0.126
## 7 AAPL 2013-07-31 0.132
## 8 AAPL 2013-08-30 0.0804
## 9 AAPL 2013-09-30 -0.0217
## 10 AAPL 2013-10-31 0.0920
## # ℹ 230 more rows
# symbols
symbols <- asset_returns_tbl %>% distinct(asset) %>% pull()
symbols
## [1] "AAPL" "GOOGL" "NFLX" "WMT"
# weights
weights <- c(0.35, 0.30, 0.20, 0.15)
weights
## [1] 0.35 0.30 0.20 0.15
w_tbl <- tibble(symbols, weights)
w_tbl
## # A tibble: 4 × 2
## symbols weights
## <chr> <dbl>
## 1 AAPL 0.35
## 2 GOOGL 0.3
## 3 NFLX 0.2
## 4 WMT 0.15
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: 60 × 2
## date returns
## <date> <dbl>
## 1 2013-01-31 0.0849
## 2 2013-02-28 0.0362
## 3 2013-03-28 0.00894
## 4 2013-04-30 0.0435
## 5 2013-05-31 0.0287
## 6 2013-06-28 -0.0555
## 7 2013-07-31 0.0849
## 8 2013-08-30 0.0350
## 9 2013-09-30 0.0216
## 10 2013-10-31 0.0949
## # ℹ 50 more rows
portfolio_skew_tidyquant_builtin_percent <- portfolio_returns_tbl %>%
tq_performance(Ra = returns,
performance_fun = table.Stats) %>%
select(Skewness)
portfolio_skew_tidyquant_builtin_percent
## # A tibble: 1 × 1
## Skewness
## <dbl>
## 1 -0.474
# Data transformation: calculate skewness
asset_skewness_tbl <- asset_returns_tbl %>%
group_by(asset) %>%
summarise(skew = skewness(returns)) %>%
ungroup() %>%
# Add portfolio units
add_row(tibble(asset = "Portfolio",
skew = skewness(portfolio_returns_tbl$returns)))
asset_skewness_tbl
## # A tibble: 5 × 2
## asset skew
## <chr> <dbl>
## 1 AAPL -0.555
## 2 GOOGL 0.867
## 3 NFLX 0.909
## 4 WMT 0.0723
## 5 Portfolio -0.474
# 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 on the skewness comparison in my scatterplot graph, both Netflix and Google are moderately right skewed but very close to being highly right skewed. Each line is a 0.25 margin and since they are both right at the top, more than halfway above a positive 0.75 skewness rate, I can only assume they are rated at about 0.9 with Netflix being slightly higher. A skewness rating of 0.5-1.0 is considered “moderate” but these 2 stocks are on the verge of returning extreme positive returns (>1.0 skewness). Compared to the portfolio as a whole, they have more frequent surges in stock price and are more likely to deliver extreme returns on a monthly basis, but that doesn’t necessarily mean they outperform the Portfolio as a whole.
The portfolio is rated at about a -0.5 skewness rating so it is considered moderately left skewed. In terms of extreme positive returns, the portfolio has a whole has a lot lower of a chance doing this but skewness isn’t the only factor that should come into play when determining whether or not to invest in this particular portfolio. Moderately left skewed investments tend to deliver small, yet steady and consistent returns while the moderately right skewed stocks, as mentioned above, typically ride on the rare surges they have to make the mean return look greater than it really is.