<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/how-to-de-bias-standard-deviation-estimates/"></div></div><div></div></div><div></div></div>
<div>
(This article was first published on <strong><a href="http://www.win-vector.com/blog/2018/11/how-to-de-bias-standard-deviation-estimates/"> R – Win-Vector Blog</a></strong>, and kindly contributed to <a rel="nofollow" href="https://www.r-bloggers.com/">R-bloggers)</a> </div>
<p>This note is about attempting to remove the bias brought in by using sample standard deviation estimates to estimate an unknown true standard deviation of a population. We establish there is a bias, concentrate on why it is <em>not</em> important to remove it for reasonable sized samples, and (despite that) give a very complete bias management solution.</p>
<p><span></span></p>
<p>
<p>Everitt’s <em>The Cambridge Dictionary of Statistics</em> 2nd Edition, Cambridge University Press defines a “statistic” as:</p>
<blockquote>
<p>
A numeric characteristic of a sample.
</p>
</blockquote>
<p>Informally we can say a statistic is a summary. And this lets us say the field of statistics is the science of relating the observed summaries of samples to the corresponding unobserved summary of the total population or universe the samples are drawn from.</p>
<p>For example: we can take our universe to be the set of female adult crew members of the Titanic, and our observable to be if they survived or not. Then our universe or total population is the following:</p>
<pre>
  20 survivors  (we will code these as 1's)
   3 fatalities (we will code these as 0's)
</pre>
<p>We can ask <a rel="nofollow" href="https://www.r-project.org/" target="_blank">R</a> to show us some common summaries: mean (which we will denote as “p”), variance, and standard_deviation.</p>
<pre>
universe = c(rep(1, 20), rep(0, 3))

p = mean(universe)
print(p)
# [1] 0.8695652

variance &lt;- mean((universe - p)^2)
print(variance)
# [1] 0.1134216

standard_deviation &lt;- sqrt(variance)
print(standard_deviation)
# [1] 0.3367812
</pre>
<p>Note, we <em>deliberately</em> did not call R’s <code>var()</code> and <code>sd()</code> methods, as they both include <a rel="nofollow" href="https://en.wikipedia.org/wiki/Bessel%27s_correction" target="_blank">Bessel’s sample correction</a>. For an entire universe (sometimes called a population) the sample correction is inappropriate, and leads to a wrong answer.</p>
<p>Python’s statistics library has a sensible approach. It presents un-corrected (population or universe versions) and corrected (sample versions) on equal footing:</p>
<blockquote>
<p>
<a rel="nofollow" href="https://docs.python.org/3/library/statistics.html" target="_blank"><img border="0" src="https://i0.wp.com/www.win-vector.com/blog/wp-content/uploads/2018/11/NewImage.png?resize=641%2C181" alt="NewImage" width="450" title="NewImage.png" data-recalc-dims="1"></a></p>
<p>
From: <a rel="nofollow" href="https://docs.python.org/3/library/statistics.html" target="_blank">https://docs.python.org/3/library/statistics.html</a>.

</blockquote>
<p>The bias in question is falling off at a rate of <code>1/n</code> (where <code>n</code> is our sample size). So the bias issue loses what little gravity it ever may have ever had when working with big data. Most sources of noise will be falling off at a slower rate of <code>1/sqrt(n)</code>, so it is <em>unlikely</em> this bias is going to be the worst feature of your sample.</p>
<p>But let’s <em>pretend</em> the sample size correction indeed is an important point for a while.</p>
<blockquote>
<p>
Under the “no bias allowed” rubric: if it is so vitally important to bias-correct the variance estimate, would it not be equally critical to correct the standard deviation estimate?
</p>
</blockquote>
<p>The practical answer seems to be: no. The straightforward standard deviation estimate itself is biased (it has to be, as a consequence of <a rel="nofollow" href="https://en.wikipedia.org/wiki/Jensen%27s_inequality" target="_blank">Jensen’s inequality</a>). And pretty much nobody cares, corrects it, or teaches how to correct it, as it just isn’t worth the trouble.</p>
<p>Let’s convert that to a worked example. We are going to investigate the statistics of drawing samples of size 5 (uniformly with replacement) from our above universe or population. To see what is going on we will draw a great number of examples (though in practice when we could do this, we could more easily directly summarize over this small universe).</p>
<p>Here is the distribution of observed sample variances for both the naive calculation (assuming the variance observed on the sample is representative of the universe it was draw from) and the Bessel corrected calculation (inflating the sample estimate of variance by <code>n/(n-1)</code>, or 25% in this case). The idea is: the observable Bessel corrected variances of samples converge to the unobserved un-corrected (or naive) variance of the population.</p>
<p><img border="0" src="https://i0.wp.com/www.win-vector.com/blog/wp-content/uploads/2018/11/run-22.png?resize=660%2C495" alt="Run 22" width="450" title="run-22.png" data-recalc-dims="1"></p>
<p>We generated the above graph by uniformly drawing samples (with replacement) of size 5 from the above universe. The experiment was repeated 100000 times and we are depicting the distribution of what variance estimates were seen as a density plot (only a few discrete values occur in small samples). The black dashed vertical line is the mean of the 100000 variance estimates, and the vertical red line and blocks structure indicates the true variance of the original universe or population.</p>
<p>Notice the Bessel corrected estimate of variance is itself a higher variance estimate: many of the adjusted observations are further away from the true value. The Bessel correction (in this case) didn’t make any one estimate better, it made the average over all possible estimates better. In this case it is inflating all the positive variance estimates to trade-off against the zero variance estimates.</p>
<p>Now suppose we run the same experiment for standard deviation estimates.</p>
<p><img border="0" src="https://i2.wp.com/www.win-vector.com/blog/wp-content/uploads/2018/11/run-23.png?resize=660%2C495" alt="Run 23" width="450" title="run-23.png" data-recalc-dims="1"></p>
<p>The Bessel adjustment helped, but was nowhere near correcting the bias. Both estimates of standard deviation are severely downward biased.</p>
<p>Here is a that experiment again, including an additional correction procedure (which we will call joint scale corrected).</p>
<p><img border="0" src="https://i0.wp.com/www.win-vector.com/blog/wp-content/uploads/2018/11/run-24.png?resize=660%2C495" alt="Run 24" width="450" title="run-24.png" data-recalc-dims="1"></p>
<p>Notice the scale corrected estimate is unbiased. We admit, if this were so massively important it would be taught more commonly. However, as standard deviations summaries are more common than variance summaries (example: <code>summary.lm()</code>): having an unbiased estimate for a standard deviation is probably <em>more important</em> than having an unbiased estimate for variance.</p>
<p>The scale corrected estimate did raise the variance of the estimation procedure, but not much more than the Bessel correction did:</p>
<pre>
     estimation_method variance_of_estimate
 1: scale_corrected_sd           0.05763954
 2:           naive_sd           0.04554781
 3:          Bessel_sd           0.05693477
</pre>
<p>How did we correct the standard deviation estimate?</p>
<p>It is quite simple:</p>
<ol>
<li>
For a binomial outcome and sample size <code>n</code>, the standard deviation estimate is essentially a lookup table that maps the pair <code>n</code> (the sample size) and <code>k</code> (the number of 1’s seen) to an estimate. Call this table <code>est(n,.)</code>. Remember <code>est(n,.)</code> is just a table of <code>n+1</code> numbers.</li>
<li>The expected value of our estimator is just going to be the sum of the probability of each observable situation times the estimate we make in that situation. This is supposed to match the unknown true standard deviation which is <code>sqrt(p(1-p))</code> (assuming we exactly knew <code>p</code>). This means for any <code>p</code> we want these two quantities near each other, or:<br>
<img border="0" src="https://i0.wp.com/www.win-vector.com/blog/wp-content/uploads/2018/11/NewImage-2.png?fit=660%2C50" alt="NewImage" title="NewImage.png" data-recalc-dims="1" height="50"><br>
<code>dbinom(k, n, p)</code> being the probability of drawing “<code>k</code>” 1’s from in n draws when 1’s have a probability of p (this is a known quantity, a value we just copy in). More examples of using this methodology can be found <a rel="nofollow" href="http://www.win-vector.com/blog/2014/07/frequenstist-inference-only-seems-easy/" target="_blank">here</a>.
</li>
<li>
We ask an optimizer to pick values for <code>est(n,.)</code> that simultaneously make many of these check equations near true. We used a discrete set of <code>p</code>‘s with a Jeffreys prior style example weighing. One could probably recast this as a calculus of variations problem over all <code>p</code>s and work closer to an analytic solution.
</li>
</ol>
<p>The concept is from signal processing: observation is an averaging filter, so we need to design an approximate inverse filter (something like an <a rel="nofollow" href="https://en.wikipedia.org/wiki/Unsharp_masking" target="_blank">unsharp mask</a>). The above inverse filter was specialized for the 0/1 binomial case, but one can use the above principles do design inverse filters for additional situations.</p>
<p>All of the above was <a rel="nofollow" href="https://github.com/WinVector/sigr/blob/master/extras/BiasEsts.md" target="_blank">easy in R</a> and we got the following estimation table (here shown graphically with the un-adjusted and also Bessel adjusted estimation procedures).</p>
<p>In the above graph: <code>n</code> is the sample size, <code>k</code> is how many 1’s are observed, and the y axis is the estimate of the unknown true population standard deviation.</p>
<p><img border="0" src="https://i0.wp.com/www.win-vector.com/blog/wp-content/uploads/2018/11/run-10.png?resize=660%2C495" alt="Run 10" width="450" title="run-10.png" data-recalc-dims="1"></p>
<p>Notice the “joint scaled” solution wiggles around a bit.</p>
<p>As we have said, the above graph is the estimator. For samples of size 5 pick the estimator you want to use and the k corresponding to how many 1’s you saw in your sample: then the y height is the estimate you should use for population standard deviation.</p>
<p>In the next graph: <code>p</code> is the unknown true frequency of 1’s, and the <code>y</code>-axis is the difference between the expected value of the estimated standard deviation and the true standard deviation.</p>
<p><img border="0" src="https://i2.wp.com/www.win-vector.com/blog/wp-content/uploads/2018/11/run-12.png?resize=660%2C495" alt="Run 12" width="450" title="run-12.png" data-recalc-dims="1"></p>
<p>Notice the joint scaled estimate reports a non-zero standard deviation even for all zeros or all ones samples. Also notice the joint estimate tries to stay near the desired difference of zero using a smooth curve that wiggles above and below the desired difference of zero.</p>
<p>The next graph is the same sort of presentation for ratios of estimated to true standard deviations.</p>
<p><img border="0" src="https://i0.wp.com/www.win-vector.com/blog/wp-content/uploads/2018/11/run-11.png?resize=660%2C495" alt="Run 11" width="450" title="run-11.png" data-recalc-dims="1"></p>
<p>We still see the Bessel corrected estimates are better than naive, but not as good as jointly adjusted.</p>
<p>And that is how you correct standard deviation estimates (at least for binomial experiments). We emphasize that nobody makes this correction in practice, and for big data and large samples the correction is entirely pointless. The variance correction is equally pointless, but since it is easier to perform it is usually added.</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="http://www.win-vector.com/blog/2018/11/how-to-de-bias-standard-deviation-estimates/"> R – Win-Vector Blog</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/-MGYCp8wAos" height="1"></a></div>