fminsearch not working, a few errors

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 Comments

Please apply the
formatting button to your code to make it distinguishable from your text.
I hope that fixed it, sorry about that.

Sign in to comment.

Answers (1)

Matt J
Matt J on 12 Oct 2013
Edited: Matt J on 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 Comments

Hm, okay. I understand your first comment.
Concerning your second comment, I can separate the loglikehood function from the rest of the code. The function essentially takes omega with 5 unknown parameters, draws random "errors" from MVN([0,0,0,0],omega), calculates a value, U(i,j), a function of the "errors", "alpha", and "beta." Then it does some other stuff comparing the U(i,j)'s and you get a value in the end.
At the end of the function, I should have a function that takes in the 7 inputs and gives me an output. 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.
Matt J
Matt J on 12 Oct 2013
Edited: Matt J 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?
I think I can.
The prob function takes in 7 inputs and gives 1 output. However, 5 of those inputs are in the var-cov matrix (omega) of the MVN(0,omega), the distribution where "error" is drawn from. However, I don't know what omega is.
However, since I am drawing "error" from a distribution, "error" is bound to change. Thus, are you saying I cannot use fminsearch for a problem like this?
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
When you draw the errors, you then use the "errors" in a function and after another step, it gives you one of 4 values: 0, 1, 2, or 3. We compare this to the "true" value, which also is either 0, 1, 2, or 3. If there is a match, it gets the value 1, if not, a value of 0 is given.
We do this for each of the 50 observations, where all the "errors" are drawn from the same distribution. I then sum up all those 0s and 1s and divide it by N.
I'm trying to ask MATLAB to give me the Var-Cov Matrix such that the value that the function gives me is 1.
Matt J
Matt J on 12 Oct 2013
Edited: Matt J 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?
Matt J
Matt J on 12 Oct 2013
Edited: Matt J on 12 Oct 2013
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.
M
M on 12 Oct 2013
Edited: M on 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 on 13 Oct 2013
Edited: Matt J on 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.

Sign in to comment.

Tags

Asked:

M
M
on 12 Oct 2013

Commented:

on 13 Oct 2013

Community Treasure Hunt

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

Start Hunting!