I'm trying to optimize a nested objective function. The main script is as follows:
c = 1;
cd data;
formatSpec = 'c_p_%d.csv';
filename = sprintf(formatSpec,c);
data = readtable(filename, 'ReadVariableNames', true);
cd ..
alphaGuess = 0.01;
rhoGuess = 0.01;
beta_bGuess = 0.5;
beta_sGuess = 0.5;
beta_nGuess = 0.5;
A = [0 0 -1 1 0; 0 0 0 -1 1];
b = [0; 0];
Aeq = [];
beq = [];
lb = [0 0 0 0 0];
ub = [1 1 1 1 1];
x0 = [alphaGuess rhoGuess beta_bGuess beta_sGuess beta_nGuess];
x = fmincon(L_c,x0,A,b,Aeq,beq,lb,ub);
alpha = x(1);
rho = x(2);
beta_b = x(3);
beta_s = x(4);
beta_n = x(5);
The objective function L_c is defined in the following file:
function L = L_c(alpha,rho,beta_b,beta_s,beta_n)
data = readtable('data');
N = length(data.id);
P_vector = zeros(N,1);
for i = 1:N
k = string(data.k(i));
theta_b = data.b_prod(i);
theta_s = data.s_prod(i);
P_vector(i) = log(P_i(alpha,rho,theta_b,theta_s,beta_b, beta_s,beta_n,k));
end
P_vector(isnan(P_vector))=0;
L = - sum(P_vector);
end
P_i, the function in L_c, is defined in another file:
function P = P_i(alpha,rho,theta_b,theta_s,beta_b,beta_s,beta_n,k)
beta = beta_b;
Psi_b = Psi_i_k(alpha,rho,theta_b,theta_s,beta);
beta=beta_s;
Psi_s = Psi_i_k(alpha,rho,theta_b,theta_s,beta);
beta=beta_n;
Psi_n = Psi_i_k(alpha,rho,theta_b,theta_s,beta);
if string(k) == 'b'
P = exp(Psi_b)/(exp(Psi_b)+exp(Psi_s)+exp(Psi_n));
elseif string(k) == 's'
P = exp(Psi_s)/(exp(Psi_b)+exp(Psi_s)+exp(Psi_n));
else
P = exp(Psi_n)/(exp(Psi_b)+exp(Psi_s)+exp(Psi_n));
end
end
Psi_i_k, the function inside P_i, is defined in another file:
function Psi = Psi_i_k(alpha,rho,theta_b,theta_s,beta)
Alpha = alpha.^(alpha/(1-alpha));
Buyer = (theta_b*beta).^(rho/(1-rho));
Seller = (theta_s*(1-beta)).^(rho/(1-rho));
Numerator = (1-alpha*beta)*Buyer + (1-alpha*(1-beta))*Seller;
Denominator = (Buyer+Seller).^((rho-alpha)/(rho*(1-alpha)));
Psi=Alpha*Numerator/Denominator;
end
When I run the main script without fmincon, Matlab returns a valid value. But when I run the fmincon function, Matlab returns the following error message:
I have attached a replication file to this post. Could someone please help me figure out what is wrong? Thanks!

 Réponse acceptée

Alan Weiss
Alan Weiss le 1 Nov 2018
What are your control variables? I mean, the variables that you want fmincon to move to look for an optimum? It looks like you want alpha,rho,beta_b,beta_s,beta_n to be the control variables.
If this is the case, then you need to make a new variable x = [alpha,rho,beta_b,beta_s,beta_n] and write L_c in terms of x:
function L = L_c(x)
alpha = x(1);
rho = x(2);
beta_b = x(3);
beta_s = x(4);
beta_n = x(5);
... % I mean the rest of your code goes here
Then you need to call the function differently:
x = fmincon(@L_c,x0,A,b,Aeq,beq,lb,ub); % Notice the @
Alan Weiss
MATLAB mathematical toolbox documentation

3 commentaires

Thanks for your response! My control variables are (alpha, rho, beta_b, beta_s, beta_n). I use fmincon to solve a log-likelihood maximization problem. I corrected the code as per your suggestion. Now the main script looks like this:
close all
clear
clc
c = 1;
cd data;
formatSpec = 'c_p_%d.csv';
filename = sprintf(formatSpec,c);
data = readtable(filename, 'ReadVariableNames', true);
cd ..
alphaGuess = 0.01;
rhoGuess = 0.01;
beta_bGuess = 0.5;
beta_sGuess = 0.5;
beta_nGuess = 0.5;
A = [0 0 -1 1 0; 0 0 0 -1 1];
b = [0; 0];
Aeq = [];
beq = [];
lb = [0 0 0 0 0];
ub = [1 1 1 1 1];
x0 = [alphaGuess rhoGuess beta_bGuess beta_sGuess beta_nGuess];
x = [alpha,rho,beta_b,beta_s,beta_n];
x = fmincon(@L_c,x0,A,b,Aeq,beq,lb,ub);
The L_c function in a separate file looks like this:
function L = L_c(x)
alpha = x(1);
rho = x(2);
beta_b = x(3);
beta_s = x(4);
beta_n = x(5);
data = readtable('data');
N = length(data.id);
P_vector = zeros(N,1);
for i = 1:N
k = string(data.k(i));
theta_b = data.b_prod(i);
theta_s = data.s_prod(i);
P_vector(i) = log(P_i(alpha,rho,theta_b,theta_s,beta_b, beta_s,beta_n,k));
end
P_vector(isnan(P_vector))=0;
L = - sum(P_vector);
end
When running the main script, however, I encounter the following error message:
Get rid of the line causing the error:
x = [alpha,rho,beta_b,beta_s,beta_n];
This line does nothing but throw an error. If you want to keep it as a reminder, comment it out.
Alan Weiss
MATLAB mathematical toolbox documentation
Xiaoji Xu
Xiaoji Xu le 2 Nov 2018
It worked. Thanks!

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Function Creation dans Centre d'aide et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by