1st start by reading: Software Estimation: demistifying the black art" by McConell.
They also have online classes with the same content.
If you do that you will be better than 80% of devs at estimating.
After that read about Montecarlo simulation on software projects.