Okay, so I have a bit of experience from doing rom hacks of the gen III Pokemon games. There isn't a single set probability of what the chance of a trainer switching, but I can share how the AI works in the game to give you a general idea since there are just so many situations that come up that there simply isn't a clear probability. The AI in the Pokemon Stadium games is much higher since there's no grinding nor are there as many trainers.
The same applies for wild Pokemon and for the rarer partner trainers like your rival teaming up with you in Heart Gold against Lance and Clair, but even these trainers have their own set AI that follows the same pattern of higher=better, but in this case, it comes down more to giving partners appropriate moves (ex, not giving them Earthquake so there's no chance of hurting your Pokemon.)
- Each trainer is given a value to determine their skill level. The exact numbers vary by the generation of the game, but the general pattern is that the higher the number is, the smarter the trainer plays
- They're typically broken up in tiers, that is, random trainers you see will have fairly low values, rivals and antagonist leaders will be higher, gym leaders, and elite 4 members will have their own values, and the champion and the "secret" trainer (Red in Johto, Steven in Emerald, etc) and so on.
- The AI will decide between attacking, using an item, or switching. The computer could choose to switch to another Pokemon (potentially up to 5), use an attack (up to 4), or use an item (varies, but typically only has healing items, so I'll just say 1), meaning that the AI has roughly 10 choices it could make.
- The AI will assign points to each decision based on your current Pokemon, their own HP and your HP, whatever affinities your Pokemon has, and the moves items, and remaining Pokemon your opponent has access to. So for example, the Pokemon will gain more points if they have access to a STAB move, if the move has high accuracy, or a super effective move, but moves also have points given to them based on their funtion code, which is basically the hexadecimal number assigned to it that determines its secondary effect. For example, the moves Grass Knot and Low Kick have the exact same effect, meaning they'd get the same increase in points. They have different typing, however, which is a different factor on its own.
- There can be a chance that the value for "choose to switch out" will be higher than any of the available moves for Pokemon, which is what leads to switches.
- The set AI value the trainer was given how many moves they will select from, meaning if they have 4 options, a player will consider the four best decisions, whereas another trainer with a higher AI would only go with the best two decisions. Note that those numbers are not absolute, I just used them for demonstrative purposes.
- The previous point is why you can use perish song and the obvious choice is to force a switch on a Pokemon, but most trainers will choose not to switch or why you randomly get those trainers that will use a buffing attack when they have the chance to defeat you.
Factors that can influence AI behavior in 3 categories (Pokemon, move, item) this is including but not limited to:
- Remaining items and effect
- Remaining HP of own Pokemon
- Remaining HP of enemy Pokemon
- What moves active Pokemon has, be it super effective moves, STAB, priority moves, moves with a chance of status or secondary effects, etc.
- The affinities of any remaining Pokemon in party (for instance, a dark type Pokemon can come out provided the trainer has a high enough AI value to think to switch and to come in to take advantage of a psychic immunity.)
- The typing of the opposing Pokemon. It's for this you can have the rare situation where a trainer will choose not to heal their Pokemon if they find it to be a waste. That or they think that the chance to take advantage of a super effective move isn't an optimal choice.
- Weather or other passive effects that influence damage.
- In the case of buffing or debuffing skills, how many stat boosts/drops are in effect influence whether they would be used again.
- Anything else I've mentioned above and pretty much anything that would influence damage amount.
Sources: Basic programming experience, Pokemon AI Tools