This question is a follow-on from this question .
I am trying to determine the probability of each horse finishing 2nd and each horse finishing 3rd. I have developed code to calculate the probabilities by implementing the formulas provided in the above mentioned question.
Each horse is represented by a 'horseData' object containing variables such as the horse id (a unique number to identify the horse), the probability of winning (Pw), the probability of finishing 2nd (P2nd), the probability of finishing third (P3rd) among other variables. All of the HorseData objects are contained in a List called hdList.
The following code implements the formula: $$ P(i,2)= \sum_{i \neq x} (P_x . \frac {P_i}{(1 - P_x) }) $$
// Calc 2nd place for each horse
for (HorseData hdi : hdList) {
for (HorseData hdx : hdList) {
if (hdi.id != hdx.id) {
term = hdx.Pw * hdi.Pw / (1 - hdx.Pw);
hd.addToP2nd(term);
}
}
}
This calculates the probability of finishing 2nd for each horse. The sum of these probabilities adds to one. All good so far.
The following code implements the formula:
$$ P(i,3)= \sum_{i \neq x \neq y}( P_x . P_{y2nd} .\frac {P_i}{(1 - P_x - P_{y2nd}) }) $$
// Calc prob 3rd place for each horse
for (HorseData hdi : hdList) {
for (HorseData hdx : hdList) {
if (hdi.id != hdx.id) {
for (HorseData hdy : hdList) {
if ((hdx.id != hdy.id) & (hdi.id != hdy.id)) {
term = hdx.Pw * hdy.P2nd * hdi.Pw / (1 - hdx.Pw - hdy.P2nd);
hd.addToP3rd(term);
}
}
}
}
}
This calculates the probability of finishing 3rd for each horse. However the sum of these probabilities does not add to one.
For testing, I have a 5 horse race, with the Pw = 0.2 for all horses.
The code to calculate P2nd returns 0.2 for each horse, however the code to calculate P3rd returns 0.16 for each horse (whereas I think it should be 0.2).
Any assistance in reviewing the formulas and the code implementation would be appreciated.

ohenerypackage. – shabbychef Oct 09 '19 at 05:20