Ejercicio 6 - M.Especificos - V.A.Continuas - Semana6

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

MÉTODOS ESPECÍFICOS PARA GENERAR VARIABLES

ALEATORIAS CONTINUAS
Semana 6

Integrantes:
 Mario Adauto Sangama
 Daniel Garcia Flores
 Evelyn Nolasco Palomino
 Tito Sullca Damian

NORMAL ESTANDAR
Transformada inversa
sigma<-sqrt(2)
mu<-20
continua.trans.inv.normal<-function(mu,sigma,cantidad){
Y<-c()
X<-c()
for(i in 1:cantidad){
U<-runif(1)
X[i]<-(U^0.135 -(1-U)^0.135)/0.1975
Y[i]<-mu+sigma*X[i]
}
return(Y)
}
datos1<-continua.trans.inv.normal(mu,sigma,1000)
head(datos1,n = 50)

## [1] 21.75903 19.46257 19.09485 21.22810 21.21333 20.34577 20.96486


17.96631
## [9] 21.64052 23.74897 20.90903 18.51840 20.54575 19.70674 19.23182
18.25309
## [17] 19.80727 20.37612 15.55501 21.39460 19.45756 20.12786 19.66337
22.31216
## [25] 19.39977 17.79250 21.67381 20.70407 22.75356 19.41593 18.63275
22.04021
## [33] 21.71842 19.44110 20.67119 20.59253 19.24678 18.96995 19.92496
20.91236
## [41] 17.84920 19.46363 21.08524 19.80005 19.32435 20.21439 18.57063
18.90761
## [49] 16.88545 19.62198

hist(datos1,freq = FALSE,breaks = "Sturges")


lines(density(datos1),col = "red")

Suma de 12 variables uniformes


sigma<-sqrt(2)
mu<-20
continua.var.normal<-function(mu,sigma,cantidad){
Y<-c()
X<-c()
for(i in 1:cantidad){
U<-runif(12)
X[i]<-(sum(U)-6)
Y[i]<-mu+sigma*X[i]
}
return(Y)
}
datos3<-continua.var.normal(mu,sigma,1000)
head(datos3, n=50)

## [1] 19.48321 19.65991 18.12383 19.54362 17.54855 23.17009 18.93898


20.33518
## [9] 17.55645 20.42661 20.59330 21.14352 20.77026 18.09601 20.77568
19.86063
## [17] 17.60751 20.86654 20.73948 20.67640 20.20855 16.71824 19.75736
20.18528
## [25] 17.66601 19.15484 17.83499 19.77467 19.98281 19.51601 21.56557
19.89844
## [33] 19.51489 17.94316 16.76485 21.31410 20.77041 21.81946 18.80287
20.31422
## [41] 21.26368 19.97678 20.44630 20.17172 18.87960 20.05813 18.72832
20.74580
## [49] 19.73524 18.99759

hist(datos3,freq = FALSE,breaks = "Sturges")


lines(density(rnorm(1000,20,sqrt(2))),col = "blue")

Cauchy
continua.trans.inv.cauchy<-function(alfa,beta,cantidad){
X<-c()
for(i in 1:cantidad){
U<-runif(1)
X[i]<-alfa+beta*tan(pi*(U-1/2))
}
return(X)
}
datos<-continua.trans.inv.cauchy(2,3,1000)
head(datos,n=50)
## [1] -1.3303778 68.8927455 0.8484586 15.3648948 3.8637587
## [6] 5.5394913 2.7231900 0.2892761 12.7438650 -0.3954591
## [11] 1.6950876 0.7879764 4.3617297 1.3379524 -2.3785011
## [16] -8.4885626 -5.4120354 5.1336693 0.7678280 4.3661723
## [21] 4.8737566 5.9033879 -0.1598204 -164.3342852 6.3159110
## [26] 0.8424461 3.7594962 -7.5679449 6.2120809 -6.0723179
## [31] 10.1297362 1.8945595 81.9654654 3.5123181 0.1875300
## [36] 0.5200484 1.5355719 4.3417564 2.8455756 5.1047637
## [41] 10.2451663 2.1936067 2.9023960 2.0710949 4.2342176
## [46] 0.8092085 -1.5555528 3.4841816 5.6763780 -11.3650119

hist(datos,freq = FALSE,breaks = "Sturges")

Gamma y Erlang
continua.trans.inv.erlang.gamma<-function(alfa,beta,cantidad){
erlang<-function(alfa){
x=0
y<-c()
for(i in 1:alfa){
y[i]<-sum(-log(runif(i)))
x<-x+y[i]
}
return(x)
}
if(beta==1){
valores<-c()
for(i in 1:cantidad){
valores<-c(valores,erlang(alfa))
}
return(valores)
}else{
valores<-c()
for(i in 1:cantidad){
valores<-c(valores,erlang(alfa))
}
return(valores/beta)
}
}
datos<-continua.trans.inv.erlang.gamma(10,2,1000)
head(datos,n=50)

## [1] 25.13391 18.91364 28.33390 26.89042 32.77517 31.07751 29.12213


23.64313
## [9] 27.62918 31.76615 24.28371 26.46806 24.24128 24.55245 28.13208
36.33587
## [17] 35.39983 31.95459 21.96049 21.56138 32.54798 22.90286 32.66461
33.98199
## [25] 23.50893 22.12927 23.27034 26.38331 27.31998 25.96103 26.42884
24.60613
## [33] 24.57166 28.83610 25.76401 31.56213 22.63250 34.84401 30.11356
23.50631
## [41] 31.24255 28.34925 26.20055 29.01596 28.93093 31.74493 27.28324
22.70157
## [49] 26.67377 22.12144

hist(datos,freq = FALSE,breaks = "Sturges")

You might also like