If your letters are incredibly bad, you can't get rid of many of them, and you think there are some good letters left for you to swap some, you might want to do it. If it's early in the game and you have a lead, so much the better. If you have a bunch of consonants and no vowels, a swap might be a good idea. If you've memorized the Scrabble dictionary and know that all your possible plays suck, it might be a good idea.
Edit: Dave's answer, and the answers on the question he links, further expand on when it's a good idea; check it out.
It's very rarely worth it. Actually calculating this, without programmatic help (cheating), is infeasible; see below. Even with help, there are a lot of factors you just can't appropriate account for.
To calculate the decision, count cards. Err, tiles. If your tiles are bad, there's some likelihood that the tiles you swap for will be better than yours. You can figure out that likelihood by subtracting your tiles and the played tiles from the set off all tiles, then factoring in the probability that someone else has the "good tiles" and the probability that you actually pick them up from the pile. Then you calculate an expected point value and decided whether the swap is worth it.
Say you want to swap one tile, for a particular letter Ψ. There are x Ψ's in the game, a have been played, you already hold b of them. That leaves c = x - a - b left.
There are y tiles on the board, plus 21 held by the other players. That leaves d = 102 - y - 21 left for you to choose from (there are 102 tiles in the game).
If you ignore the fact that the tiles picked up by another player also effect everyone else's probability of picking them up, not just yours, the probability that you pick up a Ψ is e = (c / d)(1 - ((d - c) / d)21). I am ignoring that fact because I haven't taken combinatorics in a few years and don't want to re-learn how to account for it :P
Now multiply e by your expected point value for the new word, j, to get f: the expected value of this move.
Now recompute f for every possible Ψ. Add them all up and divide by the number of them, to get g, the expected value of swapping one letter.
If g is greater than the value of the best move you can make right now, then you should do the swap if you're following a greedy algorithm.
Other factors to consider:
- Would the other players hold the letter for an abnormally long time if they picked it up earlier? The above assumes all letters are treated equally.
- Will someone steal the spot you're considering for your next move after the swap? Do you have other spots? Will a better one open up? You can't account for the behavior of the other players.
- If you swap more than one letter, you have even more complicated math to do.
- As mentioned above, this doesn't properly account for the other players' tiles. It will overestimate your chances slightly.
- How does this affect you in the long run, beyond only the next move?
- What's the probability that you pick up good letters if you play some of the bad ones you're holding, instead of swapping?
TL;DR Even the simplified math says you're wasting your time even thinking about it, most of the time. When you think you're really stuck, try a swap; develop a gut instinct for when it works and when it doesn't. I don't think you can do much better than that, apart from rare situations where it's very clear.