MATLAB Answers

Conversion to double from function_handle is not possible

11 ビュー (過去 30 日間)
giulio graziani
giulio graziani 2019 年 12 月 9 日
Commented: giulio graziani 2019 年 12 月 10 日
Hi, I'm new to Matlab and I got this error while trying to write down a function.
Can someone help me understanding where is the problem in my code?
function [errors] = first_fun(market_vol,strike_prices,ExerciseDates)
n = size(market_vol,2);
m = size(market_vol,1);
errors = zeros (n);
for j = 1:n
for i = 1:m
errors(i,j) = @(X) market_vol(i,j) - blackvolbysabr(X(1),Beta,X(2),X(3),Settle,ExerciseDates{j},ForwardValue,strike_prices(i));
end
end
end
Thanks

  0 件のコメント

サインイン to comment.

採用された回答

Walter Roberson
Walter Roberson 2019 年 12 月 9 日
errors = zeros (n);
That says that errors is to be created as an n by n array of double precision values initialized to all zeros.
errors(i,j) = @(X) market_vol(i,j) - blackvolbysabr(X(1),Beta,X(2),X(3),Settle,ExerciseDates{j},ForwardValue,strike_prices(i));
The right hand side starting from @(X) says that what follows is to be the handle to a function that accepts a single parameter, and that if the handle were ever to be invoked, then at the time of invocation it should calculate the part that follows, substituting the passed-in parameter in place of the variable named X. The only calculation involved in this process is to look through the right hand side expression and find all of the variables such as market_vol that are mentioned, and take copies of those variables and store them along with the function handle, but the subtraction and so on are to be deferred until the function handle is invoked.
You then try to store that function handle into a location in the array that is initialized to all 0. But function handles are not a compatible data type to themselves be converted to double.
What you can do is
errors = cell(m,n);
for j = 1:n
for i = 1:m
errors{i,j} = @(X) market_vol(i,j) - blackvolbysabr(X(1),Beta,X(2),X(3),Settle,ExerciseDates{j},ForwardValue,strike_prices(i));
end
end
This would result in a cell array of function handles being returned; the function handles can then be extracted and used in various ways, such as
optim_2_1 = fminunc(errors{2,1}, rand(1,3)); %minimize function value

  3 件のコメント

giulio graziani
giulio graziani 2019 年 12 月 9 日
Thanks for your answer!
I have only partially understood what s the problem, I ll try to explain my whole goal.
I needed that function to run a 'least squares non linear optimization' between the matrix 'market_vol' and the matrix created by the command 'blackvolbysabr(...)'.
X = lsqnonlin(errors, [0.5 0 0.5], [0 -1 0], [Inf 1 Inf]);
My problem is that the second matrix depends on the vector 'X', and that's why I was trying to create that function.
How can I use that cell i obtain modifying my code with your suggestion?
This is my code
Thanks again
Walter Roberson
Walter Roberson 2019 年 12 月 9 日
Your current code is setting up to run a calculation between an element of market_vol and a value created by running blackvolbysabr() with parameters determined in part by position in the matrix.
It looks to me as if perhaps you want something like
function [best_errors, best_X] = first_fun(market_vol, strike_prices, ExerciseDates, Beta, Settle, ForwardValue)
best_X = lsqnonlin(@(X) first_fun_helper(X, market_vol, stripe_prices, ExerciseDates, Beta, Settle, ForwardValue), [0.5 0 0.5], [0 -1 0], [inf 1 inf]);
best_errors = first_fun_helper(best_X, market_vol, stripe_prices, ExerciseDates, Beta, Settle, ForwardValue);
end
function errors = first_fun_helper(X, market_vol, stripe_prices, ExerciseDates, Beta, Settle, ForwardValue)
n = size(market_vol,2);
m = size(market_vol,1);
errors = zeros(m,n);
for j = 1:n
errors(:,j) = market_vol(:,j) - blackvolbysabr(X(1), Beta, X(2), X(3), Settle, ExerciseDates{j}, ForwardValue, strike_prices(1:m));
end
end
Note that you need the three additional parameters Beta, Settle, ForwardValue in order to do the calculation -- unless, that is, you are willing to hard-code their values inside first_fun()
giulio graziani
giulio graziani 2019 年 12 月 10 日
Is there a way to express immense gratitude for your help ?

サインイン to comment.

More Answers (0)

サインイン してこの質問に回答します。


Translated by