Expected Value for Discrete Random Variables

If the discrete random variable X can take values x1,x2,x3,…, then the expected value is defined as

E(x) = \[\sum_{i=1} x_{i} * P(X=x_{i})\] where xi is the outcome and P(X=xi) is the probability.

The expected value or mean of a random variable is the center of its distribution.

Com = function(n, x) {
  factorial(n) / factorial(n-x) / factorial(x)
}

Question 1.

A quiz consists of three multiple choice questions with choices (a) and (b) for each. An unprepared student marks answers at random. Let Y be the random variable representing the number of correct answers on the quiz. What is E(Y)?

nloop=10000
count1=0
for (iloop in 1:nloop){
  mc=c()
  for(i in 1:3){
    mchoice= sample(0:1,1,replace=T)
    if(mchoice==1){
      count1=count1+1
     }
  }
   mc=c(mc,count1)
}
mean(mc)/nloop
## [1] 1.503

Since there are three questions, these are the possibilities: P(Y=0), P(Y=1), P(Y=2), P(Y=3)

E(Y)= y0 * P(Y=0) + y1 * P(Y=1)+ y2 * P(Y=2) + y3 * P(Y=3)

(E= 0 *Com(3,0)*(1/2)^3 + 1 * Com(3,1)*(1/2)^1*(1/2)^2 + 2 * Com(3,2)*(1/2)^2*(1/2)^1 + 3 * Com(3,3)*(1/2)^3)
## [1] 1.5
## [1] "The expected correct answer for 3 multiple questions with 2 choices is 1.5"



Question 2.

In an American roulette, there are 18 red slots, 18 black slots, and 2 green slots. If you bet 100 dollars on red, you will win 100 dollars if the marble drops in a red slot at random; otherwise you will lose $100. Calculate your expected winnings.

nloop=100000
money=0
for (iloop in i:nloop){
  roulette = sample(0:1,1,replace=T, prob=c(20/38,18/38))
  if(roulette==0){
    money=money-100
  }
  else{
    money=money+100
  }
}
money/nloop
## [1] -5.468

Since there are two possibilities: P(Y=0), P(Y=1)

E(Y)= ybg * P(Y=BG) + yr* P(Y=R)

(E= -100 * (20/38) + 100 * (18/38) )
## [1] -5.263158
## [1] "The expected wining is -5.26 dollars"



Question 3.

Bag A has three blue balls and five yellow balls. Bag B has four blue balls and two yellow balls. A game consists of two stages. First, you toss a fair coin; if heads you get bag A, if tails you get bag B. You (randomly) choose a ball from the bag. You win 100 dollars for each blue ball you draw and lose 80 dollars for a yellow ball. What is the expected amount of money that you win?

nloop =100000
moneytot= c()
coin = c("H","T")
bag1= c(c(rep("B",3),rep("Y",5)))
bag2= c(c(rep("B",4),rep("Y",2)))
for (iloop in 1:nloop){
  money=0
  sam = sample(coin ,1)
  sam1 = sample(bag1,1)
  sam2 = sample(bag2,1)
  if( (sam=="H" & sam1=="B") | (sam=="T" & sam2=="B")){
    money = money+100
  }
  else{
    money = money-80
  }
  moneytot= c(moneytot,money)
  }
mean(moneytot)
## [1] 13.942

Since there are two possibilities:

E(Y)= yL * P(Y=L) + yW * P(Y=W) where yL =-80 and yW= 100

(Ex_Win= -80 * (1/2)*((5/8)+(2/6)) + 100 * (1/2)*((3/8)+(4/6)))
## [1] 13.75
## [1] "The expected wining is 13.75 dollars"



Question 4.

You have 100 dollars and play the following game. An urn contains two white balls and three black balls. You draw the balls out one at a time without replacement until all the balls are gone. On each draw, you bet half of your present fortune that you will draw a white ball. What is your expected final fortune? Use simulations in R.

m =100
count= c("W","W","B","B", "B")
for (iloop in 1:length(count)){
  len= length(count)
  sam= sample(count,1, replace = T)
  if(sam=="B"){(m=m-m/2) ; (count=count[-len]) }
  else        {(m=m+m/2) ; (count=count[-1]) }
  print(count)
  
}
## [1] "W" "W" "B" "B"
## [1] "W" "W" "B"
## [1] "W" "B"
## [1] "B"
## character(0)
print(m)
## [1] 28.125



Question 5.

You will roll a fair, two six-sided dice until a six appears. If the sum is greater than 12, he pays you 10 dollars. Otherwise, you pay him 10 dollars. Using simulations in R, determine your expected profit from the game.

