El método de la transformada inversa es una técnica utilizada en simulación para generar valores aleatorios a partir de una distribución de probabilidad específica, a partir de números aleatorios uniformes. Este método se basa en la relación entre la función de distribución acumulativa (FDA) y la función inversa de dicha distribución.
a <- 317
c <- 15
m <- 571
X_n <- 41 # semilla
random.number<-numeric(100) # vector numérico de longitud 100
for (i in 1:100)
{X_n<-(a*X_n+c)%%m
random.number[i]<-X_n/m # números en el intervalo [0,1]
}
random.number
## [1] 0.78809107 0.85113835 0.83712785 0.39579685 0.49387040 0.58318739
## [7] 0.89667250 0.27145359 0.07705779 0.45359019 0.81436077 0.17863398
## [13] 0.65323993 0.10332750 0.78108581 0.63047285 0.88616462 0.94045534
## [19] 0.15061296 0.77057793 0.29947461 0.95971979 0.25744308 0.63572680
## [25] 0.55166375 0.90367776 0.49211909 0.02802102 0.90893170 0.15761821
## [31] 0.99124343 0.25043783 0.41506130 0.60070053 0.44833625 0.14886165
## [37] 0.21541156 0.31173380 0.84588441 0.17162872 0.43257443 0.15236427
## [43] 0.32574431 0.28721541 0.07355517 0.34325744 0.83887916 0.95096322
## [49] 0.48161121 0.69702277 0.98248687 0.47460595 0.47635727 0.03152364
## [55] 0.01926445 0.13309982 0.21891419 0.42206655 0.82136602 0.39929947
## [61] 0.60420315 0.55866900 0.12434326 0.44308231 0.48336252 0.25218914
## [67] 0.97022767 0.58844133 0.56217163 0.23467601 0.41856392 0.71103327
## [73] 0.42381786 0.37653240 0.38704028 0.71803853 0.64448336 0.32749562
## [79] 0.84238179 0.06129597 0.45709282 0.92469352 0.15411559 0.88091068
## [85] 0.27495622 0.18739054 0.42907180 0.04203152 0.35026270 0.05954466
## [91] 0.90192644 0.93695271 0.04028021 0.79509632 0.07180385 0.78809107
## [97] 0.85113835 0.83712785 0.39579685 0.49387040
# generando una variable exponencial con beta = 3
x <- -3*log(1-random.number)
round(x,1) # redondeando a una cifra decimal
## [1] 4.7 5.7 5.4 1.5 2.0 2.6 6.8 1.0 0.2 1.8 5.1 0.6 3.2 0.3 4.6
## [16] 3.0 6.5 8.5 0.5 4.4 1.1 9.6 0.9 3.0 2.4 7.0 2.0 0.1 7.2 0.5
## [31] 14.2 0.9 1.6 2.8 1.8 0.5 0.7 1.1 5.6 0.6 1.7 0.5 1.2 1.0 0.2
## [46] 1.3 5.5 9.0 2.0 3.6 12.1 1.9 1.9 0.1 0.1 0.4 0.7 1.6 5.2 1.5
## [61] 2.8 2.5 0.4 1.8 2.0 0.9 10.5 2.7 2.5 0.8 1.6 3.7 1.7 1.4 1.5
## [76] 3.8 3.1 1.2 5.5 0.2 1.8 7.8 0.5 6.4 1.0 0.6 1.7 0.1 1.3 0.2
## [91] 7.0 8.3 0.1 4.8 0.2 4.7 5.7 5.4 1.5 2.0
data.frame(1:length(random.number),random.number,round(x,1))
## X1.length.random.number. random.number round.x..1.
## 1 1 0.78809107 4.7
## 2 2 0.85113835 5.7
## 3 3 0.83712785 5.4
## 4 4 0.39579685 1.5
## 5 5 0.49387040 2.0
## 6 6 0.58318739 2.6
## 7 7 0.89667250 6.8
## 8 8 0.27145359 1.0
## 9 9 0.07705779 0.2
## 10 10 0.45359019 1.8
## 11 11 0.81436077 5.1
## 12 12 0.17863398 0.6
## 13 13 0.65323993 3.2
## 14 14 0.10332750 0.3
## 15 15 0.78108581 4.6
## 16 16 0.63047285 3.0
## 17 17 0.88616462 6.5
## 18 18 0.94045534 8.5
## 19 19 0.15061296 0.5
## 20 20 0.77057793 4.4
## 21 21 0.29947461 1.1
## 22 22 0.95971979 9.6
## 23 23 0.25744308 0.9
## 24 24 0.63572680 3.0
## 25 25 0.55166375 2.4
## 26 26 0.90367776 7.0
## 27 27 0.49211909 2.0
## 28 28 0.02802102 0.1
## 29 29 0.90893170 7.2
## 30 30 0.15761821 0.5
## 31 31 0.99124343 14.2
## 32 32 0.25043783 0.9
## 33 33 0.41506130 1.6
## 34 34 0.60070053 2.8
## 35 35 0.44833625 1.8
## 36 36 0.14886165 0.5
## 37 37 0.21541156 0.7
## 38 38 0.31173380 1.1
## 39 39 0.84588441 5.6
## 40 40 0.17162872 0.6
## 41 41 0.43257443 1.7
## 42 42 0.15236427 0.5
## 43 43 0.32574431 1.2
## 44 44 0.28721541 1.0
## 45 45 0.07355517 0.2
## 46 46 0.34325744 1.3
## 47 47 0.83887916 5.5
## 48 48 0.95096322 9.0
## 49 49 0.48161121 2.0
## 50 50 0.69702277 3.6
## 51 51 0.98248687 12.1
## 52 52 0.47460595 1.9
## 53 53 0.47635727 1.9
## 54 54 0.03152364 0.1
## 55 55 0.01926445 0.1
## 56 56 0.13309982 0.4
## 57 57 0.21891419 0.7
## 58 58 0.42206655 1.6
## 59 59 0.82136602 5.2
## 60 60 0.39929947 1.5
## 61 61 0.60420315 2.8
## 62 62 0.55866900 2.5
## 63 63 0.12434326 0.4
## 64 64 0.44308231 1.8
## 65 65 0.48336252 2.0
## 66 66 0.25218914 0.9
## 67 67 0.97022767 10.5
## 68 68 0.58844133 2.7
## 69 69 0.56217163 2.5
## 70 70 0.23467601 0.8
## 71 71 0.41856392 1.6
## 72 72 0.71103327 3.7
## 73 73 0.42381786 1.7
## 74 74 0.37653240 1.4
## 75 75 0.38704028 1.5
## 76 76 0.71803853 3.8
## 77 77 0.64448336 3.1
## 78 78 0.32749562 1.2
## 79 79 0.84238179 5.5
## 80 80 0.06129597 0.2
## 81 81 0.45709282 1.8
## 82 82 0.92469352 7.8
## 83 83 0.15411559 0.5
## 84 84 0.88091068 6.4
## 85 85 0.27495622 1.0
## 86 86 0.18739054 0.6
## 87 87 0.42907180 1.7
## 88 88 0.04203152 0.1
## 89 89 0.35026270 1.3
## 90 90 0.05954466 0.2
## 91 91 0.90192644 7.0
## 92 92 0.93695271 8.3
## 93 93 0.04028021 0.1
## 94 94 0.79509632 4.8
## 95 95 0.07180385 0.2
## 96 96 0.78809107 4.7
## 97 97 0.85113835 5.7
## 98 98 0.83712785 5.4
## 99 99 0.39579685 1.5
## 100 100 0.49387040 2.0
hist(x, breaks = 20, probability = TRUE, col = "lightblue", ylim=c(0,0.35), main = "Distribución Exponencial (beta = 3)", xlab = "Valor de X")
# Añadimos la función de densidad teórica de la distribución exponencial
curve(dexp(x,rate=1/3), col = "red", lwd = 2, add = TRUE)
# Generar 100 variables aleatorias uniformes entre 0 y 1
uniform_random_01 <- runif(100, min = 0, max = 1)
uniform_random_01
## [1] 0.3948811290 0.3287626738 0.3630355329 0.8832009283 0.1694781438
## [6] 0.8389948057 0.9759607944 0.8175741346 0.7603944214 0.5254389565
## [11] 0.6449367236 0.1712949378 0.3191645027 0.4515792902 0.2038103910
## [16] 0.2869904267 0.8358311169 0.5431354928 0.4389488576 0.5586761159
## [21] 0.0748203809 0.7052195442 0.1980348967 0.7305414292 0.8098844618
## [26] 0.2064839886 0.1533661566 0.4302310739 0.2088353152 0.6604894490
## [31] 0.7387226806 0.3470213914 0.5941688744 0.7988044349 0.0659905938
## [36] 0.9134475985 0.9308412531 0.5254213766 0.1672064147 0.9988683835
## [41] 0.4438091172 0.6914529572 0.8243213133 0.9328402258 0.6724504961
## [46] 0.9784688840 0.1747318923 0.0619331261 0.2111575599 0.8994111735
## [51] 0.0566542018 0.4715712203 0.1445119083 0.2456129964 0.9520448383
## [56] 0.9350713666 0.4525826836 0.4752344226 0.5477049958 0.3546492106
## [61] 0.2091683964 0.5428128329 0.2006642055 0.1614530827 0.9526468141
## [66] 0.5883630295 0.4068993418 0.6638919322 0.7750827142 0.4043007081
## [71] 0.2447931198 0.9476270135 0.3130556631 0.9839732908 0.1665929924
## [76] 0.6469857532 0.6854989349 0.0791987504 0.0549865430 0.7520058623
## [81] 0.8733634136 0.5505427991 0.1762584385 0.3731067583 0.3326019128
## [86] 0.3883941548 0.7293389093 0.8571954817 0.6404252916 0.4828672095
## [91] 0.1801319083 0.0009521383 0.4087905751 0.7241027867 0.6913728511
## [96] 0.7519658071 0.7549814507 0.0871369902 0.2543530774 0.3350329963
# Generar 100 variables normales con media 30 y desviación estándar 3.5
set.seed(123)
normal_random <- rnorm(100, mean = 30, sd = 3.5)
normal_random
## [1] 28.03834 29.19438 35.45548 30.24678 30.45251 36.00273 31.61321 25.57229
## [9] 27.59602 28.44018 34.28429 31.25935 31.40270 30.38739 28.05456 36.25420
## [17] 31.74248 23.11684 32.45475 28.34523 26.26262 29.23709 26.40898 27.44888
## [25] 27.81236 24.09657 32.93225 30.53681 26.01652 34.38835 31.49262 28.96725
## [33] 33.13294 33.07347 32.87553 32.41024 31.93871 29.78331 28.92913 28.66835
## [41] 27.56853 29.27229 25.57111 37.59135 34.22787 26.06912 28.58990 28.36671
## [49] 32.72988 29.70821 30.88661 29.90009 29.84995 34.79011 29.20980 35.30765
## [57] 24.57937 32.04615 30.43349 30.75580 31.32874 28.24187 28.83377 26.43499
## [65] 26.24873 31.06235 31.56873 30.18551 33.22794 37.17530 28.28139 21.91791
## [73] 33.52008 27.51780 27.59197 33.58950 29.00329 25.72749 30.63456 29.51388
## [81] 30.02017 31.34848 28.70269 32.25532 29.22830 31.16124 33.83894 31.52314
## [89] 28.85924 34.02083 33.47726 31.91939 30.83556 27.80233 34.76228 27.89909
## [97] 37.65567 35.36414 29.17505 26.40753
hist(normal_random,col=3,main="Gráfico de la normal ",xlim=c(15,45))
numexp <- rexp(100,1/3)
round(numexp,1)
## [1] 5.4 0.1 3.9 0.6 5.3 5.3 2.5 1.0 9.9 1.2 3.3 4.0 1.9 0.6 1.4
## [16] 1.1 10.4 3.8 3.2 0.9 0.3 8.9 5.9 2.0 4.8 1.8 0.3 1.0 5.3 0.8
## [31] 2.8 1.3 3.9 0.5 4.7 0.2 1.7 5.2 3.9 3.9 1.4 0.1 0.5 3.1 3.2
## [46] 11.2 0.0 0.0 5.1 1.6 1.4 0.2 1.6 4.4 3.8 4.3 6.4 4.3 2.7 8.1
## [61] 6.7 1.6 0.2 1.7 13.1 1.7 1.4 0.8 2.1 2.9 2.2 3.3 1.8 2.0 4.9
## [76] 0.3 1.2 8.2 8.6 0.1 1.4 2.0 2.9 3.4 0.2 4.9 0.5 4.4 0.3 0.7
## [91] 2.0 4.6 5.8 0.6 1.7 5.8 10.8 4.0 2.8 0.9
binom_random <- rbinom(100,12,0.25)
binom_random
## [1] 6 4 2 2 6 3 4 2 4 3 5 2 2 1 2 6 2 4 4 1 2 2 1 1 7 7 1 5 3 3 3 4 1 2 4 2 4
## [38] 2 3 2 2 6 2 3 0 3 3 3 2 5 3 2 2 2 5 6 3 2 7 2 3 1 3 1 2 2 3 4 2 5 4 4 1 2
## [75] 4 3 2 2 1 2 5 4 4 1 1 6 3 3 2 3 2 2 4 2 4 1 4 3 4 4
evento_pois <- rpois(100,5)
evento_pois
## [1] 5 6 0 3 6 3 7 4 6 9 2 3 6 4 7 10 2 5 6 2 11 3 2 6 7
## [26] 4 4 4 2 4 3 5 5 9 4 5 2 7 4 6 3 5 7 2 7 4 4 4 1 5
## [51] 6 8 1 11 4 9 6 5 7 1 6 4 7 5 4 7 2 7 4 6 9 2 7 7 4
## [76] 4 3 5 8 5 5 6 5 5 1 2 8 7 3 6 6 4 7 5 8 5 8 5 4 9
tablapois <- table(evento_pois)
tablapois
## evento_pois
## 0 1 2 3 4 5 6 7 8 9 10 11
## 1 4 10 8 19 17 14 14 5 5 1 2