Transformada Inversa

El método de transformada inversa es una técnica utilizada para generar valores aleatorios a partir de una distribución de probabilidad dada. Se basa en la idea de transformar una variable aleatoria uniforme 𝑈 ∼ 𝑈 ( 0 , 1 ) U∼U(0,1) en una variable aleatoria 𝑋 X con la distribución deseada.

a <- 317 
c <- 15  
m <- 571
X_n <- 41 # semilla
random.number<-numeric(50) # vector numérico de longitud 50
 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)

Simulando Variables Aleatorias con funciones propias de R

  1. Generando variable aleatoria uniforme
# Generar 100 variables aleatorias uniformes entre 0 y 1
uniform_random_01 <- runif(100, min = 0, max = 1)
uniform_random_01
##   [1] 0.549336787 0.678099861 0.530762485 0.418186092 0.347423793 0.954834715
##   [7] 0.982722536 0.320687802 0.027474460 0.278048850 0.396454090 0.590104893
##  [13] 0.614186120 0.982510939 0.677378194 0.942323952 0.938076384 0.695193199
##  [19] 0.572538672 0.004519613 0.687111585 0.104118224 0.024727312 0.384303484
##  [25] 0.433817580 0.266134617 0.647565697 0.493335744 0.735969213 0.795003109
##  [31] 0.578456266 0.313899508 0.640155830 0.469308808 0.684835513 0.011324642
##  [37] 0.489533709 0.904872423 0.895645313 0.344952173 0.381967718 0.500580297
##  [43] 0.381902895 0.359902173 0.144014309 0.707601397 0.854677419 0.264958739
##  [49] 0.737561780 0.418117836 0.044812228 0.115427511 0.138352373 0.356154714
##  [55] 0.879825468 0.220161027 0.751502792 0.097464172 0.602119891 0.430244899
##  [61] 0.271144408 0.672071239 0.236696003 0.704617109 0.129269674 0.313063985
##  [67] 0.147295280 0.377945638 0.805931550 0.868939239 0.243002580 0.960651330
##  [73] 0.074201730 0.457570229 0.575644832 0.537052281 0.823201836 0.530062543
##  [79] 0.266102010 0.111477716 0.050086226 0.627102587 0.774124813 0.678765761
##  [85] 0.938825893 0.318877882 0.858626687 0.514461691 0.104774268 0.632743567
##  [91] 0.981254340 0.677676253 0.471779206 0.478251905 0.589236320 0.768460107
##  [97] 0.988432239 0.676723337 0.203754146 0.670547446
  1. Generando una variable aleatoria de una distribucion normal
# Generar 100 variables normales con media 30 y desviación estándar 3.5
normal_random <- rnorm(100, mean = 30, sd = 3.5)
normal_random
##   [1] 30.09904 26.29316 34.94318 29.40490 29.63721 22.95658 29.00918 35.54842
##   [9] 32.70026 35.47148 28.60669 34.95912 31.78040 31.41578 29.34253 28.50933
##  [17] 30.16759 29.36341 31.77078 33.81286 27.60367 22.61941 31.66103 27.90272
##  [25] 32.31789 34.57128 32.29555 33.19365 28.84368 33.04370 38.31616 30.02133
##  [33] 26.29525 25.71510 25.60011 34.94580 29.29027 30.80082 27.02712 30.69506
##  [41] 31.93067 26.89217 30.79210 29.73449 30.99980 29.73203 30.46079 30.76027
##  [49] 27.54173 37.07919 28.08562 20.19537 29.14635 28.81219 28.33861 30.44170
##  [57] 27.17499 34.06354 31.11443 27.48803 29.72605 29.58243 27.76193 33.56888
##  [65] 27.46696 32.43936 30.04127 29.22498 33.09665 34.67938 28.19154 19.28708
##  [73] 22.28724 30.11496 27.29933 29.54083 26.41998 26.23768 24.44167 28.55004
##  [81] 29.58292 29.37674 26.69502 32.46162 33.42406 34.28255 33.57550 26.65568
##  [89] 29.89018 34.87903 34.28818 28.95424 29.38893 36.59199 29.24129 29.09014
##  [97] 33.03819 24.18316 30.08053 31.81592
hist(normal_random, col=3, main="Grafico de La normal", xlim=c(15,40))

  1. Generando Variable Aleatoria de una exponencial Para generar variables aleatorias con distribución exponencial con parámetro β . Ejemplo:

Simular El tiempo de servicio en una caja de un banco cuyo comportamiento sigue una forma exponencial con media de 3 minutos/cliente

numexp <- rexp(100,1/3)
round(numexp,1)
##   [1]  2.1  8.8  1.6  0.9  8.1  0.0  0.3  2.3  0.7  0.3  1.2  3.8  8.7  0.3  0.4
##  [16]  3.7  0.2  9.5  0.4  4.9  0.3  0.9  6.6  3.3  7.5  1.7  3.8  2.6 28.4  0.7
##  [31]  0.3  1.0  8.0  3.3  3.5  2.3  3.3 12.6  0.6  6.9  0.7  6.4  5.7  1.5  0.6
##  [46]  0.6  2.0  1.6  2.2  1.5  1.2  0.7  4.1  0.1  1.1  6.8  1.1  2.2  2.2  1.1
##  [61]  5.4  4.0  3.3  3.7  0.4  0.7  1.7  3.2  0.9  2.0  3.7  1.1  3.6  3.7  0.3
##  [76]  3.2  4.2  2.1  6.3  0.2  6.8  1.0  2.2  1.5  0.5  0.5  2.7  3.6  3.9  0.9
##  [91]  1.3  1.6  1.7  2.4  2.4  0.8  0.0  0.0  4.9  3.7
  1. Generando variable aleatoria de una binomial
binom_random <-rbinom(100,12,0.25) 
binom_random 
##   [1] 4 2 2 2 2 4 0 3 5 2 1 6 3 2 1 5 0 4 3 2 0 5 2 5 2 3 5 4 0 3 2 3 4 2 1 3 3
##  [38] 2 4 4 1 2 3 3 2 5 5 1 2 1 6 2 3 1 3 1 5 2 2 3 3 4 3 1 6 3 0 1 4 5 1 3 2 3
##  [75] 3 3 1 1 2 2 4 1 5 2 6 1 2 4 2 1 1 2 1 3 3 4 2 4 3 3
  1. Generando variables aleatorias de una poisson
evento_pois <- rpois(100,5)

tablapois <- table(evento_pois)
tablapois
## evento_pois
##  0  1  2  3  4  5  6  7  8  9 10 11 12 18 
##  1  7  7  7 15 20 15 14  5  2  3  2  1  1
evento_pois
##   [1]  2  5  6  3  4  7 11  4  4  3  5  2 10  1  1  2  7  7  4  8  5  1  5 18  4
##  [26]  3  4 12  5  6  3  5  7  5  5  8  6  6  2  5  7  7  6  6  9  6  5  4  9  4
##  [51]  7  5  7 10  4  3  4  6  5  1  2  6 10  5  5  6  5  5  1  3  5  4  8  4  5
##  [76]  3  6  7  7  4  7  6  6  5  1  6 11  8  0  2  2  7  8  6  4  7  1  4  5  7