Created
May 1, 2018 21:38
-
-
Save HendrikSikke/f81cf3c60de6f138f797241fb0189700 to your computer and use it in GitHub Desktop.
to compute problemhttps://math.stackexchange.com/questions/2759407/ on math stack exchange
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
const double student1_probs[5] = {0.3, 0.4, 0.9, 0.7, 0.1}; /* item i is P(student 1 has question i correct) */ | |
const double student2_probs[5] = {0.4, 0.5, 0.2, 0.8, 0.1}; | |
int main(void){ | |
double successes1[6]= {0.0, 0.0, 0.0, 0.0, 0.0, 0.0}; | |
double successes2[6]= {0.0, 0.0, 0.0, 0.0, 0.0, 0.0}; | |
unsigned int i, b,j; | |
for (i=0; i < 32;i++){ | |
double p1 = 1.0; | |
double p2 = 1.0; | |
unsigned int oks=0; | |
b=1; j=0; | |
while (b != (1 <<5)){ | |
if (i & b){ | |
p1 *= student1_probs[j]; | |
p2 *= student2_probs[j]; | |
oks++; | |
} else { | |
p1 *= (1-student1_probs[j]); | |
p2 *= (1-student2_probs[j]); | |
} | |
b <<=1; | |
j++; | |
} /* now total probability of that combo us determined*/ | |
successes1[oks] += p1; | |
successes2[oks] += p2; | |
} | |
for (i=0; i< 6; i++){ | |
fprintf(stdout, "P student 1 has %u correct = %f\n", i, successes1[i]); | |
fprintf(stdout, "P student 2 has %u correct = %f\n", i, successes2[i]); | |
} | |
double P_1_gt_2 = 0.0; /* the chance that 1 has more than 2 */ | |
double P_1_eq_2 = 0.0; | |
double P_2_gt_1 = 0.0; | |
unsigned int score1, score2; | |
for (score1 = 0; score1 < 6; score1++){ | |
for (score2=0; score2 < 6; score2++){ | |
if (score1 == score2){ | |
P_1_eq_2 += successes1[score1] * successes2[score2]; | |
} else if (score1 < score2){ | |
P_2_gt_1 += successes1[score1] * successes2[score2]; | |
} else { | |
P_1_gt_2 += successes1[score1] * successes2[score2]; | |
} | |
} | |
} | |
fprintf(stdout, "1 does better with probability %f\n", P_1_gt_2); | |
fprintf(stdout, "2 does better with probability %f\n", P_2_gt_1); | |
fprintf(stdout, "equal score with probability %f\n", P_1_eq_2); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment