#0: #Experiment zur Demostration von WS- und Verteilungsfunktion der Binomialverteilung. source(file="http://www.wiwi.uni-bielefeld.de/~wolf/software/R-wtools/slider/slider.R") binomial.experiment<-function(){ refresh.code<-function(...){ # Modellierung prozent.ok<-slider(no=1); n<-slider(no=2); s0<-slider(no=3) p<-prozent.ok/100 # Experimentparameter wd<-slider(no=4); set.seed(zz<-slider(no=5)) # Umsetzung stpr<-sample(c(1,0), size=n*wd, prob=c(p, 1-p),replace=T) result.tab<-table(result<-apply(matrix(stpr,nrow=n),2,sum))/wd s<-0:n;f.s<-dbinom(s,n,p);F.s<-pbinom(s,n,p) f.s0<-dbinom(s0,n,p);F.s0<-pbinom(s0,n,p); F.quer.s0<-1-pbinom(s0,n,p) # Ergebnisdarstellung par(mfrow=c(2,1)) plot(as.character(names(result.tab)),result.tab, xlab="s",ylab="f(s), f.dach(s)", xlim=c(0,n), # ylim=c(0,2.0*dbinom(round(n*.5),n,.5)), col="blue",bty="n", lty=2,type="p") points(s,f.s,type="h") nice<-function(x)paste(floor(x),".",substring(round(10000*(1+x)),2,5),sep="") x<-0; y<-0.90*par()$usr[4]; if(p<0.5){ x<-n/2 } text(x,y,paste("P(S=",s0,")=",nice(f.s0),sep=""),pos=4) lines(c(s0,s0,0),c(0,f.s0,f.s0),lty=2,col="red") x<-as.character(names(result.tab));y<-cumsum(result.tab) plot(x,y, xlab="s",ylab="F(s), F.dach(s)", xlim=c(0,n),ylim=c(0,1.1),col="blue",bty="n",type="p",lty=2) points(c(0,s,n+1),c(0,F.s,1),type="s") text(n/2,1.05,"o Simulationsergebnis",col="blue") x<-0; y<-0.95; if(p<0.5){ x<-n/2; y<-0.15 } text(x,y,paste("P(S<=",s0,")=",nice(F.s0),sep=""),pos=4) text(x,y-0.1,paste("P(S> ",s0,")=",nice(F.quer.s0),sep=""),pos=4) lines(c(s0,s0,0),c(0,F.s0,F.s0),lty=2,col="red") par(mfrow=c(1,1)) title(paste("Binomialverteilung,", "p*100=", prozent.ok,", n=",n,"\nSimulation: WD=",wd,", Zufall=",zz,sep="")) } slider(refresh.code, c("p*100 Erfolgsprozentsatz","n Experimentanzahl","s0 spezielle Realisation", "wd Simulationsanzahl","Zufallsstart"), c(1,1,0,100,1),c(99,100,100,3000,30),c(1,1,1,100,1),c(50,10,5,50,13) ) } #:0 #3: binomial.experiment() #:3