Statistics/Basic: Difference between revisions

→‎{{header|jq}}: remove pending repairs
(→‎{{header|jq}}: 100 million)
(→‎{{header|jq}}: remove pending repairs)
Line 2,016:
0.8: **************************************************
0.9: **************************************************</pre>
 
=={{header|jq}}==
{{works with|jq}}
'''Works with gojq, the Go implementation of jq'''
 
The following jq program uses a streaming approach so that only one PRN (pseudo-random number)
need be in memory at a time.
For PRNs in [0,1], as here, the program is thus essentially only limited by CPU time available.
In the example section below, we include N=100 million.
 
Since jq does not currently have a built-in PRNG, we will use an external source
of entropy; there are, however, RC entries giving PRN generators written in jq that could be used,
e.g. https://rosettacode.org/wiki/Subtractive_generator#jq
 
For the sake of illustration, we will use /dev/urandom encapsulated in a shell function:
<lang sh># Usage: prng N width
function prng {
cat /dev/urandom | tr -cd '0-9' | fold -w "$2" | head -n "$1"
}</lang>
 
'''basicStats.jq'''
<lang jq> # $histogram should be a JSON object, with buckets as keys and frequencies as values.
# $keys should be an array of all the potential bucket names (possibly integers)
# in the order to be used for display:
def pp($histogram; $keys):
([$histogram[]] | add) as $n # for scaling
| ($keys|length) as $length
| $keys[]
| "\(.) : \("*" * (($histogram[tostring] // 0) * 20 * $length / $n) // "" )" ;
 
# `basic_stats` computes the unadjusted standard deviation
# and assumes the sum of squares (ss) can be computed without concern for overflow.
# The histogram is based on allocation to a bucket, which is made
# using `bucketize`, e.g. `.*10|floor`
def basic_stats(stream; bucketize):
# Use
reduce stream as $x ({histogram: {}};
.count += 1
| .sum += $x
| .ss += $x * $x
| .mean = (.sum / .count)
| .stddev = (((.ss/.count) - .mean*.mean) | sqrt)
| ($x | bucketize | tostring) as $bucket
| .histogram[$bucket] += 1) ;
 
basic_stats( "0." + inputs | tonumber; .*10|floor)
| "
 
Basic statistics for \(.count) PRNs in [0,1]:
mean: \(.mean)
stddev: \(.stddev)
Histogram dividing [0,1] into 10 equal intervals:",
pp(.histogram; [range(0;10)] )</lang>
'''Driver Script''' (e.g. bash)
<lang>for n in 100 1000 1000000 100000000 ; do
prng $n 10 | jq -nrR -f basicStats.jq
done</lang>
{{out}}
<pre>
 
 
Basic statistics for 100 PRNs in [0,1]:
mean: 0.5044075207950001
stddev: 0.31917493709846156
Histogram dividing [0,1] into 10 equal intervals:
0 : **************************
1 : **********************
2 : ************************
3 : ************
4 : ******************
5 : **************
6 : ******************
7 : ****************
8 : **************
9 : ************************************
 
 
Basic statistics for 1000 PRNs in [0,1]:
mean: 0.5064192836948004
stddev: 0.2848043056196128
Histogram dividing [0,1] into 10 equal intervals:
0 : ********************
1 : *****************
2 : ******************
3 : *****************
4 : ************************
5 : *******************
6 : **********************
7 : ********************
8 : *********************
9 : ******************
 
 
Basic statistics for 1000000 PRNs in [0,1]:
mean: 0.5000192501821095
stddev: 0.2887484604110784
Histogram dividing [0,1] into 10 equal intervals:
0 : ********************
1 : *******************
2 : ********************
3 : ********************
4 : *******************
5 : *******************
6 : *******************
7 : ********************
8 : ********************
9 : *******************
 
 
Basic statistics for 100000000 PRNs in [0,1]:
mean: 0.500033396019585
stddev: 0.08333461745875925
Histogram dividing [0,1] into 10 equal intervals:
0 : *******************
1 : *******************
2 : ********************
3 : *******************
4 : ********************
5 : *******************
6 : *******************
7 : ********************
8 : ********************
9 : ********************
</pre>
 
=={{header|Jsish}}==
2,496

edits