Generando variables aletorias - metodo transformata inversa

El método de la transformada inversa es una técnica utilizada para generar variables aleatorias con una distribución dada. Consiste en aplicar la función inversa de la distribución acumulativa a números generados uniformemente entre 0 y 1. Es útil en simulaciones estocásticas y modelos probabilísticos donde se requiere generar datos sintéticos.

a <- 317 
c <- 15  
m <- 571
X_n <- 41 # semilla
random.number<-numeric(100) # 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 = "#ff0", 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 = "#333", lwd = 2, add = TRUE)

Simulando variables aleatorias uniformes con funciones nativas de R

  1. Generando variable aleatoria - distribución uniforme
# Generar 100 variables aleatorias uniformes entre 0 y 1
uniform_random_01 <- runif(100, min = 0, max = 1)
uniform_random_01
##   [1] 0.21940949 0.68140778 0.11028349 0.67078290 0.23115694 0.80431310
##   [7] 0.69051437 0.10934850 0.21810775 0.39701839 0.80859522 0.74786706
##  [13] 0.35620019 0.93365717 0.48371629 0.39698004 0.44724126 0.33389470
##  [19] 0.77232813 0.21179037 0.29868013 0.96676635 0.51115613 0.98507358
##  [25] 0.69315123 0.52792896 0.74839340 0.08146925 0.54523825 0.99341289
##  [31] 0.59517821 0.22994125 0.34322740 0.17204199 0.63580795 0.33891511
##  [37] 0.59922737 0.95008518 0.37697581 0.89052955 0.63045980 0.25016675
##  [43] 0.57464596 0.07186596 0.62458295 0.49073017 0.76972726 0.92357779
##  [49] 0.31665706 0.88841929 0.85498605 0.32674633 0.90007994 0.45664308
##  [55] 0.51646176 0.39525174 0.27240466 0.19233639 0.05965992 0.33001796
##  [61] 0.86392067 0.87426083 0.43686308 0.49167528 0.70294225 0.48246487
##  [67] 0.58797340 0.42558880 0.17304015 0.41871474 0.12254922 0.42155856
##  [73] 0.31249353 0.96062987 0.07414983 0.49050537 0.19634478 0.25827691
##  [79] 0.43284728 0.85535595 0.83014907 0.78094982 0.11714990 0.02345938
##  [85] 0.38177141 0.06812099 0.97370528 0.67686672 0.19988425 0.16737346
##  [91] 0.39328413 0.33937655 0.07640546 0.12964954 0.37890820 0.21740581
##  [97] 0.05450982 0.11231534 0.69853312 0.12604289
  1. Generando variable aleatoria - dsitribución normal
# Generar 100 variables aleatorias 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, breaks = 20, probability = TRUE, col = "#ff0", ylim=c(0,0.35),  main = "Distribución Normal (media = 30, desviación estándar = 3.5)", xlim=c(20,40),    xlab = "Valor de X")

  1. Generando variable aleatoria - distribución exponencial
# Generar 100 variables aleatorias exponenciales con media de 3min/cliente
exponential_random <- rexp(100, 1/3)
round(exponential_random,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
  1. Generando variable aleatoria - distribución binomial
# Generar 100 variables aleatorias binomiales con 12 ensayos y probabilidad de éxito 0.25
binomial_random <- rbinom(100, size = 12, prob = 0.25)
binomial_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
  1. Generando variable aleatoria - distribución de Poisson
# Generar 100 variables aleatorias de Poisson con media de 5 eventos por minuto
poisson_random <- rpois(100, lambda = 5)
poisson_random
##   [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
tablaPoisson <- table(poisson_random)
tablaPoisson
## poisson_random
##  0  1  2  3  4  5  6  7  8  9 10 11 
##  1  4 10  8 19 17 14 14  5  5  1  2