Question

I have some obscurely long symbolic expression of 3 variables (g_eq), which i would like to minimize with some constraints. I am trying to do so by converting it to a function handle and using fmicon:

cfun = matlabFunction(g_eq,'vars',[kappa;theta;sigma]);
A = [-1 0 0; 0 -1 0; 0 0 -1];
b = [0; 0; 0];
[x,fval] = fmincon(@(kappa, theta, sigma) cfun, x0, A, b)

Which Matlab doesn't like:

FMINCON requires all values returned by user functions to be of data type double.

I would suspect, that the problem is with cfun, as it is full of numbers with symbolic precision, can I somehow convert it, so that they're double? Or better (computation time wise) while creating my objective function (cfun) (complicated process of some transformation of data and a parametric model), can I use symbols or some other "proxy for the variables" with double for the numeric part of the expressions?

Thanks, J.

Edit - MCVE: My aim is to find parameters of a model by minimizing a difference between the model implied and data implied laplace transforms over some weighted regions. Here I provide the problem over one small region without use of weights over the regions and some further simplifications. In part 0, I provide the code for functions of the transformation, in part II I make the parametric transformation, while in III the data transformation and attempt to minimize it in IV.

%% 0. Functions used

%% 0.1 L_V1 - transform of parametric
function lv = L_V1(u,sigma,kappa,theta)
lv = (1/(1+u.*sigma^2/(2*kappa))).^(2*kappa*theta/sigma^2);
end

%% 0.2 LV_2 - transform of data
function lv = L_hat1(u,D,n,T)
A_u = cos(sqrt(2 .*u) *sqrt(n) .*D);
Z_u = 1/n * sum(A_u);
lv = 1/T * sum(Z_u);
end


%% I. Pre-estimation

ulng1=100; %select number of points on the evaluated interval
u1 = linspace(.8, 1.6,ulng1); % create region of interest

%% II. Parametric part

par_mat1 = sym(zeros(ulng1,1)); % create interval for parametric

syms sigma kappa theta LV_par1;

for i = 1:ulng1
      par_mat1(i) = L_V1(u1(i),sigma,kappa,theta); %transformations of parametric

end

LV_par1 = sum(par_mat1); %sum of parametric over the region


%% III. Data part
n = 100; %choose number of days
T = 20;  %choose number of obs over a day
D = rand([n-1, T]); %vector of observations, here just random numbers

for i = 1:ulng1
    hat_mat1(i) = L_hat1(u1(i),D,n,T); %transformations of data
end

hat_1  = sum(hat_mat1); %sum of transforms over the region

%% IV. Minimize
W = 1; %weighting matrix, here just one region, hence 1
MC = hat_1 - LV_par1 ; %moment condition
g_eq = (MC) * (W) *(MC.'); %objective function (symbolic)
cfun = matlabFunction(g_eq,'vars',[kappa;theta;sigma]); %create a function handle from the symbolic

x0 = [.5; 1; .5];
A = [-1 0 0; 0 -1 0; 0 0 -1]; %constrains
b = [0; 0; 0]; %constrains
[x,fval] = fmincon(@(kappa, theta, sigma) cfun, x0, A, b) %minimize
Était-ce utile?

La solution

The optimization parameters are always passed as vector.

[x,fval] = fmincon(@(x) cfun(x(1),x(2),x(3)), x0, A, b)
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top