<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>Stocks on R Views</title>
    <link>https://rviews.rstudio.com/tags/stocks/</link>
    <description>Recent content in Stocks on R Views</description>
    <generator>Hugo -- gohugo.io</generator>
    <language>en-us</language>
    <lastBuildDate>Thu, 04 Jan 2018 00:00:00 +0000</lastBuildDate>
    <atom:link href="https://rviews.rstudio.com/tags/stocks/" rel="self" type="application/rss+xml" />
    
    
    
    
    <item>
      <title>Introduction to Kurtosis</title>
      <link>https://rviews.rstudio.com/2018/01/04/introduction-to-kurtosis/</link>
      <pubDate>Thu, 04 Jan 2018 00:00:00 +0000</pubDate>
      
      <guid>https://rviews.rstudio.com/2018/01/04/introduction-to-kurtosis/</guid>
      <description>
        
&lt;script src=&#34;/rmarkdown-libs/htmlwidgets/htmlwidgets.js&#34;&gt;&lt;/script&gt;
&lt;script src=&#34;/rmarkdown-libs/jquery/jquery.min.js&#34;&gt;&lt;/script&gt;
&lt;script src=&#34;/rmarkdown-libs/proj4js/proj4.js&#34;&gt;&lt;/script&gt;
&lt;link href=&#34;/rmarkdown-libs/highcharts/css/motion.css&#34; rel=&#34;stylesheet&#34; /&gt;
&lt;script src=&#34;/rmarkdown-libs/highcharts/highstock.js&#34;&gt;&lt;/script&gt;
&lt;script src=&#34;/rmarkdown-libs/highcharts/highcharts-3d.js&#34;&gt;&lt;/script&gt;
&lt;script src=&#34;/rmarkdown-libs/highcharts/highcharts-more.js&#34;&gt;&lt;/script&gt;
&lt;script src=&#34;/rmarkdown-libs/highcharts/modules/annotations.js&#34;&gt;&lt;/script&gt;
&lt;script src=&#34;/rmarkdown-libs/highcharts/modules/broken-axis.js&#34;&gt;&lt;/script&gt;
&lt;script src=&#34;/rmarkdown-libs/highcharts/modules/data.js&#34;&gt;&lt;/script&gt;
&lt;script src=&#34;/rmarkdown-libs/highcharts/modules/drilldown.js&#34;&gt;&lt;/script&gt;
&lt;script src=&#34;/rmarkdown-libs/highcharts/modules/exporting.js&#34;&gt;&lt;/script&gt;
&lt;script src=&#34;/rmarkdown-libs/highcharts/modules/funnel.js&#34;&gt;&lt;/script&gt;
&lt;script src=&#34;/rmarkdown-libs/highcharts/modules/heatmap.js&#34;&gt;&lt;/script&gt;
&lt;script src=&#34;/rmarkdown-libs/highcharts/modules/map.js&#34;&gt;&lt;/script&gt;
&lt;script src=&#34;/rmarkdown-libs/highcharts/modules/no-data-to-display.js&#34;&gt;&lt;/script&gt;
&lt;script src=&#34;/rmarkdown-libs/highcharts/modules/offline-exporting.js&#34;&gt;&lt;/script&gt;
&lt;script src=&#34;/rmarkdown-libs/highcharts/modules/solid-gauge.js&#34;&gt;&lt;/script&gt;
&lt;script src=&#34;/rmarkdown-libs/highcharts/modules/treemap.js&#34;&gt;&lt;/script&gt;
&lt;script src=&#34;/rmarkdown-libs/highcharts/plugins/annotations.js&#34;&gt;&lt;/script&gt;
&lt;script src=&#34;/rmarkdown-libs/highcharts/plugins/draggable-legend.js&#34;&gt;&lt;/script&gt;
&lt;script src=&#34;/rmarkdown-libs/highcharts/plugins/draggable-points.js&#34;&gt;&lt;/script&gt;
&lt;script src=&#34;/rmarkdown-libs/highcharts/plugins/export-csv.js&#34;&gt;&lt;/script&gt;
&lt;script src=&#34;/rmarkdown-libs/highcharts/plugins/grouped-categories.js&#34;&gt;&lt;/script&gt;
&lt;script src=&#34;/rmarkdown-libs/highcharts/plugins/motion.js&#34;&gt;&lt;/script&gt;
&lt;script src=&#34;/rmarkdown-libs/highcharts/plugins/pattern-fill-v2.js&#34;&gt;&lt;/script&gt;
&lt;script src=&#34;/rmarkdown-libs/highcharts/plugins/tooltip-delay.js&#34;&gt;&lt;/script&gt;
&lt;script src=&#34;/rmarkdown-libs/highcharts/custom/reset.js&#34;&gt;&lt;/script&gt;
&lt;script src=&#34;/rmarkdown-libs/highcharts/custom/symbols-extra.js&#34;&gt;&lt;/script&gt;
&lt;script src=&#34;/rmarkdown-libs/highcharts/custom/text-symbols.js&#34;&gt;&lt;/script&gt;
&lt;link href=&#34;/rmarkdown-libs/fontawesome/font-awesome.min.css&#34; rel=&#34;stylesheet&#34; /&gt;
&lt;link href=&#34;/rmarkdown-libs/htmlwdgtgrid/htmlwdgtgrid.css&#34; rel=&#34;stylesheet&#34; /&gt;
&lt;script src=&#34;/rmarkdown-libs/highchart-binding/highchart.js&#34;&gt;&lt;/script&gt;


