Os resultados seguintes não servem como prova. Seguem apenas como verificação computacional do resultado analítico. Para todas simulações utilizou-se N = 1e+05
Sejam \(X\), \(Y\), \(Z\) independentes, cada uma tendo distribuição uniforme em \([0,1]\). Qual a probabilidade da equação quadrática \(Xt^2 + Yt + Z = 0\) ter raízes reais.
set.seed(2020/10/01)
# gerando NPAs
x <- runif(N)
y <- runif(N)
z <- runif(N)
# regiao
w <- y^2 - 4 * x * z
# resultados
mean(w >= 0) # resultado simulado
## [1] 0.25755
(3*log(4)+5)/36 # resultado teorico
## [1] 0.2544134
h <- hist(w, probability = T)
cuts <- cut(h$breaks, c(-Inf, -0.01, Inf))
par(new = T)
plot(h, col = c('gray','tomato')[cuts], axes = F, xlab = '', ylab = '', main = '')
Sejam \(X\) e \(Y\) variáveis aleatórias independentes, com \(X \sim U[0,a]\) e \(Y \sim U[a,a + b]\), onde \(a>0\), \(b>0\). Qual a probabilidade de que os três segmentos possam formar um triângulo?
# definindo a e b, a,b > 0
a <- 1 # a != 0
b <- 2 # a < b ou a > b
# gerando NPAs
x <- runif(N, 0, a)
y <- runif(N, a, a + b)
# definindo regioes
r1 <- x
r2 <- y - x
r3 <- a + b - y
regiao <- r1 <= r2 + r3 & r2 <= r1 + r3 & r3 <= r1 + r2
# resultados
mean(regiao) # valor simulado
## [1] 0.24962
min(c(a, b)) / (2 * max(c(a, b))) # valor teorico
## [1] 0.25
Sejam \(X\) e \(Y\) variáveis aleatórias independentes com distribuição uniforme em \([\theta - 1/2, \theta + 1/2]\), onde \(\theta \in \Bbb R\). Prove que a distribuição \(X-Y\) não depende de \(\theta\), achando a sua densidade.
theta <- c(-10, 10) # theta pertencente aos reais
# verificacao
par(mfrow = c(4,3))
for (i in 1:2) {
# gerando NPAs
x <- runif(N, theta[i] - 1/2, theta[i] + 1/2)
y <- runif(N, theta[i] - 1/2, theta[i] + 1/2)
z <- x - y
# verificacao por histogramas e fda
hist(x, main = paste0('theta: ', theta[i]), probability = T)
hist(y, main = paste0('theta: ', theta[i]), probability = T)
hist(z, main = paste0('theta: ', theta[i]), probability = T)
plot(ecdf(x), main = paste0('theta: ', theta[i]), xlab = 'x')
plot(ecdf(y), main = paste0('theta: ', theta[i]), xlab = 'y')
plot(ecdf(z), main = paste0('theta: ', theta[i]), xlab = 'z')
}
par(mfrow = c(1,1))
Inferência dos parâmetros da distribuição triangular
fit <- fitdist(
z,
"triang",
method = "mge",
start = list(min = -1, mode = 0, max = 1),
gof = "CvM"
)
summary(fit)
## Fitting of the distribution ' triang ' by maximum goodness-of-fit
## Parameters :
## estimate
## min -1.00583201
## mode -0.00285027
## max 1.00062465
## Loglikelihood: -50144.34 AIC: 100294.7 BIC: 100323.2
plot(fit)
Certo supermercado tem duas entradas, A e B. Fregueses entram pela A conforme processo de Poisson com taxa média de 15 clientes por minuto e, pela B, 10 por minuto. A é independente de B.
lambda1 <- 15 # A
lambda2 <- 10 # B
# entrada A: numero de cientes
A <- rpois(N, lambda1)
# entrada B: numero de cientes
B <- rpois(N, lambda2)
# A ind de B
X <- A + B
# inferencia
fit <- fitdist(X, 'pois')
# valor teorico
lambda1 + lambda2
## [1] 25
# valor estimado
summary(fit)
## Fitting of the distribution ' pois ' by maximum likelihood
## Parameters :
## estimate Std. Error
## lambda 24.99814 0.0158108
## Loglikelihood: -302556.6 AIC: 605115.1 BIC: 605124.6
# qualidade da inferencia
plot(fit)
# entrada A: tempo primeiro cliente entra
T1 <- rexp(N, lambda1)
# entrada B: tempo primeiro cliente entra
T2 <- rexp(N, lambda2)
# calculando a distribuicao do minimo entre A e B
Y <- apply(cbind(T1, T2), 1, function(x) min(x))
hist(Y, probability = T)
# inferencia
fit <- fitdist(Y, 'exp')
# valor teorico
lambda1 + lambda2
## [1] 25
# valor estimado
summary(fit)
## Fitting of the distribution ' exp ' by maximum likelihood
## Parameters :
## estimate Std. Error
## rate 24.98141 0.07899814
## Loglikelihood: 221813.2 AIC: -443624.4 BIC: -443614.9
# qualidade da inferencia
plot(fit)
# 1: entrada A e 0: entrada B
entrada <- sample(c(T, F), N, prob = c(15, 10)/25, replace = T)
# valor simulado
mean(entrada)
## [1] 0.59624
# valor teorico
lambda1 / (lambda1 + lambda2)
## [1] 0.6
Sejam \(X\) e \(Y\) variáveis aleatórias independentes, $X4 tendo distribuição de Poisson com parâmetro \(\lambda = 5\), e \(Y \sim [0,1]\). Ache a densidade de \(Z = X + Y\).
# densidade
fz <- function(z, lambda = 5) exp(-lambda) * lambda^floor(z) / factorial(floor(z))
# visualizacao
curve(fz, from = 0, to = 20, xlab = 'z', ylab = 'fz', type = 's')
Lança-se um dado equilibrado duas vezes, independentemente. Sejam \(X\) e \(Y\) as variáveis aleatórias que representam os números obtidos em respectivamente, o primeiro e o segundo lançamento.
# X ind de Y
X <- sample(1:6, N, replace = T)
Y <- sample(c(1,3,5), N, replace = T)
# W = abs(X - Y)
W = abs(X - Y)
# probabilidades estimadas
(w <- table(W)/sum(table(W)))
## W
## 0 1 2 3 4 5
## 0.16694 0.27917 0.22130 0.16443 0.11097 0.05719
# visualizacao
barplot(w, space = 0, main = 'Histogram of W')
Escolhe-se um ponto ao acaso(i.e. conforme a distribuição uniforme) dos lados do quadrado de vértices \((1,1); (1,-1); (-1,-1)\) e \((-1,1)\). Sejam \(X\) e \(Y\) as coordenadas do ponto escolhido.
# X ind de Y
X <- runif(N, -1, 1)
Y <- runif(N, -1, 1)
# Z = X + Y
Z = X + Y
# valor teorico
z <- seq(-2, 2, length.out = 1000)
fz_z <- ifelse(z >= -2 & z <= 0, (2 + z)/4, (2 - z)/4)
# visualizacao
hist(Z, probability = T)
par(new = T)
plot(z, fz_z, type = 'l', axes = F, xlab = '', ylab = '', main = '', col = 'red')
# W = max(X, Y)
W <- apply(cbind(X, Y), 1, max)
# valor teorico
3/4
## [1] 0.75
# valor simulado
mean(W > 0)
## [1] 0.7493
# visualizacao
z <- seq(-2, 2, length.out = 1000)
fz_z <- ifelse(z >= -2 & z <= 0, (2 + z)/4, (2 - z)/4)
h <- hist(W, probability = T)
cuts <- cut(h$breaks, c(-Inf, -0.01, Inf))
par(new = T)
plot(h, col = c('gray','tomato')[cuts], axes = F, xlab = '', ylab = '', main = '')
Sejam X e Y variáveis aleatórias independentes com distribuição comum \(Exp(\lambda)\). Prove que \(Z = \frac{X}{X+Y} \sim U[0, 1]\).
alpha <- 1
beta <- 1
# X ind de Y
X <- rgamma(N, alpha, beta)
Y <- rgamma(N, alpha, beta)
# Z = X/(X + Y)
Z = X/(X + Y)
# inferencia
fit <- fitdist(Z, 'beta')
# valor estimado
summary(fit)
## Fitting of the distribution ' beta ' by maximum likelihood
## Parameters :
## estimate Std. Error
## shape1 1.005665 0.004163318
## shape2 1.002397 0.004147069
## Loglikelihood: 1.00742 AIC: 1.98516 BIC: 21.01101
## Correlation matrix:
## shape1 shape2
## shape1 1.0000000 0.6458214
## shape2 0.6458214 1.0000000
# qualidade da inferencia
plot(fit)
# valor teorico
z <- seq(0, 10, length.out = 10000)
fz <- function(z, alpha1 = 1, alpha2 = 2) {
z^(alpha1 - 1) * (1 + z)^(-(alpha1 + alpha2)) * gamma(alpha1 + alpha2)/(gamma(alpha1) * gamma(alpha2))
}
curve(fz, xlim = c(0, 10))
Sejam X e Y variável aleatória independentes, tendo distribuição comum \(U[0,1]\), e sejam \(R = \sqrt{2 \log(1 / (1 - X))}\) e \(\Theta = \pi(2Y - 1)\).
x <- runif(N)
y <- runif(N)
# distribuicao R = sqrt(2 * log(1/(1 - x)))
R = sqrt(2 * log(1/(1 - x)))
hist(R, probability = T)
# inferencia
drayleigh <- function(x, sigma) (x / sigma^2) * exp(-(x^2)/(2 * sigma^2))
prayleigh <- function(x, sigma) 1 - exp(-(x^2)/(2 * sigma^2))
qrayleigh <- function(x, sigma) sqrt(-2 * sigma^2 * log(1 - x))
fit <- fitdist(R, distr = 'rayleigh', start = list(sigma = 1))
## Warning in fitdist(R, distr = "rayleigh", start = list(sigma = 1)): The
## prayleigh function should have its first argument named: q as in base R
# valor estimado
summary(fit)
## Fitting of the distribution ' rayleigh ' by maximum likelihood
## Parameters :
## estimate Std. Error
## sigma 1.000978 0.001582677
## Loglikelihood: -94329.2 AIC: 188660.4 BIC: 188669.9
# qualidade da inferencia
plot(fit)
# distribuicao theta = pi * (2 * y - 1)
theta = pi * (2 * y - 1)
hist(theta, probability = T)
# inferencia
fit <- fitdist(theta, distr = 'unif')
# valor estimado
summary(fit)
## Fitting of the distribution ' unif ' by maximum likelihood
## Parameters :
## estimate Std. Error
## min -3.141569 NA
## max 3.141585 NA
## Loglikelihood: NA AIC: NA BIC: NA
## Correlation matrix:
## [1] NA
# qualidade da inferencia
plot(fit)
x <- runif(N)
y <- runif(N)
R = sqrt(2 * log(1/(1 - x)))
theta = pi * (2 * y - 1)
# z e w
z <- R * cos(theta)
w <- R * sin(theta)
par(mfrow = c(1, 2))
hist(z, probability = T)
hist(w, probability = T)
par(mfrow = c(1, 1))
tmp <- cbind.data.frame(z, w)
ggplot(tmp, aes(x = z, y = w)) +
geom_density2d_filled()
Sejam \(X_1, X_2, \dots X_n\) variáveis independentes e identicamente distribuídas, com densidade \(U[0, \theta]\), no qual \(\theta > 0\). Sejam \[ U = \min_{1\leq i\leq n} X_i \quad \quad V=\max_{1\leq i \leq n}X_i \]
# va's iid U(0, theta), theta >0
theta <- 5
n <- 10
X <- matrix(NA, nrow = N, ncol = n)
for (j in 1:n) X[, j] <- runif(N, 0, theta)
# definindo U e V
U <- apply(X, 1, min)
V <- apply(X, 1, max)
# verificando U e V
par(mfrow = c(1, 2))
hist(U, probability = T)
hist(V, probability = T)
par(mfrow = c(1, 1))
# W = V - U
W <- V - U
# resultado empirico
hist(W, probability = T)
# resultado teorico em vermelho
fw <- function(w, n, theta) n * (n - 1) * w^(n - 2) * (1 - w/theta) / (theta^(n - 1))
curve(fw(x, n= n, theta = theta), add = T, col = 'red')
Mostre que se \(X \sim t(1)\), então \(X\) tem distribuição de Cauchy.
n <- 1
Z <- rnorm(N)
Y <- rchisq(N, n)
tn <- Z/sqrt(Y/n)
# inferencia
fit <- fitdist(tn, 'cauchy')
# valor estimado
summary(fit)
## Fitting of the distribution ' cauchy ' by maximum likelihood
## Parameters :
## estimate Std. Error
## location -0.004028011 0.004495996
## scale 1.005378144 0.004496425
## Loglikelihood: -253872.2 AIC: 507748.5 BIC: 507767.5
## Correlation matrix:
## location scale
## location 1.000000000 0.003435531
## scale 0.003435531 1.000000000
# qualidade da inferencia
plot(fit)
# simulando cauchy e aplicando ks.test
rfit <- rcauchy(N, location = fit$estimate[1], scale = fit$estimate[2])
ks.test(tn, rfit)
## Warning in ks.test(tn, rfit): p-value will be approximate in the presence of
## ties
##
## Two-sample Kolmogorov-Smirnov test
##
## data: tn and rfit
## D = 0.00367, p-value = 0.511
## alternative hypothesis: two-sided