This paper develops a maximum likelihood (ML) bunching estimator of the elasticity of taxable income (ETI). Our structural approach provides a natural framework to simultaneously account for unobserved preference heterogeneity and optimization errors and for measuring their relative importance. We characterize the conditions under which the parameters of the model are identified and show that the ML estimator performs well in terms of bias and precision. The paper also contains an empirical application using Swedish data, showing that both the ETI and the standard deviation of the optimization friction are precisely estimated, albeit relatively small.