Jump to content

Statistics/Basic: Difference between revisions

Added Algol 68
m (→‎{{header|Phix}}: reduced size to fit)
(Added Algol 68)
Line 553:
 
The same program should still work fine for sample size 10^18, but I'll need my PC in the meantime. ;-)
 
=={{header|ALGOL 68}}==
Suitable for the moderate sample sizes millions or billions probably - not suitable for e.g.: a trillion samples (with early 21st century hardware).
<syntaxhighlight lang="algol68">
BEGIN # calculate the mean and standard deviation of some data and draw a #
# histogram of the data #
 
# return the mean of data #
OP MEAN = ( []REAL data )REAL:
IF INT len = ( UPB data - LWB data ) + 1;
len < 1
THEN 0
ELSE REAL sum := 0;
FOR i FROM LWB data TO UPB data DO
sum +:= data[ i ]
OD;
sum / len
FI # MEAN # ;
 
# returns the standard deviation of data #
OP STDDEV = ( []REAL data )REAL:
IF INT len = ( UPB data - LWB data ) + 1;
len < 1
THEN 0
ELSE REAL m = MEAN data;
REAL sum := 0;
FOR i FROM LWB data TO UPB data DO
sum +:= ( data[ i ] - m ) ^ 2
OD;
sqrt( sum / len )
FI # STDDEV # ;
 
# generates a row of n random numbers in the range [0..1) #
PROC random row = ( INT n )REF[]REAL:
BEGIN
REF[]REAL data = HEAP[ 1 : n ]REAL;
FOR i TO n DO
data[ i ] := next random
OD;
data
END # random row # ;
 
# returns s right-padded with spaces to at least w characters #
PROC right pad = ( STRING s, INT w )STRING:
IF INT len = ( UPB s - LWB s ) + 1; len >= w THEN s ELSE s + ( " " * ( w - len ) ) FI;
 
# prints a histogram of data ( assumed to be in [0..1) ) with n bars #
# scaled to fit in h scale characters #
PROC print histogram = ( []REAL data, INT n, h scale )VOID:
IF n > 0 AND h scale > 0 THEN
[ 0 : n - 1 ]INT count;
FOR i FROM LWB count TO UPB count DO count[ i ] := 0 OD;
FOR i FROM LWB data TO UPB data DO
count[ ENTIER ( data[ i ] * n ) ] +:= 1
OD;
INT max count := 0;
FOR i FROM LWB count TO UPB count DO
IF count[ i ] > max count THEN max count := count[ i ] FI
OD;
INT len = ( UPB data - LWB data ) + 1;
REAL v := 0;
REAL scale = max count / h scale;
FOR i FROM LWB count TO UPB count DO
print( ( fixed( v, -4, 2 ), ": " ) );
print( ( right pad( "=" * ROUND ( count[ i ] / scale ), h scale ) ) );
print( ( " (", whole( count[ i ], 0 ), ")", newline ) );
v +:= 1 / n
OD
FI # print histogram # ;
 
# task #
 
# generate n random data items, calculate the mean and stddev and show #
# a histogram of the data #
PROC show statistics = ( INT n )VOID:
BEGIN
[]REAL data = random row( n );
print( ( "Sample size: ", whole( n, -6 ) ) );
print( ( ", mean: ", fixed( MEAN data, -8, 4 ) ) );
print( ( ", stddev: ", fixed( STDDEV data, -8, 4 ) ) );
print( ( newline ) );
print histogram( data, 10, 32 );
print( ( newline ) )
END # show statistics # ;
 
show statistics( 100 );
show statistics( 1 000 );
show statistics( 10 000 );
show statistics( 100 000 )
 
END
</syntaxhighlight>
{{out}}
<pre>
Sample size: 100, mean: 0.5092, stddev: 0.2783
0.00: ==================== (10)
0.10: ==================== (10)
0.20: ========== (5)
0.30: ======================== (12)
0.40: ======================== (12)
0.50: ================ (8)
0.60: ================================ (16)
0.70: ================ (8)
0.80: ======================== (12)
0.90: ============== (7)
 
Sample size: 1000, mean: 0.4989, stddev: 0.2855
0.00: ========================== (92)
0.10: =============================== (110)
0.20: =========================== (97)
0.30: ============================ (100)
0.40: ============================ (102)
0.50: ========================== (94)
0.60: ================================ (115)
0.70: ========================== (92)
0.80: =========================== (98)
0.90: ============================ (100)
 
Sample size: 10000, mean: 0.5011, stddev: 0.2863
0.00: ============================= (942)
0.10: ============================== (996)
0.20: ================================ (1057)
0.30: ============================= (968)
0.40: =============================== (1028)
0.50: ============================== (1000)
0.60: =============================== (1008)
0.70: =============================== (1019)
0.80: ============================== (1003)
0.90: ============================== (979)
 
Sample size: 100000, mean: 0.4996, stddev: 0.2881
0.00: =============================== (9917)
0.10: ================================ (10136)
0.20: =============================== (9936)
0.30: =============================== (9850)
0.40: ================================ (10123)
0.50: ================================ (10139)
0.60: ================================ (10167)
0.70: =============================== (9840)
0.80: =============================== (9905)
0.90: =============================== (9987)
</pre>
 
=={{header|BASIC}}==
3,038

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.