CSC 4780/6780 Homework 05

$30.00 $24.00

What are we doing? Using Bayes’ Rule to make inferences based on evidence is crucial skill for data scientists. Bayes’ Rule lets us nd the best explanation for what we see, and lets us quantify our con dence in that explanation. And you get to write some code! Do inference Your friend has two pairs…

Rate this product

You’ll get a: zip file solution

 

Categorys:

Description

Rate this product
  • What are we doing?

Using Bayes’ Rule to make inferences based on evidence is crucial skill for data scientists. Bayes’ Rule lets us nd the best explanation for what we see, and lets us quantify our con dence in that explanation.

And you get to write some code!

  • Do inference

Your friend has two pairs of dice. She rolls the rst pair and notes their sum (But does not tell you what that sum is!) She repeatedly rolls the second pair and tells you if the new sum is higher than (H), lower than(L), or equal to (E) the rst pair. You are trying to guess what the sum of the rst pair of dice is.

Based on the squence of H, L, and E, you need to gure out the probability of each possible sum.

For example, if she tells you HHL, you would compute the following probabilities (to six places after the decimal point):

You will need to use Bayes’ Law, which says \The probability that the original sum was d given a sequence s is given by

P (djs) = P (sjd)P (d)

P (s)

To calculate P (s), you will use:

X

P (s) = P (sjd)P (d)

d2D

  • Criteria for success

If your name is Fred Jones, you will turn in a zip le called HW04 Jones Fred.zip of a directory called HW05 Jones Fred. It will contain:

  • dice.py

  • input.txt

  • output26.csv

  • output57.csv

Be sure to format your python code with black before you submit it.

We will run your code like this:

cd HW04_Jones_Fred

python3 dice.py 2 6 input.txt output26.csv python3 dice.py 5 7 input.txt output57.csv

Your program will read the input.txt. The game was played with 2 6-sided dice. Output your inferences. On the second run, same thing, but the game was played with 5 7-sided dice.

Do this work by yourself. Stackover ow is OK. A hint from another student is OK. Looking at another student’s code is not OK.

  • 6780 Only: Infer on long sequences using logs

If your dice.py is used for very long sequences of rolls, it will under ow the oating-point numbers on your computer. Try it:

> python3 dice.py 2 6 long_input.txt long_output26.csv

This probably results in:

input,P(d=2),P(d=3),P(d=4),P(d=5),P(d=6),P(d=7),P(d=8),P(d=9),P(d=10),P(d=11),P(d=12),guess HHEH…,nan,nan,nan,nan,nan,nan,nan,nan,nan,nan,nan,0 LLLL…,nan,nan,nan,nan,nan,nan,nan,nan,nan,nan,nan,0 LLLL…,nan,nan,nan,nan,nan,nan,nan,nan,nan,nan,nan,0

You are going to use logarithms to enable your code to deal with very long sequences like these.

Copy dice.py to long dice.py

Your lookup table will contain the log of the likelihoods. It will add those instead of multiplying them. At the end, just before dividing by the marginal likelihood, you will exponentiate them (ex) so that you get exactly the same results as the previous exercise. Before exponentiating them, you can add a large positive constant to all the log likelihooods.

Here is the code:

unnormed_posteriors = np.exp(unnormed_log_posteriors np.max(unnormalized_log_posteriors))

normalized_posteriors = unnormed_posteriors / np.sum(unnormed_posteriors)

You can test your code on long input.txt:

> python3 long_dice.py 2 6 long_input.txt long_output26.csv

It should look like this:

input,P(d=2),P(d=3),P(d=4),P(d=5),P(d=6),P(d=7),P(d=8),P(d=9),P(d=10),P(d=11),P(d=12),guess

HHEH…,0.00000,0.00000,0.00000,0.00000,1.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,6

LLLL…,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,0.00000,1.00000,0.00000,0.00000,10

(Yes, with long sequences you get pretty con dent.)

Your zip le should include:

  • long dice.py

  • long input.txt

  • long output26.csv

4

CSC 4780/6780 Homework 05
$30.00 $24.00