Thermodynamics of anharmonic lattices from first principles