Quantcast
Viewing all articles
Browse latest Browse all 41

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

Hi all,

While running our script to estimate some parameters we bumped into a problem.
We are using fminsearch and Matlab returns the following 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 multiple times but we can't find the problem.
We used the debugger (DBSTOP as well) to check what goes wrong in line 189 of fminsearch and apparently fv(:,1) is 1x3 and funfcn(x,varargin{:}); is 1x177, which is exactly what the error describes.

Our code is as follows:

Script:
meanV = mean(V);
varV = var(V);

Guess = [meanV,varV]; % The starting guess

[x,fval,exitflag,output] = fminsearch(@(Guess)MLEfunc( Guess, V, r ),Guess);

Function:
% pre-define:
% n = # of observations (number)
% h = length of time between two obs measured in years (number)
% r = riskfree rate (vector)
% F = face value of zero coupon debt (vector)
% V = estimates of the market value of assets (vector)

% assumptions:
% A year has 252 trading days

function [ LLFS ] = MLEfunc( X, V, r )

n = size(V);
h = 1/252;
F = 100;
T = size(V);

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


V is a 177x1 vector, r is a 177x1 vector. The parameters that have to be estimated are mu and sigma, which means x (output in fminsearch) is supposed to be 1x2.

We have no clue how to solve this problem, so any kind of help would be greatly appreciated!

Viewing all articles
Browse latest Browse all 41

Trending Articles