&lt;p&gt;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.&lt;/p&gt;
&lt;p&gt;We ended 2017 by tackling &lt;a href=&#34;https://rviews.rstudio.com/2017/12/13/introduction-to-skewness/&#34;&gt;skewness&lt;/a&gt;, and we will begin 2018 by tackling kurtosis.&lt;/p&gt;
&lt;p&gt;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.&lt;/p&gt;
&lt;p&gt;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.&lt;/p&gt;
&lt;p&gt;Enough with the faux investopedia entry, let’s get to the calculations, R code and visualizations.&lt;/p&gt;
&lt;p&gt;Here’s the equation for excess kurtosis. Note that we subtract 3 at the end:&lt;/p&gt;
&lt;p&gt;&lt;span class=&#34;math display&#34;&gt;\[Kurtosis=\sum_{t=1}^n (x_i-\overline{x})^4/n \bigg/ (\sum_{t=1}^n (x_i-\overline{x})^2/n)^{2}-3 \]&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;By way of reminder, we will be working with our usual portfolio consisting of:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;+ SPY (S&amp;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%&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Before we can calculate kurtosis, we need to find portfolio monthly returns, which was covered in &lt;a href=&#34;https://rviews.rstudio.com/2017/10/11/from-asset-to-portfolio-returns/&#34;&gt;this post&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Building off that previous work, we will be working with two objects of portfolio returns:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;+ portfolio_returns_xts_rebalanced_monthly (an xts of monthly returns)
+ portfolio_returns_tq_rebalanced_monthly (a tibble of monthly returns)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now we are going to test our past self’s &lt;a href=&#34;https://rviews.rstudio.com/2017/12/13/introduction-to-skewness/&#34;&gt;work on skewness&lt;/a&gt;, 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.&lt;/p&gt;
&lt;p&gt;For the xts world, we use the &lt;code&gt;kurtosis()&lt;/code&gt; function instead of the &lt;code&gt;skewness()&lt;/code&gt; function.&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;kurt_xts &amp;lt;-  kurtosis(portfolio_returns_xts_rebalanced_monthly$returns)

kurt_xts&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## [1] 0.5267736&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;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 &lt;code&gt;kurt_byhand&lt;/code&gt;, and involves quite a few parentheticals to map it back to the kurtosis equation above.&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;kurt_tidy &amp;lt;-
  portfolio_returns_tq_rebalanced_monthly %&amp;gt;% 
  summarise(
            kurt_builtin = kurtosis(returns),
            kurt_byhand = 
              ((sum((returns - mean(returns))^4)/length(returns))/
              ((sum((returns - mean(returns))^2)/length(returns))^2)) - 3) %&amp;gt;% 
  select(kurt_builtin, kurt_byhand)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Let’s confirm that we have consistent calculations.&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;kurt_xts&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## [1] 0.5267736&lt;/code&gt;&lt;/pre&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;kurt_tidy$kurt_builtin&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## [1] 0.5267736&lt;/code&gt;&lt;/pre&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;kurt_tidy$kurt_byhand&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## [1] 0.5267736&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We have consistent results from &lt;code&gt;xts&lt;/code&gt; 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&lt;/p&gt;
&lt;p&gt;Let’s do the same with the visualizations and head straight for a density plot, starting with the same &lt;code&gt;portfolio_density_plot&lt;/code&gt;.&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;portfolio_density_plot &amp;lt;- 
  portfolio_returns_tq_rebalanced_monthly %&amp;gt;% 
  ggplot(aes(x = returns)) +
  stat_density(geom = &amp;quot;line&amp;quot;, alpha = 1, colour = &amp;quot;cornflowerblue&amp;quot;)

portfolio_density_plot&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;/post/2018-01-03-introduction-to-kurtosis_files/figure-html/unnamed-chunk-4-1.png&#34; width=&#34;672&#34; /&gt;&lt;/p&gt;
&lt;p&gt;We are interested in &lt;em&gt;both&lt;/em&gt; 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).&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;mean &amp;lt;- mean(portfolio_returns_tq_rebalanced_monthly$returns)

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

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

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

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

  portfolio_density_plot &amp;lt;-  
    portfolio_density_plot + 
    geom_area(data = sd_pos_shaded_area, aes(x = x, y = y), fill=&amp;quot;pink&amp;quot;, alpha = 0.5) +
    geom_area(data = sd_neg_shaded_area, aes(x = x, y = y), fill=&amp;quot;pink&amp;quot;, alpha = 0.5) +
    scale_x_continuous(breaks = scales::pretty_breaks(n = 10))
  
  portfolio_density_plot&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;/post/2018-01-03-introduction-to-kurtosis_files/figure-html/unnamed-chunk-5-1.png&#34; width=&#34;672&#34; /&gt;&lt;/p&gt;
&lt;p&gt;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.&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;mean &amp;lt;- mean(portfolio_returns_tq_rebalanced_monthly$returns)

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


portfolio_density_plot +
  
geom_segment(data = mean_line_data, aes(x = mean, y = 0, xend = mean, yend = density), 
               color = &amp;quot;black&amp;quot;, linetype = &amp;quot;dotted&amp;quot;) +
  
  annotate(geom = &amp;quot;text&amp;quot;, x = mean, y = 5, label = &amp;quot;mean&amp;quot;, 
           fontface = &amp;quot;plain&amp;quot;, angle = 90, alpha = .8, vjust =  1.75)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;/post/2018-01-03-introduction-to-kurtosis_files/figure-html/unnamed-chunk-6-1.png&#34; width=&#34;672&#34; /&gt;&lt;/p&gt;
