The two main effects underlying the magnetic shape memory effect in Ni2MnGa are martensitic transformations and magnetic anisotropy energies. Both issues are addressed here with first-principles calculations. First, we examine how the tetragonality in the martensitic phase varies with the composition. Then, the actual transformation is investigated by comparing the free energies of different phases. The transition from the cubic structure to the tetragonal structure with c/a=1.27 is driven by the vibrational free energy and occurs at a temperature of 200 K which is in the experimental range. Finally, we focus on the magnetic anisotropy energy for the tetragonal structure with c/a=0.94. It is shown to be a magnetically nearly ideal uniaxial system determined by the first-order anisotropy constant. However, it is estimated that the twinned microstructure can cause higher-order anisotropies to show up in the measured anisotropy. © 2002 American Institute of Physics.