RosettaCodeData/Task/Statistics-Basic/MiniScript/statistics-basic.mini

40 lines
940 B
Plaintext

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