How to resolve the algorithm Statistics/Basic step by step in the MiniScript programming language
How to resolve the algorithm Statistics/Basic step by step in the MiniScript programming language
Table of Contents
Problem Statement
Statistics is all about large groups of numbers.
When talking about a set of sampled data, most frequently used is their mean value and standard deviation (stddev).
If you have set of data xi where i =1,2,...n
When examining a large quantity of data, one often uses a histogram, which shows the counts of data samples falling into a prechosen set of intervals (or bins).
When plotted, often as bar graphs, it visually indicates how often each data value occurs.
Task Using your language's random number routine, generate real numbers in the range of [0, 1]. It doesn't matter if you chose to use open or closed range.
Create 100 of such numbers (i.e. sample size 100) and calculate their mean and stddev.
Do so for sample size of 1,000 and 10,000, maybe even higher if you feel like.
Show a histogram of any of these sets.
Do you notice some patterns about the standard deviation?
Extra Sometimes so much data need to be processed that it's impossible to keep all of them at once. Can you calculate the mean, stddev and histogram of a trillion numbers? (You don't really need to do a trillion numbers, just show how it can be done.)
For a finite population with equal probabilities at all points, one can derive:
Or, more verbosely.
See also: Statistics/Normal distribution
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Statistics/Basic step by step in the MiniScript programming language
Source code in the miniscript programming language
Stats = {}
Stats.count = 0
Stats.sum = 0
Stats.sumOfSquares = 0
Stats.histo = null
Stats.add = function(x)
self.count = self.count + 1
self.sum = self.sum + x
self.sumOfSquares = self.sumOfSquares + x*x
bin = floor(x*10)
if not self.histo then self.histo = [0]*10
self.histo[bin] = self.histo[bin] + 1
end function
Stats.mean = function()
return self.sum / self.count
end function
Stats.stddev = function()
m = self.sum / self.count
return sqrt(self.sumOfSquares / self.count - m*m)
end function
Stats.histogram = function()
for i in self.histo.indexes
print "0." + i + ": " + "=" * (self.histo[i]/self.count * 200)
end for
end function
for sampleSize in [100, 1000, 10000]
print "Samples: " + sampleSize
st = new Stats
for i in range(sampleSize)
st.add rnd
end for
print "Mean: " + st.mean + " Standard Deviation: " + st.stddev
st.histogram
end for
You may also check:How to resolve the algorithm Short-circuit evaluation step by step in the Ada programming language
You may also check:How to resolve the algorithm Color wheel step by step in the M2000 Interpreter programming language
You may also check:How to resolve the algorithm Singly-linked list/Traversal step by step in the Python programming language
You may also check:How to resolve the algorithm Munching squares step by step in the Icon and Unicon programming language
You may also check:How to resolve the algorithm Unicode variable names step by step in the Nim programming language