Question: I cant get the step _ e to work properly as seen in the photo def dice _ posterior ( sample _ draw: List [

I cant get the step_e to work properly as seen in the photo
def dice_posterior(sample_draw: List[int],
die_type_counts: Tuple[int],
dice: Tuple[Die])-> float:
"" "Calculates the posterior probability of a type 1 vs a type 2 die,
based on the number of times each face appears in the draw, and the
relative numbers of type 1 and type 2 dice in the bag, as well as the
face probabilities for type 1 and type 2 dice. The single number returned
is the posterior probability of the Type 1 die. Note: we expect a BagOfDice
object with only two dice.
*"=
Requiring only two dice with the sane number of faces simplifies the
problen a bit.
if len(dice)!=2:
raise ValueError('This code requires exactly 2 dice')
if dice[0].nun_faces != dice[1].num_faces:
raise ValueError('This code requires two dice with the same number of faces')
if len(sample_draw)!= dice[0].num_faces:
raise ValueError('The sample draw is a list of observed counts for the \
faces. Its length nust be equal to the number of faces \
on the dice.')
yOUR CODE hERE. You may want to use your safe_exponenate.
prior_die_1= die_type_counts[0]/ sum(die_type_counts)
prior_die_2= die_type_counts[1]/ sum(die_type_counts)
1ikelihood_die_1= np.prod([safe_exponentiate(dice[0].face_probs[i], sample_draw[i])
for 1 in range(dice[0].num_faces)])
likelihood_die_2= np.prod([safe_exponentiate(dice[1].face_probs[1], sample_draw[[1])
for 1 in range(dice[1],num_faces)])
return (likelihood_die_1* prior_die_1)/((likelihood_die_1* prior_die_1)
(1ikelihood_die_2* prior_die_2))
def e_step(experiment_data: List[NDArray[np.int_]],
bag_of_dice: BagofDice)-> NDArray:
-""Performs the Expectation Step of the EM algorithm for the dice problem.
Given a set of sample rolls and a current estimate of the bag of
dice parameters, this function computes the expected counts of how
many times each face of each die was rolled, based on the current
dice parameters.
:paran experiment_data: A list of numpy arrays. Each array has length equal
to the number of faces and records the number of times each face
was rolled for a given draw. The number of arrays is equal to the
number of draws in the data.|
:type experiment_data: list of numpy arrays with integer entries
:paran bag_of_dice: A BagofDice object. This object stores the
parameters of the dice in the bag.
:type bag_of_dice: BagOfDice
:return: An array that is the same length as the number of dice in the bag.
Each entry in the array is an array of floats which represent the
expected number of times each face was rolled for the corresponding
die.
:rtype: np.array of np.arrays of floats
-""
\ Initialize the expected counts object for each die
max_number_of_faces = max([len(die) for die in bag_of_dice.dice])
\ Initialize expected_counts to zero. It is a list of lists. The number
w}\mathrm{ of inner lists is equal to the number of dice and the length of each
*}\mathrm{ inner list is the number of faces of the die with the most faces.
expected_counts = np.zeros((len(bag_of_dice), max_number_of_faces))
\ Iterate over draws. For each draw, calculate the the posterior probability
that each die type was rolled on that draw by calling dice_posterior.
Then combine the posterior for each die type with the observed counts for
the current draw to get the expected counts for each die type on this dram.
To get the total expected counts for each type, you sum the expected
*}\mathrm{ counts for each type over all the draws.
PUT YOUR CODE HERE, FOLLOWING THE DIRECTIONS ABOVE
die_type_counts = np.array(bag_of_dice.die_priors)* len(experiment_data)
for dray in experiment_data:
posteriors =[
dice_posterior(draw, die_type_counts)
for die_idx in range(len(bag_of_dice))
]
for die_idx in range(len(bag_of_dice)):
expected_counts[die_idx]+= posteriors[die_idx]* draw
return expected_counts
I cant get the step _ e to work properly as seen

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!