fminsearch not working, a few errors
Show older comments
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!
Answers (1)
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 Comments
M
on 12 Oct 2013
Concerning your second comment, I can separate the loglikehood function from the rest of the code.
Did you mean to say you "can" or you "can't"?
You will not be able to minimize a function with randomly varying output using fminsearch or any other MATLAB solver. MATLAB solvers are for minimizing deterministic functions.
I've also just now noticed an additional error. You are using "prob" both for the name of the function, and its output argument.
I have "prob(-7,-12,13,2,3,2,6)" just to test if the function works and it does since it gives me a single value.
If a single value is all that is required to know the function is working, why not just have the function return a value of 1 all the time?
M
on 12 Oct 2013
It makes no sense to be minimizing a function that varies randomly every time you evaluate it. How do you even define the minimum, when the graph of the function changes randomly with each call?
What is the minimum of the following function f(x)
function y=f(x)
y=( x-rand(1) )^2;
end
M
on 12 Oct 2013
I'm trying to ask MATLAB to give me the Var-Cov Matrix such that the value that the function gives me is 1.
But for any fixed covariance matrix that you pick, the value that the function outputs will change randomly each time it is executed. So how can you ever select a covariance matrix that makes the function give you 1 all the time?
Here is a bit more information that perhaps will be relevant to you.
FMINSEARCH is an iterative solver. It works by evaluating the function repeatedly, sampling its graph multiple times, feeling its way downhill until it finds a minimum. If you don't freeze the value of the random draw while the iterative minimization is in progress, the location of the minimum becomes a randomly moving target, and fminsearch will not be able to find it.
Thus, I repeat, it only makes sense to freeze all randomly generated data that the function depends on until its minimum has been computed. How would you do that? By not doing the random draw inside the function itself. Do the draw outside the function and then pass it to the function as a fixed parameter.
Categories
Find more on Direct Search in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!