Data update

This commit is contained in:
Ingy döt Net 2026-04-30 12:34:36 -04:00
parent 4bb20c9b71
commit cbaf4c4b64
12390 changed files with 318560 additions and 27248 deletions

View file

@ -0,0 +1,60 @@
100 REM Statistics/Basic
110 DECLARE EXTERNAL SUB BasicStats
120 CALL BasicStats(100)
130 PRINT
140 CALL BasicStats(1000)
150 PRINT
160 CALL BasicStats(10000)
170 PRINT
180 END
190 REM ***
200 EXTERNAL SUB BasicStats (SampleSize)
210 IF SampleSize < 1 THEN EXIT SUB
220 DIM R(1 TO 10000)
230 LET MaxSampleSize = UBOUND(R)
240 IF SampleSize > MaxSampleSize THEN
250 PRINT "Sample size too large"
260 STOP
270 END IF
280 RANDOMIZE
290 DIM H(0 TO 9) ! all zero by default
300 LET Sum = 0
310 LET HSum = 0
320 REM Gener!ate 'SampleSize' random numbers in the interval [0, 1)
330 REM calculate their Sum
340 REM and in which box they will fall when drawing the histogram
350 FOR I = 1 TO SampleSize
360 LET R(I) = RND
370 LET Sum = Sum + R(I)
380 LET H(INT(R(I) * 10)) = H(INT(R(I) * 10)) + 1
390 NEXT I
400 FOR I = 0 TO 9
410 LET HSum = HSum + H(I)
420 NEXT I
430 REM adjust one of the H() values if necessary to ensure HSum = SampleSize
440 LET Adj = SampleSize - HSum
450 IF Adj <> 0 THEN
460 FOR I = 0 TO 9
470 LET H(I) = H(I) + Adj
480 IF H(I) >= 0 THEN EXIT FOR
490 LET H(I) = H(I) - Adj
500 NEXT I
510 END IF
520 LET Mean = Sum / SampleSize
530 LET Sum = 0
540 REM Now calculate their standard deviation
550 FOR I = 1 TO SampleSize
560 LET Sum = Sum + (R(I) - Mean) ^ 2
570 NEXT I
580 LET Sd = SQR(Sum / SampleSize)
590 REM Draw a histogram of the data with interval 0.1
600 REM If sample size > 500 then normalize histogram to 500
610 LET Scale = 1
620 IF SampleSize > 500 THEN LET Scale = 500 / SampleSize
630 PRINT "Sample size"; SampleSize
640 PRINT USING " Mean #.###### SD #.######": Mean, Sd
650 FOR I = 0 TO 9
660 PRINT USING " #.## : ##### ": I / 10, H(I);
670 PRINT REPEAT$("*", ROUND(H(I) * Scale, 0))
680 NEXT I
690 END SUB

View file

