library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.5
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ ggplot2   3.5.1     ✔ tibble    3.2.1
## ✔ lubridate 1.9.3     ✔ tidyr     1.3.1
## ✔ purrr     1.0.2     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(plyr)
## ------------------------------------------------------------------------------
## You have loaded plyr after dplyr - this is likely to cause problems.
## If you need functions from both plyr and dplyr, please load plyr first, then dplyr:
## library(plyr); library(dplyr)
## ------------------------------------------------------------------------------
## 
## Attaching package: 'plyr'
## 
## The following objects are masked from 'package:dplyr':
## 
##     arrange, count, desc, failwith, id, mutate, rename, summarise,
##     summarize
## 
## The following object is masked from 'package:purrr':
## 
##     compact

Experimento aleatorio

Lanzar dos monedas distinguibles y justas al aire

Espacio muestral o de resultados

\[ \Omega=\left\{(cara,cara),(sello,cara),(cara,sello),(sello,sello)\right\} \]

Omega <- expand.grid(c("cara","sello"),c("cara","sello"))
colnames(x=Omega) <- c("moneda1","moneda2")
Omega
##   moneda1 moneda2
## 1    cara    cara
## 2   sello    cara
## 3    cara   sello
## 4   sello   sello

Eventos

\(A:=\text{"La primera moneda es cara"}\)

A <- Omega[Omega[,1]=="cara",]
A
##   moneda1 moneda2
## 1    cara    cara
## 3    cara   sello

\(B:=\text{"La segunda moneda es cara"}\)

B <- Omega[Omega[,2]=="cara",]
B
##   moneda1 moneda2
## 1    cara    cara
## 2   sello    cara

Probabilidades

\(Prob(A)=\frac{\#(A)}{\#(\Omega)}\)

ProbA <- nrow(A)/nrow(Omega)
ProbA
## [1] 0.5

\(Prob(B)=\frac{\#(B)}{\#(\Omega)}\)

ProbB <- nrow(B)/nrow(Omega)
ProbB
## [1] 0.5

Intersección de eventos

\(A{\cap}B=\text{"La primera y última monedas son caras"}\)

AB <- Omega[Omega[,1]=="cara" & Omega[,2]=="cara",]
AB
##   moneda1 moneda2
## 1    cara    cara

Unión de eventos

\(A{\cup}B=\text{"La primera o última moneda es cara"}\)

AUB <- Omega[Omega[,1]=="cara" | Omega[,2]=="cara",]
AUB
##   moneda1 moneda2
## 1    cara    cara
## 2   sello    cara
## 3    cara   sello

Probabilidades

Propiedad: Los eventos \(A\) y \(B\) son independientes y por tanto

\[ Prob(A{\cap}B)=Prob(A){\times}Prob(B) \]

ProbAB <- nrow(AB)/nrow(Omega)
ProbAB
## [1] 0.25

Verifiquemos la propiedad

ProbAB==ProbA*ProbB
## [1] TRUE

Propiedad: Sean dos eventos \(A\) y \(B\) cualesquiera siempre se tiene que:

\[ Prob(A{\cup}B)=Prob(A){+}Prob(B)-Prob(A{\cap}B) \]

ProbAUB <- nrow(AUB)/nrow(Omega)
ProbAUB
## [1] 0.75

Verifiquemos la propiedad

ProbAUB==ProbA+ProbB-ProbAB
## [1] TRUE

Simulación de lanzamientos de monedas

\[ A{\cap}B:=\text{"Las dos monedas son caras"} \]

lanzamientos <- function(N){
  resultados <- matrix(data=NA,nrow=N,ncol=2)
  for(i in 1:N){
    resultados[i,] <- sample(x=c("cara","sello"),size=2,replace=TRUE)
  }
  RESULTADOS <- data.frame(moneda1=resultados[,1],moneda2=resultados[,2])
  ProbabilidadesD <- cumsum(RESULTADOS[,1]=="cara" & RESULTADOS[,2]=="cara")/1:N
  data.frame(Tiradas=1:N,Probabilidades=ProbabilidadesD) %>% 
    ggplot(mapping=aes(x=Tiradas,y=Probabilidades)) + 
    geom_point(size=1/3) + 
    geom_line(color=1:N) + 
    geom_hline(yintercept=0.25,linewidth=1/3,color="gray")
}
lanzamientos(N=10000)

rdply(
  .n=3,
  .expr=lanzamientos(N=100000)$data
) %>% 
  ggplot(
    mapping=aes(
      x=log(Tiradas),
      y=Probabilidades,
      color=factor(.n)
    )
  ) + 
  geom_hline(
    yintercept=0.25,
    color="darkgray"
  ) + 
  geom_line()