There is no formula for this.
All the numbers are revealed to us, you can check them out here.
Basically, hatched Pokemon start with 120 happiness, while Pokemon caught in the friendship ball start with 200.
I believe that all the others have 70 base happiness when you catch them(except N's Pokemon who all have max happiness).
No matter what the base happiness of a Pokemon is, if it is traded it will automatically be set to 70.
Hope I helped.