<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Untitled Publication]]></title><description><![CDATA[Untitled Publication]]></description><link>https://blog.danstockham.com</link><generator>RSS for Node</generator><lastBuildDate>Fri, 01 May 2026 07:20:51 GMT</lastBuildDate><atom:link href="https://blog.danstockham.com/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Calculate Standard Deviation with C#]]></title><description><![CDATA[Standard deviation in a data distribution is the amount of variance in between values within a population or sample. The higher the standard deviation, the greater the variance in the dataset. 
Although the calculation is complex and can be time cons...]]></description><link>https://blog.danstockham.com/calculate-standard-deviation-with-c</link><guid isPermaLink="true">https://blog.danstockham.com/calculate-standard-deviation-with-c</guid><category><![CDATA[C#]]></category><category><![CDATA[statistics]]></category><dc:creator><![CDATA[Dan Stockham]]></dc:creator><pubDate>Wed, 27 Apr 2022 03:13:34 GMT</pubDate><enclosure url="https://cdn.hashnode.com/res/hashnode/image/upload/v1651029182516/Ogb7_Dadr.jpg" length="0" type="image/jpeg"/><content:encoded><![CDATA[<p>Standard deviation in a data distribution is the amount of variance in between values within a population or sample. The higher the standard deviation, the greater the variance in the dataset. </p>
<p>Although the calculation is complex and can be time consuming to do by hand. Therefore, it's easier to calculate the standard deviation using a spreadsheet program or a calculation feature offered by software. </p>
<p>In this blog post, I'll be showing you how to write a small program that can take a dataset or list of values and find the standard deviation in that dataset.</p>
<h3 id="heading-how-to-do-the-manual-process">How to Do the Manual Process</h3>
<p>These are the steps for finding standard deviation:</p>
<ul>
<li><p>Find the mean or average in the dataset</p>
</li>
<li><p>Once you find the average you'll need to find the distance between the mean and the datapoint value</p>
</li>
<li><p>Then find the sum of your calculated values from the previous step</p>
</li>
<li><p>Divide your sum by the number of datapoints that you have. If this dataset is a sample of a population then you would subtract one from your count of datapoints</p>
</li>
<li><p>Finally square root the result</p>
</li>
</ul>
<h3 id="heading-writing-the-solution">Writing the Solution</h3>
<p>Let's write the code. Open up Visual Studio and create a new solution. Next, let us create a class called <code>StandardDeviation</code> </p>
<pre><code><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">StandardDeviation</span></span>
{

}
</code></pre><p>In order to find the standard deviation we need a dataset to perform our calculation. We'll need to create a constructor that will accept the dataset. The datatype should be an interface <code>IEnumerable</code> so we can accept collection types that use that interface. Also we want to store this value after the calculation and use it by reference in other parts of our code. We shall call this class field <code>Value</code> and make it getter only. </p>
<pre><code><span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title">StandardDeviation</span>
{
      <span class="hljs-keyword">public</span> <span class="hljs-keyword">double</span> Value { <span class="hljs-keyword">get</span>; <span class="hljs-keyword">internal</span> <span class="hljs-keyword">set</span>; }

      <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-title">StandardDeviation</span>(<span class="hljs-params">IEnumerable&lt;<span class="hljs-keyword">double</span>&gt; dataset, <span class="hljs-keyword">bool</span> sample = <span class="hljs-literal">false</span></span>)</span>
      {

      }
}
</code></pre><p>The first step in finding standard deviation is getting the average or mean of all datapoints. But first, we need to create a method that will house our calculation steps. This method will be called <code>CalculateStandardDeviation</code> and it will used when the class is constructed.</p>
<pre><code><span class="hljs-keyword">public</span> class StandardDeviation
{
      <span class="hljs-keyword">public</span> double Value { get; <span class="hljs-keyword">internal</span> set; }

      <span class="hljs-keyword">public</span> StandardDeviation(IEnumerable<span class="hljs-operator">&lt;</span>double<span class="hljs-operator">&gt;</span> dataset, <span class="hljs-keyword">bool</span> sample <span class="hljs-operator">=</span> <span class="hljs-literal">false</span>)
      {
            Value <span class="hljs-operator">=</span> CalculateStandardDeviation(dataset, sample);
      }

      <span class="hljs-keyword">private</span> double CalculateStandardDeviation(IEnumerable<span class="hljs-operator">&lt;</span>double<span class="hljs-operator">&gt;</span> dataset, <span class="hljs-keyword">bool</span> sample <span class="hljs-operator">=</span> <span class="hljs-literal">false</span>)
      {
            <span class="hljs-keyword">int</span> datapointCount <span class="hljs-operator">=</span> sample ? dataset.Count() <span class="hljs-operator">-</span> <span class="hljs-number">1</span> : dataset.Count();
            double mean <span class="hljs-operator">=</span> dataset.Sum() <span class="hljs-operator">/</span> datapointCount;
      }
}
</code></pre><p>Depending how we are deriving dataset, we'll need to decide when to find the mean using the entire count of the dataset or subtracting one count when calculating a sample. We added additional argument to determine if the dataset is a sample using <code>sample</code>.</p>
<p>The next step we'll need to find the distance of each datapoint from the mean</p>
<pre><code><span class="hljs-keyword">public</span> class StandardDeviation
{
      <span class="hljs-keyword">public</span> double Value { get; <span class="hljs-keyword">internal</span> set; }

      <span class="hljs-keyword">public</span> StandardDeviation(IEnumerable<span class="hljs-operator">&lt;</span>double<span class="hljs-operator">&gt;</span> dataset, <span class="hljs-keyword">bool</span> sample <span class="hljs-operator">=</span> <span class="hljs-literal">false</span>)
      {
            Value <span class="hljs-operator">=</span> CalculateStandardDeviation(dataset, sample);
      }

       <span class="hljs-keyword">private</span> double CalculateStandardDeviation(IEnumerable<span class="hljs-operator">&lt;</span>double<span class="hljs-operator">&gt;</span> dataset, <span class="hljs-keyword">bool</span> sample <span class="hljs-operator">=</span> <span class="hljs-literal">false</span>)
      {
            List<span class="hljs-operator">&lt;</span>double<span class="hljs-operator">&gt;</span> squaredDistances <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> List<span class="hljs-operator">&lt;</span>double<span class="hljs-operator">&gt;</span>();
            double meanSquaredDistances <span class="hljs-operator">=</span> <span class="hljs-number">0</span>;

            <span class="hljs-keyword">int</span> datapointCount <span class="hljs-operator">=</span> sample ? dataset.Count() <span class="hljs-operator">-</span> <span class="hljs-number">1</span> : dataset.Count();
            double mean <span class="hljs-operator">=</span> dataset.Sum() <span class="hljs-operator">/</span> datapointCount;

            foreach (double datapoint in dataset)
            {
                double distance <span class="hljs-operator">=</span> Math.Pow(Math.Abs(datapoint <span class="hljs-operator">-</span> mean), <span class="hljs-number">2</span>);
                squaredDistances.Add(distance);
            }
       }
}
</code></pre><p>This can found by iterating over our datapoints and finding the difference between the datapoint value and the mean. Since standard deviation cannot be negative, we're finding the absolute value of our result and raising it to the power of two. Once we have our value, we inject this value into a collection variable that contains our distances.</p>
<p>Next we'll sum our distances and divide by our number of datapoints (or number of datapoints minus 1). Finally we find the square root of mean of distances.</p>
<pre><code><span class="hljs-keyword">public</span> class StandardDeviation
{
      <span class="hljs-keyword">public</span> double Value { get; <span class="hljs-keyword">internal</span> set; }

      <span class="hljs-keyword">public</span> StandardDeviation(IEnumerable<span class="hljs-operator">&lt;</span>double<span class="hljs-operator">&gt;</span> dataset, <span class="hljs-keyword">bool</span> sample <span class="hljs-operator">=</span> <span class="hljs-literal">false</span>)
      {
            Value <span class="hljs-operator">=</span> CalculateStandardDeviation(dataset, sample);
      }

     <span class="hljs-keyword">private</span> double CalculateStandardDeviation(IEnumerable<span class="hljs-operator">&lt;</span>double<span class="hljs-operator">&gt;</span> dataset, <span class="hljs-keyword">bool</span> sample <span class="hljs-operator">=</span> <span class="hljs-literal">false</span>)
        {
            List<span class="hljs-operator">&lt;</span>double<span class="hljs-operator">&gt;</span> squaredDistances <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> List<span class="hljs-operator">&lt;</span>double<span class="hljs-operator">&gt;</span>();
            double meanSquaredDistances <span class="hljs-operator">=</span> <span class="hljs-number">0</span>;

            <span class="hljs-keyword">int</span> datapointCount <span class="hljs-operator">=</span> sample ? dataset.Count() <span class="hljs-operator">-</span> <span class="hljs-number">1</span> : dataset.Count();
            double mean <span class="hljs-operator">=</span> dataset.Sum() <span class="hljs-operator">/</span> datapointCount;

            foreach (double datapoint in dataset)
            {
                double distance <span class="hljs-operator">=</span> Math.Pow(Math.Abs(datapoint <span class="hljs-operator">-</span> mean), <span class="hljs-number">2</span>);
                squaredDistances.Add(distance);
            }

            meanSquaredDistances <span class="hljs-operator">=</span> squaredDistances.Sum() <span class="hljs-operator">/</span> datapointCount;

            <span class="hljs-keyword">return</span> Math.Sqrt(meanSquaredDistances);
      }
}
</code></pre><p>In this article, I showed how to code a straightforward standard deviation solution using C#. The solution takes in a dataset and we declare if the dataset is the population or the sample. It then finds the mean, calculates the datapoint values against the mean, finds the mean of the distances, and finally square roots that mean.</p>
<p>Thanks for checking out my blog post!</p>
]]></content:encoded></item></channel></rss>