### 7.4.23 Distribution fitting by maximum likelihood : fitdistr

fitdistr takes two arguments, a list L of presumably independent and identically distributed samples and a distribution type, which may be normal, exponential, Poisson, geometric, gamma, beta, Cauchy or Weibull. The type is specified as normal (normald), exp (exponential or exponentiald), poisson, geometric, gammad, betad, cauchy (cauchyd) or weibull (weibulld), respectively. The command returns the distribution of the specified type with parameters that fit the given samples most closely according to the method of maximum likelihood.

For example, input :

S:=:; fitdistr(randvector(1000,weibulld,1/2,1),weibull)

Output :

weibulld(0.498920254339,0.971148738409)

Input :

X:=randvar(normal,stddev=9.5):; Y:=randvar(normal,stddev=1.5):; S:=sample(eval(X/Y,0),1000):; Z:=fitdistr(S,cauchy)

Output :

cauchyd(-0.13160176167,6.2569300393)

Input :

histogram(select(x->(x>-100 and x<100),S)); plot(Z(x),x=-100..100,display=red+line_width_2)

Output :

Input :

kolmogorovt(S,Z)

Output :

["D=",0.0125864995943,"K=", 0.398020064869, "1-kolmogorovd(K)=",0.997387219452]

The Kolmogorov-Smirnov test indicates that the samples from S are drawn from Z with high probability.

Fitting a lognormal distribution to samples x1,x2,…,xn can be done by fitting a normal distribution to the sample logarithms logx1,logx2,…,logxn because log-likelihood functions are the same. For example, generate some samples according to the lognormal rule with parameters µ=5 and σ2=2 :

X:=randvar(normal,mean=5,variance=2):; S:=sample(exp(X),1000):;

Now fit normal distribution to logS :

Y:=fitdistr(log(S),normal)

Output :

normald(5.04754808715,1.42751619912)

The mean of Y is about 5.05 and the variance is about 2.04. Now the variable Z=exp(Y) has the sought lognormal distribution.