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,74 @@
100 REM Statistics/Normal distribution
110 DECLARE EXTERNAL SUB NormalStats
120 CALL NormalStats(100)
130 PRINT
140 CALL NormalStats(1000)
150 PRINT
160 CALL NormalStats(10000)
170 PRINT
180 END
190 REM ***
200 EXTERNAL SUB NormalStats (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(-1 TO 10) ! all zero by default
300 LET Sum = 0
310 LET HSum = 0
320 REM Gener!ate 'SampleSize' normally distributed random numbers with Mean 0.5 and standard deviation 0.25
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 RandomNormal = COS(2 * PI * RND) * SQR(-2 * LOG(RND))
370 LET R(I) = .5 + RandomNormal / 4
380 LET Sum = Sum + R(I)
390 IF R(I) < 0 THEN
400 LET H(-1) = H(-1) + 1
410 ELSEIF R(I) >= 1 THEN
420 LET H(10) = H(10) + 1
430 ELSE
440 LET H(INT(R(I) * 10)) = H(INT(R(I) * 10)) + 1
450 END IF
460 NEXT I
470 FOR I = -1 TO 10
480 LET HSum = HSum + H(I)
490 NEXT I
500 REM adjust one of the H() values if necessary to ensure HSum = SampleSize
510 LET Adj = SampleSize - HSum
520 IF Adj <> 0 THEN
530 FOR I = -1 TO 10
540 LET H(I) = H(I) + Adj
550 IF H(I) >= 0 THEN EXIT FOR
560 LET H(I) = H(I) - Adj
570 NEXT I
580 END IF
590 LET Mean = Sum / SampleSize
600 LET Sum = 0
610 REM Now calculate their standard deviation
620 FOR I = 1 TO SampleSize
630 LET Sum = Sum + (R(I) - Mean) ^ 2
640 NEXT I
650 LET Sd = SQR(Sum / SampleSize)
660 REM Draw a histogram of the data with interval 0.1
670 REM If sample size > 300 then normalize histogram to 300
680 LET Scale = 1
690 IF SampleSize > 300 THEN LET Scale = 300 / SampleSize
700 PRINT "Sample size "; SampleSize
710 PRINT USING " Mean #.###### SD #.######": Mean, Sd
720 FOR I = -1 TO 10
730 IF I = -1 THEN
740 PRINT "< 0.00 : ";
750 ELSEIF I = 10 THEN
760 PRINT ">=1.00 : ";
770 ELSE
780 PRINT USING " #.## : ": I / 10;
790 END IF
800 PRINT USING "##### ": H(I);
810 PRINT REPEAT$("*", ROUND(H(I) * Scale, 0))
820 NEXT I
830 END SUB

View file

@ -0,0 +1,51 @@
func logn n .
return log n 0
.
func randnorm .
return cos (360 * randomf) * sqrt (-2 * logn randomf)
.
global smpl[] .
func mean .
for v in smpl[] : sum += v
return sum / len smpl[]
.
func stddev .
avg = mean
for v in smpl[] : squares += (avg - v) * (avg - v)
return sqrt (squares / len smpl[])
.
proc mksmpl n .
smpl[] = [ ]
for i to n
v = 100 + randnorm * 15
smpl[] &= v
.
.
proc histo .
len count[] 199
for v in smpl[]
ind = floor (v + 0.5)
ind = higher 1 ind
ind = lower 199 ind
count[ind] += 1
.
n = len smpl[]
for i = 40 to 160
v = count[i]
h = floor (v * 1500 / n)
s$ = ""
for j to h : s$ &= "*"
print i & " " & v & " " & s$
.
.
numfmt 5 4
proc stats size .
mksmpl size
print "Size: " & size
print "Mean: " & mean
print "Stddev: " & stddev
print ""
histo
print ""
.
stats 1000000

View file

@ -0,0 +1,96 @@
(* Statistics/Normal distribution *)
block
(* Generates normally distributed random numbers with mean 0 and standard deviation 1 *)
unit random_normal: function: real;
const
pi = 3.14159265358979;
begin
result := cos(2.0 * pi * random) * sqrt(-2.0 * ln(random))
end random_normal;
unit normal_stats: procedure (sample_size: integer);
var
r: arrayof real,
sum, mean, diff, scale, sd: real,
h: arrayof integer,
h_sum, adj, i, j: integer;
begin
if sample_size < 1 then return fi;
array r dim (1 : sample_size);
array h dim (-1 : 10); (* all zero by default *)
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 := 1 to sample_size
do
r(i) := .5 + random_normal / 4.0;
sum := sum + r(i);
if r(i) < 0.0
then h(-1) := h(-1) + 1
else
if r(i) >= 1.0
then h(10) := h(10) + 1
else h(entier(r(i) * 10)) := h(entier(r(i) * 10)) + 1
fi
fi
od;
for i := -1 to 10 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 := -1 to 10
do
h(i) := h(i) + adj;
if h(i) >= 0 then exit fi;
h(i) := h(i) - adj
od
fi;
mean := sum / sample_size;
(* Now calculate their standard deviation *)
sum := 0.0;
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 > 300 then normalize histogram to 300 *)
scale := 1.0;
if sample_size > 300 then scale := 300.0 / sample_size fi;
writeln("Sample size ", sample_size);
writeln(" Mean ", mean: 8: 6, " SD ", sd: 8: 6);
for i := -1 to 10
do
if i = -1 then write("< 0.00 : ")
else
if i = 10
then write(">=1.00 : ")
else write(" ", i / 10.0 : 4: 2, " : ")
fi
fi;
write(h(i): 5, " ");
for j := 1 to entier(h(i) * scale + .5) do write("*") od;
writeln
od;
end normal_stats;
begin
call normal_stats(100);
writeln;
call normal_stats(1000);
writeln;
call normal_stats(10000);
writeln;
end;

