Fitting a distribution with custom functions

You can just to implement the gradient for your function, here is the guide in theano:

http://deeplearning.net/software/theano/extending/extending_theano.html