profit <-c()
nloop=10000
for (iloop in 1:nloop){
  money= 0
  sam=0
  while(!(6 %in% sam)){
    sam = sample(1:6,2,replace=T)
    if (sum(sam)>12){
      money =money+10
    }
    else{
      money= money-10
    }
    profit <- c(profit,money)
  }
}
mean(profit)
## [1] -32.55795
## [1] "If you play, by average, you will lose -32.56 dollars"



Question 6.

There are two blue balls and twelve yellow balls in an urn. You (randomly) choose a ball from the bag (without replacement). You will win 30 dollars if you pick blue ball or lose 10 dollars if you pick yellow ball. The game will stop when you choose a blue ball. What is the expected profit from the game?

profit <-c(); nloop=10000
for (iloop in 1:nloop){
  money= 0;
  balls = c(rep("B",2),rep("Y",12))
  sam = sample(balls,1,replace=F)
  repeat{ 
    sam = sample(balls,1,replace=F)
    len = length(balls)
    if(sam =="B"){
      money =money+40
      break
    }
    else {
      money = money-10
      balls=balls[-len]
    }
  }
  profit <- c(profit,money)
}
mean(profit)
## [1] 0.068
## [1] "If you play, by average, you will win 0.07 dollars"



Question 7.

A multiple choice exam is given. A problem has four possible answers, and exactly one answer is correct. The student is allowed to choose a subset of the four possible answers as his answer. If his chosen subset contains the correct answer, the student receives three points, but he loses one point for each wrong answer in his chosen subset.

Find the expected score using simulation.

(E_X = Com(4,4)/Com(4,4)*(3-3) + Com(3,2)/Com(4,3)*(3-2) + Com(3,3)/Com(4,3)*(0-3) + Com(3,1)/Com(4,2)*(3-1) + Com(3,2)/Com(4,2)*(0-2) + Com(3,0)/Com(4,1)*(3-0) + Com(3,1)/Com(4,1)*(0-1))
## [1] 0
## [1] "The expected score is 0"
nloop=100000
answer = c("C",rep("W",3))
totalpoint <-c()
for (iloop in 1:nloop){
  sub1= sample(1:4,1)
  subset = sample(answer,sub1,replace=F)
  len = length(subset)
  point= 0;
  if ("C" %in% subset){
    point = point+3-len+1
  } 
  else {
    point = point - length(subset)
  }
  totalpoint <- c(totalpoint,point)
}
mean(totalpoint)
## [1] 0.0019



Question 8.

It has been said that a Dr. B. Muriel Bristol declined a cup of tea stating that she preferred a cup into which milk had been poured first. The famous statistician R. A. Fisher carried out a test to see if she could tell whether milk was put in before or after the tea. Assume that for the test Dr. Bristol was given eight cups of tea—four in which the milk was put in before the tea and four in which the milk was put in after the tea.

For more info: https://www.irishtimes.com/news/science/how-a-tea-tasting-test-led-to-a-breakthrough-in-statistics-1.3998786

(a) What is the expected number of correct guesses the lady would make if she had no information after each test and was just guessing?

nloop= 100000
tea_arr =c()
tea = c(c(rep("T",4)),c(rep("M",4)))
for (iloop in 1:nloop){
  count=0
  mix   = sample(tea,8, replace=F)
  guess = sample(tea,8, replace=F)
  for (i in 1:8){
    if(mix[i]==guess[i]){
      count =  count+1
    }
  }
  tea_arr = c(tea_arr,count)
}
mean(tea_arr)
## [1] 3.99696

(b) Find the expected number of correct guesses if she was told the result of each guess and used an optimal guessing strategy.

# By using this formula:
# E(x) = S + Summation (x_to_C)(1/2)*Com(S,x)*Com(C,x)/Com(S+C,2x) where x=1, S=4 and C=4 here.
(E_X = 4+ 1/2*( (Com(4,1)*Com(4,1)/Com(8,2))+(Com(4,2)*Com(4,2)/Com(8,4))+(Com(4,3)*Com(4,3)/Com(8,6))+(Com(4,4)*Com(4,4)/Com(8,8)) ))
## [1] 5.328571
## [1] "The expected score is 5 2"
nloop= 10000
tea_arr =c()
for (iloop in 1:nloop){
  count=0
  tea = c(c(rep("T",4)),c(rep("M",4)))
   for (i in 1:8){
     guess = sample(tea,1, replace=F)
     mix   = sample(tea,1, replace=F)
     if(mix==guess){
       count=count+1
     }
     tea=tea[-((which(tea==mix))[1])]
   }
  tea_arr = c(tea_arr,count)
}
mean(tea_arr)
## [1] 4.9956




*******************