#################################################################################
# Charles B. Moss - cbmoss@ufl.edu                                              #
# ricardo.ifas.ufl.edu  Or www.charlesbmoss.com:8080                            #
#################################################################################
#                             Odds example                                      #
# Suppose we are interested in a bet that pays when a one occurs (i.e., a heads #
# but we are going to warp the coin). Suppose that a one occurs 30 percent of   #
# the time. How would we model this event?                                      #
#################################################################################

library(LaplacesDemon)
ndraw <- 100

#                                                                               #
# Let us draw the event ndraw times                                             #
#                                                                               #

x <- rbern(ndraw,0.30)

pay1 <- sum(x)
pay2 <- sum(1-x)

#                                                                               #
# The number of times that the bet will pay off is pay1 while the number of     #
# times the event will not pay off is pay2                                      #
#                                                                               #

print(cbind(pay1,pay2))

#                                                                               #
# The ratio of the number of times the bet pays over the number of times that   #
# the event does not pay is the odds ratio                                      #
#                                                                               #

print(pay1/pay2)

print(cbind("Theoretical odds ratio",.3/.7))

#                                                                               #
# Suppose that you bet the odds ratio - or you require a payoff of 1/odds ratio #
#   - Using the empirical payoff ratio                                          #
#                                                                               #

bet.Theory <- (.7/.3)*x-1*(1-x)
print(sum(bet.Theory))

#                                                                               #
#   - Using the observed payoff ratio                                           #
#                                                                               #

bet.Obser <- (pay2/pay1)*x-1*(1-x)
print(sum(bet.Obser))

#                                                                               #
#   - The difference is sampling theory                                     #
#                                                                               #

print(sum(pay2/pay1-.7/.3))

#                                                                               #
# Example of a subroutine or function                                           #
#                                                                               #

chuckBern <- function(z) {
  k <- runif(1)
  if(k <= z) z <- 1 else z <- 0
  return(z)
}

chuckBern(.3)

xChuck <- sapply(matrix(.3,nrow=ndraw,ncol=1),chuckBern)

pay3 <- sum(xChuck)
pay4 <- sum(1-xChuck)

print(pay3/pay4)
