Sunday, May 23, 2010

Fitting Data with MATLAB's "dfittool"

If we have data and a distribution in our mind, "dfittool" can help us find the distribution's parameters and visualize the result.

Let's generate normal samples (mu=5, sigma=2) in MATLAB and fit it with a normal distribution  using "dfittool".
 >> x = normrnd(5,2,100,1);



That's very good fit, isn't it? Looking at the results, it finds the mu to be 4.98 and sigma 2.05, which can be calculated from mean and standard deviation of x.
>> mu = mean(x)
mu =
    4.9822

>> sig = sqrt(var(x))
sig =
    2.0518

It also says "Log Likelihood: -213.267". Where does it come from?

Recall that the pdf of normal distribution is
\[f(X|(\mu,{\sigma ^2})) = \frac{1}{{\sqrt {2\pi } \sigma }}{e^{ - \frac{{{{(x - \mu)}^2}}}{{2{\sigma ^2}}}}}\]
and, therefore, the likelihood function is
\[\varphi (\mu,{\sigma ^2}) = \prod\limits_{i = 1}^n {\frac{1}{{\sqrt {2\pi } \sigma }}{e^{ - \frac{{{{({x_i} - \mu)}^2}}}{{2{\sigma ^2}}}}}} \]
and the log-likelihood function is
\[\log \varphi (\mu,{\sigma ^2}) = \sum\limits_{i = 1}^n {(\log \frac{1}{{\sqrt {2\pi } }} - \log \sigma - \frac{{{{({x_i} - \mu)}^2}}}{{2{\sigma ^2}}})} \]
\[ = n\log \frac{1}{{\sqrt {2\pi } }} - n\log \sigma - \frac{1}{{2{\sigma ^2}}}\sum\limits_{i = 1}^n {{{({x_i} - \mu )}^2}} \]
Substitute mu and sigma to the last equation, using the MATLAB command 
>> n*log(1/sqrt(2*pi))-n*log(sig)- (1/(2*sig^2))*sum((x-mu).^2)
ans =
 -213.2672

No comments:

Post a Comment