*NOTE: Do not add/delete values from the data set once you've already entered all the values. This will cause the mean and MAD to become inaccurate, because the mean and MAD shown will apply to the old data set. To change the data set, just run the project again. The Mean Absolute Deviation, or MAD, is a concept that I have revently learned in my math class. I found this concept interesting and decided to figure out how to calculate it using code! Here's how the MAD is calculated: 1. You need a data set, which is just a group of numbers. For this example, I will be using this data set: 1, 2, 3, 4, 5 The MAD is the average difference from each value to the mean. So, we first need to find the mean, which basically means the average. It is calculated by adding up all the values, and dividing that by the number of values. So: 1 + 2 + 3 + 4 + 5 = 15 The sum of all the values is 15. 15 / 5 = 3 The sum of the values divided by the number of values is 3. So, the mean of this data set is 3! Now we can use the mean to calculate the MAD, which, as I mentioned earlier, is the average distance from each number to the mean. It is used to interpet the spread of a data set. So, first find each value's distance from the mean. (Remember, the mean is 3.) This is the data set: 1, 2, 3, 4, 5 The first value in this data set is 1. To find the distance between 1 and the mean, we can subtract the mean from 1. 1 - 3 is -2. But you might be thinking, how could the distance be a negative number? Well, we can fix this by turning it into a positive number using a concept called absolute value. In simple terms, absolute value is just removing the negative sign from a number. If the number is already positive, or a zero, you don't need to change anything—the number itself is the absolute value! (You've probably realized that absolute value is basically a number's distance from 0.) The symbol used for absolute value: | So, if 1 - 3 is -2, then | 1 - 3 | is 2. That's the first distance! Now let's find the distances for the rest of the values: | 2 - 3 | = 1 | 3 - 3 | = 0 | 4 - 3 | = 1 | 5 - 3 | = 2 So, the distances from each value to the mean are 1, 0, 1, and 2. Remember that the MAD (mean absolute deviation) is the average distance from each value to the mean. And the term for average is mean! So basically, we need to find the mean distance from each number to the mean of the data set. Do you remember how to find the mean? It's the sum of all values, divided by the number of values. So, we need to find the mean distance by first adding up all the distances: 2 + 1 + 0 + 1 + 2 = 6 Now, divide by the number of values! 6 / 5 = 1.2 So, the MAD of this data set is 1.2! This shows that the average distance from each number to the mean is 1.2. So, the spread of the data is pretty small. Look inside to see how I calculated this using code!
I had originally created this script on my test account, @f-l-o-r-a-l. I was pretty happy with it, so I decided to make a project on my main account using this script! All code is by me, and was originally made on my other account! @f-l-o-r-a-l The thumbnail font is from cooltext.com. Everything else is by me. Oh wait—the music is from bensound.com. Despite the fact that it seems complicated, this code actually didn't take me very long to make. I'd say it probably took me around 30 minutes to code this. But even though it was pretty easy, I'm pretty proud of myself for figuring this out! Enjoy :) - @floral_sunset