필터 지우기
필터 지우기

fminsearch not working, a few errors

조회 수: 1 (최근 30일)
M
M 2013년 10월 12일
댓글: Matt J 2013년 10월 13일
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
Matt J
Matt J 2013년 10월 12일
Please apply the
formatting button to your code to make it distinguishable from your text.
M
M 2013년 10월 12일
I hope that fixed it, sorry about that.

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

답변 (1개)

Matt J
Matt J 2013년 10월 12일
편집: Matt J 2013년 10월 12일
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
M
M 2013년 10월 12일
편집: M 2013년 10월 12일
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 2013년 10월 13일
편집: Matt J 2013년 10월 13일
Yes, it has to be known. When you generate simulated data, such as "error" you always have to know the ground truth parameters.

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

카테고리

Help CenterFile Exchange에서 Solver Outputs and Iterative Display에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by