Given every horse's probability of winning a race, what is the probability that a specific horse will finish 2nd and 3rd? 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.
 A: The win probabilities do not determine the prob that horse $k$ finishes in position $j$.
What you are using is known as Harville's method. 
See the paper by D.A. Harville in Journal of the Amer. Stat. Assn. 1973.
What he did: Suppose we assume the time it takes for horse $i$ to run the race is $X_i$= exponential with parameter $\lambda_i.$ Assume independence among horses. Let $s=\lambda_1+...+\lambda_n$ Then we can compute prob that $i$ wins $P_i=P(X_i<X_j,j\ne i)=\lambda_i/s.$ And we compute the conditional prob of a $(i,j,k)$ trifecta as $$\frac{P_iP_jP_k}{(1-P_i)(1-(P_i+P_j))} $$
These are totally unrealistic assumptions but they do give reasonable values. And we only need all permutations of the first 3 horses for betting purposes.
Read this book: link. 
It is mainly about the authors method of place and show betting which requires them to compute Harville probabilities. And then they adjust the Harville values based on a regression with track data. This paperback is a condensed version of their book $ \it \text{Beat the Racetrack}.$ Probably you do not need that one, too. They also have a few academic papers. The most relevant one is:
${}_\text{EFFICIENCY OF THE MARKET FOR RACETRACK BETTING
By: HAUSCH, DB ZIEMBA, WT RUBINSTEIN, M}$
${}_\text{MANAGEMENT SCIENCE  Volume:27 Pages:1435-1452 1981}$
In my younger days, I tried to follow their method at the track. It is mathematically sound but requires a decision be made with data from as close to post time as possible. These days it seems even more difficult because the money bet off-track is not added to the tote board until after you can no longer make a bet. That squeezes out the profit potential.
In your program you should get 0.2 for third for each horse: Each trifecta has prob (1/5)(1/4)(1/3) and there are 4*3 ways the other horses can finish 1st and 2nd. Looks like you are in error using the prob of 2nd place when computing prob of 3rd. What you want is to fix $k$ and sum over all $i$ and $j$ of this:
$$\frac{P_iP_jP_k}{(1-P_i)(1-(P_i+P_j))} $$
with $i\ne j,j\ne k, i\ne k.$
Do not substitute a 2nd place prob here.
I found it useful to use a 2D array for exacta prob. and a 3D array to store and sum the trifecta prob.
There is a considerable number of academic papers on horse racing. Most are concerned with market efficiency (are win odds accurate) or are some bettors more knowledgeable (late money) and appear in the economics literature. I remember at least one attempt to use another model like Harville, but with normal random variables instead. Ugly multivariate normal integrals to approximate.
A: I recently published an article in the SIAM Journal on Quantitative Finance that addresses this issue, and also provides a discussion of why other answers in this thread, past, present, and perhaps future, are likely to be inaccurate. Those answers typically assume Luce's axiom of choice and thereby re-invent the formula named for Harville. Unfortunately there isn't a dreadfully compelling theoretical or empirical reason to believe that the conditional probability of a horse finishing second should be equal to its renormalized probability of winning (re-normalized by removal of the winner, of course).
In the paper I instead consider the general problem of calibrating a model for performance to winning probabilities. In a continuous horse race problem we might assumes a density $f^*$ with distribution $F^*$ and then seek parameters $(a_1,\dots,a_n)$ modifying $f^*$ in some way (typically by scale or location) to satisfy
\begin{equation}
\label{eqn:continuous}
    p_i = \int f^*(x;a_i) \Pi_{j\neq i}^n \left( 1- F^*(x;a_j)\right) dx
\end{equation}
for some specified winning probabilities $p_i$. I pay particular attention to the case where each horse's performance distribution is a translation of every other. I provide a fast numerical algorithm and open source code solving this inversion problem.
Once one has backed into a reasonable performance model, all other quantities can be computed including the odds of finishing 2nd and 3rd. You can find more details in the paper and I'm happy to copy more here if that's requested. However, I suspect you'll want the code and the README explains usage.
The result is a performance model like the following:

By the way I'm editing this after several years so I'll leave here the fact that this follows on from an earlier discussion of the general case at
http://finmathblog.blogspot.com/2013/09/the-horse-race-problem-general-solution.html
