0
votes

I am facing an error when using the function fminunc in the context of a maximum-likelihood estimation. I am afraid that it is very straight forward however my experience with MATLAB is very limited.

The function "normal" contains the log-likelihood function. I seek to estimate the expectation and std. deviation of a normal distribution given the observations stored in the variable x.

function f = normal(X, theta)

mean = theta(1);
sigma = theta(2);
z = (X-mean)./sigma;

f = -(sum(-log(sigma) -(1/2).*z.^2 ));

I basically execute the following code:

theta = [1,1]

f = @(theta)normal(x, theta)

[est, fval, exitflag, output, grad, hessian] = fminunc('normal', x, theta)

The error is the following:

Warning: Struct field assignment overwrites a value with class "double". See MATLAB R14SP2 Release Notes, Assigning Nonstructure Variables As Structures Displays Warning, for details.

In createOptionFeedback at 34 In prepareOptionsForSolver at 31 In fminunc at 157 Warning: Struct field assignment overwrites a value with class "double". See MATLAB R14SP2 Release Notes, Assigning Nonstructure Variables As Structures Displays Warning, for details. In fminunc at 203 Error using feval Undefined function 'normal' for input arguments of type 'double'.

Error in fminunc (line 254) f = feval(funfcn{3},x,varargin{:});

Caused by: Failure in initial user-supplied objective function evaluation. FMINUNC cannot continue.

Unfortunately the manual did not help me to fix the code. Calling

[est, fval, exitflag, output, grad, hessian] = fminunc(f, x, theta)

did not help either. What am I doing wrong?

Thank you in advance!

1

1 Answers

1
votes

You have called fminunc with the wrong sintax, please refer to the documentation.

A way to fix your code is by defining the function normal to accept only one parameter: theta.

function f = normal(theta)

global X

mean = theta(1);
sigma = theta(2);
z = (X-mean)./sigma;
f = -(sum(-log(sigma) -(1/2).*z.^2 ));

and call fminunc with

global X
X = randn(100, 1);  % A possible time series.
theta0 = [1,1];
[est, fval, exitflag, output, grad, hessian] = fminunc(@normal, theta0);