Facebook
From Reliable Lion, 4 Years ago, written in R.
Embed
Download Paste or View Raw
Hits: 239
  1. alpha=2
  2. lambda=2
  3. n=100000
  4.  
  5. X=rgamma(n, alpha, lambda) #p,d,g
  6. histogram(X)
  7. mean(X) #alpha/lambda
  8. var(X) #alpha/lambda^2
  9. hist(X, prob=TRUE, ylim=range(0,0.8), breaks=100, col='red')
  10. #hist(X)
  11. #x=c(1:100)
  12. #X1=dgamma(x, 100)
  13. #plot(X1)
  14. curve(dgamma(x,alpha, lambda), 0, 5, add=TRUE)
  15.  
  16.  
  17. ###
  18. n=100
  19. X=rgamma(n, alpha, lambda) #p,d,g
  20. plot(ecdf(X), col="red")
  21. rug(X)
  22. curve(pgamma(x,alpha,lambda), add=TRUE)
  23.  
  24.  
  25. die <- function(n){
  26.   X <- runif(n)*6
  27.   floor(X)+1
  28.   return(floor(X)+1)
  29. }
  30. m=100 #moze byc cos nie tak jak wypadnie tylko 5 wartosci
  31. hist(die(m), prob=TRUE)
  32. x=die(m)
  33. x=factor(x, levels=c(1:6))
  34. T=table(x)
  35. #T=T/m
  36. #T=factor(T, levels=c(1,2,3,4,5,6))
  37. chisq.test(T, p=c(1/6, 1/6, 1/6, 1/6, 1/6, 1/6)) #samemu napisac chisq test
  38.  
  39.  
  40.  
  41. ####
  42.  
  43. losowanie <- function(m){
  44.   lista=c(1:m)
  45.   for (i in 1:m){
  46.     U=runif(1)
  47.     X=rbinom(1,9,U) #X|U z Bin(n,U)
  48.     lista[i]=X
  49.   }
  50.   return(lista)
  51. }
  52.  
  53. T=table((losowanie(10000)))
  54.