View file

@ -0,0 +1,83 @@
<?php
// Statistics/Normal distribution
normal_stats(100);
echo PHP_EOL;
normal_stats(1000);
echo PHP_EOL;
normal_stats(10000);
echo PHP_EOL;
// Generates normally distributed random numbers with $mean 0 and standard deviation 1
function random_normal() {
return cos(2.0 * M_PI * mt_rand() / mt_getrandmax()) *
sqrt(-2.0 * log(mt_rand() / mt_getrandmax()));
}
function normal_stats ($sample_size) {
if ($sample_size < 1) return;
srand((double)microtime() * 1000000);
$r = array_fill(0, $sample_size, 0);
$h = array_fill(0, 12, 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] = .5 + random_normal() / 4.0;
$sum += $r[$i];
if ($r[$i] < 0.0)
$h[0]++;
else if ($r[$i] >= 1.0)
$h[11]++;
else
$h[ceil($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 <= 11; $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 > 300)
$scale = 300.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 = -1;
foreach ($h as $h_item) {
if ($i == -1)
echo '< 0.00 : ';
else if ($i == 10)
echo '>=1.00 : ';
else
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,59 @@
require "table2"
require "io2"
local fmt = require "fmt"
-- Box-Muller method from Wikipedia.
local function normal(mu, sigma)
local u1 = math.random()
local u2 = math.random()
local mag = sigma * math.sqrt(-2 * math.log(u1))
local z0 = mag * math.cos(2 * math.pi * u2) + mu
local z1 = mag * math.sin(2 * math.pi * u2) + mu
return {z0, z1}
end
local N = 100_000
local NUM_BINS = 12
local bins = table.rep(NUM_BINS, 0)
local binSize = 0.1
local samples = table.rep(N, 0)
local mu = 0.5
local sigma = 0.25
for i = 0, N / 2 - 1 do
local rns = normal(mu, sigma)
for j = 0, 1 do
local rn = rns[j + 1]
local bn
if rn < 0 then
bn = 0
elseif rn >= 1 then
bn = 11
else
bn = rn // binSize + 1
end
bins[bn + 1] += 1
samples[i * 2 + j + 1] = rn
end
end
local labels = table.rep(NUM_BINS, "")
for i = 1, NUM_BINS do
if i == 1 then
labels[i] = "-inf ..< 0.00 "
elseif i < NUM_BINS then
labels[i] = string.format("%4.2f ..< %4.2f ", binSize * (i-1), binSize * i)
else
labels[i] = "1.00 ... +inf "
end
end
fmt.print("Normal distribution with mean %0.2f and S/D %0.2f for %,s samples:\n", mu, sigma, N)
local title = " Range Number of samples within that range"
io.barchart(title, 85, labels, bins, true, true)
local m = samples:mean()
fmt.print("\nActual mean for these samples : %0.5f", m)
local c = #samples
local v = (samples:sqsum() - m * m * c) / (c - 1)
fmt.print("Actual S/D for these samples : %0.5f", math.sqrt(v))

View file

@ -0,0 +1,86 @@
# Statistics/Normal distribution
# Generates normally distributed random numbers with $mean 0 and standard deviation 1
function Get-RandomNormal {
return [Math]::Cos(2.0 * [Math]::PI * (Get-Random -Maximum 1.0)) * [Math]::Sqrt(-2.0 * [Math]::Log((Get-Random -Maximum 1.0)))
}
function Write-NormalStats {
param ([uint32]$SampleSize)
if ($SampleSize -lt 1) {
return
}
$r = [double[]]::new($SampleSize)
$h = [uint32[]]::new(12) # all zero by default
$sum = 0.0
$hSum = 0
# Generate '$SampleSize' 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
$iTo = $SampleSize - 1
foreach ($i in 0..$iTo) {
$r[$i] = .5 + $(Get-RandomNormal) / 4.0
$sum += $r[$i]
if ($r[$i] -lt 0.0) {
$h[0]++
} elseif ($r[$i] -ge 1.0) {
$h[11]++
} else {
$h[[Math]::Ceiling($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 > 300 then normalize histogram to 300
$scale = 1.0
if ($SampleSize -gt 300) {
$scale = 300.0 / $SampleSize
}
Write-Output "Sample size $SampleSize"
Write-Output (' Mean {0,8:N6} SD {1,8:N6}' -f $mean, $sd)
$i = -1
foreach ($hItem in $h) {
if ($i -eq -1) {
$out = '< 0.00 : '
} elseif ($i -eq 10) {
$out = '>=1.00 : '
} else {
$out = ' {0,4:N2} : ' -f ($i / 10.0)
}
$out += ('{0,5} ' -f $hItem)
Write-Output ($out + ('*' * [Math]::Round($hItem * $scale)))
$i++
}
}
Write-NormalStats 100
Write-Output ""
Write-NormalStats 1000
Write-Output ""
Write-NormalStats 10000
Write-Output ""

View file

@ -0,0 +1,92 @@
DECLARE FUNCTION randomNormal! ()
DECLARE SUB normalStats (sampleSize)
CONST pi = 3.141592653589793#
RANDOMIZE TIMER
CLS
CALL normalStats(100)
PRINT
CALL normalStats(1000)
PRINT
CALL normalStats(10000)
PRINT
CALL normalStats(100000)
PRINT "Press any key to quit"
END
SUB normalStats (sampleSize)
IF sampleSize < 1 THEN EXIT SUB
DIM r(1 TO sampleSize) AS SINGLE
DIM h(-1 TO 10) AS INTEGER
DIM sum AS DOUBLE: sum = 0!
DIM hSum AS INTEGER: hSum = 0
DIM i AS INTEGER
' Generate samples: mean 0.5, std 0.25
FOR i = 1 TO sampleSize
r(i) = .5 + randomNormal! / 4!
sum = sum + r(i)
IF r(i) < 0! THEN
h(-1) = h(-1) + 1
ELSEIF r(i) >= 1! THEN
h(10) = h(10) + 1
ELSE
h(INT(r(i) * 10)) = h(INT(r(i) * 10)) + 1
END IF
NEXT i
FOR i = -1 TO 10
hSum = hSum + h(i)
NEXT i
' Adjust if necessary (rounding error)
DIM adj AS INTEGER: adj = sampleSize - hSum
IF adj <> 0 THEN
FOR i = -1 TO 10
h(i) = h(i) + adj
IF h(i) >= 0 THEN EXIT FOR
h(i) = h(i) - adj
NEXT i
END IF
DIM mean AS DOUBLE: mean = sum / sampleSize
' Standard deviation
sum = 0!
FOR i = 1 TO sampleSize
sum = sum + (r(i) - mean) ^ 2!
NEXT i
DIM sd AS DOUBLE: sd = SQR(sum / sampleSize)
PRINT "Sample size"; sampleSize
PRINT
PRINT USING " Mean #.######"; mean;
PRINT USING " SD #.######"; sd
PRINT
' Histogram (scale large samples)
DIM numStars AS INTEGER
DIM scale AS DOUBLE: scale = 1!
IF sampleSize > 300 THEN scale = 300 / sampleSize
FOR i = -1 TO 10
IF i = -1 THEN
PRINT "< 0.00 : ";
ELSEIF i = 10 THEN
PRINT ">=1.00 : ";
ELSE
PRINT USING " #.## : "; i / 10;
END IF
PRINT USING "##### "; h(i);
numStars = INT(h(i) * scale + .5)
PRINT STRING$(numStars, "*")
NEXT i
END SUB
FUNCTION randomNormal!
randomNormal! = COS(2 * pi * RND) * SQR(-2 * LOG(RND))
END FUNCTION

View file

@ -0,0 +1,82 @@
Rebol [
title: "Rosetta code: Statistics/Normal_distribution"
file: %Statistics-Normal_distribution.r3
url: https://rosettacode.org/wiki/Statistics/Normal_distribution
author: @ldci
needs: 3.19.0
]
random/seed 1
randMax: 2147483647 ;; max integer value
nMax: 500000 ;; number of random values, can be modified
;; Normal random numbers generator using Marsaglia algorithm.
;; Generates 2 independent series of random values in the range [-1.0, 1.0].
generate: function [
n [integer!] ;; number of pairs to generate
][
m: n * 2
values: make vector! [f64! :m] ;; vector to hold generated values
for i 1 m 2 [
rsq: 0.0
;; Repeat while radius squared is outside the unit circle or zero
while [any [(rsq >= 1.0) (rsq == 0.0)]] [
x: (2.0 * random randMax) / randMax - 1.0
y: (2.0 * random randMax) / randMax - 1.0
rsq: (x * x) + (y * y)
]
f: sqrt ((-2.0 * log-e rsq) / rsq)
values/(i): x * f
values/(i + 1): y * f
]
values
]
;; Show histogram of the values vector
printHistogram: function [
values [vector!]
][
width: 50.0 ;; width of histogram bars
low: -3.0 ;; lower bound of histogram
high: 3.0 ;; upper bound of histogram
delta: 0.1 ;; bin width
n: values/length ;; length of data
nbins: to integer! ((high - low) / delta) ;; number of bins (60)
bins: make vector! [i32! :nbins] ;; initialize bins vector
repeat i n [
j: to integer! ((values/:i - low) / delta)
if all [(j >= 1) (j <= nbins)] [
bins/:j: bins/:j + 1 ;; increment bin counter
]
]
maxi: bins/maximum ;; max count in any bin
repeat j nbins [
lbin: round/to (low + j * delta - high + 0.25) 0.01 ;; low limit for bin
hbin: round/to (low + j + 1 * delta - high + 0.25) 0.01 ;; high limit for bin
s: ajoin ["[" lbin " " hbin "] "] ;; bin label string
pad s -15 ;; pad string left for alignment
k: width * bins/:j / maxi ;; number of block characters to print
while [k > 0] [
append s to-char 9609 ;; append block character (unicode 9609)
k: k - 1
]
append s ajoin [" " round/to (bins/:j * 100 / n) 0.01 "%"] ;; append percentage
print s
]
]
;;********************** Main ***********************
print "Be patient! Generating Data and Gaussian Histogram..."
print-horizontal-line
time: dt [
values: generate nMax ;; generate nMax pairs of random values
printHistogram values ;; print histogram of generated values
]
print-horizontal-line
print [nMax * 2 "Values processed in:" round/to third time 0.01 "sec"]
print ["Mean: " values/mean]
print ["STD : " values/sample-deviation]
print-horizontal-line

View file

@ -0,0 +1,73 @@
normalStats(100)
print
normalStats(1000)
print
normalStats(10000)
print
normalStats(100000)
end
sub randomNormal()
local u1, u2
u1 = ran()
u2 = ran()
return cos(2 * pi * u1) * sqrt(-2 * log(u2))
end sub
sub normalStats(n)
local r(n), h(11)
local i, sum, hSum, mean, sd, scale, stars
sum = 0
hSum = 0
// Generate samples: mean 0.5, std 0.25
for i = 1 to n
r(i) = 0.5 + randomNormal() / 4
sum = sum + r(i)
if r(i) < 0 then
h(0) = h(0) + 1
elsif r(i) >= 1 then
h(11) = h(11) + 1
else
h(int(r(i)*10)+1) = h(int(r(i)*10)+1) + 1
fi
next i
for i = 0 to 11
hSum = hSum + h(i)
next i
mean = sum / n
// Standard deviation
sum = 0
for i = 1 to n
sum = sum + (r(i) - mean)^2
next i
sd = sqrt(sum / n)
print "Sample size ", n
print
print " Mean ", mean using("#.######");
print " SD ", sd using("#.######")
print
// Histogram (scale for large samples)
scale = 1
if n > 300 scale = 300 / n
for i = 0 to 11
if i = 0 then
print "< 0.00 : ", h(0) using("#####"), " ";
elsif i = 11 then
print ">=1.00 : ", h(1) using("#####");
else
//print using(" #.## : ##### ", (i-1)/10); h(i);
print " ", (i-1)/10 using("#.##"), " : ", h(i) using("#####"), " ";
fi
stars = int(h(i) * scale + 0.5)
print string$(stars, "*")
next i
end sub