<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-kurtosis/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="https://rviews.rstudio.com/2018/01/04/introduction-to-kurtosis/"> 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>Happy 2018 and welcome to our first reproducible finance post of the year! What better way to ring in a new beginning than pondering/calculating/visualizing returns distributions.</p>
<p>We ended 2017 by tackling <a rel="nofollow" href="https://rviews.rstudio.com/2017/12/13/introduction-to-skewness/" target="_blank">skewness</a>, and we will begin 2018 by tackling kurtosis.</p>
<p>Kurtosis is a measure of the degree to which portfolio returns appear in the tails of our distribution. A normal distribution has a kurtosis of 3, which follows from the fact that a normal distribution does have some of its mass in its tails. A distribution with a kurtosis greater than 3 has more returns out in its tails than the normal, and one with kurtosis less than 3 has fewer returns in its tails than the normal. That matters to investors because more bad returns out in tails means that our portfolio might be at risk of a rare but huge downside. The terminology is a bit confusing. Negative kurtosis is considered less risky because it has fewer returns out in the tails. Negative == less risky? We’re not used to that in finance.</p>
<p>Kurtosis is often has the word ‘excess’ appended to its description, as in ‘negative excess kurtosis’ or ‘positive excess kurtosis’. That ‘excess’ is in comparison to a normal distribution kurtosis of 3. A distribution with negative excess kurtosis equal to -1 has an actual kurtosis of 2.</p>
<p>Enough with the faux investopedia entry, let’s get to the calculations, R code and visualizations.</p>
<p>Here’s the equation for excess kurtosis. Note that we subtract 3 at the end:</p>
<p><span>\[Kurtosis=\sum_{t=1}^n (x_i-\overline{x})^4/n \bigg/ (\sum_{t=1}^n (x_i-\overline{x})^2/n)^{2}-3 \]</span></p>
<p>By way of reminder, 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 kurtosis, 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>Now we are going to test our past self’s <a rel="nofollow" href="https://rviews.rstudio.com/2017/12/13/introduction-to-skewness/" target="_blank">work on skewness</a>, and reuse that code flow to expedite the kurtosis work. The logic will remain the same, but we will call different built-in functions and different by-hand calculations.</p>
<p>For the xts world, we use the <code>kurtosis()</code> function instead of the <code>skewness()</code> function.</p>
<pre><code>kurt_xts &lt;-  kurtosis(portfolio_returns_xts_rebalanced_monthly$returns)

kurt_xts</code></pre>
<pre><code>## [1] 0.5267736</code></pre>
<p>For tidy, we have the same piped flow and use the formula for kurtosis for our by-hand calculations. Our by-hand result is labeled with <code>kurt_byhand</code>, and involves quite a few parentheticals to map it back to the kurtosis equation above.</p>
<pre><code>kurt_tidy &lt;-
  portfolio_returns_tq_rebalanced_monthly %>% 
  summarise(
            kurt_builtin = kurtosis(returns),
            kurt_byhand = 
              ((sum((returns - mean(returns))^4)/length(returns))/
              ((sum((returns - mean(returns))^2)/length(returns))^2)) - 3) %>% 
  select(kurt_builtin, kurt_byhand)</code></pre>
<p>Let’s confirm that we have consistent calculations.</p>
<pre><code>kurt_xts</code></pre>
<pre><code>## [1] 0.5267736</code></pre>
<pre><code>kurt_tidy$kurt_builtin</code></pre>
<pre><code>## [1] 0.5267736</code></pre>
<pre><code>kurt_tidy$kurt_byhand</code></pre>
<pre><code>## [1] 0.5267736</code></pre>
<p>We have consistent results from <code>xts</code> and the tidy built-in/by-hand worlds, and we were able to reuse our code from above to shorten the development time here. dd</p>
<p>Let’s do the same with the visualizations and head straight for a density plot, starting with the same <code>portfolio_density_plot</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/2018-01-03-introduction-to-kurtosis_files/figure-html/unnamed-chunk-4-1.png?w=450&ssl=1" data-recalc-dims="1"></p>
<p>We are interested in <em>both</em> tails for kurtosis, so let’s shade at 2 standard deviations above and below the mean return (for our skewness work, we only shaded the negative tail).</p>
<pre><code>mean &lt;- mean(portfolio_returns_tq_rebalanced_monthly$returns)

sd_pos &lt;-  mean + (2 * sd(portfolio_returns_tq_rebalanced_monthly$returns))

sd_neg &lt;- mean - (2 * sd(portfolio_returns_tq_rebalanced_monthly$returns))

sd_pos_shaded_area &lt;- 
  ggplot_build(portfolio_density_plot)$data[[1]] %>% 
  filter(x > sd_pos )

