C3_W2_Assignment Exercise 10

Thank you for your reply!
I wrote code to verify the 4th statement, below is my simple code:

def load_dice(n_sides, loaded_number):
    
    # All probabilities are initially the same
    probs = np.array([1/(n_sides+1) for _ in range(n_sides)])
    
    # Assign the loaded side a probability that is twice as the other ones
    probs[loaded_number-1] = 1 - sum(probs[:-1])
    
    # Check that all probabilities sum up to 1
    if not np.isclose(sum(probs), 1):
        print("All probabilities should add up to 1")
        return
    
    return probs 


n_sides = 6
for loaded_number in range(1,7):
    print(f"the loaded_number is: {loaded_number}")
    prob = load_dice(n_sides, loaded_number)
    # key: the sum result of two dice
    # value: the probability to the corresponding sum
    s_p_map = {num: 0 for num in range(2,13)}
    for i in range(6):
        for j in range(6):
            s = i + j + 2
            prob_s = prob[i] * prob[j]
            s_p_map[s] = s_p_map[s] + prob_s
    expectation = 0
    exp_square = 0
    
    for s,p in s_p_map.items():
        
        expectation += s * p
        exp_square += (s**2) * p

    print(f"expectation: {expectation}")
    # calculate the variance based on: var(X) = E(X^2) - (E(X))^2
    var_calculated = exp_square - expectation**2
    print(f"calculated variance: {var_calculated}")
    var = 0
    for s,p in s_p_map.items():
        var += ((s - expectation)**2) * p
    print(f"naive  variance: {var}")

the loaded side will have probability 2/7, and other sides will have probability 1/7, and this is the output:

the loaded_number is: 1
expectation: 6.2857142857142865
c variance: 6.530612244897952
  variance: 6.530612244897961
the loaded_number is: 2
expectation: 6.571428571428573
c variance: 5.551020408163247
  variance: 5.551020408163266
the loaded_number is: 3
expectation: 6.8571428571428585
c variance: 5.0612244897959116
  variance: 5.0612244897959195
the loaded_number is: 4
expectation: 7.142857142857146
c variance: 5.061224489795883
  variance: 5.061224489795919
the loaded_number is: 5
expectation: 7.42857142857143
c variance: 5.551020408163254
  variance: 5.551020408163266
the loaded_number is: 6
expectation: 7.714285714285717
c variance: 6.530612244897938
  variance: 6.53061224489796

it shows that 1 and 6 have same variance, 2 and 5, 3 and 4
basically I calculated the variance in two different ways:

Variance(X) = E(X^2) - (E(X))^2
Variance(X) = sum of [((X - E(X))^2) * prob]

this output really conflicts with all statements…

1 Like