CMPU-145, Spring 2013 Asmt. 6 Due: Wednesday, May 1 @ 1:30 p.m. ---------> This assignment involves a lot of parts. Make sure that you follow the directions carefully and that your definitions and interactions files LOOK GOOD! Every function should have a contract/comment section. Put at least one blank line between each function or variable definition. Comment your code appropriately (clear and concise is best). --------> Use the functions/stuff defined in asmt5-helper.txt and asmt6-helper.txt. ==> Be sure to download all of the files in the asmt6 directory. Then place the following expression in your definitions file: (load "asmt6-helper.txt") Have a look at this file. It defines some useful stuff for the second problem. It automatically loads "asmt5-helper.txt" and "asmt-helper.txt"; so you don't need to worry about them. Thus, you can use N-CHOOSE-K, TESTER, HEADER, PROBLEM, etc. ------------------------------------------------------------ PROBLEM 1: Tossing five dice and getting two pair! ------------------------------------------------------------ For this problem, you will compute the probability of tossing five dice and getting two pair. For example, if you toss five dice and get (3,3,4,5,4), then you have two pair; however, if you get (3,3,4,5,6), then you do not have two pair. NOTE: It turns out to be easier to approach this problem as if ORDER MATTERS. (And, since we're throwing dice, REPETITIONS are allowed.) Part A. Define a variable, called *NUM-WAYS-OF-GETTING-2-PAIR*, whose value is the number of ways of getting two pair in a sequence of five dice. As suggested by the above note, your answer should assume that ORDER MATTERS. Thus, (3,3,4,5,4) and (4,3,3,5,4) would be counted as two distinct ways of getting two pair in five dice. Next, define a variable, called *NUM-WAYS-OF-ROLLING-5-DICE*, whose value is the number of distinct SEQUENCES of 5 dice values. Again, ORDER MATTERS and REPETITION IS ALLOWED! Thus, (1,2,3,4,5) and (5,4,3,2,1) would be considered to be distinct sequences. HINT: Choose the dice values for the pairs first (analogous to choosing the "ranks" for the pairs like in a poker hand). Then figure out how many sequences those pairs can be fitted into (i.e., how many ways can you choose which slots will hold which dice values, being careful about *double-counting*). Finally, define a variable, called *PROB-GETTING-2-PAIR-IN-5-DICE*, whose value is the probability of getting two pair in five dice. Use TESTER expressions to display the values of these variables in the interactions window. Part B. Define a function, called ROLL-5-DICE, that takes no inputs. It should return as its output a list of length five, each of whose elements is a randomly chosen number from 1 to 6. HINT: Use the built-in RANDOM function---many times! Next, define a function, called HAS-2-PAIR?, that takes a list of five numbers as its only input. It should return #t if that list contains two pair (i.e., two numbers that each appear twice, and one number that appears only once). NOTE: You may wish to use GET-HISTY, SORT and REMOVE-ALL, as discussed previously. ------------------------------------------------------------ PROBLEM 2: Expected Winnings in Powerball. ------------------------------------------------------------ Recall that a ticket in Powerball consists of 5 numbers drawn from the set {1,2,...,59} where order doesn't matter and repetitions are not allowed; plus one number drawn from {1,2,...,35}. There are NINE different ways of winning in Powerball. In other words, there are NINE different winning events. Recall that each event is a subset of outcomes. In this case, each outcome is one of the possible powerball tickets. The "payoff function" for the winning events is as follows. MATCH PRIZE 5 + powerball $200,000,000 <--- grand prize 5 $ 1,000,000 4 + powerball $ 10,000 4 $ 100 3 + powerball $ 100 3 $ 7 2 + powerball $ 7 1 + powerball $ 4 powerball $ 4 NOTE: For example, "3 + powerball" means that exactly three of the first five numbers appeared among the first five numbers on the DRAWN ticket; and the powerball matched too. In contrast, "3" means that exactly three of the first five numbers appeared among the first five numbers on the DRAWN ticket, but the powerball did NOT match. NOTE: For this problem, we are assuming that if you win the grand prize, you will get $200,000,000. In reality, the payoff for the grand prize depends on the number of tickets sold. Part A. Store the payoff amounts for the above events in a list, as follows: (define *pb-payoffs* (200000000 1000000 10000 100 100 7 7 4 4)) Then use the TESTER-SHOW-LIST function, defined in "asmt6-helper.txt" to display the contents of the *pb-payoffs* list in the Interactions Window, as follows: (tester '*pb-payoffs*) Don't forget the QUOTE!! Part B. For each of the winning events, use the N-CHOOSE-K function to compute the exact number of tickets (i.e., outcomes) that belong to that event. For convenience, you may assume that the numbers on the DRAW TICKET (i.e., the winning ticket) are (1,2,3,4,5; 20). Next, store the numbers of tickets (i.e., outcomes) for the corresponding events in a list called *pb-num-outcomes*. Be sure that the first numbers in this list correspond to the payoff amounts in the *pb-payoffs* list. For example, the first number in the *pb-num-outcomes* list should correspond to the $200,000,000 prize. As above, use TESTER-SHOW-LIST to display the contents of *pb-num-outcomes* in the Interactions Window. Part C. Create a variable, called *pb-probabilities*, whose value is a list of the probabilities for the winning events described above. Be sure to multiply each number by 1.0 to force it to come out as a "floating point" number instead of some ugly fractions. ==> Hint: Use MAP and *pb-num-outcomes* Once again, use TESTER-SHOW-LIST to display the contents of this list in the interactions window. Part D. Next, compute the "odds" of each kind of winning event using the following expression: (define *pb-odds* (map (lambda (p) (round (/ 1.0 p))) *pb-probabilities*)) Once again, use TESTER-SHOW-LIST to display the contents of this list in the interactions window. NOTE: If the probability of some event E is p, then the "odds" of event E happening are "1 in X" where X = 1/p. Usually, the "X" is rounded, which explains the use of the built-in "round" function in the above expression. Part E. Now, use the *pb-payoffs* and *pb-probabilities* lists to compute the expected winnings from playing this version of Powerball (in which the Grand Prize is fixed at $200,000,000). Store that value in a variable called *pb-exp-winnings* and display its value using TESTER. HINT: Recall the definition of Expected Value seen in class (and discussed in Chapter 6 of the textbook). NOTE: A powerball ticket costs $2. Part F. What is the expected winnings in Powerball if there is no Grand Prize (i.e., if you get $0 for matching all 5 numbers plus the powerball)? Put the value in a variable called *pb-exp-winnings-no-grand-prize* and display its value using TESTER. HINT: Use (rest *pb-payoffs*) and (rest *pb-probabilities*). Part G. What is the probability of getting a NON-WINNING ticket (i.e., an outcome that does not belong to any of the winning events)? Put the value in a variable called *pb-no-winnings* and display its value using TESTER. HINT: This is EASY given what you have already computed. ------------------------------------------------------------------- PROBLEM 3: Estimating Expected Winnings in POWERBALL. ------------------------------------------------------------------- Part A. Define a function, called PB-WINNINGS, that takes as its only input, PB-TICKET, which is a list of 6 numbers representing a powerball ticket. NOTE: The last element of PB-TICKET is the powerball. PB-WINNINGS should compute the winnings for PB-TICKET assuming that the drawn numbers are 1,2,3,4,5; 20. NOTE: Use the payoffs in *pb-payoff*. ===> IMPORTANT HINT: Your function will be *MUCH* more efficient if you check the MOST PROBABLE events first. For example, start by checking whether the ticket is NOT a winner; then check whether it wins only $4; etc. (If you don't do it this way, your simulation in part B will go much slower.) Part B. Define a function, called ESTIMATE-PB-EXP-WINNINGS, that takes a single input, N, that is a non-negative integer. This function should run N random trials. For each trial, it should use GEN-RAND-PB-TICKET (defined in "asmt6-helper.txt") to generate a random powerball ticket, and PB-WINNINGS to compute the winnings for that ticket. It should accumulate the winnings from the N trials and then return the AVERAGE winnings as the output value. ==> See the ESTIMATE-BIASED-COUNTER-EXP-VALUE function defined in Lab 9 for guidance. It is very similar. Run ESTIMATE-PB-EXP-WINNINGS *several* times using N = 1000. Then run it *several* times using N = 10,000. Then run it a few times using N = 100,000. (Make sure you use TESTER so that your results appear in the Interactions Window.) Do the values approach the true expected value or do they jump around? ==> Explain why in a comment at the end of your definitions file. ======================================================= When you are done, electronically submit a directory containing your definitions and interactions files as usual. Also, turn in printouts of your two files. (You can put them in the box on the wall outside my office.) ===> Please do NOT print out any helper files!!