Cost function builder. For fitting distributions.