@ -0,0 +1,63 @@
with Ada.Text_IO, Ada.Command_Line, Ada.Numerics.Float_Random,
Ada.Numerics.Generic_Elementary_Functions;
procedure Basic_Stat is
package FRG renames Ada.Numerics.Float_Random;
package TIO renames Ada.Text_IO;
type Counter is range 0 .. 2**31-1;
type Result_Array is array(Natural range <>) of Counter;
package FIO is new TIO.Float_IO(Float);
procedure Put_Histogram(R: Result_Array; Scale, Full: Counter) is
begin
for I in R'Range loop
FIO.Put(Float'Max(0.0, Float(I)/10.0 - 0.05),
Fore => 1, Aft => 2, Exp => 0); TIO.Put("..");
FIO.Put(Float'Min(1.0, Float(I)/10.0 + 0.05),
Fore => 1, Aft => 2, Exp => 0); TIO.Put(": ");
for J in 1 .. (R(I)* Scale)/Full loop
Ada.Text_IO.Put("X");
end loop;
Ada.Text_IO.New_Line;
end loop;
end Put_Histogram;
procedure Put_Mean_Et_Al(Sample_Size: Counter;
Val_Sum, Square_Sum: Float) is
Mean: constant Float := Val_Sum / Float(Sample_Size);
package Math is new Ada.Numerics.Generic_Elementary_Functions(Float);
begin
TIO.Put("Mean: ");
FIO.Put(Mean, Fore => 1, Aft => 5, Exp => 0);
TIO.Put(", Standard Deviation: ");
FIO.Put(Math.Sqrt(abs(Square_Sum / Float(Sample_Size)
- (Mean * Mean))), Fore => 1, Aft => 5, Exp => 0);
TIO.New_Line;
end Put_Mean_Et_Al;
N: Counter := Counter'Value(Ada.Command_Line.Argument(1));
Gen: FRG.Generator;
Results: Result_Array(0 .. 10) := (others => 0);
X: Float;
Val_Sum, Squ_Sum: Float := 0.0;
begin
FRG.Reset(Gen);
for I in 1 .. N loop
X := FRG.Random(Gen);
Val_Sum := Val_Sum + X;
Squ_Sum := Squ_Sum + X*X;
declare
Index: Integer := Integer(X*10.0);
begin
Results(Index) := Results(Index) + 1;
end;
end loop;
TIO.Put_Line("After sampling" & Counter'Image(N) & " random numnbers: ");
Put_Histogram(Results, Scale => 600, Full => N);
TIO.New_Line;
Put_Mean_Et_Al(Sample_Size => N, Val_Sum => Val_Sum, Square_Sum => Squ_Sum);
end Basic_Stat;

View file

@ -0,0 +1,77 @@
with Ada.Text_IO, Ada.Command_Line, Ada.Numerics.Float_Random,
Ada.Numerics.Generic_Elementary_Functions;
procedure Long_Basic_Stat is
package FRG renames Ada.Numerics.Float_Random;
package TIO renames Ada.Text_IO;
type Counter is range 0 .. 2**63-1;
type Result_Array is array(Natural range <>) of Counter;
type High_Precision is digits 15;
package FIO is new TIO.Float_IO(Float);
procedure Put_Histogram(R: Result_Array; Scale, Full: Counter) is
begin
for I in R'Range loop
FIO.Put(Float'Max(0.0, Float(I)/10.0 - 0.05),
Fore => 1, Aft => 2, Exp => 0); TIO.Put("..");
FIO.Put(Float'Min(1.0, Float(I)/10.0 + 0.05),
Fore => 1, Aft => 2, Exp => 0); TIO.Put(": ");
for J in 1 .. (R(I)* Scale)/Full loop
Ada.Text_IO.Put("X");
end loop;
Ada.Text_IO.New_Line;
end loop;
end Put_Histogram;
procedure Put_Mean_Et_Al(Sample_Size: Counter;
Val_Sum, Square_Sum: Float) is
Mean: constant Float := Val_Sum / Float(Sample_Size);
package Math is new Ada.Numerics.Generic_Elementary_Functions(Float);
begin
TIO.Put("Mean: ");
FIO.Put(Mean, Fore => 1, Aft => 5, Exp => 0);
TIO.Put(", Standard Deviation: ");
FIO.Put(Math.Sqrt(abs(Square_Sum / Float(Sample_Size)
- (Mean * Mean))), Fore => 1, Aft => 5, Exp => 0);
TIO.New_Line;
end Put_Mean_Et_Al;
N: Counter := Counter'Value(Ada.Command_Line.Argument(1));
Gen: FRG.Generator;
Results: Result_Array(0 .. 10) := (others => 0);
X: Float;
Val_Sum, Squ_Sum: High_Precision := 0.0;
begin
FRG.Reset(Gen);
for Outer in 1 .. 1000 loop
for I in 1 .. N/1000 loop
X := FRG.Random(Gen);
Val_Sum := Val_Sum + High_Precision(X);
Squ_Sum := Squ_Sum + High_Precision(X)*High_Precision(X);
declare
Index: Integer := Integer(X*10.0);
begin
Results(Index) := Results(Index) + 1;
end;
end loop;
if Outer mod 50 = 0 then
TIO.New_Line(1);
TIO.Put_Line(Integer'Image(Outer/10) &"% done; current results:");
Put_Mean_Et_Al(Sample_Size => (Counter(Outer)*N)/1000,
Val_Sum => Float(Val_Sum),
Square_Sum => Float(Squ_Sum));
else
Ada.Text_IO.Put(".");
end if;
end loop;
TIO.New_Line(4);
TIO.Put_Line("After sampling" & Counter'Image(N) & " random numnbers: ");
Put_Histogram(Results, Scale => 600, Full => N);
TIO.New_Line;
Put_Mean_Et_Al(Sample_Size => N,
Val_Sum => Float(Val_Sum), Square_Sum => Float(Squ_Sum));
end Long_Basic_Stat;

View file

@ -0,0 +1,71 @@
(* Statistics/Basic *)
block
unit basic_stats: procedure (sample_size: integer);
var
r: arrayof real,
sum, scale, mean, diff, sd: real,
h: arrayof integer,
h_sum, i, j, adj: integer;
begin
if sample_size < 1 then return fi;
array r dim (1 : sample_size);
array h dim (0 : 9); (* all zero by default *)
sum := 0.0;
h_sum := 0;
(* Generate 'sample_size' random numbers in the interval [0, 1).
Calculate their sum and in which box they will fall when drawing
the histogram *)
for i := 1 to sample_size
do
r(i) := random;
sum := sum + r(i);
h(entier(r(i) * 10)) := h(entier(r(i) * 10)) + 1
od;
for i := 0 to 9 do h_sum := h_sum + h(i) od;
(* Adjust one of the h() values if necessary to ensure h_sum = sample_size *)
adj := sample_size - h_sum;
if adj =/= 0
then
for i := 0 to 9
do
h(i) := h(i) + adj;
if h(i) >= 0 then exit fi;
h(i) := h(i) - adj
od;
fi;
mean := sum / sample_size;
sum := 0.0;
(* Now calculate their standard deviation *)
for i := 1 to sample_size
do
diff := r(i) - mean;
sum := sum + diff * diff
od;
sd := sqrt(sum / sample_size);
(* Draw a histogram of the data with interval 0.1.
If sample size > 500 then normalize histogram to 500. *)
scale := 1.0;
if sample_size > 500 then scale := 500.0 / sample_size fi;
writeln("Sample size", sample_size);
writeln(" Mean ", mean: 8: 6, " SD ", sd: 8: 6);
for i := 0 to 9
do
write(" ", i / 10.0: 4: 2, " : ", h(i): 5, " ");
for j := 1 to entier(h(i) * scale + .5) do write("*") od;
writeln
od;
end basic_stats;
begin
call basic_stats(100);
writeln;
call basic_stats(1000);
writeln;
call basic_stats(10000);
writeln;
end;

View file

@ -0,0 +1,67 @@
<?php
// Statistics/Basic
basic_stats(100);
echo PHP_EOL;
basic_stats(1000);
echo PHP_EOL;
basic_stats(10000);
echo PHP_EOL;
function basic_stats (int $sample_size) {
if ($sample_size < 1) return;
srand((double)microtime() * 1000000);
$r = array_fill(0, $sample_size, 0);
$h = array_fill(0, 10, 0);
$sum = 0.0;
$h_sum = 0;
// Generate '$sample_size' normally distributed random numbers with $mean 0.5
// and standard deviation 0.25
// calculate their $sum
// and in which box they will fall when drawing the histogram
for ($i = 0; $i < $sample_size; $i++) {
$r[$i] = mt_rand() / mt_getrandmax();
$sum += $r[$i];
$h[floor($r[$i] * 10)]++;
}
foreach ($h as $h_item)
$h_sum += $h_item;
// adjust one of the $h values if necessary to ensure $h_sum = $sample_size
$adj = $sample_size - $h_sum;
if ($adj != 0) {
for ($i = 0; $i < 10; $i++) {
$h[$i] += $adj;
if ($h[$i] >= 0) break;
$h[$i] -= $adj;
}
}
$mean = $sum / $sample_size;
$sum = 0.0;
// Now calculate their standard deviation
foreach ($r as $r_item)
$sum += pow($r_item - $mean, 2);
$sd = sqrt($sum / $sample_size);
// Draw a histogram of the data with interval 0.1
// If sample size > 300 then normalize histogram to 300
$scale = 1.0;
if ($sample_size > 500)
$scale = 500.0 / $sample_size;
echo 'Sample size '.$sample_size.PHP_EOL;
echo ' Mean '.
str_pad(number_format($mean, 6, '.', ''), 8, ' ', STR_PAD_LEFT).
' SD '.
str_pad(number_format($sd, 6, '.', ''), 8, ' ', STR_PAD_LEFT).PHP_EOL;
$i = 0;
foreach ($h as $h_item) {
echo ' '.
str_pad(number_format($i / 10.0, 2, '.', ''), 4, ' ', STR_PAD_LEFT).
' : ';
echo str_pad($h_item, 5, ' ', STR_PAD_LEFT).' '.
str_repeat('*', round($h_item * $scale)).PHP_EOL;
$i++;
}
}
?>

View file

@ -0,0 +1,22 @@
require "table2"
for {100, 1000, 10000} as i do
local a = table.create(i)
for j = 1, i do a[j] = math.random() end
print($"For {i} random numbers:")
print($" mean = {a:mean()}")
print($" std/dev = {a:stddev()}")
local scale = math.round(i / 100)
print($" scale = {scale} per asterisk")
local sums = table.rep(10, 0)
for a as e do
local f = math.floor(e * 10)
sums[f + 1] += 1
end
for j = 1, 9 do
sums[j] = math.round(sums[j] / scale)
print($" 0.{j - 1} - 0.{j}: {string.rep("*", sums[j])}")
end
sums[10] = 100 - sums:slice(1, 9):sum()
print($" 0.9 - 1.0: {string.rep("*", sums[10])}\n")
end

View file

@ -0,0 +1,68 @@
# Statistics/Basic
function Write-BasicStats {
param ([uint32]$SampleSize)
if ($SampleSize -lt 1) {
return
}
$r = [double[]]::new($SampleSize)
$h = [uint32[]]::new(10) # all zero by default
$sum = 0.0
$hSum = 0
# Generate '$SampleSize' random numbers in the interval [0, 1)
# calculate their $sum
# and in which box they will fall when drawing the histogram
$iTo = $SampleSize - 1
foreach ($i in 0..$iTo) {
$r[$i] = (Get-Random -Maximum 1.0)
$sum += $r[$i]
$h[[Math]::floor($r[$i] * 10)]++
}
foreach ($hItem in $h) {
$hSum += $hItem
}
# adjust one of the $h[] values if necessary to ensure $hSum = $SampleSize
$adj = $SampleSize - $hSum
if ($adj -ne 0) {
$iTo = $h.Length - 1
foreach ($i in 0..$iTo) {
$h[$i] += $adj
if ($h[$i] -ge 0) {
break
}
$h[$i] -= $adj
}
}
$mean = $sum / $SampleSize
$sum = 0.0
# Now calculate their standard deviation
foreach ($rItem in $r) {
$sum += [Math]::Pow($rItem - $mean, 2)
}
$sd = [Math]::Sqrt($sum / $SampleSize)
# Draw a histogram of the data with interval 0.1
# If sample size > 500 then normalize histogram to 500
[double]$scale = 1.0
if ($SampleSize -gt 500) {
$scale = 500.0 / $SampleSize
}
Write-Output "Sample size $SampleSize"
Write-Output (' Mean {0,8:N6} SD {1,8:N6}' -f $mean, $sd)
$i = 0
foreach ($hItem in $h) {
$out = ' {0,4:N2} : {1,5} ' -f ($i / 10.0), $hItem
Write-Output ($out + ('*' * [Math]::Round($hItem * $scale)))
$i++
}
}
Write-BasicStats 100
Write-Output ""
Write-BasicStats 1000
Write-Output ""
Write-BasicStats 10000
Write-Output ""