# Luennot vko 36, vrt H1.6/2006 # Satunnaiskoe # Lantin heitto ##################### # Lotto; Valitaan 7 numeroa 39:stä sample(1:39,7) # [1] 15 10 39 8 25 38 2 sample(c("R","L"),10,replace=T) # [1] "R" "L" "R" "R" "L" "L" "L" "R" "L" "R" sample(0:1,10,replace=T) n<-10 sample(c(0,1),n,replace=T) sum(sample(c(0,1),n,replace=T))/n sample(c(0,1),n,replace=T,prob=c(0.4,0.6)) sum(sample(c(0,1),n,replace=T,prob=c(0.4,0.6)))/n ######################################################## # Suhteellinen frekvenssi heittojen lukumäärän funktiona ########## Suhtfr <- function(n) # Heitetään rahaa n kertaa; Lasketaan kruunujen (1) suht.fr # jokaisen heiton jälkeen { x<-sample(c(0,1), n, replace = TRUE) x<-cumsum(x); i<-1:n; x<-x/i return(x) } # Plotataan suhteellinen frekvenssi n<-100 #par(mfrow=c(2,2)) plot(1:n,Suhtfr(n),ylim=c(0,1),type="l",axes=F) axis(2,c(0,0.2,0.4,0.5,0.6,0.8,1)) axis(1,seq(0,n,n/10)) abline(h=c(0.5),lty=3) ######################################################## # Empiirinen kertymäfunktio ########################### x<-c(18,12,14,11,24,14,24,22,24,10,8,19,21,22,24,24,24,6,24,21) length(x) sort(x) # [1] 6 8 10 11 12 14 14 18 19 21 21 22 22 24 24 24 24 24 24 24 median(x) # Empiirinen kertymäfunktio ############################ n<-length(x) plot(sort(x),(1:n)/n,type="s") # plot(sort(x),(1:n)/n,type="s",ylim=c(0,1)) # Funktio Fn <- ecdf(x) plot(Fn) plot(Fn, verticals= TRUE, do.p = FALSE) plot(Fn, do.p = FALSE) summary(Fn) Empirical CDF: 11 unique values with summary Min. 1st Qu. Median Mean 3rd Qu. Max. 6.0 10.5 14.0 15.0 20.0 24.0 ############################### # Empiirinen jakaumafunktio ############################ # P(8,14)=Fn(14)-Fn(8) x[8