From: Leo Ge on
I defined the normal pdf in M file as:
function pdf=betapdf(x,a,b)
pdf=1/(2*pi)^0.5/b*exp(-(x-a)^2/2/b^2);

And simulate s with normal(0,1)
N=2000
p=rand(1,N)
s=norminv(p(1:N),0,1)

Then mle but just get error:
[phat,pci] = mle(s,'pdf',@betapdf,'alpha',.05,'start',[-1,0.5]);
??? Error using ==> stats\private\mlecustom>llf_pdfcdf
The PDF function returned NaN or infinite values.
Error in ==> fminsearch at 175
fv(:,1) = funfcn(x,varargin{:});
Error in ==> stats\private\mlecustom at 176
[phat,nll,err,output] = ...
Error in ==> mle at 219
[phat, pci] = mlecustom(data,varargin{:});

But if I just use
[phat,pci] = mle(s,'distribution','normal','alpha',.05);
It all works well.

I do another practise but also with error:
%define beta-df by M-file:
function logpdf = betalogpdf(x,a,b)
logpdf = (a-1)*log(x)+(b-1)*log(1-x)-betaln(a,b);
Then mle by:
x = betarnd(1.23,3.45,25,1);
phat = mle(x,'logpdf',@betalogpdf,'start',[1,2])
??? Error using ==> stats\private\mlecustom>checkFunErrs
The following error occurred while trying to evaluate
the user-supplied logpdf function 'betalogpdf':
Error using ==> feval
Undefined command/function 'betalogpdf'.
Error in ==> stats\private\mlecustom at 156
checkFunErrs('logpdf',logpdfFun,start,uncensData,[],[],logpdfAddArgs);
Error in ==> mle at 217
phat = mlecustom(data,varargin{:});

I am confused why I can't specify the pdf by myself. Thank you very much for your kindly help.

Leo
From: Leo Ge on
"Leo Ge" <qwaszxgecl(a)hotmail.com> wrote in message <hdpj42$lml$1(a)fred.mathworks.com>...
> I defined the normal pdf in M file as:
> function pdf=betapdf(x,a,b)
> pdf=1/(2*pi)^0.5/b*exp(-(x-a)^2/2/b^2);
>
> And simulate s with normal(0,1)
> N=2000
> p=rand(1,N)
> s=norminv(p(1:N),0,1)
>
> Then mle but just get error:
> [phat,pci] = mle(s,'pdf',@betapdf,'alpha',.05,'start',[-1,0.5]);
> ??? Error using ==> stats\private\mlecustom>llf_pdfcdf
> The PDF function returned NaN or infinite values.
> Error in ==> fminsearch at 175
> fv(:,1) = funfcn(x,varargin{:});
> Error in ==> stats\private\mlecustom at 176
> [phat,nll,err,output] = ...
> Error in ==> mle at 219
> [phat, pci] = mlecustom(data,varargin{:});
>
> But if I just use
> [phat,pci] = mle(s,'distribution','normal','alpha',.05);
> It all works well.
>
> I do another practise but also with error:
> %define beta-df by M-file:
> function logpdf = betalogpdf(x,a,b)
> logpdf = (a-1)*log(x)+(b-1)*log(1-x)-betaln(a,b);
> Then mle by:
> x = betarnd(1.23,3.45,25,1);
> phat = mle(x,'logpdf',@betalogpdf,'start',[1,2])
> ??? Error using ==> stats\private\mlecustom>checkFunErrs
> The following error occurred while trying to evaluate
> the user-supplied logpdf function 'betalogpdf':
> Error using ==> feval
> Undefined command/function 'betalogpdf'.
> Error in ==> stats\private\mlecustom at 156
> checkFunErrs('logpdf',logpdfFun,start,uncensData,[],[],logpdfAddArgs);
> Error in ==> mle at 217
> phat = mlecustom(data,varargin{:});
>
> I am confused why I can't specify the pdf by myself. Thank you very much for your kindly help.
>
> Leo

I find a problem that I don't save the m-file with the same name of the function.But after revising it, the first example still have error:
??? Error using ==> stats\private\mlecustom>checkFunErrs
The following error occurred while trying to evaluate
the user-supplied logpdf function 'normlogpdf':

Error using ==> mpower
Matrix must be square.

Error in ==> stats\private\mlecustom at 156
checkFunErrs('logpdf',logpdfFun,start,uncensData,[],[],logpdfAddArgs);

Error in ==> mle at 219
[phat, pci] = mlecustom(data,varargin{:});
I don't know what's wrong. I am a newer to Matlab, and thank you for your kindly help.
From: Tom Lane on
>> pdf=1/(2*pi)^0.5/b*exp(-(x-a)^2/2/b^2);
....
> Error using ==> mpower
> Matrix must be square.

Leo, the expression you wrote is fine as long as everything is a scalar. But
if x is a vector,

(x-a)^2

requests that you square the entire array, using matrix multiplication. You
can use

(x-a).^2

if your intent is to square each element of the array.

-- Tom


From: Leo Ge on
Yeah, it works, many thx!