Error using fmincon (not enough input arguments)

Dear community,
I'm having some issues in using fmincon for a function of two-variables. I have attached a mat file with the date I'm using (the variable "equity"), and the functions I call in the script. Here's my attempt
F = 50;
T = 1;
r = 0.05;
% sigma0 is realised equity volatility
% equity is daily time-series of equity price
N = size(equity, 1);
dt = 1./N;
retE = diff(log(equity));
mu0 = mean(retE).*N;
sigma0 = std(retE).*N.^.5;
x0 = [sigma0, mu0];
TTM = T*ones(N, 1);
for t = 1:N
TTM(t) = T - (t - 1)/N;
end
Asset = @(sigma) assetMerton1(equity, sigma, r, F, TTM);
retA = @(sigma) diff(log(Asset(sigma)));
ND1 = @(sigma) auxiliaryNd1(Asset(sigma), sigma, r, F, TTM);
S1 = @(sigma) arrayfun(@(x)(sum(log(Asset(x))) + N./2.*log(2.*pi.*x.^2.*dt)), sigma);
S2 = @(sigma, mu) arrayfun(@(x,y)((0.5)./(x.^2.*dt).*(sum(retA(x).^2) + (y - x.^2./2).^2.*dt ...
- 2.*(y - x.^2./2).*dt.*sum(retA(x)))), sigma, mu);
S3 = @(sigma) arrayfun(@(x)(sum(log(ND1(x)))), sigma);
S = @(sigma, mu) arrayfun(@(x,y)(S1(x) + S2(x, y) + S3(x)), sigma, mu);
D = fmincon(S, x0, [], [], [], [], [0 -Inf], [Inf Inf]);
In particular, I'm able to compute the function S, like
S(0.2, 0.1)
returning 438.9673. So the function S appears well defined. However, when I try to run the last line (fmincon) I get the following error
Not enough input arguments.
Error in @(sigma,mu)arrayfun(@(x,y)(S1(x)+S2(x,y)+S3(x)),sigma,mu)
Error in fmincon (line 573)
initVals.f = feval(funfcn{3},X,varargin{:});
Caused by:
Failure in initial objective function evaluation. FMINCON cannot continue.
Do you know what is causing it? any suggestions? Thank you very much for your help!

 Accepted Answer

The optimisation functions want a vector argument.
Create:
Sfcn = @(b) S(b(1),b(2));
to do that, and it works —
LD = load('equity.mat')
LD = struct with fields:
equity: [252×1 double]
equity = LD.equity;
F = 50;
T = 1;
r = 0.05;
% sigma0 is realised equity volatility
% equity is daily time-series of equity price
N = size(equity, 1);
dt = 1./N;
retE = diff(log(equity));
mu0 = mean(retE).*N;
sigma0 = std(retE).*N.^.5;
x0 = [sigma0, mu0];
TTM = T*ones(N, 1);
for t = 1:N
TTM(t) = T - (t - 1)/N;
end
Asset = @(sigma) assetMerton1(equity, sigma, r, F, TTM);
retA = @(sigma) diff(log(Asset(sigma)));
ND1 = @(sigma) auxiliaryNd1(Asset(sigma), sigma, r, F, TTM);
S1 = @(sigma) arrayfun(@(x)(sum(log(Asset(x))) + N./2.*log(2.*pi.*x.^2.*dt)), sigma);
S2 = @(sigma, mu) arrayfun(@(x,y)((0.5)./(x.^2.*dt).*(sum(retA(x).^2) + (y - x.^2./2).^2.*dt ...
- 2.*(y - x.^2./2).*dt.*sum(retA(x)))), sigma, mu);
S3 = @(sigma) arrayfun(@(x)(sum(log(ND1(x)))), sigma);
S = @(sigma, mu) arrayfun(@(x,y)(S1(x) + S2(x, y) + S3(x)), sigma, mu);
Sfcn = @(b) S(b(1),b(2));
D = fmincon(Sfcn, x0, [], [], [], [], [0 -Inf], [Inf Inf]);
Local minimum possible. Constraints satisfied. fmincon stopped because the size of the current step is less than the value of the step size tolerance and constraints are satisfied to within the value of the constraint tolerance.
fprintf('sigma = %9.6f\nmu = %9.6f\n',D)
sigma = 0.138249 mu = -0.020935
.

2 Comments

Thank you so much! It works perfectly
As always, my pleasure!

Sign in to comment.

More Answers (0)

Categories

Tags

Community Treasure Hunt

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

Start Hunting!