fminsearch: Assignment has more non-singleton rhs dimensions than non-singleton subscripts

1 view (last 30 days)
Hi all,
We are trying to estimate parameters using fminsearch but we get the following error:
_Error:_
Assignment has more non-singleton rhs dimensions than non-singleton subscripts
Error in fminsearch (line 189)
fv(:,1) = funfcn(x,varargin{:});
Error in MLE (line 6)
[x,fval,exitflag,output] = fminsearch(@(Guess)MLEfunc( Guess, V, r ),Guess);
We went through our code a couple of times but we are unable to find the error. From debugging (we used DBSTOP as well) we know that fv(:,1) returns a 1x3 vector and funfcn returns a 1x177 vector, which is exactly what the error implies. Our code is as follows:
Code:
meanV = mean(V(:,1));
varV = var(V(:,1));
Guess = [meanV,varV]; % The starting guess
[x,fval,exitflag,output] = fminsearch(@(Guess)MLEfunc( Guess, V, r ),Guess);
function [ LLFS ] = MLEfunc( X, V, r )
n = size(V,2);
h = 1/252;
F = 100;
T = size(V,2);
mu = X(1);
sigma = X(2);
% Compute Rk = log(Vkh/Vk-1h)^2
R = zeros(n,1);
for k = 2:n
R(k-1) = (log(V(k)/V(k-1)))^2;
end
% Compute Rrk
Rr = zeros(length(R),1);
for k = 1:length(R)
Rr(k) = (R(k)-(mu-(sigma^2/2)*h))/(sigma^2*h);
end
% LLF when V observed
LLFV1 = -(n/2)*log(2*pi*sigma^2*h);
LLFV2 = -(1/2)*sum(Rr);
LLFV3 = sum(log(V));
LLFV = LLFV1 + LLFV2 + LLFV3;
% Compute dkh(sigma)
d_sigma = zeros(n,1);
norm_d_sigma = zeros(n,1);
for k = 1:n
d_sigma(k) = (log(V(k)/F)+(r(k)+(sigma^2/2))*(T-k*h))/(sigma*sqrt(T-k*h));
norm_d_sigma(k) = normcdf(d_sigma(k));
end
% LLF when V unobserved
LLFS1 = LLFV;
LLFS2 = -sum(norm_d_sigma);
LLFS = LLFS1 + LLFS2;
LLFS = -LLFS;
end
fminsearch has to return the two parameters that have to be estimated, mu and sigma. Since we don't have a clue how to solve this, any kind of help would be greatly appreciated!
  2 Comments
Patricia Lockwood
Patricia Lockwood on 30 Mar 2016
I have just had the same problem with the same error when using fminsearch. I believe it might be to do with matlab2015b, as fminsearch works fine and without error in matlab2014a
Alireza Mounesisohi
Alireza Mounesisohi on 23 Apr 2017
The error is due to the input of the function. Basically, your fminsearch cannot close the loop to optimize it. Remind that the input of the function must not be called in the function which is plugged into the fminsearch at any point.

Sign in to comment.

Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!