Previous Up Next

20.4.23  Distribution fitting by maximum likelihood

The fitdistr command finds the parameters for a distribution of a specified type that best fits a set of samples.

Examples

fitdistr(randvector(1000,weibulld,1/2,1),weibull)
     
weibulld
0.517079036032,1.05683817484
          
X:=randvar(normal,stddev=9.5):; Y:=randvar(normal,stddev=1.5):; S:=sample(eval(X/Y,0),1000):; Z:=fitdistr(S,cauchy)
     
cauchyd
0.347058460176,6.55905486387
          
histogram(select(x->(x>-100 and x<100),S)); plot(Z(x),x=-100..100,display=red+line_width_2)
kolmogorovt(S,Z)
     

“D=”,0.0161467485236,“K=”,0.510605021406,“1-kolmogorovd(K)=”,0.956753826255
          

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

You can fit a lognormal distribution to samples x1,x2,…,xn by fitting a normal distribution to the sample logarithms log x1,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(eval(exp(X),0),1000):;

Then fit the normal distribution to logS:

Y:=fitdistr(log(S),normal)
     
normald(5.04754808715,1.42751619912)           

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


Previous Up Next