daily pastebin goal
67%
SHARE
TWEET

exercicios/lista 2

l0rd_s1t4 Apr 16th, 2018 (edited) 58 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #1.Gerar uma permutação dos números 1; 2; 3; : : : ; n,
  2. ##considerando todas as n! possiveis permutações igualmente
  3. ##prováveis (exemplo 4b).
  4.  
  5. n = 10
  6. Pi = c(1:n)
  7.  
  8. for(i in 1:n){
  9.   ale = sample(i, 1:n, replace = FALSE)
  10.  
  11.   aux = round((ale)*runif(1)+1)
  12.   if(aux != 1){
  13.     aux2 = round((ale - 1)*runif(1)+1)
  14.     Pi[aux] = Pi[aux2]
  15.   }
  16. };Pi
  17.  
  18. #2. Gerar valores de X ~ Geométrica(p) (exemplo 4b).
  19. p = 0.4; n = 1000
  20. inv = floor((log(runif(n))/(log(1-p))))
  21. barplot(table(inv) +1)
  22.  
  23. #3. Implementar o algoritmo para gerar valores de X ~ Poisson(lamb) (seção 4.2).
  24.  
  25. val = runif(1)
  26. n3 = 100
  27.  
  28. for(i in 1:n3){
  29.   lamb = i
  30.   pe = exp(-lamb)
  31.   fe = pe
  32.   if(val < fe){
  33.     pe = (lamb*pe)/(i+1)
  34.     fe = fe +pe
  35.   }
  36.  
  37. }
  38.  
  39. # bernoulli
  40.  
  41. berinv = function( nc, p){
  42.   X = c(1:nc)
  43.   p1 = (1-p)
  44.  
  45.   for(i in 1:nc){
  46.     y = runif(1)
  47.     if(y < p1)
  48.       X[i] = y
  49.     else
  50.       X[i] = 1
  51.   }
  52.   X
  53. }
  54. berinv(5,0.3)
  55. #####################################################################################################################
  56. # 2 - Cap.4 :
  57. #1. Implementar algoritmo BINOMIAL da p. 56
  58. #2. exercício 10
  59. #3. exercício 12
  60. #4. exercício 13
  61. #5. exercício 14
  62.  
  63. ######## inversa da binomial ##########
  64.  
  65. m = 1000
  66. n = 15
  67. p = 0.3
  68.  
  69. numbin = function(m, n, p){
  70.   y = 0
  71.   for (j in 1:m) {
  72.     U = runif(n)
  73.     x = 0
  74.    
  75.     for (i in 1:n) {
  76.      
  77.       if (U[i] <= 1 - p) {
  78.         x[i] = 0
  79.       } else {
  80.         x[i] = 1
  81.         }
  82.     }
  83.     y[j] = sum(x)
  84.   }
  85.   return(y)
  86. }
  87. test = numbin(m, n, p)
  88. table(test)
  89.  
  90.  
  91. ######## 10.a ##########
  92. ##binomial negativa
  93. #X ~ NBin (1; p) = Geo(p)
  94. #X ~ NBin (r; p) = somatorio r i=1 Geo(p)
  95. ng = 1000
  96. r = 3
  97. p = 0.5
  98. tut = c()
  99. geo = c()
  100.  
  101. for(j in 1:ng){
  102.  
  103.     tut[j] = sum (floor(((log(runif(r)))/(log(1-p)))) + 1)  # rgeom(1, p) ((log(runif(1)))/(log(p))) + 1
  104.  
  105.   #geo[j] = sum(tut)
  106. }
  107. barplot((table(tut)))
  108.  
  109.  
  110. ######## 10.b teorico ######
  111.  
  112. #p = j
  113. #j = r, r+1, ...
  114.  
  115. p1 = 0.3
  116. r1 = 5
  117. vl = 0
  118. for (i in 1:100) {
  119.   j1 = r1 + i
  120.   vl[i] = ((j1*(1-p1))/(j1+1-r1))*p1
  121.  
  122. }
  123. ((j(1-p))/(j+1-r))*p
  124.  
  125. r2 = 5
  126. vl2 = 0
  127. p2 = 0.3
  128. for(i in 0:r2){
  129.   j2 = r2 + i
  130.   #p2 = j2
  131.   vl2[i] = (factorial(j2))/((factorial(j2+1+r2)*factorial(r2-1)))*(p2^r2)*(1-p2)^(j2+1-r2)
  132.  
  133. }
  134.  
  135. # pj = (factorial(j))/((factorial(j+1+r)*factorial(r-1)))*(p^r)*(1-p)^(j+1-r)
  136.  
  137. n = 100
  138. r = 5
  139. j = r
  140. p = 0.3
  141. vla = 0
  142. ja = c()
  143. #pj = ((factorial(j-1))/((factorial(j-r)*factorial(r-1))))*(p^r)*(1-p)^(j+1-r)
  144.  
  145. for (i in 0:n) {
  146.   #ja <- c()
  147.   j = j+1
  148.   ja[i] = j
  149.  
  150.   vla[i] = ((ja[i]*(1-p)*(ja[i]+1-r))/(ja[i]+1-r))*((factorial(ja[i]-1))/((factorial(ja[i]-r)*factorial(r-1))))*(p^r)*(1-p)^(ja[i]+1-r)
  151.  
  152.  
  153.   }
  154. plot(vla)
  155.  
  156.  
  157. ######## 10.c 4.3 ######
  158.  
  159. ####### 12 #######
  160. #prob condicional x>k
  161.  
  162. #gerando as probs
  163. lamb = 3
  164. k = 11
  165. #x<k
  166. #x = i
  167. probabilidades = function(lamb, k){
  168.   ia = 0
  169.   for( i in 1:k-1){
  170.     ia[i] = (exp(-lamb)*lamb^i)/factorial(i)
  171.   }
  172.  
  173.   ja = 0
  174.   for( j in 1:(k)){
  175.     ja[j] = (((exp(-lamb)*lamb^j)/factorial(j)))
  176.   }
  177.  
  178.   ij =0
  179.   for (l in 1:k-1){
  180.     ij[l] = ia[l]/sum(ja)
  181.   }
  182.   return( ij)
  183. };pipa = probabilidades(lamb, k)
  184. plot(pipa)
  185. #
  186. ####gerando os num.
  187. ####seria pela inversa
  188. X = 0
  189. i = 0
  190. j = 1
  191.  
  192. while (j < k){
  193.   ru = runif(1)
  194.   print(ru)
  195.   if(ru < pipa[j]){
  196.     X[j] = i
  197.     j = j +1
  198.   }else{
  199.     i = i +1}
  200. }
  201.  
  202. table(X)
  203.  
  204.  
  205. ####### 13 #####
  206.  
  207. ####### 14 ####
  208. tam = 100000
  209.  
  210. j = 0
  211. X = 0
  212. prob.vetor = c(0.11, 0.20, 0.31, 0.40, 0.51, 0.60, 0.71, 0.80, 0.91)
  213. valor.vetor = 5:14
  214.  
  215. for(i in 1:tam){
  216.     u = runif(1)
  217.     if( u <= prob.vetor[1]){
  218.       X[i] = valor.vetor[1]
  219.     }else{
  220.       if( u <= prob.vetor[2]){
  221.         X[i] = valor.vetor[2]
  222.       }else{
  223.         if( u <= prob.vetor[3]){
  224.           X[i] = valor.vetor[3]
  225.         }else{
  226.           if( u <= prob.vetor[4]){
  227.             X[i] = valor.vetor[4]
  228.           }else{
  229.             if( u <= prob.vetor[5]){
  230.               X[i] = valor.vetor[5]
  231.             }else{
  232.               if( u <= prob.vetor[6]){
  233.                 X[i] = valor.vetor[6]
  234.               }else{
  235.                 if( u <= prob.vetor[7]){
  236.                   X[i] = valor.vetor[7]
  237.                 }else{
  238.                   if( u <= prob.vetor[8]){
  239.                     X[i] = valor.vetor[8]
  240.                   }else{
  241.                     if( u <= prob.vetor[9]){
  242.                       X[i] = valor.vetor[9]
  243.                     }else{
  244.                       X[i] = valor.vetor[10]
  245.                     }
  246.                   }
  247.                 }
  248.               }
  249.             }
  250.           }
  251.         }
  252.       }
  253.     }
  254.    
  255.   }
  256.  
  257. barplot(table(X)/tam)
  258.  
  259.  
  260.  
  261. # Lista 3 - Cap.5:
  262. #1. Implementar algoritmo do exemplo 5f.
  263. #2. exercício 1
  264. #3. exercício 2
  265. #4. exercício 6
  266. #5. exercício 8a
  267.  
  268. #### gerar rv de uma normal ####
  269.  
  270. t = 1000
  271. i = 1
  272. lambida = 1
  273.  
  274.  
  275. X = 0
  276.  
  277. while(i <= t){
  278.   Y = rexp(1, lambida)
  279.   U = runif(1)
  280.  
  281.   if(U <= exp((-(Y - 1)^2)/2)){
  282.     X[i] = Y
  283.     i = i +1
  284.   }
  285. }
  286. h = round(X,2)
  287. hist(h, freq = FALSE)
  288.  
  289. #### 01 ####
  290.  
  291. fx = function(x) exp(x)/(exp(1) - 1)
  292. plot(fx)
  293.  
  294. u = runif(1)
  295.  
  296. t = log(u*(exp(1) - 1)+1)
  297.  
  298. m = 100000
  299. oct = 0
  300.  
  301. for( i in 1:m){
  302.   u = runif(1)
  303.   oct[i] = log(u*(exp(1) - 1)+1)
  304. }
  305. hist(table(round(oct, 2))/m, freq = FALSE)
  306. mean(oct)
  307. var(oct)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top