Advertisement
organofcorti

mean n binom

Apr 9th, 2012
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 5.35 KB | None | 0 0
  1.  
  2.  
  3. ###########
  4. ### comparison of simulated cumulative sum of
  5. ### geometric variables with mean = 0
  6. ### (ie. cumsum(geomVariable(p)-1/p))
  7. ### with a mean of negative binomial distributions
  8. ###vars definition
  9. D        <- 1e06                      # D=1/p
  10. y        <- c(2, 5, 10, 15, 20, 25)   # n to test
  11. x.length <- 200                       # number of x's to test
  12. x<-seq(-D*10,D*10,length=x.length)    # sequence of x.length variables from min and max x
  13.                                       # x being the the cumulative sum assessed
  14.  
  15. ### dnb.mean.func calculates the mean negative binomial density
  16. ### for 1:n sucesses after making mean = 0
  17. ###
  18. dnb.mean.func<-function(x,y,D){
  19.     z<-matrix(0,y,1)
  20.     for(i in 1:y){
  21.     z[i]<-dnbinom(floor(x+i*(1-1/D)/(1/D)),i,1/D)
  22.     }
  23.     return(mean(z))
  24. }
  25.  
  26. plot.3.1.mat<-matrix(0, x.length, 3);plot.3.1.mat[,1]<-x
  27. plot.3.2.mat<-matrix(0, x.length ,3);plot.3.2.mat[,1]<-x
  28. plot.3.3.mat<-matrix(0, x.length ,3);plot.3.3.mat[,1]<-x
  29. plot.3.4.mat<-matrix(0, x.length, 3);plot.3.4.mat[,1]<-x
  30. plot.3.5.mat<-matrix(0, x.length ,3);plot.3.5.mat[,1]<-x
  31. plot.3.6.mat<-matrix(0, x.length ,3);plot.3.6.mat[,1]<-x
  32.  
  33. ###assessing the mean negative binomial density at x
  34.  
  35. for(i in 1:200){
  36.     plot.3.1.mat[i,2]<-dnb.mean.func(plot.3.1.mat[i,1],y[1],D) #density values for first n to test
  37.     plot.3.2.mat[i,2]<-dnb.mean.func(plot.3.2.mat[i,1],y[2],D) #density values for second n to test
  38.     plot.3.3.mat[i,2]<-dnb.mean.func(plot.3.3.mat[i,1],y[3],D) #density values for third n to test
  39.     plot.3.4.mat[i,2]<-dnb.mean.func(plot.3.4.mat[i,1],y[4],D) #density values for fourth n to test
  40.     plot.3.5.mat[i,2]<-dnb.mean.func(plot.3.5.mat[i,1],y[5],D) #density values for fifth n to test
  41.     plot.3.6.mat[i,2]<-dnb.mean.func(plot.3.6.mat[i,1],y[6],D) #density values for sixth n to test
  42.     print(i)
  43. }
  44.  
  45. ###simulation of cumulative sum - D
  46. ###creates many simulations of a cumulative sum for each n
  47. ###can be normalised to btc instead of number of fails (shares submitted)
  48. ###
  49. iter   <- 5e05      #number of iterations of the cumulative sum,
  50.                     #more = less variability in result, takes longer
  51. rgIter <- vector('integer', (iter*3))
  52. rg.mat <- c(rep(y[1], iter),rep(y[2], iter),rep(y[3], iter),
  53.             rep(y[4], iter),rep(y[5], iter),rep(y[6], iter))
  54.  
  55. rg<-function(n){cumsum(rgeom(n,1/D)-D)}
  56.  
  57. ###applies rg to rg.mat, taking n from rg.mat[,1]
  58. rgIter <- mapply(rg,rg.mat)
  59.  
  60. ###creates vectors from rgIter list
  61. rgIter1 <- unlist(rgIter[1:iter])
  62. rgIter2 <- unlist(rgIter[(iter+1):(iter*2)])
  63. rgIter3 <- unlist(rgIter[(iter*2+1):(iter*3)])
  64. rgIter4 <- unlist(rgIter[(iter*3+1):(iter*4)])
  65. rgIter5 <- unlist(rgIter[(iter*4+1):(iter*5)])
  66. rgIter6 <- unlist(rgIter[(iter*5+1):(iter*6)])
  67. rgIter<-0;rg.mat<-0
  68.  
  69. ###collecting simulation density values for quicker plotting
  70. rgDens1 <- density(rgIter1)
  71. rgDens2 <- density(rgIter2)
  72. rgDens3 <- density(rgIter3)
  73. rgDens4 <- density(rgIter4)
  74. rgDens5 <- density(rgIter5)
  75. rgDens6 <- density(rgIter6)
  76.  
  77. ###simple plot of simulation densities and 'mean' negative binomial densities
  78. png('mean-dnb-plot.png', height=640*3,width=640*16/9,res=130)
  79. par(mfrow=c(3, 2))
  80.  
  81. plot(plot.3.1.mat[,1], plot.3.1.mat[, 2],type='l',col='red',lwd=1.5,xlim=range(x),xlab='',ylab='')
  82. lines(rgDens1$x,rgDens1$y,type='l',lwd=1)
  83. legend("topright",legend=c('Simulation\ndensity', 'Mean\nnegative\nbinomial\nprobability\ndensities'),col=c('black','red'),lwd=c(1,1),bty='n')
  84. title(main=paste(y[1],"sucesses"),xlab="Cumulative sum of total round shares minus D", ylab="Density")
  85.  
  86. plot(plot.3.2.mat[,1],plot.3.2.mat[,2],type='l',col='red',lwd=1.5,xlim=range(x),xlab='',ylab='')
  87. lines(rgDens2$x,rgDens2$y,type='l',lwd=1)
  88. legend("topright",legend=c('Simulation\ndensity', 'Mean\nnegative\nbinomial\nprobability\ndensities'),col=c('black','red'),lwd=c(1,1),bty='n')
  89. title(main=paste(y[2],"sucesses"),xlab="Cumulative sum of total round shares minus D", ylab="Density")
  90.  
  91. plot(plot.3.3.mat[,1],plot.3.3.mat[,2],type='l',col='red',lwd=1.5,xlim=range(x),xlab='',ylab='')
  92. lines(rgDens3$x,rgDens3$y,type='l',lwd=1)
  93. legend("topright",legend=c('Simulation\ndensity', 'Mean\nnegative\nbinomial\nprobability\ndensities'),col=c('black','red'),lwd=c(1,1),bty='n')
  94. title(main=paste(y[3],"sucesses"),xlab="Cumulative sum of total round shares minus D", ylab="Density")
  95.  
  96. plot(plot.3.4.mat[,1],plot.3.4.mat[,2],type='l',col='red',lwd=1.5,xlim=range(x),xlab='',ylab='')
  97. lines(rgDens4$x,rgDens4$y,type='l',lwd=1)
  98. legend("topright",legend=c('Simulation\ndensity', 'Mean\nnegative\nbinomial\nprobability\ndensities'),col=c('black','red'),lwd=c(1,1),bty='n')
  99. title(main=paste(y[4],"sucesses"),xlab="Cumulative sum of total round shares minus D", ylab="Density")
  100.  
  101. plot(plot.3.5.mat[,1],plot.3.5.mat[,2],type='l',col='red',lwd=1.5,xlim=range(x),xlab='',ylab='')
  102. lines(rgDens5$x,rgDens5$y,type='l',lwd=1)
  103. legend("topright",legend=c('Simulation\ndensity', 'Mean\nnegative\nbinomial\nprobability\ndensities'),col=c('black','red'),lwd=c(1,1),bty='n')
  104. title(main=paste(y[5],"sucesses"),xlab="Cumulative sum of total round shares minus D", ylab="Density")
  105.  
  106. plot(plot.3.6.mat[,1],plot.3.6.mat[,2],type='l',col='red',lwd=1.5,xlim=range(x),xlab='',ylab='')
  107. lines(rgDens6$x,rgDens6$y,type='l',lwd=1)
  108. legend("topright",legend=c('Simulation\ndensity', 'Mean\nnegative\nbinomial\nprobability\ndensities'),col=c('black','red'),lwd=c(1,1),bty='n')
  109. title(main=paste(y[6],"sucesses"),xlab="Cumulative sum of total round shares minus D", ylab="Density")
  110.  
  111. dev.off()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement