Wednesday, November 17, 2010

Probability paired events

clg
graphsize 700,300
font "arial", 60,50
text 0,180,"<" :text 70,210,"<" : text 70,150,"<"
font "arial", 12,100
r1=0 : r2=0 : b1=0 : b2=0 : f=1.2
color yellow
rect 0,80,50,50 : rect 70,80,50,50
For y=1 to 4
For x=1 to 4
box1red=int(rand*f): box2red=int(rand*f)
box1blue=int(rand*(f-box1red)) : box2blue=int(rand*(f-box2red))
r1=r1+box1red : r2=r2+box2red
b1=b1+box1blue : b2=b2+box2blue
color red
if box1red=1 then circle x*10,80+y*10,3
if box2red=1 then circle 70+x*10,80+y*10,3
color blue
if box1blue=1 then circle x*10,80+y*10,3
if box2blue=1 then circle 70+x*10,80+y*10,3
next x
next y
color black
text 120,160,"P(RR)" :text 120,200,"P(RB)"
text 120,220,"P(BR)" : text 120,260,"P(BB)"
input "Redraw a ball at random from each box. What is the probability of each combination",a$
prr=(r1*r2)/((r1+b1)*(r2+b2)) : prb=(r1*b2)/((r1+b1)*(r2+b2))
pbr=(b1*r2)/((r1+b1)*(r2+b2)) : pbb=(b1*b2)/((r1+b1)*(r2+b2))
trr=0:trb=0:tbr=0:tbb=0
for n = 1 to 1000
draw=rand
if draw < prr then
trr=trr+1
print "RR"
end if
if prr < draw and draw < prr+prb then
trb=trb+1
Print "RB"
end if
if prr+prb < draw and draw < prr+prb+pbr then
tbr=tbr+1
Print "BR"
end if
if prr+prb+pbr < draw and draw < prr+prb+pbr+pbb then
tbb=tbb+1
Print "BB"
end if
rect 170,170,trr,5
rect 170,210,trb,5
rect 170,230,tbr,5
rect 170,270,tbb,5
next n
Print "Theorical probability"
print "P(RR)="+prr+"="+(r1*r2)+"/"+((r1+b1)*(r2+b2))
print "P(RB)="+prb+"="+(r1*b2)+"/"+((r1+b1)*(r2+b2))
print "P(BR)="+pbr+"="+(b1*r2)+"/"+((r1+b1)*(r2+b2))
print "P(BB)="+pbb+"="+(b1*b2)+"/"+((r1+b1)*(r2+b2))
Print "Experimental results for "+(n-1)+" trials"
print "P(RR)="+(trr/(n-1))+"="+trr+"/"+(n-1)
print "P(RB)="+(trb/(n-1))+"="+trb+"/"+(n-1)
print "P(BR)="+(tbr/(n-1))+"="+tbr+"/"+(n-1)
print "P(BB)="+(tbb/(n-1))+"="+tbb+"/"+(n-1)

No comments:

Post a Comment