MSE XXXXXXXXXXExercise 4 Introduction to Bayes Rule with Applications Week of February 11th, 2019 This week the structure of the lab activity will be a little different instead of some reading...

1 answer below »
It is about doing statistics in matlab



MSE 2321 - Exercise 4 Introduction to Bayes Rule with Applications Week of February 11th, 2019 This week the structure of the lab activity will be a little different instead of some reading followed by questions, the questions will be in the reading. Hopefully that will let you follow along and guide you a little easier. Exercise #1 Conditional Probabilities Let's introduce the notion of conditional probabiltiies with an example: Eddy current inspection is a common non-destrictive inspection (NDI) to look for cracks in aircraft components (https://en.wikipedia.org/wiki/Eddy-current_testing). Let's envision the following scenario. A bracket on a military aircraft, made of a 5xxx series Al alloy, is known to be susceptiable to cracking in- serivice. A service bulletin was issued mandating that the brackets should be removed and inspected for cracks. Based on data from failed parts it is estimated (not a guess a real eningeering estimate based on data) that 1% of the brackets contain cracks that require removal from service. The eddy current test is known to be 90% accurate when detecting cracks. That means, in parts with a crack, the test detects the crack 90% of the time and misses the crack 10% of the time. Unfortunatley the test also has a 8% false positive rate. That means that in parts without cracks 92% of the time the test will be clean but 8% of safe brackets (with no cracks) will fail the test. Q1.1 Before the eddy current test was performed, what was the best estimate of the probability that the part had a crack? After we perform the test we have new information. The interesting question is "What is the probability that a part that fails the test actually has a crack?" Q1.2 Try to reason the question out. Come up with an answer and describe your process. Do not change it if it turns out you are wrong later. You will be graded on your write-up and how you describe your reasoning not your final answer (or wrong methods/logic). Ok let's reason it out. Let's assume a population of 1,000 parts. 99% of that population does not have a crack and 1% does have a crack. • ????? parts with no crack & ???? parts with crack 1 • Out of the 990 parts with no crack 8% will fail the test • ????? parts fail but no crack. • Out of the 10 parts with a crack 90% will fail the test • ?????? parts fail with crack • total # of parts that fail test = ?????????? • Probability that a part that fails has a crack Q1.3 What is the probability that a part that fails the test has a crack? What is the probability that a part that passes the test has a crack? Q1.4 If 1,000 parts are tested how many are expected to be removed from service even though they don't have a crack (because of false positives)? This is an example of the application of conditional probabilities: The probabilty of event A occuring given that event B already happened. We denote this In the example above we were interested in We understand intuitively that the likelihood of the two events are linked, the probability that the part had a crack is different for parts that passed vs parts that failed the test. The formal definition of conditional probability is given as is the intersection of A and B. For our example Awas the probability the part had a crack and B was that the part failed the test. The intersection of A and B is all of the parts that are both cracked and failed the test. This shouldn't be confused with the union of A and B or which is the set of parts that are cracked or failed the test. Q1.5 Determine the following probabilities and . So the conditional probability is the probability of both A and B divided by the total probability of B. In our example above we end back with exactly like we worked out by logic. 2 Exercis #2 Bayes' Rule Bayes' rule is a convenient mathematical formulation to deal with conditional probabilities. It stems form the observation that with some re-arranging we get So why is this handy? Well a lot of time is unknown or hard to estimate but we can very easily compute . In the example above the was unknown but was given to us. Q2.1 Use the defintiion of conditional probabilities to prove Bayes' rule. Q2.2 Let's consider the example of eddy current testing for cracked parts but let's play with the numbers a bit. Write a function that takes 1) The overal fraction of parts with cracks, 2) the accuracy of th eddy current test and 3) the rate of false positives in as inputs and returns the . Q2.3 Assume the rate of fale positives is fixed at 8%. Write a program to 1) Systematically vare the overall fraction of parts with cracks from 0% to 10% and the accuracy of the eddy current test from 50% to 100%. and 2) Record the for each case. Vary the overall fraction of parts with crack in `% increments, and vary the accuracy of eddy current tests in 5% increments. You will have a for loop over the accuracy and inside that loop you will have a second loop for the fraction of parts with cracks, Q2.4 Plot the results of your analysis as a surface. Use 'doc surface' to get the syntax and see examples. Exercise #3 Updating our hypthesis with Bayes' rule Let's put a different interpretation to the above equation. Let A represent some hypothesis and B represents some experimental observation of data that can potentially either support or refute the hypothesis. Let's consider the case of a coin flip. We want to know if the coin is fair or biased. A "fair" coin is one that has equal probability of landing of heads or tails when flipped. A is our hypthesis in this case . B is the data or observation it is just outcomes of coin tosses. Intuitively we know if we flip it 10 times and it comes up heads 9 times and tails only once the coin might not be fair. Bayes's rules let's us assign a probability to our hypothesis. We can ask "What is the probability of the coin being fair given that I just flipped it 10 times and got 9 heads?." What about if we flipped it 100 times and got 60 heads and 40 tails? How strong is the evidence that it is fair? is called the prior probability. It is the probabilty of our hypothesis before we do any testing. It is what we know (or think we know) about our sytem before we make any observations. Suppose we pulled the coin out of our pocked and had no reason to believe it is biased. We would ascribe a high prior probability to the coin being fair and a low prior probability of the coin being unfair. 3 is called the likelihood. It tells us how likely we are to observe the data given our hypothesis. The probability of getting Nheads in M throws for a fair coin is The probability of getting 9 heads in 10 throws given iour hyopothesis that the coin is fair (the likelihood) is 0.0098. is called the posterior probability. It is our updated probability of our hypothesis being correct after seeing the data. Notice we left out .This is called the evidence. It is the total probability of getting 9 heads out of tails on an arbitrary coin. To calculate this we would have to integrate over every possible way a coin can be biased. Often the evidence is very difficult to calculate or estimate. Luckily we don't actually need it. We have a trick to get rid of it. Notice that doesn't depend on the hypothesis A doesn't appear in this term. It is a constant for all hypothesis. It is a normalization constant to make sure the probabilities over all possible hypothesis sum to 1 (unity). For this example our hypothesis is the coin is fair. The counter hypothesis is the coin is biased. We know that . Here is programming shorthand for "not A." is simply a normalization constant that makes sure everything sums up to 1. We can also eliminate the evidence by considering the ratio of our hypothesis and its compliment. We want to know how much more likely is a fair coin to an unfair coin and viola the evidence term is divided away. Ok for this exercise we are going to look at how many coin flips it takes to determine the probability of a biased coin. The following function returns the result of coin flip for a biased coin. function outcome=biased_flip(bias) %function takes a coin bias in as input %bias is defined as the probability of flipping the coin and getting heads %P('heads')=bias %a bias of 0 would always return tails %a bias of 1 would always return heads %outcome is the output of the function % outcome = 1 if the flip is heads or % outcome = 0 if the flip is tails test_number=rand; if test_number<=bias outcome=1; else outcome=0; 4 end end let's pretend we have a coin that has a bias of 0.2. meaning the . let's also pretend we don't know this fact. i wan't to know our best guess at the bias after 1 flip, 10 flips, 100 flips, 1000 flips. how many flips de we need to observe to get ? in other words how many times do we have to flip until we are 90% sure that our bias is 0.2 (note this is not a rigoruous statement. may the statistics gods forgive my tradeoff of exactness for the sake of conveying the idea). here is the general procedure we are going to follow 1. assume we know nothing. we don't know whether the coin is fair or not. so we decide all possible biases have equal probabilty. this is called a "flat" prior. 2. flip the coin and make a single observation. 3. calculate our posterior probability of the bias given the outcome of the coin flip. 4. we have learned a little bit from our first flip but not very much. after all how do we tell if the coin is biased from only one toss. we are going to have to do more tosses. 5. for the second toss we want to incorporate what we already know from the first. so our posterior probability from the first toss becomes the prior probability for the second toss. 6. repeat 2-5 each time taking the previous posterior probability to be the new prior. in order to make this outcome="1;" else="" outcome="0;" 4="" end="" end="" let's="" pretend="" we="" have="" a="" coin="" that="" has="" a="" bias="" of="" 0.2.="" meaning="" the="" .="" let's="" also="" pretend="" we="" don't="" know="" this="" fact.="" i="" wan't="" to="" know="" our="" best="" guess="" at="" the="" bias="" after="" 1="" flip,="" 10="" flips,="" 100="" flips,="" 1000="" flips.="" how="" many="" flips="" de="" we="" need="" to="" observe="" to="" get="" in="" other="" words="" how="" many="" times="" do="" we="" have="" to="" flip="" until="" we="" are="" 90%="" sure="" that="" our="" bias="" is="" 0.2="" (note="" this="" is="" not="" a="" rigoruous="" statement.="" may="" the="" statistics="" gods="" forgive="" my="" tradeoff="" of="" exactness="" for="" the="" sake="" of="" conveying="" the="" idea).="" here="" is="" the="" general="" procedure="" we="" are="" going="" to="" follow="" 1.="" assume="" we="" know="" nothing.="" we="" don't="" know="" whether="" the="" coin="" is="" fair="" or="" not.="" so="" we="" decide="" all="" possible="" biases="" have="" equal="" probabilty.="" this="" is="" called="" a="" "flat"="" prior.="" 2.="" flip="" the="" coin="" and="" make="" a="" single="" observation.="" 3.="" calculate="" our="" posterior="" probability="" of="" the="" bias="" given="" the="" outcome="" of="" the="" coin="" flip.="" 4.="" we="" have="" learned="" a="" little="" bit="" from="" our="" first="" flip="" but="" not="" very="" much.="" after="" all="" how="" do="" we="" tell="" if="" the="" coin="" is="" biased="" from="" only="" one="" toss.="" we="" are="" going="" to="" have="" to="" do="" more="" tosses.="" 5.="" for="" the="" second="" toss="" we="" want="" to="" incorporate="" what="" we="" already="" know="" from="" the="" first.="" so="" our="" posterior="" probability="" from="" the="" first="" toss="" becomes="" the="" prior="" probability="" for="" the="" second="" toss.="" 6.="" repeat="" 2-5="" each="" time="" taking="" the="" previous="" posterior="" probability="" to="" be="" the="" new="" prior.="" in="" order="" to="" make="">
Answered Same DayFeb 19, 2021

