<p><div><div><div><div data-show-faces="false" data-height="21" data-width="100" data-layout="button_count" data-send="true" data-href="https://www.r-bloggers.com/introduction-to-skewness/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://rviews.rstudio.com/2017/12/13/introduction-to-skewness/"> R Views</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p> <br>
<br>


<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>



</p>
<p>In previous posts <a rel="nofollow" href="https://rviews.rstudio.com/2017/07/12/introduction-to-volatility/" target="_blank">here</a>, <a rel="nofollow" href="https://rviews.rstudio.com/2017/07/18/introduction-to-rolling-volatility/" target="_blank">here</a>, and <a rel="nofollow" href="https://rviews.rstudio.com/2017/07/21/visualizing-portfolio-volatility/" target="_blank">here</a>, we spent quite a bit of time on portfolio volatility, using the standard deviation of returns as a proxy for volatility. Today we will begin to a two-part series on additional statistics that aid our understanding of return dispersion: skewness and kurtosis. Beyond being fancy words and required vocabulary for CFA level 1, these two concepts are both important and fascinating for lovers of returns distributions. For today, we will focus on skewness.</p>
<p>Skewness is the degree to which returns are asymmetric around the mean. Since a normal distribution is symmetric around the mean, skewness can be taken as one measure of how returns are not distributed normally. Why does skewness matter? If portfolio returns are right, or positively, skewed, it implies numerous small negative returns and a few large positive returns. If portfolio returns are left, or negatively, skewed, it implies numerous small positive returns and few large negative returns. The phrase “large negative returns” should trigger Pavlovian sweating for investors, even if it’s preceded by a diminutive modifier like “just a few”. For a portfolio manager, a negatively skewed distribution of returns implies a portfolio at risk of rare but large losses. This makes us nervous and is a bit like saying, “I’m healthy, except for my occasional massive heart attack.”</p>
<p>Let’s get to it.</p>
<p>First, have a look at one equation for skewness:</p>
<p><span>\[Skew=\sum_{t=1}^n (x_i-\overline{x})^3/n \bigg/ (\sum_{t=1}^n (x_i-\overline{x})^2/n)^{3/2}\]</span></p>
<p>Skew has important substantive implications for risk, and is also a concept that lends itself to data visualization. In fact, I find the visualizations of skewness more illuminating than the numbers themselves (though the numbers are what matter in the end). In this section, we will cover how to calculate skewness using <code>xts</code> and <code>tidyverse</code> methods, how to calculate rolling skewness, and how to create several data visualizations as pedagogical aids. We will be working with our usual portfolio consisting of:</p>
<pre><code>+ SPY (S&amp;P500 fund) weighted 25%
+ EFA (a non-US equities fund) weighted 25%
+ IJS (a small-cap value fund) weighted 20%
+ EEM (an emerging-mkts fund) weighted 20%
+ AGG (a bond fund) weighted 10%</code></pre>
<p>Before we can calculate the skewness, we need to find portfolio monthly returns, which was covered in <a rel="nofollow" href="https://rviews.rstudio.com/2017/10/11/from-asset-to-portfolio-returns/" target="_blank">this post</a>.</p>
<p>Building off that previous work, we will be working with two objects of portfolio returns:</p>
<pre><code>+ portfolio_returns_xts_rebalanced_monthly (an xts of monthly returns)
+ portfolio_returns_tq_rebalanced_monthly (a tibble of monthly returns)</code></pre>
<p>Let’s begin in the <code>xts</code> world and make use of the <code>skewness()</code> function from <code>PerformanceAnalytics</code>.</p>
<pre><code>library(PerformanceAnalytics)
skew_xts &lt;-  skewness(portfolio_returns_xts_rebalanced_monthly$returns)

