First-principles model hamiltonians for ferroelectric phase transitions