Another update from ingydotnet^djgoku

This commit is contained in:
Ingy döt Net 2015-11-18 06:14:39 +00:00
parent 91df62d461
commit 948b86eafa
7604 changed files with 108452 additions and 22726 deletions

View file

@ -0,0 +1,29 @@
p = [1/i for i in 5:11]
plen = length(p)
q = [0.0, [sum(p[1:i]) for i = 1:plen]]
plab = [char(i) for i in 0x05d0:(0x05d0+plen)]
hi = 10^6
push!(p, 1.0 - sum(p))
plen += 1
accum = zeros(Int, plen)
for i in 1:hi
accum[sum(rand() .>= q)] += 1
end
r = accum/hi
println("Rates at which items are selected (", hi, " trials).")
println(" Item Expected Actual")
for i in 1:plen
println(@sprintf(" \u2067%s %8.6f %8.6f", plab[i], p[i], r[i]))
end
println()
println("Rates at which items are selected (", hi, " trials).")
println(" Item Count Expected Actual")
for i in 1:plen
println(@sprintf(" %s yields %6d %8.6f %8.6f",
plab[i], accum[i], p[i], r[i]))
end

View file

@ -0,0 +1,45 @@
probch: Proc Options(main);
Dcl prob(8) Dec Float(15) Init((1/5.0), /* aleph */
(1/6.0), /* beth */
(1/7.0), /* gimel */
(1/8.0), /* daleth */
(1/9.0), /* he */
(1/10.0), /* waw */
(1/11.0), /* zayin */
(1759/27720));/* heth */
Dcl what(8) Char(6) Init('aleph ','beth ','gimel ','daleth',
'he ','waw ','zayin ','heth ');
Dcl ulim(0:8) Dec Float(15) Init((9)0);
Dcl i Bin Fixed(31);
Dcl ifloat Dec Float(15);
Dcl one Dec Float(15) Init(1);
Dcl num Dec Float(15) Init(1759);
Dcl denom Dec Float(15) Init(27720);
Dcl x Dec Float(15) Init(0);
Dcl pr Dec Float(15) Init(0);
Dcl (n,nn) Bin Fixed(31);
Dcl cnt(8) Bin Fixed(31) Init((8)0);
nn=1000000;
Do i=1 To 8;
ifloat=i+4;
If i<8 Then
prob(i)=one/ifloat;
Else
prob(i)=num/denom;
Ulim(i)=ulim(i-1)+prob(i);
/* Put Skip list(i,prob(i),ulim(i));*/
End;
Do n=1 To nn;
x=random();
Do i=1 To 8;
If x<ulim(i) Then Leave;
End;
cnt(i)+=1;
End;
Put Edit('letter occurs frequency expected ')(Skip,a);
Put Edit('------ ------ ---------- ----------')(Skip,a);
Do i=1 To 8;
pr=float(cnt(i))/float(nn);
Put Edit(what(i),cnt(i),pr,prob(i))(Skip,a,f(10),x(2),2(f(11,8)));
End;
End;

View file

@ -1,21 +1,19 @@
constant TRIALS = 1e4;
constant TRIALS = 1e6;
my @event = <aleph beth gimel daleth he waw zayin heth>;
constant @event = <aleph beth gimel daleth he waw zayin heth>;
my @P = 1 «/« (5 .. 11), 1759/27720;
my @cP = [\+] @P;
constant @P = flat (1 X/ 5 .. 11), 1759/27720;
constant @cP = [\+] @P;
my @results;
for ^TRIALS {
@results[
first { @cP[$_] > state $ = rand }, ^@P;
]++;
}
@results[ @cP.first: { $_ > once rand }, :k ]++ xx TRIALS;
say 'Event Occurred Expected Difference';
for ^@results {
my ($occurred, $expected) = @results[$_], @P[$_]*TRIALS;
my ($occurred, $expected) = @results[$_], @P[$_] * TRIALS;
printf "%-9s%8.0f%9.1f%12.1f\n",
@event[$_], $occurred, $expected,
abs( $occurred - $expected );
@event[$_],
$occurred,
$expected,
abs $occurred - $expected;
}

