I came across this one question in my textbook:
For each question on a multiple-choice test with 5 questions, there are five possible answers, of which exactly one is correct. If a student selects answers at random, give the probability that the first question answered correctly is question 4.
And I was wondering what would happen if the teacher also graded the test in a random order.
The way I approached the problem was by looking at a permutation of the sequence {1,2,3,4,5}. There's then a function that maps 1,2,3,4,5 to their position in the permutation we can call f.
The probability that the teacher grades in the order of that permutation and that question #4 is the first question right is then:
0.8^(f(4)-1)*0.2/(5!) since we need every question that were graded before #4 to be wrong and then for #4 to be right. The 1/5! is the probability that that specific grading order was chosen.
We can group the permutation functions by how they map 4. So the set of functions that map 4 to 1 would be one group and the set of functions that map 4 to 5 would be another group. There are 4! elements in each set (4! arrangements of 5 objects with one object being fixed) so the total probability is then
the sum from n=1 to 5 of 0.8^(n-1)*1/25 which I got to be roughly 0.134.
I think my answer is right but when I tried to check it experimentally in Python (simulating these probability problems makes for some good coding problems which is nice), I got 0.0711.
Here's the code:
import random
def experi():
li = [];
index = random.sample(range(1, 6), 5);
for i in range(1,6):
li.append(random.randint(1,6));
#check if 4th question is right if not, return 0
if li[3] != 1:
return 0;
#li is a list of random integers from 1-5
#for simplicity we assume that all questions have same answer, 1
#generate random integer, that is question number we need to get right first
#then we shuffle range(1:5) to get a random indexing
#we then iterate through the shuffle and check if first index to be right is 4
# goes through index if it hits 4 then nothing before was correct
#if it hits a right answer before 4, then we return false
for i in index:
if i == 3:
return 1;
if li[i-1] == 1:
return 0;
return 0;
count = 0;
N = 100000;
for i in range(N):
count += experi();
print(count/N);
I would really appreciate it if you guys could tell me whether it's my code or my math that's wrong (or both).
The answer to the first question is $$\left(\frac 45\right)^3\times \frac 15=.1024$$
For the second problem: work recursively (on the number of questions). Let $p_n$ be the desired probability if there are $n$ questions other than the special question.
Of course $p_0=\frac 15$, since the teacher only grades the special question, which has a $\frac 15$ chance of being right.
For $p_1$ note that there are two paths to success. Either the teacher starts with the special question or with the other. It follows that $$p_1=\frac 12\times \frac 15+\frac 12\times \frac 45\times \frac 15=\frac 9{50}$$
More broadly, we have the recursion $$p_n=\frac 1{n+1}\times \frac 15+\frac {n}{n+1}\times \frac 45\times p_{n-1}$$
Which gets us to $$p_4=.134464$$
Which matches your claimed result, though not the simulation.
Worth remarking: In the first problem, the special question was the fourth one the teacher consults, which is bad luck. If the teacher grades randomly, there is an excellent chance that the special question comes up earlier than fourth, and only a $.2$ chance that it comes up worse than fourth. Thus you should expect the answer to the second problem to be greater than the answer to the first, leading me to doubt your simulator.