📅 21 de Octubre de 2025
# Definir la función de densidad
f <- function(x) {
ifelse(x > 0 & x < 1, x,
ifelse(x >= 1 & x < 2, 2 - x, 0))}
# Función de distribución acumulada F(x)
F <- function(x) {
ifelse(x < 0, 0,
ifelse(x < 1, (x^2) / 2,
ifelse(x < 2, 2*x - (x^2)/2 - 1, 1)))} #Función integrada
total_area <- integrate(f, lower = 0, upper = 2)$value
cat("Área total =", total_area, "\n")## Área total = 1
# P(X < 1.2), como es sobre 100 horas, el porcentaje de 120 horas será 1.2
prob_a <- F(1.2)
cat("P(X < 1.2) =", prob_a, "\n") ## P(X < 1.2) = 0.68
## P(0.5 ≤ X ≤ 1) = 0.375
library(ggplot2)
x <- seq(-0.5, 2.5, by = 0.001)
tb <- data.frame(x = x, y = f(x))
ggplot(tb, aes(x = x, y = y)) +
geom_line(color = "black", size = 1.2) +
geom_area(data = subset(tb, x >= 0 & x <= 1.2),
aes(x = x, y = y), fill = "lightblue", alpha = 0.4) +
geom_area(data = subset(tb, x >= 0.5 & x <= 1),
aes(x = x, y = y), fill = "blue", alpha = 0.5) +
labs(title = "Funcion de densidad f(x)",
subtitle = "Areas sombreadas: P(X<1.2) morado, P(0.5≤X≤1) naranja",
x = "x (centenas de horas)", y = "f(x)") +
theme_bw()## 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.
x <- seq(-0.5, 2.5, by = 0.001)
tb_F <- data.frame(x = x, y = F(x))
ggplot(tb_F, aes(x = x, y = y)) +
geom_line(color = "darkblue", size = 1.2) +
geom_point(aes(x = 1.2, y = F(1.2)), color = "red", size = 3) +
geom_hline(yintercept = F(1.2), linetype = "dashed", color = "gray") +
labs(title = "Funcion de distribucion acumulada F(x)",
subtitle = "P(X < 1.2) = 0.68",
x = "x (centenas de horas)", y = "F(x)") +
theme_bw()## Warning in geom_point(aes(x = 1.2, y = F(1.2)), color = "red", size = 3): All aesthetics have length 1, but the data has 3001 rows.
## ℹ Please consider using `annotate()` or provide this layer with data containing
## a single row.
f.x <- function(x){
ifelse(x > 0 & x < 1, 2*(x + 2)/5, 0)}
F.x <- function(x){
ifelse(x <= 0, 0,
ifelse(x < 1, (x^2 + 4*x)/5, 1))} #Función integradatotal_area <- integrate(f.x, lower = 0, upper = 1)$value
cat("Integral total (0,1) =", total_area, "\n")## Integral total (0,1) = 1
p_a <- 1
p_b <- integrate(f.x, lower = 1/4, upper = 1/2)$value
cat("P(1/4 < X < 1/2) =", p_b, " (fraccion = 19/80 = 0.2375)\n")## P(1/4 < X < 1/2) = 0.2375 (fraccion = 19/80 = 0.2375)
# Crear tabla para graficas
x <- seq(-0.2, 1.2, by = 0.001)
df <- data.frame(x = x, pdf = f.x(x), cdf = F.x(x))
# Grafica con area sombreada entre 1/4 y 1/2
df_area_b <- subset(df, x >= 1/4 & x <= 1/2)
g1 <- ggplot(df, aes(x = x, y = pdf)) +
geom_line(size = 1.1, color = "black") +
geom_area(data = subset(df, x >= 0 & x <= 1), aes(x=x, y=pdf), fill = "grey90") +
geom_area(data = df_area_b, aes(x = x, y = pdf), fill = "blue", alpha = 0.6) +
labs(title = "Funcion de densidad f(x)",
subtitle = "Area azul = P(1/4 < X < 1/2)",
x = "x (proporcion, 0-1)", y = "f(x)") +
theme_bw()
print(g1)# Grafica de distribución
g2 <- ggplot(df, aes(x = x, y = cdf)) +
geom_line(size = 1.1, color = "blue") +
geom_vline(xintercept = c(1/4, 1/2), linetype = "dashed") +
geom_point(data = data.frame(x=c(1/4,1/2), y=c(F.x(1/4), F.x(1/2))),
aes(x=x,y=y), color="red", size=2) +
labs(title = "Funcion de distribucion acumulada F(x)",
x = "x (proporcion, 0-1)", y = "F(x)") +
theme_bw()
print(g2)\(\small X\) \[ \begin{array}{c|c} x & f(x) \\ \hline 0 & 0.41 \\ 1 & 0.37 \\ 2 & 0.16 \\ 3 & 0.05 \\ 4 & 0.01 \\ \end{array} \]
Función de distribución acumulativa
\[ F(X) = \begin{cases} 0 & \text{si } x < 0, \\ 0.41 & \text{si } 0 \le x < 1, \\ 0.78 & \text{si } 1 \le x < 2, \\ 0.94 & \text{si } 2 \le x < 3, \\ 0.99 & \text{si } 3 \le x < 4, \\ 1 & \text{si } x \ge 4 \end{cases} \]
Construcción de la función de distribución:
F.x <- function(x){
ifelse(x<0,0,
ifelse(x<1,0.41,
ifelse(x < 2, 0.78,
ifelse(x < 3,0.94,
ifelse(x<4,0.99,1)))))
}
F.x(2.8)## [1] 0.94
library(flextable)
library(ggplot2)
# Definir la función f(t)
F_t <- function(t) {
ifelse(t < 1, 0,
ifelse(t < 3, 1/4,
ifelse(t < 5, 1/2,
ifelse(t < 7, 3/4, 1))))}
# Valores posibles de T y sus probabilidades puntuales
valores_T <- c(1, 3, 5, 7)
prob_T <- rep(1/4, 4)
tabla_T <- data.frame(
"T (años)" = valores_T,
"P(T=t)" = prob_T
)
ft_tabla_T <- flextable(tabla_T)
ft_tabla_T <- set_header_labels(ft_tabla_T,
"T (años)" = "T (años)",
"P(T=t)" = "P(T = t)")
ft_tabla_T <- theme_vanilla(ft_tabla_T)
ft_tabla_T <- autofit(ft_tabla_T)
ft_tabla_T <- color(ft_tabla_T, color = "black", part = "all")
ft_tabla_T <- bg(ft_tabla_T, bg = "#e6f0ff", part = "body")
ft_tabla_T <- bold(ft_tabla_T, part = "header")
ft_tabla_TT..años. | P.T.t. |
|---|---|
1 | 0.25 |
3 | 0.25 |
5 | 0.25 |
7 | 0.25 |
t_vals <- seq(0, 8, 0.01)
df_F <- data.frame(t = t_vals, F = F_t(t_vals))
ggplot(df_F, aes(x = t, y = F)) +
geom_segment(aes(x = 1, xend = 3, y = 0.25, yend = 0.25), color = "blue", size = 1.3) +
geom_segment(aes(x = 3, xend = 5, y = 0.5, yend = 0.5), color = "blue", size = 1.3) +
geom_segment(aes(x = 5, xend = 7, y = 0.75, yend = 0.75), color = "blue", size = 1.3) +
geom_segment(aes(x = 7, xend = 8, y = 1, yend = 1), color = "blue", size = 1.3) +
geom_point(data = data.frame(t = valores_T, F = F_t(valores_T)),
aes(x = t, y = F), color = "red", size = 3) +
labs(title = "Función de distribución acumulativa F(t)",
subtitle = "Ejercicio 3.12 — Bonos municipales",
x = "t (años)", y = "F(t)") +
theme_bw() +
theme(plot.title = element_text(face = "bold"))## Warning in geom_segment(aes(x = 1, xend = 3, y = 0.25, yend = 0.25), color = "blue", : All aesthetics have length 1, but the data has 801 rows.
## ℹ Please consider using `annotate()` or provide this layer with data containing
## a single row.
## Warning in geom_segment(aes(x = 3, xend = 5, y = 0.5, yend = 0.5), color = "blue", : All aesthetics have length 1, but the data has 801 rows.
## ℹ Please consider using `annotate()` or provide this layer with data containing
## a single row.
## Warning in geom_segment(aes(x = 5, xend = 7, y = 0.75, yend = 0.75), color = "blue", : All aesthetics have length 1, but the data has 801 rows.
## ℹ Please consider using `annotate()` or provide this layer with data containing
## a single row.
## Warning in geom_segment(aes(x = 7, xend = 8, y = 1, yend = 1), color = "blue", : All aesthetics have length 1, but the data has 801 rows.
## ℹ Please consider using `annotate()` or provide this layer with data containing
## a single row.
# Definir la función de densidad f(x)
f <- function(x) {
k <- 3/2 # Valor de k calculado
ifelse(x > 0 & x < 1, k * sqrt(x), 0)}
# Definir la función de distribución acumulada F(x)
F <- function(x) {
ifelse(x <= 0, 0,
ifelse(x < 1, x^(3/2), 1))}
total_area <- integrate(f, lower = 0, upper = 1)$value
cat("Área total =", total_area, "\n")## Área total = 1
integral_valor <- integrate(function(x) sqrt(x), lower = 0, upper = 1)$value
k <- 1 / integral_valor
cat("El valor de k que hace que f(x) sea una densidad es: k =", k, "\n")## El valor de k que hace que f(x) sea una densidad es: k = 1.5
P(0.3 < X < 0.6).
# Definir nuevamente la función f(x) con el valor de k = 1.5
f <- function(x) {
ifelse(x > 0 & x < 1, 1.5 * sqrt(x), 0)}
# Calcular P(0.3 < X < 0.6)
prob_b <- integrate(f, lower = 0.3, upper = 0.6)$value
cat("P(0.3 < X < 0.6) =", prob_b, "\n")## P(0.3 < X < 0.6) = 0.3004412
# Validar que el área total bajo f(x) sea 1
total_area <- integrate(f, lower = 0, upper = 1)$value
cat("Área total =", total_area, "\n")## Área total = 1
## Warning in sqrt(x): Se han producido NaNs
ggplot(tb, aes(x = x, y = y)) +
geom_line(color = "black", size = 1.2) +
geom_area(data = subset(tb, x >= 0.3 & x <= 0.6),
aes(x = x, y = y), fill = "lightblue", alpha = 0.5) +
labs(title = "Ejercicio 3.21 - f(x) = 1.5√x",
subtitle = "Área azul = P(0.3 < X < 0.6)",
x = "x", y = "f(x)") +
theme_bw()x <- seq(-0.2, 1.2, by = 0.001)
tb_F <- data.frame(x = x, y = F(x))
ggplot(tb_F, aes(x = x, y = y)) +
geom_line(color = "darkblue", size = 1.2) +
geom_point(aes(x = 0.3, y = F(0.3)), color = "red", size = 3) +
geom_point(aes(x = 0.6, y = F(0.6)), color = "red", size = 3) +
geom_segment(aes(x = 0.3, y = 0, xend = 0.3, yend = F(0.3)), linetype="dashed", color="gray") +
geom_segment(aes(x = 0.6, y = 0, xend = 0.6, yend = F(0.6)), linetype="dashed", color="gray") +
labs(title = "Ejercicio 3.21 - Función de distribución F(x)",
subtitle = "P(0.3 < X < 0.6) = F(0.6) - F(0.3)",
x = "x", y = "F(x)") +
theme_bw()## Warning in geom_point(aes(x = 0.3, y = F(0.3)), color = "red", size = 3): All aesthetics have length 1, but the data has 1401 rows.
## ℹ Please consider using `annotate()` or provide this layer with data containing
## a single row.
## Warning in geom_point(aes(x = 0.6, y = F(0.6)), color = "red", size = 3): All aesthetics have length 1, but the data has 1401 rows.
## ℹ Please consider using `annotate()` or provide this layer with data containing
## a single row.
## Warning in geom_segment(aes(x = 0.3, y = 0, xend = 0.3, yend = F(0.3)), : All aesthetics have length 1, but the data has 1401 rows.
## ℹ Please consider using `annotate()` or provide this layer with data containing
## a single row.
## Warning in geom_segment(aes(x = 0.6, y = 0, xend = 0.6, yend = F(0.6)), : All aesthetics have length 1, but the data has 1401 rows.
## ℹ Please consider using `annotate()` or provide this layer with data containing
## a single row.
creamos una función que integra f.x desde 0 hasta x (para x >= 0)
library(flextable)
# ---- Cálculo de F(x) por integral ----
F_from_integral <- function(x) {
sapply(x, function(t) {
if (t < 0) return(0)
integrate(f.x, lower = 0, upper = t)$value
})
}
# Puntos a evaluar
x_eval <- c(0, 500, 1000, 2000, 3000)
F_integral_vals <- F_from_integral(x_eval)
# ---- Cálculo analítico ----
F_analytic <- function(x) ifelse(x < 0, 0, 1 - exp(-x / 2000))
F_analytic_vals <- F_analytic(x_eval)
# ---- Tabla comparativa ----
tabla_F <- data.frame(
"x (horas)" = x_eval,
"F(x) por integral" = round(F_integral_vals, 6),
"F(x) analítica" = round(F_analytic_vals, 6),
"Diferencia" = round(abs(F_integral_vals - F_analytic_vals), 6)
)
# ---- Mostrar con formato ----
flextable(tabla_F) |>
set_caption("Comparación entre F(x) por integración numérica y forma analítica") |>
theme_vanilla() |>
color(color = "black", part = "all") |>
bg(bg = "#e6f0ff", part = "header") |>
align(align = "center", part = "all") |>
autofit()x..horas. | F.x..por.integral | F.x..analítica | Diferencia |
|---|---|---|---|
0 | 0.000000 | 0.000000 | 0 |
500 | 0.221199 | 0.221199 | 0 |
1,000 | 0.393469 | 0.393469 | 0 |
2,000 | 0.632121 | 0.632121 | 0 |
3,000 | 0.776870 | 0.776870 | 0 |
Usando la forma analítica: P(X > 1000) = 1 - F(1000) = exp(-1000/2000)
P_mas_1000 <- 1 - F_analytic(1000)
cat("P(X > 1000) =", round(P_mas_1000, 6), " (≈", round(P_mas_1000*100,2), "% )\n")## P(X > 1000) = 0.606531 (≈ 60.65 % )
P_menor_2000 <- F_analytic(2000)
cat("P(X < 2000) =", round(P_menor_2000, 6), " (≈", round(P_menor_2000*100,2), "% )\n")## P(X < 2000) = 0.632121 (≈ 63.21 % )
# Ejemplo extra: P(1000 < X < 3000)
P_entre_1000_3000 <- F_analytic(3000) - F_analytic(1000)
cat("P(1000 < X < 3000) =", round(P_entre_1000_3000, 6), "\n\n")## P(1000 < X < 3000) = 0.3834
library(ggplot2)
x_seq <- seq(0, 8000, by = 1)
df_den <- data.frame(x = x_seq, y = f.x(x_seq))
p_den <- ggplot(df_den, aes(x = x, y = y)) +
geom_line(color = "black", size = 1) +
geom_area(data = subset(df_den, x >= 0 & x <= 2000), aes(x = x, y = y),
fill = "lightblue", alpha = 0.4) +
geom_area(data = subset(df_den, x >= 1000 & x <= 3000), aes(x = x, y = y),
fill = "steelblue", alpha = 0.35) +
labs(title = "Ejercicio 3.27 — Densidad f(x) = (1/2000)e^{-x/2000}",
subtitle = "Área claro = P(X < 2000). Área oscuro = P(1000 < X < 3000)",
x = "Horas", y = "f(x)") +
theme_bw()
print(p_den)df_cdf <- data.frame(x = x_seq, y = F_analytic(x_seq))
p_cdf <- ggplot(df_cdf, aes(x = x, y = y)) +
geom_line(color = "darkblue", size = 1) +
geom_vline(xintercept = c(1000, 2000, 3000), linetype = "dashed", color = "gray40") +
geom_point(aes(x = 1000, y = F_analytic(1000)), color = "red", size = 2) +
geom_point(aes(x = 2000, y = F_analytic(2000)), color = "red", size = 2) +
geom_point(aes(x = 3000, y = F_analytic(3000)), color = "red", size = 2) +
annotate("text", x = 1000, y = F_analytic(1000)+0.03,
label = paste0("F(1000)=", round(F_analytic(1000),3)), size = 3) +
annotate("text", x = 2000, y = F_analytic(2000)+0.03,
label = paste0("F(2000)=", round(F_analytic(2000),3)), size = 3) +
annotate("text", x = 3000, y = F_analytic(3000)+0.03,
label = paste0("F(3000)=", round(F_analytic(3000),3)), size = 3) +
labs(title = "Ejercicio 3.27 — Función de distribución acumulada F(x)",
x = "Horas", y = "F(x)") +
theme_bw()
print(p_cdf)## Warning in geom_point(aes(x = 1000, y = F_analytic(1000)), color = "red", : All aesthetics have length 1, but the data has 8001 rows.
## ℹ Please consider using `annotate()` or provide this layer with data containing
## a single row.
## Warning in geom_point(aes(x = 2000, y = F_analytic(2000)), color = "red", : All aesthetics have length 1, but the data has 8001 rows.
## ℹ Please consider using `annotate()` or provide this layer with data containing
## a single row.
## Warning in geom_point(aes(x = 3000, y = F_analytic(3000)), color = "red", : All aesthetics have length 1, but the data has 8001 rows.
## ℹ Please consider using `annotate()` or provide this layer with data containing
## a single row.