How to resolve the algorithm Statistics/Basic step by step in the Jsish programming language
How to resolve the algorithm Statistics/Basic step by step in the Jsish 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 Jsish programming language
Source code in the jsish programming language
#!/usr/bin/env jsish
"use strict";
function statisticsBasic(args:array|string=void, conf:object=void) {
var options = { // Rosetta Code, Statistics/Basic
rootdir :'', // Root directory.
samples : 0 // Set sample size from options
};
var self = { };
parseOpts(self, options, conf);
function generateStats(n:number):object {
var i, sum = 0, sum2 = 0;
var hist = new Array(10);
hist.fill(0);
for (i = 0; i < n; i++) {
var r = Math.random();
sum += r;
sum2 += r*r;
hist[Math.floor((r*10))] += 1;
}
var mean = sum/n;
var stddev = Math.sqrt((sum2 / n) - mean*mean);
var obj = {n:n, sum:sum, mean:mean, stddev:stddev};
return {n:n, sum:sum, mean:mean, stddev:stddev, hist:hist};
}
function reportStats(summary:object):void {
printf("Samples: %d, mean: %f, stddev: %f\n", summary.n, summary.mean, summary.stddev);
var max = Math.max.apply(summary, summary.hist);
for (var i = 0; i < 10; i++) {
printf("%3.1f+ %-70s %5d\n", i * 0.1, 'X'.repeat(70 * summary.hist[i] / max), summary.hist[i]);
}
return;
}
function main() {
LogTest('Starting', args);
switch (typeof(args)) {
case 'string': args = [args]; break;
case 'array': break;
default: args = [];
}
if (self.rootdir === '')
self.rootdir=Info.scriptDir();
Math.srand(0);
if (self.samples > 0) reportStats(generateStats(self.samples));
else if (args[0] && parseInt(args[0])) reportStats(generateStats(parseInt(args[0])));
else for (var n of [100, 1000, 10000]) reportStats(generateStats(n));
debugger;
LogDebug('Done');
return 0;
}
return main();
}
provide(statisticsBasic, 1);
if (isMain()) {
if (!Interp.conf('unitTest'))
return runModule(statisticsBasic);
;' statisticsBasic unit-test';
; statisticsBasic();
}
/*
=!EXPECTSTART!=
' statisticsBasic unit-test'
statisticsBasic() ==> Samples: 100, mean: 0.534517, stddev: 0.287124
0.0+ XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX 8
0.1+ XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX 11
0.2+ XXXXXXXXXXXXXXXXXXXXXXXXXX 6
0.3+ XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX 10
0.4+ XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX 10
0.5+ XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX 11
0.6+ XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX 8
0.7+ XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX 16
0.8+ XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX 7
0.9+ XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX 13
Samples: 1000, mean: 0.490335, stddev: 0.286562
0.0+ XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX 98
0.1+ XXXXXXXX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XXXXXXXXXXXXXXXX 1031
0.6+ XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX 971
0.7+ XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX 999
0.8+ XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX 991
0.9+ XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX 996
0
=!EXPECTEND!=
*/
You may also check:How to resolve the algorithm Copy a string step by step in the sed programming language
You may also check:How to resolve the algorithm FizzBuzz step by step in the Yabasic programming language
You may also check:How to resolve the algorithm Fivenum step by step in the Kotlin programming language
You may also check:How to resolve the algorithm Stack traces step by step in the PHP programming language
You may also check:How to resolve the algorithm Lychrel numbers step by step in the Wren programming language