Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Mathlab. Help w/ Q.1.4. ( ignore Q.1.1.--it's only their for context.) 1. Estimating the weight of a coin One day, as you are walking across
Mathlab. Help w/ Q.1.4. (ignore Q.1.1.--it's only their for context.)
1. Estimating the weight of a coin One day, as you are walking across campus, a mysterious person appears before you. She offers you a simple gamble. If you pay me $5, I will flip a coin. If it lands on heads you lose, but if it lands on tails, I will pay you $15." You run a quick expected value calculation, assuming the coin is fair' (heads and tails are equally likely to occur), EV (coin toss) = P(heads)(-5) + P(tails)(10) = 0.5 ~ -5 +0.5 x 10 = -2.5 + 5 = 2.5, and so you decide to play. Because you get caught up in the game, you play five times. The results are as follows (heads = H, tails = T): HTHHH. You go back to your dorm, $10 poorer, and start thinking. Would it be possible that the game was unfair, in other words that the coin was 'weighted"? You decide to do a quick-and-dirty form of grid search, computing the likelihood of this sequence of outcomes for a range of degrees of the 'weight' of the coin. Question 1.1 (1 point): Define the parameter as the weight of the coin so that P(H) = 0 and P(T) = 1 -0. Fill out the table on the following page with the likelihoods of the sequence HTHHH for a range of weights (0 = 0.1, 0 = 0.3, 0 = 0.5, 0 = 0.7, 0 = 0.9). Recall that the likelihood is defined as: P(DC) = II;P(d|0) = P(01|0P(d20P(d310) ...P(d,10). If everything is correct, you have now discovered that your best estimate of 0 depends on the granularity of the range of parameter values that you computed P(DO). You can't help but shake the feeling that there must be an easy solution to this parameter estimation problem, but you also don't feel like manually computing likelihoods anymore. Therefore, you decide to first kill this fly with a sledgehammer called MATLAB. Open MATLAB. Create a new, clearly named, folder for all code for this section of the lab. First, create a vector that contain the coin flip results using the command window: >> data = [1 0 1 1 1]; So, in this vector each element stores one outcome (heads = 1, tails = 0), and these are ordered in the way in which they occurred. Question 1.4 (4 points): Create a new file with a clearly named, function that computes the likelihood of a sequence of flips (data) given a coin weight (theta): function L = lik_function_name (data, theta) % this is where the code goes, don't use this function's name! end You can implement this function in a variety of ways, for example by using logical indexing or a for loop to create a vector that reflect the probabilities of each outcome (0 and 1-0), and then using prod (look up what this function does with help prod). Alternatively, you could use the realization that the order in which the outcomes occurred does not matter, and so therefore you can write code that computes the likelihood as P(nh, n410) = Anh (1 - 0)". You can test whether your code works appropriately by checking it against the likelihoods you computed in Questions 1.1 and 1.3. That is, >> lik_function_name (data, 0.5) should produce the same output as your calculation of this value in the table in Question 1.1. With this code in hand, grid search becomes much easier to implement. To see this, we'll define a range of Os: >> thetas = 0:0.01:1 As you'll see, this produces a vector of thetas ranging from 0 to 1 in 0.01 increments. Now, let's calculate the likelihood of data for each element in theta using a for loop: >> for i = 1:length(thetas) L(i) = lik_function_name (data, thetas(i)); end 1. Estimating the weight of a coin One day, as you are walking across campus, a mysterious person appears before you. She offers you a simple gamble. If you pay me $5, I will flip a coin. If it lands on heads you lose, but if it lands on tails, I will pay you $15." You run a quick expected value calculation, assuming the coin is fair' (heads and tails are equally likely to occur), EV (coin toss) = P(heads)(-5) + P(tails)(10) = 0.5 ~ -5 +0.5 x 10 = -2.5 + 5 = 2.5, and so you decide to play. Because you get caught up in the game, you play five times. The results are as follows (heads = H, tails = T): HTHHH. You go back to your dorm, $10 poorer, and start thinking. Would it be possible that the game was unfair, in other words that the coin was 'weighted"? You decide to do a quick-and-dirty form of grid search, computing the likelihood of this sequence of outcomes for a range of degrees of the 'weight' of the coin. Question 1.1 (1 point): Define the parameter as the weight of the coin so that P(H) = 0 and P(T) = 1 -0. Fill out the table on the following page with the likelihoods of the sequence HTHHH for a range of weights (0 = 0.1, 0 = 0.3, 0 = 0.5, 0 = 0.7, 0 = 0.9). Recall that the likelihood is defined as: P(DC) = II;P(d|0) = P(01|0P(d20P(d310) ...P(d,10). If everything is correct, you have now discovered that your best estimate of 0 depends on the granularity of the range of parameter values that you computed P(DO). You can't help but shake the feeling that there must be an easy solution to this parameter estimation problem, but you also don't feel like manually computing likelihoods anymore. Therefore, you decide to first kill this fly with a sledgehammer called MATLAB. Open MATLAB. Create a new, clearly named, folder for all code for this section of the lab. First, create a vector that contain the coin flip results using the command window: >> data = [1 0 1 1 1]; So, in this vector each element stores one outcome (heads = 1, tails = 0), and these are ordered in the way in which they occurred. Question 1.4 (4 points): Create a new file with a clearly named, function that computes the likelihood of a sequence of flips (data) given a coin weight (theta): function L = lik_function_name (data, theta) % this is where the code goes, don't use this function's name! end You can implement this function in a variety of ways, for example by using logical indexing or a for loop to create a vector that reflect the probabilities of each outcome (0 and 1-0), and then using prod (look up what this function does with help prod). Alternatively, you could use the realization that the order in which the outcomes occurred does not matter, and so therefore you can write code that computes the likelihood as P(nh, n410) = Anh (1 - 0)". You can test whether your code works appropriately by checking it against the likelihoods you computed in Questions 1.1 and 1.3. That is, >> lik_function_name (data, 0.5) should produce the same output as your calculation of this value in the table in Question 1.1. With this code in hand, grid search becomes much easier to implement. To see this, we'll define a range of Os: >> thetas = 0:0.01:1 As you'll see, this produces a vector of thetas ranging from 0 to 1 in 0.01 increments. Now, let's calculate the likelihood of data for each element in theta using a for loop: >> for i = 1:length(thetas) L(i) = lik_function_name (data, thetas(i)); endStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started