# Load packages

# Core
library(tidyverse)
library(tidyquant)

Goal

Visualize and compare skewness of your portfolio and its assets.

Choose your stocks.

from 2012-12-31 to 2017-12-31

1 Import stock prices

symbols <- c("TSLA", "META", "XOM", "AAPL", "PG", "AMZN")

prices <- tq_get(x    = symbols, 
                 get. = "stock.prices",
                 from = "2012-12-31", 
                 to   = "2017-12-31")

2 Convert prices to returns (monthly)

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"))

3 Assign a weight to each asset (change the weigting scheme)

# symbols
symbols <- asset_returns_tbl %>% distinct(asset) %>% pull()
symbols
## [1] "AAPL" "AMZN" "META" "PG"   "TSLA" "XOM"
# weights
weights <- c(0.2, 0.2, 0.2, 0.1, 0.2, 0.1)
weights
## [1] 0.2 0.2 0.2 0.1 0.2 0.1
w_tbl <- tibble(symbols, weights)
w_tbl
## # A tibble: 6 × 2
##   symbols weights
##   <chr>     <dbl>
## 1 AAPL        0.2
## 2 AMZN        0.2
## 3 META        0.2
## 4 PG          0.1
## 5 TSLA        0.2
## 6 XOM         0.1

4 Build a portfolio

#?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: 60 × 2
##    date        returns
##    <date>        <dbl>
##  1 2013-01-31  0.0458 
##  2 2013-02-28 -0.0450 
##  3 2013-03-28  0.00821
##  4 2013-04-30  0.0767 
##  5 2013-05-31  0.111  
##  6 2013-06-28  0.00424
##  7 2013-07-31  0.174  
##  8 2013-08-30  0.0615 
##  9 2013-09-30  0.0789 
## 10 2013-10-31  0.0223 
## # ℹ 50 more rows

5 Compute Skewness

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.405

6 Plot: Skewness Comparison

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.

# 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: 7 × 2
##   asset         skew
##   <chr>        <dbl>
## 1 AAPL      -0.555  
## 2 AMZN       0.187  
## 3 META       1.15   
## 4 PG         0.0728 
## 5 TSLA       0.944  
## 6 XOM       -0.00356
## 7 Portfolio  0.405
# 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") 

Observations from Skewness Plot

Upon analyzing the skewness values of my individual assets in comparison to the portfolio skewness (0.405), the following observations can be made:

Positive skewness assets include AMZN, META, PG, and TSLA. These assets exhibit positive skewness values, indicating a distribution of returns with a longer tail to the right. It is essential to note, however, that this does not necessarily imply extreme positive returns; it signifies a longer tail to the right, unrelated to the actual magnitude of returns.

Among the positively skewed assets, META and TSLA have relatively higher skewness values, suggesting a greater likelihood of returns with a longer right tail compared to the ohter assets. This implies a higher probability of observing extreme positive returns for these two assets.

Negative skewness is observed in AAPL and XOM. These negative skewness values indicate that the distribution of their returns has a longer tail to the left. Similar to positive values, negative skewness does not signify extreme negative returns but rather a longer left tail. However, in relation to the portfolio’s skewness (0.405), the negative-skewed assets imply a lower likelihood of extreme positive returns compared to a symmetric distribution.

It is worth mentioning that both AAPL and XOM have skewness values closer to zero, indicating distributions that are closer to symmetry. This suggests that these assets have return distributions that are less skewed compared to the other assets in the portfolio.

To summarize, while assets like META and TSLA exhibit positive skewness values, suggesting a higher probability of returns with a longer right tail and a greater potential for extreme positive returns, it does not necessarily reflect the magnitude 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, 2012-2016",
         y = "Frequency",
         x = "Rate of Returns",
         capition = "A typical monthly return is higher for SPY and IJS than for AGG, EEM, and EFA")

Observations from Apply 4

Based on the results from Apply 4, I also have to take the following into account:

AAPL, AMZN, and META had higher rates of return compared to PG, TSLA, and XOM.

This indicates that AAPL, AMZN, and META had more positive returns on average during the observed period. The higher rates of return suggest a greater likelihood of experiencing extreme positive returns for these assets.

The peak of the line plot was most positive for AAPL, AMZN, and META.

A higher peak in the line plot suggests a higher frequency of positive returns and a potential for extreme positive returns.

Conclusion

Combining both observations, the following conclusion can be drawn:

AMZN and META have exhibited higher skewness values than the portfolio, indicating a higher probability of returns with a longer right tail and a greater potential for extreme positive returns, keeping in mind that this does not solely reflect the magnitude of returns. The line plot’s peak being most positive for AMZN and META further supports the notion that these assets have a greater potential for extreme positive returns.

Additionally, the line plot’s peak being most positive for AMZN and META further supports the notion that these assets have a greater potential for extreme positive returns.

Considering both the skewness values and the distribution of monthly returns, it appears that AMZN and META have a higher likelihood of returning extreme positive returns compared to AAPL, PG, TSLA, and XOM in the portfolio.

While AAPL does have a positive value for the peak in the line plot from Apply 4, it exhibits a very low skewness. This suggests that the likelihood for AAPL to show extreme positive returns may not be as significant as that for AMZN and META, which possess both high skewness and positive peaks in the line plots.