skew_xts</code></pre>
<pre><code>## [1] -0.1710568</code></pre>
<p>Our portfolio is relatively balanced, and a slight negative skewness of -0.1710568 is unsurprising and unworrisome. However, that final number could be omitting important information and we will resist the temptation to stop there. For example, is that slight negative skew being caused by one very large negative monthly return? If so, what happened? Or is it caused by several medium-sized negative returns? What caused those? Were they consecutive? Are they seasonal? We need to investigate further.</p>
<p>Before doing so and having fun with data visualization, let’s explore the <code>tidyverse</code> methods and confirm consistent results.</p>
<p>We will make use of the same <code>skewness()</code> function, but because we are using a tibble, we use <code>summarise()</code> as well and call <code>summarise(skew = skewness(returns)</code>. It’s not necessary, but we are also going to run this calculation by hand, the same as we have done with standard deviation. Feel free to delete the by-hand section from your code should this be ported to enterprise scripts, but keep in mind that there is a benefit to forcing ourselves and loved ones to write out equations: it emphasizes what those nice built-in functions are doing under the hood. If a client, customer or risk officer were ever to drill into our skewness calculations, it would be nice to have a super-firm grasp on the equation.</p>
<pre><code>library(tidyverse)
library(tidyquant)
skew_tidy &lt;-
  portfolio_returns_tq_rebalanced_monthly %>% 
  summarise(skew_builtin = skewness(returns),
            skew_byhand = 
              (sum((returns - mean(returns))^3)/length(returns))/
              ((sum((returns - mean(returns))^2)/length(returns)))^(3/2)) %>% 
  select(skew_builtin, skew_byhand)</code></pre>
<p>Let’s confirm that we have consistent calculations.</p>
<pre><code>skew_xts</code></pre>
<pre><code>## [1] -0.1710568</code></pre>
<pre><code>skew_tidy$skew_builtin</code></pre>
<pre><code>## [1] -0.1710568</code></pre>
<pre><code>skew_tidy$skew_byhand</code></pre>
<pre><code>## [1] -0.1710568</code></pre>
<p>The results are consistent using xts and our <code>tidyverse</code>, by-hand methods. Again, though, that singular number -0.1710568 does not fully illuminate the riskiness or distribution of this portfolio. To dig deeper, let’s first visualize the density of returns with <code>stat_density</code> from <code>ggplot2</code>.</p>
<pre><code>portfolio_density_plot &lt;- 
  portfolio_returns_tq_rebalanced_monthly %>% 
  ggplot(aes(x = returns)) +
  stat_density(geom = &quot;line&quot;, alpha = 1, colour = &quot;cornflowerblue&quot;)

portfolio_density_plot</code></pre>
<p><img src="https://i1.wp.com/rviews.rstudio.com/post/2017-12-13-introduction-to-skewness_files/figure-html/unnamed-chunk-4-1.png?w=450&ssl=1" data-recalc-dims="1"></p>
<p>The slight negative skew is a bit more evident here. It would be nice to shade the area that falls below some threshold again, and let’s go with the mean return. To do that, let’s create an object called <code>shaded_area</code> using <code>ggplot_build(portfolio_density_plot)$data[[1]] %>% filter(x &lt; mean(portfolio_returns_tq_rebalanced_monthly$returns))</code>. That snippet will take our original <code>ggplot</code> object and create a new object filtered for x values less than mean return. Then we use <code>geom_area</code> to add the shaded area to <code>portfolio_density_plot</code>.</p>
<pre><code>shaded_area_data &lt;- 
  ggplot_build(portfolio_density_plot)$data[[1]] %>% 
  filter(x &lt; mean(portfolio_returns_tq_rebalanced_monthly$returns))

portfolio_density_plot_shaded &lt;- 
  portfolio_density_plot + 
  geom_area(data = shaded_area_data, aes(x = x, y = y), fill=&quot;pink&quot;, alpha = 0.5)

portfolio_density_plot_shaded</code></pre>
<p><img src="https://i2.wp.com/rviews.rstudio.com/post/2017-12-13-introduction-to-skewness_files/figure-html/unnamed-chunk-5-1.png?w=450&ssl=1" data-recalc-dims="1"></p>
<p>The shaded area highlights the mass of returns that fall below the mean. Let’s add a vertical line at the mean and median, and some explanatory labels. This will help to emphasize that negative skew indicates a mean less than the median.</p>
<p>First, create variables for mean and median so that we can add a vertical line.</p>
<pre><code>median &lt;- median(portfolio_returns_tq_rebalanced_monthly$returns)
mean &lt;- mean(portfolio_returns_tq_rebalanced_monthly$returns)</code></pre>
<p>We want the vertical lines to just touch the density plot so we once again use a call to <code>ggplot_build(portfolio_density_plot)$data[[1]]</code>.</p>
<pre><code>median_line_data &lt;- 
  ggplot_build(portfolio_density_plot)$data[[1]] %>% 
  filter(x &lt;= median)</code></pre>
<p>Now we can start adding aesthetics to the latest iteration of our graph, which is stored in the object <code>portfolio_density_plot_shaded</code>.</p>
<pre><code>portfolio_density_plot_shaded +
  
  geom_segment(aes(x = 0, y = 1.9, xend = -.045, yend = 1.9),
               arrow = arrow(length = unit(0.5, &quot;cm&quot;)), size = .05) +
  
  annotate(geom = &quot;text&quot;, x = -.02, y = .1, label = &quot;returns &lt;= mean&quot;, 
           fontface = &quot;plain&quot;, alpha = .8, vjust =  -1) + 
  
  geom_segment(data = shaded_area_data, aes(x = mean, y = 0, xend = mean, yend = density), 
               color = &quot;red&quot;, linetype = &quot;dotted&quot;) +
  
  annotate(geom = &quot;text&quot;, x = mean, y = 5, label = &quot;mean&quot;, color = &quot;red&quot;, 
           fontface = &quot;plain&quot;, angle = 90, alpha = .8, vjust =  -1.75) +
  
  geom_segment(data = median_line_data, aes(x = median, y = 0, xend = median, yend = density), 
               color = &quot;black&quot;, linetype = &quot;dotted&quot;) +
  
  annotate(geom = &quot;text&quot;, x = median, y = 5, label = &quot;median&quot;, 
           fontface = &quot;plain&quot;, angle = 90, alpha = .8, vjust =  1.75) +
  
  
  ggtitle(&quot;Density Plot Illustrating Skewness&quot;)</code></pre>
<p><img src="https://i2.wp.com/rviews.rstudio.com/post/2017-12-13-introduction-to-skewness_files/figure-html/unnamed-chunk-8-1.png?w=450&ssl=1" data-recalc-dims="1"></p>
<p>We added quite a bit to the chart, possibly too much, but it’s better to be over-inclusive now to test different variants. We can delete any of those features when using this chart later, or refer back to these lines of code should we ever want to reuse some of the aesthetics.</p>
<p>At this point, we have calculated the skewness of this portfolio throughout its history, and done so using three methods. We have also created an explanatory visualization.</p>
<p>Similar to the portfolio standard deviation, though, our work is not complete until we look at rolling skewness. Perhaps the first two years of the portfolio were positive skewed, and last two were negative skewed but the overall skewness is slightly negative. We would like to understand how the skewness has changed over time, and in different economic and market regimes. To do so, we calculate and visualize the rolling skewness over time.</p>
<p>In the xts world, calculating rolling skewness is almost identical to calculating rolling standard deviation, except we call the <code>skewness()</code> function instead of <code>StdDev()</code>. Since this is a rolling calculation, we need a window of time for each skewness; here, we will use a six-month window.</p>
<pre><code>window &lt;- 6
rolling_skew_xts &lt;- na.omit(rollapply(portfolio_returns_xts_rebalanced_monthly, window, 
                           function(x) skewness(x)))</code></pre>
<p>Now we pop that <code>xts</code> object into <code>highcharter</code> for a visualization. Let’s make sure our y-axis range is large enough to capture the nature of the rolling skewness fluctuations by setting the range to between 3 and -3 with <code>hc_yAxis(..., max = 3, min = -3)</code>. I find that if we keep the range from 1 to -1, it makes most rolling skews look like a roller coaster.</p>
<pre><code>library(highcharter)
highchart(type = &quot;stock&quot;) %>%
  hc_title(text = &quot;Rolling&quot;) %>%
  hc_add_series(rolling_skew_xts, name = &quot;Rolling skewness&quot;, color = &quot;cornflowerblue&quot;) %>%
  hc_yAxis(title = list(text = &quot;skewness&quot;),
           opposite = FALSE,
           max = 3, 
           min = -3) %>% 
  hc_navigator(enabled = FALSE) %>% 
  hc_scrollbar(enabled = FALSE) </code></pre>
<div></div>
<p></p>
<p>For completeness of methods, we can calculate rolling skewness in a <code>tibble</code> and then use <code>ggplot</code>.</p>
<p>We will make use of <code>rollapply()</code> from within <code>tq_mutate</code> in <code>tidyquant</code>.</p>
<pre><code>rolling_skew_tidy &lt;- 
  portfolio_returns_tq_rebalanced_monthly %>% 
  tq_mutate(select = returns, 
            mutate_fun = rollapply,
            width      = window,
            FUN        = skewness,
            col_rename = &quot;skew&quot;)</code></pre>
<p><code>rolling_skew_tidy</code> is ready for <code>ggplot</code>. <code>ggplot</code> is not purpose-built for time series plotting, but we can set <code>aes(x = date, y = skew)</code> to make the x-axis our date values.</p>
<pre><code>library(scales)
theme_update(plot.title = element_text(hjust = 0.5))
rolling_skew_tidy %>% 
  ggplot(aes(x = date, y = skew)) +
  geom_line(color = &quot;cornflowerblue&quot;) +  
  ggtitle(&quot;Rolling Skew with ggplot&quot;) +
  ylab(paste(&quot;Rolling&quot;, window, &quot;month skewness&quot;, sep = &quot; &quot;)) + 
  scale_y_continuous(limits = c(-3, 3), breaks = pretty_breaks(n = 8)) + 
  scale_x_date(breaks = pretty_breaks(n = 8))</code></pre>
<p><img src="https://i1.wp.com/rviews.rstudio.com/post/2017-12-13-introduction-to-skewness_files/figure-html/unnamed-chunk-12-1.png?w=450&ssl=1" data-recalc-dims="1"></p>
<p>The rolling charts are quite illuminating and show that the six-month-interval skewness has been positive for about half the lifetime of this portfolio. Today, the overall skewness is negative, but the rolling skewness in mid-2016 was positive and greater than 1. It took a huge plunge starting at the end of 2016, and the lowest reading was -1.65 in March of 2017, most likely caused by one or two very large negative returns when the market was worried about the US election. We can see those worries start to abate as the rolling skewness becomes more positive throughout 2017.</p>
<p>That’s all for today. Thanks for reading and see you next time when we tackle kurtosis.</p>
<p> </p>
	<p>
<div>
<div>To <strong>leave a comment</strong> for the author, please follow the link and comment on their blog: <strong><a href="https://rviews.rstudio.com/2017/12/13/introduction-to-skewness/"> R Views</a></strong>.</div>
<hr>
<a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers.com</a> offers <strong><a rel="nofollow" href="https://feedburner.google.com/fb/a/mailverify?uri=RBloggers">daily e-mail updates</a></strong> about <a rel="nofollow" href="https://www.r-project.org/" title="The R Project for Statistical Computing">R</a> news and <a rel="nofollow" href="https://www.r-bloggers.com/search/tutorial" title="R tutorials">tutorials</a> on topics such as: <a rel="nofollow" href="https://www.r-bloggers.com/search/data%20science" title="Data science">Data science</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Big%20Data" title="Big Data">Big Data, <a rel="nofollow" href="https://www.r-users.com/" title="R jobs">R jobs</a>, visualization (<a rel="nofollow" href="https://www.r-bloggers.com/search/ggplot2" title="ggplot and ggplot2 tutorials">ggplot2</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/boxplot" title="Boxplots using lattice and ggplot2 tutorials">Boxplots</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/map" title="Maps and gis">maps</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/animation" title="Animation in R">animation</a>), programming (<a rel="nofollow" href="https://www.r-bloggers.com/search/RStudio" title="RStudio IDE for R">RStudio</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/sweave" title="Sweave and literate programming">Sweave</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/LaTeX" title="LaTeX in R">LaTeX</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/SQL" title="SQL and databases">SQL</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/eclipse" title="Eclipse IDE for R">Eclipse</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/git" title="git and github, Version Control System">git</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/hadoop" title="Large data in R using Hadoop">hadoop</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/Web+Scraping" title="Web Scraping of google, facebook, yahoo, twitter and more using R">Web Scraping</a>) statistics (<a rel="nofollow" href="https://www.r-bloggers.com/search/regression" title="Regressions and ANOVA analysis tutorials">regression</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/PCA" title="principal component analysis tutorial">PCA</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/time+series" title="Time series">time series</a>, <a rel="nofollow" href="https://www.r-bloggers.com/search/trading" title="finance trading">trading</a>) and more...
<img width="1" alt="" src="http://feeds.feedburner.com/~r/RBloggers/~4/hpzWLzHxiJ4" height="1"></a></div>