I had a long discussion with a psychiatrist about this and they more or less acknowledged that it really isn't a science and is more of an art. You try this SSRI or that SSDI with a low dosage and see what happens. Then you Amp it up gently. If it works great. If it doesn't, then you dial it back and try something else. Slowly working to drugs with more and more adverse side effects until something works.
They have an idea of why it works but they don't really know precisely for every person.