&lt;p&gt;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 &lt;code&gt;fun = kurtosis&lt;/code&gt; instead of &lt;code&gt;fun = skewness&lt;/code&gt;.&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;window &amp;lt;- 6
rolling_kurt_xts &amp;lt;- na.omit(apply.rolling(portfolio_returns_xts_rebalanced_monthly, window, 
                           fun = kurtosis))&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now we pop that &lt;code&gt;xts&lt;/code&gt; object into &lt;code&gt;highcharter&lt;/code&gt; for a visualization.&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;highchart(type = &amp;quot;stock&amp;quot;) %&amp;gt;%
  hc_title(text = &amp;quot;Rolling Kurt&amp;quot;) %&amp;gt;%
  hc_add_series(rolling_kurt_xts, name = &amp;quot;Rolling kurtosis&amp;quot;, color = &amp;quot;cornflowerblue&amp;quot;) %&amp;gt;%
  hc_yAxis(title = list(text = &amp;quot;kurtosis&amp;quot;),
           opposite = FALSE,
           max = .03, 
           min = -.03) %&amp;gt;% 
  hc_navigator(enabled = FALSE) %&amp;gt;% 
  hc_scrollbar(enabled = FALSE) &lt;/code&gt;&lt;/pre&gt;
&lt;div id=&#34;htmlwidget-1&#34; style=&#34;width:100%;height:500px;&#34; class=&#34;highchart html-widget&#34;&gt;&lt;/div&gt;
&lt;script type=&#34;application/json&#34; data-for=&#34;htmlwidget-1&#34;&gt;{&#34;x&#34;:{&#34;hc_opts&#34;:{&#34;title&#34;:{&#34;text&#34;:&#34;Rolling Kurt&#34;},&#34;yAxis&#34;:{&#34;title&#34;:{&#34;text&#34;:&#34;kurtosis&#34;},&#34;opposite&#34;:false,&#34;max&#34;:0.03,&#34;min&#34;:-0.03},&#34;credits&#34;:{&#34;enabled&#34;:false},&#34;exporting&#34;:{&#34;enabled&#34;:false},&#34;plotOptions&#34;:{&#34;series&#34;:{&#34;turboThreshold&#34;:0},&#34;treemap&#34;:{&#34;layoutAlgorithm&#34;:&#34;squarified&#34;},&#34;bubble&#34;:{&#34;minSize&#34;:5,&#34;maxSize&#34;:25}},&#34;annotationsOptions&#34;:{&#34;enabledButtons&#34;:false},&#34;tooltip&#34;:{&#34;delayForDisplay&#34;:10},&#34;series&#34;:[{&#34;data&#34;:[[1375228800000,0.00854775829739259],[1377820800000,0.00444785082581815],[1380499200000,0.0104092039506155],[1383177600000,0.0128359457242099],[1385683200000,0.0164325330568209],[1388448000000,0.0224217167324512],[1391126400000,0.00789080127302483],[1393545600000,0.0191481706943007],[1396224000000,0.0119153871442633],[1398816000000,0.0066512115146186],[1401408000000,0.0069340953984468],[1404086400000,0.00804186927781203],[1406764800000,0.0127245048219774],[1409270400000,0.00997562223278388],[1412035200000,0.00106782119332109],[1414713600000,0.004269190603526],[1417132800000,0.00218739200640291],[1419984000000,-0.0033686842425531],[1422576000000,-0.00308812576416303],[1424995200000,0.000672300702671621],[1427760000000,0.0063800007202866],[1430352000000,0.00602025921117102],[1432857600000,0.00450860859731235],[1435622400000,0.00359285608324082],[1438300800000,0.00510762504715069],[1440979200000,-0.0135237670918179],[1443571200000,-0.017272859188811],[1446163200000,-0.0104452656749339],[1448841600000,-0.0101050819169769],[1451520000000,-0.0116837282083901],[1454025600000,-0.0185112647100233],[1456704000000,-0.00879112875305636],[1459382400000,0.00889238266836386],[1461888000000,0.000582532447263954],[1464652800000,0.000410213890434274],[1467244800000,0.00639455232238465],[1469750400000,0.0210143856721272],[1472601600000,0.0228061076525519],[1475193600000,0.0119133789927622],[1477872000000,0.00660677259528886],[1480464000000,0.0100397694050085],[1483056000000,0.0117902641682449],[1485820800000,0.0090557870395865],[1488240000000,0.0113804250332236],[1490918400000,0.0120564550652874],[1493337600000,0.017696157218908],[1496188800000,0.0170231562654927],[1498780800000,0.0157307217365092],[1501459200000,0.0159944354163099],[1504137600000,0.0128801041981877],[1506643200000,0.0149609794697498],[1509408000000,0.0156222680756303],[1512000000000,0.0159171082293594],[1514505600000,0.0151986230947173],[1514937600000,0.0133193338956239]],&#34;name&#34;:&#34;Rolling kurtosis&#34;,&#34;color&#34;:&#34;cornflowerblue&#34;}],&#34;navigator&#34;:{&#34;enabled&#34;:false},&#34;scrollbar&#34;:{&#34;enabled&#34;:false}},&#34;theme&#34;:{&#34;chart&#34;:{&#34;backgroundColor&#34;:&#34;transparent&#34;}},&#34;conf_opts&#34;:{&#34;global&#34;:{&#34;Date&#34;:null,&#34;VMLRadialGradientURL&#34;:&#34;http =//code.highcharts.com/list(version)/gfx/vml-radial-gradient.png&#34;,&#34;canvasToolsURL&#34;:&#34;http =//code.highcharts.com/list(version)/modules/canvas-tools.js&#34;,&#34;getTimezoneOffset&#34;:null,&#34;timezoneOffset&#34;:0,&#34;useUTC&#34;:true},&#34;lang&#34;:{&#34;contextButtonTitle&#34;:&#34;Chart context menu&#34;,&#34;decimalPoint&#34;:&#34;.&#34;,&#34;downloadJPEG&#34;:&#34;Download JPEG image&#34;,&#34;downloadPDF&#34;:&#34;Download PDF document&#34;,&#34;downloadPNG&#34;:&#34;Download PNG image&#34;,&#34;downloadSVG&#34;:&#34;Download SVG vector image&#34;,&#34;drillUpText&#34;:&#34;Back to {series.name}&#34;,&#34;invalidDate&#34;:null,&#34;loading&#34;:&#34;Loading...&#34;,&#34;months&#34;:[&#34;January&#34;,&#34;February&#34;,&#34;March&#34;,&#34;April&#34;,&#34;May&#34;,&#34;June&#34;,&#34;July&#34;,&#34;August&#34;,&#34;September&#34;,&#34;October&#34;,&#34;November&#34;,&#34;December&#34;],&#34;noData&#34;:&#34;No data to display&#34;,&#34;numericSymbols&#34;:[&#34;k&#34;,&#34;M&#34;,&#34;G&#34;,&#34;T&#34;,&#34;P&#34;,&#34;E&#34;],&#34;printChart&#34;:&#34;Print chart&#34;,&#34;resetZoom&#34;:&#34;Reset zoom&#34;,&#34;resetZoomTitle&#34;:&#34;Reset zoom level 1:1&#34;,&#34;shortMonths&#34;:[&#34;Jan&#34;,&#34;Feb&#34;,&#34;Mar&#34;,&#34;Apr&#34;,&#34;May&#34;,&#34;Jun&#34;,&#34;Jul&#34;,&#34;Aug&#34;,&#34;Sep&#34;,&#34;Oct&#34;,&#34;Nov&#34;,&#34;Dec&#34;],&#34;thousandsSep&#34;:&#34; &#34;,&#34;weekdays&#34;:[&#34;Sunday&#34;,&#34;Monday&#34;,&#34;Tuesday&#34;,&#34;Wednesday&#34;,&#34;Thursday&#34;,&#34;Friday&#34;,&#34;Saturday&#34;]}},&#34;type&#34;:&#34;stock&#34;,&#34;fonts&#34;:[],&#34;debug&#34;:false},&#34;evals&#34;:[],&#34;jsHooks&#34;:[]}&lt;/script&gt;
&lt;p&gt;We didn’t cover this before, but what if we wanted to use &lt;code&gt;ggplot&lt;/code&gt; for the rolling kurtosis? We could convert that &lt;code&gt;xts&lt;/code&gt; object to a tibble with &lt;code&gt;tk_tbl()&lt;/code&gt; from the &lt;code&gt;timetk&lt;/code&gt; package, and then pipe straight to &lt;code&gt;ggplot&lt;/code&gt;.&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;rolling_kurt_xts %&amp;gt;% 
  tk_tbl(preserve_index = TRUE, rename_index = &amp;quot;date&amp;quot;) %&amp;gt;% 
  rename(rolling_kurtosis = calcs) %&amp;gt;% 
  ggplot(aes(x = date, y = rolling_kurtosis)) + 
  geom_line(color = &amp;quot;cornflowerblue&amp;quot;) +
  xlab(&amp;quot;date&amp;quot;) +
  ylab(&amp;quot;rolling kurtosis&amp;quot;) + 
  ggtitle(&amp;quot;Rolling Kurtosis&amp;quot;)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;/post/2018-01-03-introduction-to-kurtosis_files/figure-html/unnamed-chunk-9-1.png&#34; width=&#34;672&#34; /&gt;&lt;/p&gt;
