Ferroelectric phase transitions from first principles