In this paper a structural model is developed to estimate a cost function that includes both an adverse selection and a moral hazard variable. It is shown how traditional cost function estimates may produced biased results if moral hazard is present. This gives an alternative reason to Feinstein and Wolak [The econometric implications of incentive compatible regulation, In: G.F. Rhodes, ed., Advances in econometrics, Vol. 9, 159-204 (JAI Press, Greenwich, CT, 1991)] of why scale economies may be overestimated in regulated industries. Parameter estimates from our model can be used to design an optimal contract or any other regulatory mechanism. We illustrate our approach using data from the Norwegian bus transport industry.