error due to function handle in for loop

Hi! I have a problem with my optimization problem. I try to minimize the difference between real option price and model option price (Its an option pricing model calibration). I use the for loop to catch every option in my dataset in the fmincon optimization to optimize the summed error of 'real' and 'model' price.
The code looks like this:
The "cl_heston()" is the pricing formla for the model price and works when inserting random numbers instead of x.
'Testset4' is the set with the information for the option price calculation
real is the actual historical price.
%Optimierung Heston Loop
clc
clear
global data;
x0 = [0.5 0.5 1 -0.5 1]; % starting values
lb = [0 0 0 -1 0]; % lower bound
ub = [1 1 6 1 20]; % upper bound
data = xlsread('Testset4');
for n = 1:length(data)
model(n) = @(x) cl_heston(data(n, 1), x(1), x(2), x(5), x(3), data(n, 4), 0, x(4), data(n, 2), data(n, 3)); % model price
real(n) = data(n, 5); % historical price
end
objective = @(x) sum(abs(real-model(x))); % objective function
%fmincon Funktion
x = fmincon(objective, x0, [], [], [], [], lb, ub, []);
objective(x)
When I run it I receive the error: Nonscalar arrays of function handles are not allowed; use cell arrays instead.
If I remove the '(n)' from 'model (n)' it works but just takes the last number in the dataset for the model price for every iteration.
Any help is highly appreciated!

댓글 수: 3

Mohammad Sami
Mohammad Sami 2020년 1월 23일
편집: Mohammad Sami 2020년 1월 23일
What if you used arrayfun in objective
objective = @(x) sum( abs( arrayfun( @(y,z)y-z(x),real,model) ) );
Thanks for the help!
Tried it but the main problem remains:
model(n) = @(x) cl_heston(data(n, 1), x(1), x(2), x(5), x(3), data(n, 4), 0, x(4), data(n, 2), data(n, 3)); % model price
This equation in the loop can not be solved. (still error: Nonscalar arrays of function handles are not allowed; use cell arrays instead) Somehow MATLAB has a problem with the function handle in the loop iteration. For:
real(n) = data(n, 5);
it is working perfectly. The problem lays in the combination of the for loop and the function handle...
Do you have an idea how I could bypass this problem?
Thanks already!
model{n} = @(x) cl_heston(data(n, 1), x(1), x(2), x(5), x(3), data(n, 4), 0, x(4), data(n, 2), data(n, 3)); % model price
Question for you:
objective = @(x) sum(abs(real-model(x))); % objective function
%f
Is that indexing your hypothetical vector of function handles at location x? Or is it invoking the vector of function handles with argument x? If so then what does it mean to invoke a vector of function handles?

댓글을 달려면 로그인하십시오.

답변 (1개)

Jonas Huwyler
Jonas Huwyler 2020년 1월 23일

0 개 추천

Thanks for the answer Walter!
So I tried model{n} and received the error: Array indices must be positive integers or logical values.
So what I try to do is using the fmincon to optimize the parameter in 'model'. I try to get for 'real(n)' and 'model{n}' arrays (price arrays). When I receive the arrays I can find the difference between 'real' and 'model'.
"If so then what does it mean to invoke a vector of function handles?" That's where I am struggling. Unfortunately, I can't tell why it is not working.

댓글 수: 1

With model being a cell array, model(x) would be attempting to index the cell array at location x
I suggest that you replace model(x) with
cellfun(@(F) F(x), model)

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Problem-Based Optimization Setup에 대해 자세히 알아보기

질문:

2020년 1월 21일

댓글:

2020년 1월 23일

Community Treasure Hunt

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

Start Hunting!

Translated by