We present a comprehensive first principles study of doped hafnia in order to understand the formation of the ferroelectric orthorhombic [001] grains. Assuming that tetragonal grains are present during the early stages of growth, matching plane analysis shows that tetragonal [100] grains can transform into orthorhombic [001] during thermal annealing, when they are laterally confined by other grains. We show that among 0%, 2% and %4 Si doping, 4% doping provides the best conditions for the tetragonal [100] → orthorhombic [001] transformation. This also holds for Al doping. We also show that for HfxZr1−xO2, where we have studied x = 1.00, 0.75, 0.50, 0.25, 0.00, the value x = 0.50 provides the most favorable conditions for the desired transformation. In order for this transformation to be preferred over the tetragonal [100] → monoclinic [100] transformation, out-ofplane confinement also needs to be present, as supplied by a top electrode. Our findings illuminate the mechanism that causes ferroelectricity in hafnia-based films and provide an explanation for common experimental observations for the optimal ranges of doping in Si:HfO2, Al:HfO2 and HfxZr1−xO2. We also present model thin film heterostructure computations of Ir/HfO2/Ir stacks in order to isolate the interface effects, which we show to be significant. ferroelectricity in HfO 2 thin films"