&lt;p&gt;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.&lt;/p&gt;
&lt;p&gt;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.&lt;/p&gt;

        &lt;script&gt;window.location.href=&#39;https://rviews.rstudio.com/2018/01/04/introduction-to-kurtosis/&#39;;&lt;/script&gt;
      </description>
    </item>
    
    <item>
      <title>Introduction to Skewness</title>
      <link>https://rviews.rstudio.com/2017/12/13/introduction-to-skewness/</link>
      <pubDate>Wed, 13 Dec 2017 00:00:00 +0000</pubDate>
      
      <guid>https://rviews.rstudio.com/2017/12/13/introduction-to-skewness/</guid>
      <description>
        
&lt;script src=&#34;/rmarkdown-libs/htmlwidgets/htmlwidgets.js&#34;&gt;&lt;/script&gt;
&lt;script src=&#34;/rmarkdown-libs/jquery/jquery.min.js&#34;&gt;&lt;/script&gt;
&lt;script src=&#34;/rmarkdown-libs/proj4js/proj4.js&#34;&gt;&lt;/script&gt;
&lt;link href=&#34;/rmarkdown-libs/highcharts/css/motion.css&#34; rel=&#34;stylesheet&#34; /&gt;
&lt;script src=&#34;/rmarkdown-libs/highcharts/highstock.js&#34;&gt;&lt;/script&gt;
&lt;script src=&#34;/rmarkdown-libs/highcharts/highcharts-3d.js&#34;&gt;&lt;/script&gt;
&lt;script src=&#34;/rmarkdown-libs/highcharts/highcharts-more.js&#34;&gt;&lt;/script&gt;
&lt;script src=&#34;/rmarkdown-libs/highcharts/modules/annotations.js&#34;&gt;&lt;/script&gt;
&lt;script src=&#34;/rmarkdown-libs/highcharts/modules/broken-axis.js&#34;&gt;&lt;/script&gt;
&lt;script src=&#34;/rmarkdown-libs/highcharts/modules/data.js&#34;&gt;&lt;/script&gt;
&lt;script src=&#34;/rmarkdown-libs/highcharts/modules/drilldown.js&#34;&gt;&lt;/script&gt;
&lt;script src=&#34;/rmarkdown-libs/highcharts/modules/exporting.js&#34;&gt;&lt;/script&gt;
&lt;script src=&#34;/rmarkdown-libs/highcharts/modules/funnel.js&#34;&gt;&lt;/script&gt;
&lt;script src=&#34;/rmarkdown-libs/highcharts/modules/heatmap.js&#34;&gt;&lt;/script&gt;
&lt;script src=&#34;/rmarkdown-libs/highcharts/modules/map.js&#34;&gt;&lt;/script&gt;
&lt;script src=&#34;/rmarkdown-libs/highcharts/modules/no-data-to-display.js&#34;&gt;&lt;/script&gt;
&lt;script src=&#34;/rmarkdown-libs/highcharts/modules/offline-exporting.js&#34;&gt;&lt;/script&gt;
&lt;script src=&#34;/rmarkdown-libs/highcharts/modules/solid-gauge.js&#34;&gt;&lt;/script&gt;
&lt;script src=&#34;/rmarkdown-libs/highcharts/modules/treemap.js&#34;&gt;&lt;/script&gt;
&lt;script src=&#34;/rmarkdown-libs/highcharts/plugins/annotations.js&#34;&gt;&lt;/script&gt;
&lt;script src=&#34;/rmarkdown-libs/highcharts/plugins/draggable-legend.js&#34;&gt;&lt;/script&gt;
&lt;script src=&#34;/rmarkdown-libs/highcharts/plugins/draggable-points.js&#34;&gt;&lt;/script&gt;
&lt;script src=&#34;/rmarkdown-libs/highcharts/plugins/export-csv.js&#34;&gt;&lt;/script&gt;
&lt;script src=&#34;/rmarkdown-libs/highcharts/plugins/grouped-categories.js&#34;&gt;&lt;/script&gt;
&lt;script src=&#34;/rmarkdown-libs/highcharts/plugins/motion.js&#34;&gt;&lt;/script&gt;
&lt;script src=&#34;/rmarkdown-libs/highcharts/plugins/pattern-fill-v2.js&#34;&gt;&lt;/script&gt;
&lt;script src=&#34;/rmarkdown-libs/highcharts/plugins/tooltip-delay.js&#34;&gt;&lt;/script&gt;
&lt;script src=&#34;/rmarkdown-libs/highcharts/custom/reset.js&#34;&gt;&lt;/script&gt;
&lt;script src=&#34;/rmarkdown-libs/highcharts/custom/symbols-extra.js&#34;&gt;&lt;/script&gt;
&lt;script src=&#34;/rmarkdown-libs/highcharts/custom/text-symbols.js&#34;&gt;&lt;/script&gt;
&lt;link href=&#34;/rmarkdown-libs/fontawesome/font-awesome.min.css&#34; rel=&#34;stylesheet&#34; /&gt;
&lt;link href=&#34;/rmarkdown-libs/htmlwdgtgrid/htmlwdgtgrid.css&#34; rel=&#34;stylesheet&#34; /&gt;
&lt;script src=&#34;/rmarkdown-libs/highchart-binding/highchart.js&#34;&gt;&lt;/script&gt;


