September Morn Update
This commit is contained in:
parent
4e2d22a71d
commit
aac6731f2c
6856 changed files with 141342 additions and 21127 deletions
|
|
@ -0,0 +1,32 @@
|
|||
USING: formatting fry io kernel math math.ranges math.statistics
|
||||
random sequences ;
|
||||
IN: rosetta-code.mean-run-density
|
||||
|
||||
: rising? ( ? ? -- ? ) [ f = ] [ t = ] bi* and ;
|
||||
|
||||
: count-run ( n ? ? -- m ? )
|
||||
2dup rising? [ [ 1 + ] 2dip ] when nip ;
|
||||
|
||||
: runs ( n p -- n )
|
||||
[ 0 f ] 2dip '[ random-unit _ < count-run ] times drop ;
|
||||
|
||||
: rn ( n p -- x ) over [ runs ] dip /f ;
|
||||
|
||||
: sim ( n p -- avg )
|
||||
[ 1000 ] 2dip [ rn ] 2curry replicate mean ;
|
||||
|
||||
: theory ( p -- x ) 1 over - * ;
|
||||
|
||||
: result ( n p -- )
|
||||
[ swap ] [ sim ] [ nip theory ] 2tri 2dup - abs
|
||||
"%.1f %-5d %.4f %.4f %.4f\n" printf ;
|
||||
|
||||
: test ( p -- )
|
||||
{ 100 1,000 10,000 } [ swap result ] with each nl ;
|
||||
|
||||
: header ( -- )
|
||||
"1000 tests each:\np n K p(1-p) diff" print ;
|
||||
|
||||
: main ( -- ) header .1 .9 .2 <range> [ test ] each ;
|
||||
|
||||
MAIN: main
|
||||
|
|
@ -0,0 +1,31 @@
|
|||
function run_test(atom p, integer len, runs)
|
||||
integer count = 0
|
||||
for r=1 to runs do
|
||||
bool v, pv = false
|
||||
for l=1 to len do
|
||||
v = rnd()<p
|
||||
count += pv<v
|
||||
pv = v
|
||||
end for
|
||||
end for
|
||||
return count/runs/len
|
||||
end function
|
||||
|
||||
procedure main()
|
||||
printf(1,"Running 1000 tests each:\n")
|
||||
printf(1," p n K p(1-p) delta\n")
|
||||
printf(1,"--------------------------------------------\n")
|
||||
for ip=1 to 10 by 2 do
|
||||
atom p = ip/10,
|
||||
p1p = p*(1-p)
|
||||
integer n = 100
|
||||
while n<=100000 do
|
||||
atom K = run_test(p, n, 1000)
|
||||
printf(1,"%.1f %6d %6.4f %6.4f %+7.4f (%+5.2f%%)\n",
|
||||
{p, n, K, p1p, K-p1p, (K-p1p)/p1p*100})
|
||||
n *= 10
|
||||
end while
|
||||
printf(1,"\n")
|
||||
end for
|
||||
end procedure
|
||||
main()
|
||||
|
|
@ -0,0 +1,29 @@
|
|||
/* REXX */
|
||||
Numeric Digits 20
|
||||
Call random(,12345) /* make the run reproducable */
|
||||
pList = '.1 .3 .5 .7 .9'
|
||||
nList = '1e2 1e3 1e4 1e5'
|
||||
t = 100
|
||||
Do While plist<>''
|
||||
Parse Var plist p plist
|
||||
theory=p*(1-p)
|
||||
Say ' '
|
||||
Say 'p:' format(p,2,4)' theory:'format(theory,2,4)' t:'format(t,4)
|
||||
Say ' n sim sim-theory'
|
||||
nl=nlist
|
||||
Do While nl<>''
|
||||
Parse Var nl n nl
|
||||
sum=0
|
||||
Do i=1 To t
|
||||
run=0
|
||||
Do j=1 To n
|
||||
one=random(1000)<p*1000
|
||||
If one & (run=0) Then
|
||||
sum=sum+1
|
||||
run=one
|
||||
End
|
||||
End
|
||||
sim=sum/(n*100)
|
||||
Say format(n,10)' ' format(sim,2,4)' 'format(sim-theory,2,6)
|
||||
End
|
||||
End
|
||||
Loading…
Add table
Add a link
Reference in a new issue