library(knitr)
library(flextable)
library(MASS)
library(ggplot2)
Ejercicios.
### Parte A.
c_a <- 1/30
x_a <- 0:3
f_a <- c_a * (x_a^2 + 4)
tabla_a <- data.frame(
x = x_a,
f.x = as.character(fractions(f_a))
)
flextable(tabla_a)
x | f.x |
|---|---|
0 | 2/15 |
1 | 1/6 |
2 | 4/15 |
3 | 13/30 |
ggplot(tabla_a, aes(x = x, y = as.numeric(f_a))) +
geom_point(size = 3, color = "purple") +
geom_segment(aes(x = x, xend = x, y = 0, yend = as.numeric(f_a)),
color = "purple", linetype = "dashed") +
labs(title = "Distribución de probabilidad - Ejercicio 3.5 (a)",
x = "Valores de X",
y = "P(X = x)") +
theme_minimal()
### Parte B.
x_b <- 0:2
c_b <- 1/10
f_b <- c_b * choose(2, x_b) * choose(3, 3 - x_b)
tabla_b <- data.frame(
x = x_b,
f.x = as.character(fractions(f_b))
)
flextable(tabla_b)
x | f.x |
|---|---|
0 | 1/10 |
1 | 3/5 |
2 | 3/10 |
graf_b <- ggplot(tabla_b, aes(x = x, y = as.numeric(f_b))) +
geom_point(size = 3, color = "purple") +
geom_segment(aes(x = x, xend = x, y = 0, yend = as.numeric(f_b)),
color = "purple", linetype = "dashed") +
labs(title = "Distribución de probabilidad - Ejercicio 3.5 (b)",
x = "Valores de X",
y = "P(X = x)") +
theme_minimal()
graf_b
\[ \small f(x) = \begin{cases} \dfrac{20,000}{(x+100)^3}, & x > 0 \\ 0, & \text{en otro caso} \end{cases} \] Calcule la probabilidad de que un frasco de esta medicina tenga una vida útil de:
f.x <- function(x) { 20000 / (x+100)^3 }
a <- integrate(f.x, lower = 200, upper = Inf)$value
a
## [1] 0.1111111
f.x <- function(x) { 20000 / (x + 100)^3 }
x_curve <- seq(0, 400, by = 0.1)
df_curve <- data.frame(x = x_curve, y = f.x(x_curve))
# Área desde 200 en adelante
x_fill_a <- seq(200, 400, by = 0.1)
df_fill_a <- data.frame(x = x_fill_a, y = f.x(x_fill_a))
ggplot() +
geom_line(data = df_curve, aes(x = x, y = y),
color = "purple", size = 1.2) +
geom_area(data = df_fill_a, aes(x = x, y = y),
fill = "purple", alpha = 0.4) +
labs(title = "Área bajo la curva: P(X ≥ 200)",
x = "x (días)", y = "f(x)") +
theme_minimal()
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
b <- integrate(f.x, lower = 80, upper = 120)$value
b
## [1] 0.1020304
x_fill_b <- seq(80, 120, by = 0.1)
df_fill_b <- data.frame(x = x_fill_b, y = f.x(x_fill_b))
ggplot() +
geom_line(data = df_curve, aes(x = x, y = y),
color = "purple", size = 1.2) +
geom_area(data = df_fill_b, aes(x = x, y = y),
fill = "purple", alpha = 0.4) +
labs(title = "Área bajo la curva: P(80 ≤ X ≤ 120)",
x = "x (días)", y = "f(x)") +
theme_minimal()
\[ \small f(x) = \begin{cases} x, & 0 < x < 1, \\ 2 - x, & 1 \leq x < 2, \\ 0, & \text{en otro caso}. \end{cases} \]
Calcule la probabilidad de que en un período de un año una familia utilice su aspiradora: a) menos de 120 horas;
f.x <- function(x){
ifelse(x > 0 & x < 1, x,
ifelse(x >= 1 & x < 2, 2 - x, 0))
}
a <- integrate(f.x, lower = 0, upper = 1.2)$value
a
## [1] 0.68
b <- integrate(f.x, lower = 0.5, upper = 1)$value
b
## [1] 0.375
f.x <- function(x) { (2*(x+2))/5 }
a <- integrate(f.x, lower = 0, upper = 1)$value
a
## [1] 1
f.x <- function(x) { (2*(x+2))/5 }
b <- integrate(f.x, lower = 1/4, upper = 1/2)$value
b
## [1] 0.2375
f.x <- function(x) { (2*(x+2))/5 }
curve(f.x, from = 0, to = 1,
col = "purple", lwd = 2,
main = "Gráfica de la función de densidad f(x)",
xlab = "x", ylab = "f(x)")
5.Una empresa de inversiones ofrece a sus clientes bonos municipales que vencen después de varios años. Dado que la función de distribución acumulativa de T; el número de años para el vencimiento de un bono que se elige al azar, es:
\[ \small F(t) = \begin{cases} 0, & t < 1, \\[6pt] \dfrac{1}{4}, & 1 \leq t < 3, \\[6pt] \dfrac{1}{2}, & 3 \leq t < 5, \\[6pt] \dfrac{3}{4}, & 5 \leq t < 7, \\[6pt] 1, & t \geq 7, \end{cases} \]
Calcule: a. \[ \small \text{a) } P(T = 5), \]
x_datos <- c(1, 3, 5, 7)
p_datos <- rep(1/4, length(x_datos))
tabla_a <- data.frame(
x = x_datos,
f.x = as.character(fractions(p_datos))
)
flextable(tabla_a)
x | f.x |
|---|---|
1 | 1/4 |
3 | 1/4 |
5 | 1/4 |
7 | 1/4 |
o
x_datos <- c(1, 3, 5, 7)
p_datos <- rep(1/4, length(x_datos))
# Usa esto:
P_a <- p_datos[x_datos == 5]
P_a
## [1] 0.25
b). \[ \small \text{b) } P(T > 3), \]
x_datos <- c(1, 3, 5, 7)
p_datos <- rep(1/4, length(x_datos))
tabla_b <- data.frame(
x = x_datos,
f.x = as.character(fractions(p_datos))
)
flextable(tabla_b)
x | f.x |
|---|---|
1 | 1/4 |
3 | 1/4 |
5 | 1/4 |
7 | 1/4 |
P_b <- sum(p_datos[x_datos > 3])
P_b
## [1] 0.5
c). \[ \text{c) } P(1.4 < T < 6), \]
x_datos <- c(1, 3, 5, 7)
p_datos <- rep(1/4, length(x_datos))
tabla_c <- data.frame(
x = x_datos,
f.x = as.character(fractions(p_datos))
)
flextable(tabla_c)
x | f.x |
|---|---|
1 | 1/4 |
3 | 1/4 |
5 | 1/4 |
7 | 1/4 |
P_c <- sum(p_datos[x_datos > 1.4 & x_datos < 6])
P_c
## [1] 0.5
d). \[ \text{d). } P(T \leq 5 \mid T \geq 2). \]
tabla_d <- data.frame(
x = c(1, 3, 5, 7),
f.x = rep(1/4, 4)
)
num <- sum(tabla_d$f.x[tabla_d$x %in% c(3, 5)])
den <- sum(tabla_d$f.x[tabla_d$x %in% c(3, 5, 7)])
prob_cond <- num / den
prob_cond
## [1] 0.6666667
x_datos <- c(1, 3, 5, 7)
F_datos <- c(1/4, 1/2, 3/4, 1)
x_plot <- c(0.9, 1, 3, 5, 7, 8)
F_plot <- c(0, 1/4, 1/2, 3/4, 1, 1)
df_cdf <- data.frame(x = x_plot, F = F_plot)
ggplot(df_cdf, aes(x = x, y = F)) +
geom_step(size = 1.2, color = "purple") +
geom_point(data = data.frame(x = x_datos, F = F_datos),
aes(x = x, y = F),
size = 3, color = "pink") +
labs(title = "Función de distribución acumulativa F(t)",
x = "t (años)", y = "F(t)") +
theme_minimal(base_size = 14)