&lt;p&gt;In previous posts &lt;a href=&#34;https://rviews.rstudio.com/2017/07/12/introduction-to-volatility/&#34;&gt;here&lt;/a&gt;, &lt;a href=&#34;https://rviews.rstudio.com/2017/07/18/introduction-to-rolling-volatility/&#34;&gt;here&lt;/a&gt;, and &lt;a href=&#34;https://rviews.rstudio.com/2017/07/21/visualizing-portfolio-volatility/&#34;&gt;here&lt;/a&gt;, 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.&lt;/p&gt;
&lt;p&gt;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.”&lt;/p&gt;
&lt;p&gt;Let’s get to it.&lt;/p&gt;
&lt;p&gt;First, have a look at one equation for skewness:&lt;/p&gt;
&lt;p&gt;&lt;span class=&#34;math display&#34;&gt;\[Skew=\sum_{t=1}^n (x_i-\overline{x})^3/n \bigg/ (\sum_{t=1}^n (x_i-\overline{x})^2/n)^{3/2}\]&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;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 &lt;code&gt;xts&lt;/code&gt; and &lt;code&gt;tidyverse&lt;/code&gt; 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:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;+ SPY (S&amp;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%&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Before we can calculate the skewness, we need to find portfolio monthly returns, which was covered in &lt;a href=&#34;https://rviews.rstudio.com/2017/10/11/from-asset-to-portfolio-returns/&#34;&gt;this post&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Building off that previous work, we will be working with two objects of portfolio returns:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;+ portfolio_returns_xts_rebalanced_monthly (an xts of monthly returns)
+ portfolio_returns_tq_rebalanced_monthly (a tibble of monthly returns)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Let’s begin in the &lt;code&gt;xts&lt;/code&gt; world and make use of the &lt;code&gt;skewness()&lt;/code&gt; function from &lt;code&gt;PerformanceAnalytics&lt;/code&gt;.&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;library(PerformanceAnalytics)
skew_xts &amp;lt;-  skewness(portfolio_returns_xts_rebalanced_monthly$returns)

