Effacer les filtres
Effacer les filtres

fminsearch not working, a few errors

1 vue (au cours des 30 derniers jours)
M
M le 12 Oct 2013
Commenté : Matt J le 13 Oct 2013
Hi all,
I'm a novice when it comes to this and I was wondering if someone could help me. I have the following function:
function [prob]=prob(alpha,beta,L32,L33,L42,L43,L44)
%Defining global variables thar are also used in this function
global char price choice N
%L matrix in a Choleski factor
L = [0 0 0 0 ; 0 1 0 0 ; 0 L32 L33 0 ; 0 L42 L43 L44];
%Var-Cov matrix calculated from the L matrix
omega = L*L.';
%Var-Cov matrix of the differences between the errors
M = [1 0 -1 0 ;
0 1 -1 0 ;
0 0 -1 1];
omega_tilda = M*omega*M.';
%Random draws from MVN(0,omega)
error = mvnrnd([0 0 0 0],omega,N);
%Calculate 4 utilities for each individual i; the resulting matrix is Nx4
U=zeros(N,4);
for i=1:N
U(i,1)=error(i,1);
for j=1:3
U(i,j+1) = char(i,j)*beta - price(i,j)*alpha + error(i,j);
end
end
%Checking if the simulation matches with the actual result
%individual i will get a 1 if there is a match, 0 if no match
C=zeros(N,1);
TF=zeros(N,1);
for i=1:N
if U(i,1)>U(i,2) && U(i,1)>U(i,3) && U(i,1)>U(i,4)
C(i,1)=0;
elseif U(i,2)>U(i,1) && U(i,2)>U(i,3) && U(i,1)>U(i,4)
C(i,1)=1;
elseif U(i,3)>U(i,1) && U(i,3)>U(i,2) && U(i,3)>U(i,4)
C(i,1)=2;
elseif U(i,4)>U(i,1) && U(i,4)>U(i,2) && U(i,4)>U(i,3)
C(i,1)=3;
end
if C(i,1)==choice(i,1)
TF(i,1)=1;
else
TF(i,1)=0;
end
end
%Choice Probability
prob=-log(sum(TF)/N);
end
Then I have the following main file:
%Defining global variables
global char price choice N
%Loading the data into MATLAB
pset1=importdata('consumptiondata.txt');
%Number of individuals in one market
N=50;
%Submatrix for the characteristic data
char=pset1(:,1:3);
%Submatrix for the price data
price=pset1(:,4:6);
%Submatrix for the alternative that individual i chooses
choice=pset1(:,7);
prob(-7,-12,13,2,3,2,6)
%Search for the optimal parameters
options = optimset('Display','iter');
[x,fval] = fminsearch(@prob,[-7 -12 1 1 1 1 1])
I get the following errors:
Error using prob (line 7)
Not enough input arguments.
Error in fminsearch (line 191)
fv(:,1) = funfcn(x,varargin{:});
Error in PSet1_IO (line 27)
[x,fval] = fminsearch(@prob,[-7 -12 1 1 1 1 1])
The function works fine when I test it with prob(-7,-12,13,2,3,2,6). Any help would be great, thanks!
  2 commentaires
Matt J
Matt J le 12 Oct 2013
Please apply the
formatting button to your code to make it distinguishable from your text.
M
M le 12 Oct 2013
I hope that fixed it, sorry about that.

Connectez-vous pour commenter.

Réponses (1)

Matt J
Matt J le 12 Oct 2013
Modifié(e) : Matt J le 12 Oct 2013
The input syntax of prob should not be prob(-7,-12,13,2,3,2,6). You need to rewrite it to accept a vector of unknowns prob([-7,-12,13,2,3,2,6]).
Also, there are better ways to pass constant data to an objective function than using global variables, see http://www.mathworks.com/help/optim/ug/passing-extra-parameters.html
Finally, your loglikelihood function should not be the place that random data gets generated. All of the following stuff should be done, I'm guessing, outside of prob with "error" then passed to prob() as constant data along with char, price, and choice
%L matrix in a Choleski factor
L = [0 0 0 0 ; 0 1 0 0 ; 0 L32 L33 0 ; 0 L42 L43 L44];
%Var-Cov matrix calculated from the L matrix
omega = L*L.';
%Var-Cov matrix of the differences between the errors
M = [1 0 -1 0 ;
0 1 -1 0 ;
0 0 -1 1];
omega_tilda = M*omega*M.';
%Random draws from MVN(0,omega)
error = mvnrnd([0 0 0 0],omega,N);
  9 commentaires
M
M le 12 Oct 2013
Modifié(e) : M le 12 Oct 2013
That makes sense. Thank you for all your help. Just one more question. When I draw from MVN([0,0,0,0],omega) outside of the function. does omega have to be known or can I leave it unknown?
Matt J
Matt J le 13 Oct 2013
Modifié(e) : Matt J le 13 Oct 2013
Yes, it has to be known. When you generate simulated data, such as "error" you always have to know the ground truth parameters.

Connectez-vous pour commenter.

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by