sd_neg_shaded_area &lt;- 
  ggplot_build(portfolio_density_plot)$data[[1]] %>% 
  filter(x &lt; sd_neg)

  portfolio_density_plot &lt;-  
    portfolio_density_plot + 
    geom_area(data = sd_pos_shaded_area, aes(x = x, y = y), fill=&quot;pink&quot;, alpha = 0.5) +
    geom_area(data = sd_neg_shaded_area, aes(x = x, y = y), fill=&quot;pink&quot;, alpha = 0.5) +
    scale_x_continuous(breaks = scales::pretty_breaks(n = 10))
  
  portfolio_density_plot</code></pre>
<p><img src="https://i0.wp.com/rviews.rstudio.com/post/2018-01-03-introduction-to-kurtosis_files/figure-html/unnamed-chunk-5-1.png?w=450&ssl=1" data-recalc-dims="1"></p>
<p>That density chart is a good look at the mass in both tails, where we have defined ‘tail’ as being two standard deviations away from the mean. We can add a line for the mean, as did in our skewness visualization, with the following.</p>
<pre><code>mean &lt;- mean(portfolio_returns_tq_rebalanced_monthly$returns)

mean_line_data &lt;- 
  ggplot_build(portfolio_density_plot)$data[[1]] %>% 
  filter(x &lt;= mean)


portfolio_density_plot +
  
geom_segment(data = mean_line_data, aes(x = mean, y = 0, xend = mean, yend = density), 
               color = &quot;black&quot;, linetype = &quot;dotted&quot;) +
  
  annotate(geom = &quot;text&quot;, x = mean, y = 5, label = &quot;mean&quot;, 
           fontface = &quot;plain&quot;, angle = 90, alpha = .8, vjust =  1.75)</code></pre>
<p><img src="https://i1.wp.com/rviews.rstudio.com/post/2018-01-03-introduction-to-kurtosis_files/figure-html/unnamed-chunk-6-1.png?w=450&ssl=1" data-recalc-dims="1"></p>
<p>Finally, we can calculate and chart the rolling kurtosis with the same logic as we did for skewness. The only difference is that here we call <code>fun = kurtosis</code> instead of <code>fun = skewness</code>.</p>
<pre><code>window &lt;- 6
rolling_kurt_xts &lt;- na.omit(apply.rolling(portfolio_returns_xts_rebalanced_monthly, window, 
                           fun = kurtosis))</code></pre>
<p>Now we pop that <code>xts</code> object into <code>highcharter</code> for a visualization.</p>
<pre><code>highchart(type = &quot;stock&quot;) %>%
  hc_title(text = &quot;Rolling Kurt&quot;) %>%
  hc_add_series(rolling_kurt_xts, name = &quot;Rolling kurtosis&quot;, color = &quot;cornflowerblue&quot;) %>%
  hc_yAxis(title = list(text = &quot;kurtosis&quot;),
           opposite = FALSE,
           max = .03, 
           min = -.03) %>% 
  hc_navigator(enabled = FALSE) %>% 
  hc_scrollbar(enabled = FALSE) </code></pre>
<div></div>
<p></p>
<p>We didn’t cover this before, but what if we wanted to use <code>ggplot</code> for the rolling kurtosis? We could convert that <code>xts</code> object to a tibble with <code>tk_tbl()</code> from the <code>timetk</code> package, and then pipe straight to <code>ggplot</code>.</p>
<pre><code>rolling_kurt_xts %>% 
  tk_tbl(preserve_index = TRUE, rename_index = &quot;date&quot;) %>% 
  rename(rolling_kurtosis = calcs) %>% 
  ggplot(aes(x = date, y = rolling_kurtosis)) + 
  geom_line(color = &quot;cornflowerblue&quot;) +
  xlab(&quot;date&quot;) +
  ylab(&quot;rolling kurtosis&quot;) + 
  ggtitle(&quot;Rolling Kurtosis&quot;)</code></pre>
<p><img src="https://i0.wp.com/rviews.rstudio.com/post/2018-01-03-introduction-to-kurtosis_files/figure-html/unnamed-chunk-9-1.png?w=450&ssl=1" data-recalc-dims="1"></p>
<p>Interestingly, this portfolio has displayed slight positive rolling excess kurtosis for most of its life, except during the last half of 2015 through early 2016.</p>
<p>That’s all for today. Our work on kurtosis was made a lot more efficient by our work on skewness – so let’s thank our 2017 selves for constructing a reproducible and reusable code flow! See you next time.</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/2018/01/04/introduction-to-kurtosis/"> 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/s3PqjIeOg9o" height="1"></a></div>