I'm both.....formally trained but play by ear also. I've always had a pretty good ear, but lately using a friend's exercise has improved my ear 1,000%. And I wasn't doing the exercise to improve my ear. It just happened.
The exercise is simple as pie. Just make up a melody line and play it VERY slowly (VERY SLOWLY) one note at a time, but.......think "pitch"...where you want the next note to fall according to the melody you hear in your head. You're on a C note and you can hear in your mind the pitch of the next note. Do you think it would it be a 2nd, 3rd, a suspended 4th, an augmented 5th, dominant 7th, flatted 9th, etc. Take a few seconds and then go for it. Hit the note you think it is. It will probably be wrong, but your mind will hear that's it's "wrong." The next time you're in that situation, your mind will remember the "wrong" note and NOT go there again. Eventually you'll start hearing the "correct" interval and your fingers will automatically go to the right place. It's taken me about 3-4 months now of doing this religiously every day, but now I can recognize intervals clearly AND quickly. As in most endeavors, nothing happens for quite a while and then one day it ALL happens. One day your pitch recognition becomes industrial strength almost overnight if you keep at it regularly.
I actually started doing the exercise because I'm fascinated with how horn players from the classic band band era improvise so easily...how they just hear in their heads what they want to play, and.......they play it. So I started listening to what they were doing and I began improvising very slowly.....one note at a time to replicate what they were doing at fast speeds.
Most of you know already I've always been into "power of the mind" principles with many things in addition to music. There's two well-known books called "The Inner Game of Tennis" and "The Inner Game of Music." They both deal with "Power of the Mind" principles as applied to tennis and musicians. Also.....Maxwell Maltz states in his book "Psycho Cybernetics" the following, which is exactly the way it is: "You will never be great at ANYTHING (that includes music) until you believe in your mind that "you are GREAT." A person who wins millions in the lottery, is still not rich until he finally believes in his mind that he is rich. It took me many years to fully understand that and apply it to music!