View file

@ -1,32 +1,32 @@
/*REXX pg shows results of probabilistic choices (gen rand#s per prob.) */
parse arg trials digits seed . /*obtain some optional arguments.*/
/*REXX program shows results of probabilistic choices, gen random #s per prob.*/
parse arg trials digits seed . /*obtain the optional arguments from CL*/
if trials=='' | trials==',' then trials=1000000
if digits=='' | digits==',' then digits=15; digits=max(10,digits)
if seed\=='' then call random ,,seed /*for repeatability*/
if seed\=='' then call random ,,seed /*for repeatability.*/
names='aleph beth gimel daleth he waw zayin heth totals'
cells=words(names) - 1; high=100000; s=0; !.=0
_=4
do n=1 for 7; _=_+1; prob.n=1/_; Hprob.n=prob.n*high; s=s+prob.n
end /*n*/ /* [↑] determine probabilities. */
end /*n*/ /* [↑] determine the probabilities. */
prob.8=1759/27720; Hprob.8=prob.8*high; s=s+prob.8; prob.9=s; !.9=trials
do j=1 for trials; r=random(1,high) /*generate X number of random #s.*/
do k=1 for cells /*now, for each cell, compute %s.*/
if r<=Hprob.k then !.k=!.k+1 /*for each range, bump da counter*/
do j=1 for trials; r=random(1,high) /*generate X number of random numbers.*/
do k=1 for cells /*for each cell, compute percentages. */
if r<=Hprob.k then !.k=!.k+1 /*for each range, bump the counter. */
end /*k*/
end /*j*/
w=digits+6; d=max(length(trials), length('count')) + 4
say center('name',15,'') center('count',d,'') center('target %',w,''),
center('actual %',w,'') /*display a formatted header line*/
do i=1 for cells+1 /*show for each cell and totals. */
say centr('name',15) centr('count',d) centr('target %') centr('actual %')
/* [↑] display a formatted header line*/
do i=1 for cells+1 /*show for each of the cells and totals*/
say ' ' left(word(names,i) , 12),
right(!.i , d-2) ' ',
left(format(prob.i *100, d), w-2),
left(format(!.i/trials*100, d), w-2)
if i==8 then say center('',15,'') center('',d,''),
center('', w,'') center('',w,'')
if i==8 then say centr(,15) centr(,d) centr() centr()
end /*i*/
/*stick a fork in it, we're done.*/
exit /*stick a fork in it, we are all done.*/
/*────────────────────────────────────────────────────────────────────────────*/
centr: return center(arg(1), word(arg(2) w,1), '')

View file

@ -0,0 +1,32 @@
item = Array("aleph","beth","gimel","daleth","he","waw","zayin","heth")
prob = Array(1/5.0, 1/6.0, 1/7.0, 1/8.0, 1/9.0, 1/10.0, 1/11.0, 1759/27720)
Dim cnt(7)
'Terminate script if sum of probabilities <> 1.
sum = 0
For i = 0 To UBound(prob)
sum = sum + prob(i)
Next
If sum <> 1 Then
WScript.Quit
End If
For trial = 1 To 1000000
r = Rnd(1)
p = 0
For i = 0 To UBound(prob)
p = p + prob(i)
If r < p Then
cnt(i) = cnt(i) + 1
Exit For
End If
Next
Next
WScript.StdOut.Write "item" & vbTab & "actual" & vbTab & vbTab & "theoretical"
WScript.StdOut.WriteLine
For i = 0 To UBound(item)
WScript.StdOut.Write item(i) & vbTab & FormatNumber(cnt(i)/1000000,6) & vbTab & FormatNumber(prob(i),6)
WScript.StdOut.WriteLine
Next