{"id":6368,"date":"2024-05-01T00:01:16","date_gmt":"2024-05-01T07:01:16","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=6368"},"modified":"2024-05-08T08:38:59","modified_gmt":"2024-05-08T15:38:59","slug":"calculating-the-standard-deviation","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=6368","title":{"rendered":"Calculating the Standard Deviation"},"content":{"rendered":"<h2>Difficulty: &#9733; &#9733; &#9734; &#9734;<\/h2>\n<p>Even when statistics has barely brushed by you on the subway, you probably know the term <em>standard deviation<\/em>. It refers to how data is distributed in a group, their distance from the mean. You can use your C programming kung fu to code the standard deviation of a data set, which is the challenge for this month&#8217;s Exercise.<br \/>\n<!--more--><br \/>\nBefore marching naked into the unforgiving jungle of statistics, here are some terms to know:<\/p>\n<ul>\n<li><strong>mean<\/strong> The sum of the values in a set divided by the number of values, symbol &#956;. The <em>mean<\/em> is what most people refer to when they say &#8220;average.&#8221;<\/li>\n<li><strong>median<\/strong> The value in the middle of a set of sorted numbers. The <em>median<\/em> of {2, 3, 6, 10, 20}  is 6.<\/li>\n<li><strong>mode<\/strong> The value that appears most frequently in a set. The <em>mode<\/em> of {2, 3, 4, 4, 4, 10} is 4.<\/li>\n<li><strong>average<\/strong> A single value that best represents a set. It could be the <em>mean<\/em> (arithmetic mean) but can also be a value that isn&#8217;t mathematically the mean.<\/li>\n<li><strong>standard deviation<\/strong> The measure of variation from the mean, symbol &#963;. The higher the standard deviation, the more atypical the data point.<\/li>\n<\/ul>\n<p>As an example, say that the mean height for a group of adult men is 5&#8217;10&#8221;, or 70 inches tall. Values shorter or taller than the mean are graphed out as shown in Figure 1, which forms the classic bell curve.<\/p>\n<div id=\"attachment_6375\" style=\"width: 560px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-6375\" src=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2024\/05\/0401-Figure1.png\" alt=\"Bell curve with standard deviations marked\" width=\"550\" height=\"375\" class=\"size-full wp-image-6375\" srcset=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2024\/05\/0401-Figure1.png 550w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2024\/05\/0401-Figure1-300x205.png 300w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2024\/05\/0401-Figure1-440x300.png 440w\" sizes=\"auto, (max-width: 550px) 100vw, 550px\" \/><p id=\"caption-attachment-6375\" class=\"wp-caption-text\">Figure 1. Distribution for adult male heights, along with standard deviation values.<\/p><\/div>\n<p>In the Figure, each standard deviation from the mean is four inches: A man 78&#8243; tall (6&#8217;6&#8243;) is two standard deviations from the norm. A man who is 58&#8243; tall (4&#8217;10&#8221;) is three standard deviations away. If a man is 72&#8243; tall, he&#8217;s within the first standard deviation, representing 68 percent of all heights in the group.<\/p>\n<p>To calculate the standard deviation, you obey these steps:<\/p>\n<ol>\n<li>Calculate the mean for the values.<\/li>\n<li>Calculate the deviation from the mean for each value and square the result.<\/li>\n<li>Calculate the mean of the results, which is a value called the variance.<\/li>\n<li>The square root of the variance is the standard deviation.<\/li>\n<\/ol>\n<p>The ugly formula for calculating the standard deviation is shown in Figure 2. It&#8217;s used when data is available for the entire population, know as the Population Standard Deviation. A different formula is used when you have only a sample of the entire population.<\/p>\n<div id=\"attachment_6378\" style=\"width: 400px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-6378\" src=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2024\/05\/0501-Figure2.png\" alt=\"Standard deviation calculation\" width=\"390\" height=\"222\" class=\"size-full wp-image-6378\" srcset=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2024\/05\/0501-Figure2.png 390w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2024\/05\/0501-Figure2-300x171.png 300w\" sizes=\"auto, (max-width: 390px) 100vw, 390px\" \/><p id=\"caption-attachment-6378\" class=\"wp-caption-text\">Figure 2. The mathematical monster used to calculate standard deviation (full set).<\/p><\/div>\n<p>In Figure 2, <em>N<\/em> is the number of items in the set, the data points. <em>x<sub>i<\/sub><\/em> represents each data point. <em>&#956;<\/em> is the mean. The result is <em>&#963;<\/em>, the standard deviation. You total the items in the set, subtracting each from the mean (the deviation) and squaring the results. This total is divided by the number data points, <em>N<\/em>, to obtain the variance. The square root of the variance is the standard deviation.<\/p>\n<p>Your challenge for this month&#8217;s Exercise is to calculate the standard deviation for the data set presented in the sample code. Specifically, you&#8217;re to write the <em>stddev()<\/em> function referenced.<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2024_05_01-Lesson.c\" rel=\"noopener\" target=\"_blank\">2024_05_01-Lesson.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\n<span class=\"comments\">\/* whole population calculation *\/<\/span>\r\ndouble stddev(int v[],int items)\r\n{\r\n}\r\n\r\nint main()\r\n{\r\n    int values[] = { 10, 12, 23, 23, 16, 23, 21, 16 };\r\n    int x,items;\r\n\r\n    <span class=\"comments\">\/* output the array's values *\/<\/span>\r\n    items = sizeof(values)\/sizeof(int);\r\n    printf(\"Values:\");\r\n    for( x=0; x&lt;items; x++ )\r\n    {\r\n        printf(\" %2d\",values[x]);\r\n    }\r\n    putchar('\\n');\r\n\r\n    printf(\"The standard deviation is %.4f\\n\",\r\n            stddev(values,items)\r\n          );\r\n\r\n    return 0;\r\n}<\/pre>\n<p>Here&#8217;s a sample run from my solution:<\/p>\n<p><code>Values: 10 12 23 23 16 23 21 16<br \/>\nThe standard deviation is 4.8990<\/code><\/p>\n<p>Please try this exercise on your own before you peek at <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=6391\">my solution<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The standard deviation is a math thing that plays into statistics, something you can code even if you don&#8217;t understand what it means. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=6368\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-6368","post","type-post","status-publish","format-standard","hentry","category-exercise"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6368","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=6368"}],"version-history":[{"count":13,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6368\/revisions"}],"predecessor-version":[{"id":6410,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6368\/revisions\/6410"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6368"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6368"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6368"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}