Lanzar dos dados al aire y como resultado sumar las caras que caen mirando hacia arriba
\[ \Omega=\left\{(1,1),(1,2),(1,3),\ldots,(6,5),(6,6)\right\} \]
# Cargar ggplot2
library(ggplot2)
# Generar todas las combinaciones posibles de dos dados
dado1 <- rep(1:6, each = 6)
dado2 <- rep(1:6, times = 6)
# Crear un data frame con los resultados
resultados <- data.frame(dado1, dado2)
# Crear el gráfico de puntos usando ggplot2
ggplot(resultados, aes(x = dado1, y = dado2)) +
geom_point(color = "blue", size = 3) +
labs(title = "Resultados de lanzar dos dados",
x = "Dado 1",
y = "Dado 2") +
theme_minimal()
\[ X:(\Omega,\mathcal{A},\mathcal{P})\rightarrow(\mathbb{R},\mathcal{B}\left(\mathbb{R})\right) \]
\[ X:=\text{"La suma de las caras de los dados que quedan mirando hacia arriba"} \]
\[ X=2,3,4,\ldots,12 \]
# Cargar ggplot2
library(ggplot2)
# Generar todas las combinaciones posibles de dos dados
dado1 <- rep(1:6, each = 6)
dado2 <- rep(1:6, times = 6)
suma <- dado1 + dado2
# Crear un data frame con los resultados
resultados <- data.frame(dado1, dado2, suma)
# Crear el gráfico de puntos usando ggplot2
ggplot(resultados, aes(x = dado1, y = dado2, color = factor(suma))) +
geom_point(size = 4) +
scale_color_viridis_d() + # Paleta de colores para mejor visibilidad
labs(title = "Resultados de lanzar dos dados",
x = "Dado 1",
y = "Dado 2",
color = "Suma") +
theme_minimal() +
theme(
plot.title = element_text(hjust = 0.5, size = 16),
axis.title = element_text(size = 14),
legend.title = element_text(size = 12),
legend.text = element_text(size = 10)
)
\[ P(X:\Omega\rightarrow\mathbb{R}):\mathbb{R}\rightarrow[0,1] \]
\[ P(X{=}k):\mathbb{R}\rightarrow[0,1] \]
\[ \begin{aligned} f_X(k)&=Prob_{X}(X=k)\\ &=\frac{6-|7-k|}{36}I_{\left\{2,3,\ldots,12\right\}}(x) \end{aligned} \]
library(MASS)
ProbabilidadSumaDados <- function(X){
fractions(x=(6-abs(x=7-X))/36)
}
ProbabilidadSumaDados(X=7)
## [1] 1/6
ProbabilidadSumaDados(X=12)
## [1] 1/36
\[ 0{\leq}Prob_{X}(X=k){\leq}1 \]
ProbabilidadSumaDados(X=2:12)
## [1] 1/36 1/18 1/12 1/9 5/36 1/6 5/36 1/9 1/12 1/18 1/36
\[ Prob_{X}(A{\cup}B)=Prob_{X}(A)+Prob_{X}(B) \]
\[ A=\left\{2\right\}\text{ y }B=\left\{3\right\} \]
\[ A{\cap}B=\left\{\right\} \]
ProbAUB=ProbabilidadSumaDados(2)+ProbabilidadSumaDados(3)
ProbAUB
## [1] 1/12
\[ Prob_{X}(C{\cup}D)=Prob_{X}(C)+Prob_{X}(D)-Prob_{X}(C{\cap}D) \]
\[ C=\left\{2,4,6,8,10,12\right\}\text{ y }D=\left\{3,6,9,12\right\} \]
\[ C{\cap}D=\left\{6,12\right\} \]
C <- c(2,4,6,8,10,12)
D <- c(3,6,9,12)
CD <- c(6,12)
ProbCUD=sum(ProbabilidadSumaDados(C))+sum(ProbabilidadSumaDados(D))-sum(ProbabilidadSumaDados(CD))
ProbCUD
## [1] 2/3
\[ \sum_{\Omega}Prob_{X}(X=k)=1 \]
sum(ProbabilidadSumaDados(2:12))
## [1] 1
# Cargar los paquetes necesarios
library(ggplot2)
library(MASS)
# Definir la función ProbabilidadesSumaDados
ProbabilidadesSumaDados <- function() {
# Definir los valores de X (enteros entre 2 y 12)
X <- 2:12
# Calcular los valores de la función
Y <- (6 - abs(7 - X)) / 36
# Convertir los resultados a fracciones
Y_fractions <- fractions(Y)
# Crear un data frame con los valores
data <- data.frame(X = X, Y = Y, Y_fractions = as.character(Y_fractions))
# Crear el gráfico de barras con ggplot2
p <- ggplot(data, aes(x = factor(X), y = Y, fill = as.factor(X))) +
geom_bar(stat = "identity", color = "black") +
labs(title = "Probabilidades de la Suma de Dos Dados",
x = "Suma de los Dados",
y = "Probabilidad",
fill = "X=Suma") +
theme_minimal() +
theme(
plot.title = element_text(hjust = 0.5, size = 16),
axis.title = element_text(size = 14),
legend.title = element_text(size = 12),
legend.text = element_text(size = 10)
)
# Imprimir el gráfico
print(p)
}
# Llamar a la función
ProbabilidadesSumaDados()
\[ P(X{\leq}k):\mathbb{R}\rightarrow[0,1] \]
\[ \begin{aligned} F_X(k)&=Prob_{X}(X{\leq}k)\\ &=\sum_{x=2}^{k}\frac{6-|7-x|}{36}I_{\left\{2,3,\ldots,12\right\}}(x) \end{aligned} \]
ProbabilidadAcumuladaSumaDados <- function(X){
sum(ProbabilidadSumaDados(2:X))
}
\[ \begin{aligned} F_X(2)&=Prob_{X}(X{\leq}2)\\ &=\sum_{x=2}^{2}\frac{6-|7-x|}{36}I_{\left\{2,3,\ldots,12\right\}}(x)\\ &=\frac{6-|7-2|}{36}\\ &=\frac{6-|5|}{36}\\ &=\frac{6-5}{36}\\ &=\frac{1}{36}\\ \end{aligned} \]
ProbabilidadAcumuladaSumaDados(X=2)
## [1] 1/36
\[ \begin{aligned} F_X(3)&=Prob_{X}(X{\leq}3)\\ &=\sum_{x=2}^{3}\frac{6-|7-x|}{36}I_{\left\{2,3,\ldots,12\right\}}(x)\\ &=\frac{6-|7-2|}{36}+\frac{6-|7-3|}{36}\\ &=\frac{6-|5|}{36}+\frac{6-|4|}{36}\\ &=\frac{6-5}{36}+\frac{6-4}{36}\\ &=\frac{1}{36}+\frac{2}{36}\\ &=\frac{1+2}{36}\\ &=\frac{3}{36}\\ &=\frac{1}{12} \end{aligned} \]
ProbabilidadAcumuladaSumaDados(X=3)
## [1] 1/12
\[ \begin{aligned} F_X(12)&=Prob_{X}(X{\leq}12)\\ &=1 \end{aligned} \]
ProbabilidadAcumuladaSumaDados(X=12)
## [1] 1
# Cargar los paquetes necesarios
library(ggplot2)
library(MASS)
# Definir la función ProbabilidadesSumaDados
ProbabilidadesAcumuladasSumaDados <- function() {
# Definir los valores de X (enteros entre 2 y 12)
X <- 2:12
# Calcular los valores de la función
Y <- (6 - abs(7 - X)) / 36
# Convertir los resultados a fracciones
Y_fractions <- fractions(cumsum(Y))
# Crear un data frame con los valores
data <- data.frame(X = X, Y = cumsum(Y), Y_fractions = as.character(cumsum(Y_fractions)))
# Crear el gráfico de barras con ggplot2
p <- ggplot(data, aes(x = factor(X), y = Y, fill = as.factor(X))) +
geom_bar(stat = "identity", color = "black") +
labs(title = "Probabilidades de la Suma Acumulada de Dos Dados",
x = "Suma Acumulada de los Dados",
y = "Probabilidad",
fill = "X = Suma Acumulada") +
theme_minimal() +
theme(
plot.title = element_text(hjust = 0.5, size = 16),
axis.title = element_text(size = 14),
legend.title = element_text(size = 12),
legend.text = element_text(size = 10)
)
# Imprimir el gráfico
print(p)
}
# Llamar a la función
ProbabilidadesAcumuladasSumaDados()
\[ \begin{aligned} \mu&=E[X]\\ &=\sum_{X{\in}\left\{2,3,\ldots,12\right\}}X{\cdot}Prob_{X}(X=x)\\ &=2{\cdot}\frac{6-|7-2|}{36}+3{\cdot}\frac{6-|7-3|}{36}+\cdots+12{\cdot}\frac{6-|7-12|}{36}\\ &=7 \end{aligned} \]
ValorEsperadoSumaDados <- function(){
X=2:12
Y=fractions(x=(6-abs(x=7-X))/36)
sum(X*Y)
}
ValorEsperadoSumaDados()
## [1] 7
\[ \begin{aligned} \sigma^2&=Var[X]\\ &=\sum_{X{\in}\left\{2,3,\ldots,12\right\}}(X-E[X])^2{\cdot}Prob_{X}(X=x)\\ &=(2-7)^2{\cdot}\frac{6-|7-2|}{36}+(3-7)^2{\cdot}\frac{6-|7-3|}{36}+\cdots+(12-7)^2{\cdot}\frac{6-|7-12|}{36}\\ &=\frac{35}{6} \end{aligned} \]
VarianzaSumaDados <- function(){
X=2:12
Z=(X-ValorEsperadoSumaDados())**2
Y=fractions(x=(6-abs(x=7-X))/36)
sum(Z*Y)
}
VarianzaSumaDados()
## [1] 35/6
Nota: No interpretable al ser un promedio de desvíos o distancias a la media elevados al cuadradso; la solución es tomar la raíz cuadrada positiva desviación estándar o típica
\[ \sigma=\sqrt{Var[X]} \]
round(sqrt(VarianzaSumaDados()),0)
## [1] 2