skew_xts&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## [1] -0.1710568&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;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.&lt;/p&gt;
&lt;p&gt;Before doing so and having fun with data visualization, let’s explore the &lt;code&gt;tidyverse&lt;/code&gt; methods and confirm consistent results.&lt;/p&gt;
&lt;p&gt;We will make use of the same &lt;code&gt;skewness()&lt;/code&gt; function, but because we are using a tibble, we use &lt;code&gt;summarise()&lt;/code&gt; as well and call &lt;code&gt;summarise(skew = skewness(returns)&lt;/code&gt;. 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.&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;library(tidyverse)
library(tidyquant)
skew_tidy &amp;lt;-
  portfolio_returns_tq_rebalanced_monthly %&amp;gt;% 
  summarise(skew_builtin = skewness(returns),
            skew_byhand = 
              (sum((returns - mean(returns))^3)/length(returns))/
              ((sum((returns - mean(returns))^2)/length(returns)))^(3/2)) %&amp;gt;% 
  select(skew_builtin, skew_byhand)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Let’s confirm that we have consistent calculations.&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;skew_xts&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## [1] -0.1710568&lt;/code&gt;&lt;/pre&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;skew_tidy$skew_builtin&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## [1] -0.1710568&lt;/code&gt;&lt;/pre&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;skew_tidy$skew_byhand&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;&lt;code&gt;## [1] -0.1710568&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The results are consistent using xts and our &lt;code&gt;tidyverse&lt;/code&gt;, 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 &lt;code&gt;stat_density&lt;/code&gt; from &lt;code&gt;ggplot2&lt;/code&gt;.&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;portfolio_density_plot &amp;lt;- 
  portfolio_returns_tq_rebalanced_monthly %&amp;gt;% 
  ggplot(aes(x = returns)) +
  stat_density(geom = &amp;quot;line&amp;quot;, alpha = 1, colour = &amp;quot;cornflowerblue&amp;quot;)

portfolio_density_plot&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;/post/2017-12-13-introduction-to-skewness_files/figure-html/unnamed-chunk-4-1.png&#34; width=&#34;672&#34; /&gt;&lt;/p&gt;
&lt;p&gt;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 &lt;code&gt;shaded_area&lt;/code&gt; using &lt;code&gt;ggplot_build(portfolio_density_plot)$data[[1]] %&amp;gt;% filter(x &amp;lt; mean(portfolio_returns_tq_rebalanced_monthly$returns))&lt;/code&gt;. That snippet will take our original &lt;code&gt;ggplot&lt;/code&gt; object and create a new object filtered for x values less than mean return. Then we use &lt;code&gt;geom_area&lt;/code&gt; to add the shaded area to &lt;code&gt;portfolio_density_plot&lt;/code&gt;.&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;shaded_area_data &amp;lt;- 
  ggplot_build(portfolio_density_plot)$data[[1]] %&amp;gt;% 
  filter(x &amp;lt; mean(portfolio_returns_tq_rebalanced_monthly$returns))

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

portfolio_density_plot_shaded&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;/post/2017-12-13-introduction-to-skewness_files/figure-html/unnamed-chunk-5-1.png&#34; width=&#34;672&#34; /&gt;&lt;/p&gt;
&lt;p&gt;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.&lt;/p&gt;
&lt;p&gt;First, create variables for mean and median so that we can add a vertical line.&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;median &amp;lt;- median(portfolio_returns_tq_rebalanced_monthly$returns)
mean &amp;lt;- mean(portfolio_returns_tq_rebalanced_monthly$returns)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We want the vertical lines to just touch the density plot so we once again use a call to &lt;code&gt;ggplot_build(portfolio_density_plot)$data[[1]]&lt;/code&gt;.&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;median_line_data &amp;lt;- 
  ggplot_build(portfolio_density_plot)$data[[1]] %&amp;gt;% 
  filter(x &amp;lt;= median)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now we can start adding aesthetics to the latest iteration of our graph, which is stored in the object &lt;code&gt;portfolio_density_plot_shaded&lt;/code&gt;.&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;portfolio_density_plot_shaded +
  
  geom_segment(aes(x = 0, y = 1.9, xend = -.045, yend = 1.9),
               arrow = arrow(length = unit(0.5, &amp;quot;cm&amp;quot;)), size = .05) +
  
  annotate(geom = &amp;quot;text&amp;quot;, x = -.02, y = .1, label = &amp;quot;returns &amp;lt;= mean&amp;quot;, 
           fontface = &amp;quot;plain&amp;quot;, alpha = .8, vjust =  -1) + 
  
  geom_segment(data = shaded_area_data, aes(x = mean, y = 0, xend = mean, yend = density), 
               color = &amp;quot;red&amp;quot;, linetype = &amp;quot;dotted&amp;quot;) +
  
  annotate(geom = &amp;quot;text&amp;quot;, x = mean, y = 5, label = &amp;quot;mean&amp;quot;, color = &amp;quot;red&amp;quot;, 
           fontface = &amp;quot;plain&amp;quot;, angle = 90, alpha = .8, vjust =  -1.75) +
  
  geom_segment(data = median_line_data, aes(x = median, y = 0, xend = median, yend = density), 
               color = &amp;quot;black&amp;quot;, linetype = &amp;quot;dotted&amp;quot;) +
  
  annotate(geom = &amp;quot;text&amp;quot;, x = median, y = 5, label = &amp;quot;median&amp;quot;, 
           fontface = &amp;quot;plain&amp;quot;, angle = 90, alpha = .8, vjust =  1.75) +
  
  
  ggtitle(&amp;quot;Density Plot Illustrating Skewness&amp;quot;)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;/post/2017-12-13-introduction-to-skewness_files/figure-html/unnamed-chunk-8-1.png&#34; width=&#34;672&#34; /&gt;&lt;/p&gt;
&lt;p&gt;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.&lt;/p&gt;
&lt;p&gt;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.&lt;/p&gt;
&lt;p&gt;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.&lt;/p&gt;
&lt;p&gt;In the xts world, calculating rolling skewness is almost identical to calculating rolling standard deviation, except we call the &lt;code&gt;skewness()&lt;/code&gt; function instead of &lt;code&gt;StdDev()&lt;/code&gt;. Since this is a rolling calculation, we need a window of time for each skewness; here, we will use a six-month window.&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;window &amp;lt;- 6
rolling_skew_xts &amp;lt;- na.omit(rollapply(portfolio_returns_xts_rebalanced_monthly, window, 
                           function(x) skewness(x)))&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now we pop that &lt;code&gt;xts&lt;/code&gt; object into &lt;code&gt;highcharter&lt;/code&gt; 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 &lt;code&gt;hc_yAxis(..., max = 3, min = -3)&lt;/code&gt;. I find that if we keep the range from 1 to -1, it makes most rolling skews look like a roller coaster.&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;library(highcharter)
highchart(type = &amp;quot;stock&amp;quot;) %&amp;gt;%
  hc_title(text = &amp;quot;Rolling&amp;quot;) %&amp;gt;%
  hc_add_series(rolling_skew_xts, name = &amp;quot;Rolling skewness&amp;quot;, color = &amp;quot;cornflowerblue&amp;quot;) %&amp;gt;%
  hc_yAxis(title = list(text = &amp;quot;skewness&amp;quot;),
           opposite = FALSE,
           max = 3, 
           min = -3) %&amp;gt;% 
  hc_navigator(enabled = FALSE) %&amp;gt;% 
  hc_scrollbar(enabled = FALSE) &lt;/code&gt;&lt;/pre&gt;
&lt;div id=&#34;htmlwidget-1&#34; style=&#34;width:100%;height:500px;&#34; class=&#34;highchart html-widget&#34;&gt;&lt;/div&gt;
&lt;script type=&#34;application/json&#34; data-for=&#34;htmlwidget-1&#34;&gt;{&#34;x&#34;:{&#34;hc_opts&#34;:{&#34;title&#34;:{&#34;text&#34;:&#34;Rolling&#34;},&#34;yAxis&#34;:{&#34;title&#34;:{&#34;text&#34;:&#34;skewness&#34;},&#34;opposite&#34;:false,&#34;max&#34;:3,&#34;min&#34;:-3},&#34;credits&#34;:{&#34;enabled&#34;:false},&#34;exporting&#34;:{&#34;enabled&#34;:false},&#34;plotOptions&#34;:{&#34;series&#34;:{&#34;turboThreshold&#34;:0},&#34;treemap&#34;:{&#34;layoutAlgorithm&#34;:&#34;squarified&#34;},&#34;bubble&#34;:{&#34;minSize&#34;:5,&#34;maxSize&#34;:25}},&#34;annotationsOptions&#34;:{&#34;enabledButtons&#34;:false},&#34;tooltip&#34;:{&#34;delayForDisplay&#34;:10},&#34;series&#34;:[{&#34;data&#34;:[[1375228800000,0.0511810666724179],[1377820800000,0.0965061660700991],[1380499200000,0.163579005279668],[1383177600000,-0.0111271137345324],[1385683200000,-0.329175858151934],[1388448000000,-0.682693636723609],[1391126400000,-0.299180325964799],[1393545600000,-1.05714468932288],[1396224000000,-1.12678024437719],[1398816000000,-0.907478344540586],[1401408000000,-0.923291364103329],[1404086400000,-0.994115098616124],[1406764800000,-0.106737545935232],[1409270400000,-0.963528231829294],[1412035200000,-0.750713278430844],[1414713600000,-0.912281062412703],[1417132800000,-0.767038118453993],[1419984000000,-0.252362341390629],[1422576000000,-0.281193932085033],[1424995200000,0.201889248675472],[1427760000000,0.758665508271901],[1430352000000,0.809337297292621],[1432857600000,0.96467960406329],[1435622400000,0.894828618685645],[1438300800000,0.917549882978621],[1440979200000,-0.859694106578632],[1443571200000,-0.416692593249242],[1446163200000,0.664519170731158],[1448841600000,0.6350949021651],[1451520000000,0.71546907245901],[1454025600000,0.99556931274623],[1456704000000,1.03166733380321],[1459382400000,0.36259225564619],[1461888000000,0.804397321645972],[1464652800000,0.81695627686931],[1467244800000,0.56636688270716],[1469750400000,0.966848135339037],[1472601600000,1.05049406444632],[1475193600000,1.28796539782198],[1477872000000,0.478246331580933],[1480464000000,-0.0334553106079278],[1483056000000,-0.314054096729488],[1485820800000,-1.18653628983962],[1488240000000,-1.52309795733004],[1490918400000,-1.65501284162553],[1493337600000,0.332506398750578],[1496188800000,0.633713296827805],[1498780800000,0.534386548695677],[1501459200000,0.719857327874752],[1504137600000,-0.0682564673333931],[1506643200000,-0.122507709915633],[1509408000000,-0.340860682326799],[1512000000000,-0.444064183403134],[1513036800000,-0.394434127262236]],&#34;name&#34;:&#34;Rolling skewness&#34;,&#34;color&#34;:&#34;cornflowerblue&#34;}],&#34;navigator&#34;:{&#34;enabled&#34;:false},&#34;scrollbar&#34;:{&#34;enabled&#34;:false}},&#34;theme&#34;:{&#34;chart&#34;:{&#34;backgroundColor&#34;:&#34;transparent&#34;}},&#34;conf_opts&#34;:{&#34;global&#34;:{&#34;Date&#34;:null,&#34;VMLRadialGradientURL&#34;:&#34;http =//code.highcharts.com/list(version)/gfx/vml-radial-gradient.png&#34;,&#34;canvasToolsURL&#34;:&#34;http =//code.highcharts.com/list(version)/modules/canvas-tools.js&#34;,&#34;getTimezoneOffset&#34;:null,&#34;timezoneOffset&#34;:0,&#34;useUTC&#34;:true},&#34;lang&#34;:{&#34;contextButtonTitle&#34;:&#34;Chart context menu&#34;,&#34;decimalPoint&#34;:&#34;.&#34;,&#34;downloadJPEG&#34;:&#34;Download JPEG image&#34;,&#34;downloadPDF&#34;:&#34;Download PDF document&#34;,&#34;downloadPNG&#34;:&#34;Download PNG image&#34;,&#34;downloadSVG&#34;:&#34;Download SVG vector image&#34;,&#34;drillUpText&#34;:&#34;Back to {series.name}&#34;,&#34;invalidDate&#34;:null,&#34;loading&#34;:&#34;Loading...&#34;,&#34;months&#34;:[&#34;January&#34;,&#34;February&#34;,&#34;March&#34;,&#34;April&#34;,&#34;May&#34;,&#34;June&#34;,&#34;July&#34;,&#34;August&#34;,&#34;September&#34;,&#34;October&#34;,&#34;November&#34;,&#34;December&#34;],&#34;noData&#34;:&#34;No data to display&#34;,&#34;numericSymbols&#34;:[&#34;k&#34;,&#34;M&#34;,&#34;G&#34;,&#34;T&#34;,&#34;P&#34;,&#34;E&#34;],&#34;printChart&#34;:&#34;Print chart&#34;,&#34;resetZoom&#34;:&#34;Reset zoom&#34;,&#34;resetZoomTitle&#34;:&#34;Reset zoom level 1:1&#34;,&#34;shortMonths&#34;:[&#34;Jan&#34;,&#34;Feb&#34;,&#34;Mar&#34;,&#34;Apr&#34;,&#34;May&#34;,&#34;Jun&#34;,&#34;Jul&#34;,&#34;Aug&#34;,&#34;Sep&#34;,&#34;Oct&#34;,&#34;Nov&#34;,&#34;Dec&#34;],&#34;thousandsSep&#34;:&#34; &#34;,&#34;weekdays&#34;:[&#34;Sunday&#34;,&#34;Monday&#34;,&#34;Tuesday&#34;,&#34;Wednesday&#34;,&#34;Thursday&#34;,&#34;Friday&#34;,&#34;Saturday&#34;]}},&#34;type&#34;:&#34;stock&#34;,&#34;fonts&#34;:[],&#34;debug&#34;:false},&#34;evals&#34;:[],&#34;jsHooks&#34;:[]}&lt;/script&gt;
&lt;p&gt;For completeness of methods, we can calculate rolling skewness in a &lt;code&gt;tibble&lt;/code&gt; and then use &lt;code&gt;ggplot&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;We will make use of &lt;code&gt;rollapply()&lt;/code&gt; from within &lt;code&gt;tq_mutate&lt;/code&gt; in &lt;code&gt;tidyquant&lt;/code&gt;.&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;rolling_skew_tidy &amp;lt;- 
  portfolio_returns_tq_rebalanced_monthly %&amp;gt;% 
  tq_mutate(select = returns, 
            mutate_fun = rollapply,
            width      = window,
            FUN        = skewness,
            col_rename = &amp;quot;skew&amp;quot;)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;code&gt;rolling_skew_tidy&lt;/code&gt; is ready for &lt;code&gt;ggplot&lt;/code&gt;. &lt;code&gt;ggplot&lt;/code&gt; is not purpose-built for time series plotting, but we can set &lt;code&gt;aes(x = date, y = skew)&lt;/code&gt; to make the x-axis our date values.&lt;/p&gt;
&lt;pre class=&#34;r&#34;&gt;&lt;code&gt;library(scales)
theme_update(plot.title = element_text(hjust = 0.5))
rolling_skew_tidy %&amp;gt;% 
  ggplot(aes(x = date, y = skew)) +
  geom_line(color = &amp;quot;cornflowerblue&amp;quot;) +  
  ggtitle(&amp;quot;Rolling Skew with ggplot&amp;quot;) +
  ylab(paste(&amp;quot;Rolling&amp;quot;, window, &amp;quot;month skewness&amp;quot;, sep = &amp;quot; &amp;quot;)) + 
  scale_y_continuous(limits = c(-3, 3), breaks = pretty_breaks(n = 8)) + 
  scale_x_date(breaks = pretty_breaks(n = 8))&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;img src=&#34;/post/2017-12-13-introduction-to-skewness_files/figure-html/unnamed-chunk-12-1.png&#34; width=&#34;672&#34; /&gt;&lt;/p&gt;
&lt;p&gt;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.&lt;/p&gt;
&lt;p&gt;That’s all for today. Thanks for reading and see you next time when we tackle kurtosis.&lt;/p&gt;

        &lt;script&gt;window.location.href=&#39;https://rviews.rstudio.com/2017/12/13/introduction-to-skewness/&#39;;&lt;/script&gt;
      </description>
    </item>
    
  </channel>
</rss>
