CS 221 Assignment 3 Fall 2015: Plot the chance (0-100%) on the y axis that you h
ID: 3764843 • Letter: C
Question
CS 221 Assignment 3 Fall 2015: Plot the chance (0-100%) on the y axis that you have the disease, given that you have a positive result for a test for the disease, vs. (the x axis) the accuracy of the test from 80-100%.
Summary of the definition of the terms:
Pr(A|X): The chance that you have the disease, and had a positive test result for the disease
Pr(X|A): The accuracy of the test (0-100%)– if it gives a positive result, you have the disease
Pr(A): The percentage of the population that have the disease
Pr(X|not A): False positive, the percentage of the time the test is positive, but you do not have the disease
Pr(not A): The percentage of the population who do not have the disease
To simplify the program, assume that Pr(X|not A):
Pr(X|not A) = 100 - Pr(X|A)
In other words, if the probability of a true positive is 80%, the probability of a false positive is 20%.
And the probability of Pr(not A):
Pr(not A) = 100 - Pr(A)
The script asks for the three percents of the condition in the general population (Pr(A)) then graphs the result for a true positive for test accuracy from 80-100%. This is the display in the command window when the user enters 1, 5, 10 for the three percentages:
Enter percent of general population with condition (0-100): 1
Enter percent of general population with condition (0-100): 5
Enter percent of general population with condition (0-100): 10
See graph in Figure 1
Match this display in the command window including the ending message ("See graph in Figure 1").
The x axis is Pr(X|A) from 80-100%: the chance of a positive test X given the actual condition A (the accuracy of the test). The y axis is Pr(A|X) the chance that you have the disease given a positive test result.
Requirements
Verify that the user enters percentages from 0-100%. Loop in the getpercent function until the user enters a valid percentage:
Enter percent of general population with condition (0-100): -1
Percent entered: -1 is invalid
Enter percent of general population with condition (0-100): 101
Percent entered: 101 is invalid
Enter percent of general population with condition (0-100): 1
(script continues execution)
You can assume that the user will enter an integer.
Use functions to reduce the duplication of code:
getpercent
bayes
The getpercent function is not passed anything. It gets the percentage from the user in an input command and returns it. It stays in a loop until the user enters a percentage from 0-100. It is called three times from the script. The bayes function is passed the percentage of the general population obtained from the user (Pr(A)), and the chance of a true positive (Pr(X|A)) that will go from 80-100. It returns Pr(A|X).
PLEASE HELP!
This is what I have thus far:
Explanation / Answer
Answer:
Note: Code has been modified as per user.
%function getpercent() that will return a valid percent getfrom %the user
function gp=getpercent()
%get the percent from the user
gp=input('Enter percent of general population with condition (0-100):');
% check for the validity
while(gp<0 ||gp>100)
fprintf('Percent entered: %f is invalid ',gp);
gp=input('Enter percent of general population with condition (0-100):');
end
end
%bayes theorem
function prax=bayes(percent,x)
xeq=x/100; %get as integer remove percentage
percenteq=percent/100;
%bayes calculation
prax=(xeq*percenteq)/((xeq*percenteq)+((100-percenteq)*(1-xeq)));
end
clear all;
close all;
y=[];
x1=[];
%for 3population
for n=1:3
%get percent
percent=getpercent();
for j=1:21
x=j+79;
x1=[x1 x];
%calculate the pr(A|X) using bayes theorem
ytemp=bayes(percent,x);
y=[y ytemp];
end
figure;
%plot for each population entered
plot(x1,y);
end
fprintf('See graph in Figure 1');