# FindDistributionParameter slow with user defined Distribution

I am trying to define my own probability distributions in order to fit data using Maximum Likelyhood. To test my distributions, I have produce a set of data Normally distributed using

data = RandomVariate[NormalDistribution[0, 2], 10^3]


defined my own Normal Distribution and fit it to data:

testDistribution[a_, b_] := ProbabilityDistribution[E^(-((x - a)^2/(2 b^2)))/(Sqrt[2 \[Pi]] b), {x, -Infinity, Infinity},Assumptions -> a \[Element] Reals && b \[Element] Reals && b > 0]
params = FindDistributionParameters[data, testDistribution[a, b],  ParameterEstimator -> "MaximumLikelihood"]


which gave the results {a -> 0.029678, b -> 1.99852} in 10.39 seconds

However, using the following line

params = FindDistributionParameters[data, NormalDistribution[a, b], ParameterEstimator -> "MaximumLikelihood"]


gave {a -> 0.0296781, b -> 1.99852}but in only 0.03 seconds. So it looks like my user defined distribution is not properly defined. This makes a large difference when I increase the size of the data.

Does anybody knows where the difference come from?

Thank you very much!

• Welcome to Mathematica.SE! 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Take the tour and check the faqs! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! – user9660 Oct 26 '16 at 17:01

$Version (* "11.0.1 for Mac OS X x86 (64-bit) (September 21, 2016)" *)$HistoryLength = 0; (* to make timings fair *)

RandomSeed; (* repeatable random data *)

data = RandomVariate[NormalDistribution[0, 2], 10^3];

testDistribution[a_, b_] :=
ProbabilityDistribution[
E^(-((x - a)^2/(2 b^2)))/(Sqrt[2 π] b),
{x, -Infinity, Infinity},
Assumptions -> a ∈ Reals && b > 0]


"MaximumLikelihood" is the default setting for the option ParameterEstimator

Options[FindDistributionParameters, ParameterEstimator]

(*  {ParameterEstimator -> "MaximumLikelihood"}  *)

params = FindDistributionParameters[
data, testDistribution[a, b]] //
AbsoluteTiming

(*  {0.530307, {a -> -0.00916218, b -> 2.01538}}  *)


If this took 10 seconds there is something else going on. Perhaps you should try restarting Mathematica or restarting your operating system.

params2 = FindDistributionParameters[
data, NormalDistribution[a, b]] //
AbsoluteTiming

(*  {0.000771, {a -> -0.00916218, b -> 2.01538}}  *)


The built-in distributions are much faster, presumably their code is highly optimized

It is even faster to not specify a distribution than define your own.

Thread[{a, b} -> List @@ FindDistribution[data]] //
AbsoluteTiming

(*  {0.344109, {a -> -0.00916218, b -> 2.01538}}  *)