Answer To: MSE XXXXXXXXXXExercise 4 Introduction to Bayes Rule with Applications Week of February 11th, 2019...

Abr Writing answered on Feb 20 2021
137 Votes
MSE 2321 - Exercise 4 Introduction to Bayes Rule with Applications
Exercise #1 Conditional Probabilities
Q1.1
Before the eddy current test was performed, the best estimate of the probability that the part had a crack is 0.01 or 1%.
Q1.2
An interesting question is "What is the probability that a part that fails the test actually has a crack?". In terms of probability, we can write the objective as t
hat is we know that the part has failed the eddy current test then what is the probability that there was actually a crack in the part? From the real engineering estimates based on data, the probability of such an conditional event can be calculated as follows.
Let's assume a population of 1,000 parts. 99% of that population does not have a crack and 1% does have a
crack.
• 990 parts with no crack & 10 parts with crack
• Out of the 990 parts with no crack 8% will fail the test
• ~79 (79.2) parts fail but no crack.
• Out of the 10 parts with a crack 90% will fail the test
• 9 part fail with crack
• total # of parts that fail test = 88
• Probability that a part that fails has a crack
Q1.3
Now, we need to find
Let's assume a population of 1,000 parts. 99% of that population does not have a crack and 1% does have a
crack.
• 990 parts with no crack & 10 parts with crack
• Out of the 990 parts with no crack 92% will pass the test
• ~911 (910.8) parts pass but no crack.
• Out of the 10 parts with a crack 10% will pass the test
• 1 part pass with crack
• total # of parts that pass test = 912
• Probability that a part that pass has a crack
Q1.4
Ww know that out od 1000 parts, 990 parts don't have a crack. But because of false positives (8%), parts will be removed from the service even though they don't have a crack.
Q1.5
The probability means that the probability that the part has failed and has crack as well. In the 1000 parts case, we have 10 parts that have a crack and out of these 10 parts 90% will fail the eddy current test. Therefore, the probability is equal to . This can also be calculated using the formal definition of conditional probability:
The probability means that the probability that the part atleast has a crack or has failed the eddy current test.
· Out of 990 parts with no crack, 79 will fail the test
· All the 10 parts that have crack will be under this condition
Therefore,
Exercise #2 Bayes' Rule
Q2.1
The formal definition of conditional probability is given as
Which can also be written as:
On equating the two equations above, we get:
Q2.2
Check the q2_2 function for more details.
Q2.3
rate_false_positives = 0.08;
cracked_fractions = linspace(0, 10)/100;
test_accuracies = linspace(50,100)/100;
surf = @(x,y) q2_2(x, y, 0.08);
Q2.4
fsurf(surf, ...
[min(cracked_fractions), ...
max(cracked_fractions), ...
min(test_accuracies), ...
max(test_accuracies)]);
xlabel('Overall fraction of parts with cracks');
ylabel('Accuracy of the Eddy Current test');
zlabel('P(cracked|failed)');
Exercise #3 Updating our hypthesis with Bayes' rule
Q3.1
%Posterior update biased coin example
clear;
close;
visualize_each_flip=0; %set to zero to only see posterior probability
Distribution for only the final flip. Set to 1 to update the graph each flip. This can take a long time if you are flipping more than 10000 times.
num_flips=50; %this is the number of flips we are going to do.
bias=0.2; %this is the bias of our coin. Do not change keep at 0.2
Initialize a list of possible bias values. We want our bias to go from 0 (alwaysflip tails) to 1 (always flip heads) and everywhere in between. We will consider bias values in 1% (0.01 probability) increments.
bias_values=0:0.01:1;
Initialize our prior probabilities. Since we don't know anything about the coin. Let's start by assuming that all possible bias values are equally likely. This is called a flat prior.
bias_priors=ones(1,101);
The priors are probabilties. That means they need to sum to 1 according to the definition of probability (The probability of the intersection of all...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here