'too many input arguments' in mhsample

I am having the error 'too many input arguments' during my function call which itself is called by another built-in MatLab function 'mhsample'. I have not been able to figure out any reason for this error. Following is the snippet of the code:
% specification of unnormalized posterior pdf to sample from
pdfPost=@(x)posteriorDist(x,C,artificialNetwork,xPrior);
% specification of proposal distribution
pdfProp=@(x)priorDist(x,xPrior);
% specification of random number generator from proposal distribution
generator = @(x) (xPrior(:,1)+rand(7,1).*(xPrior(:,2)-xPrior(:,1)));
% calculation of posterior of model parameters (lambda)
smpl = mhsample(x0,nsamples,'pdf',pdfPost,'proppdf',pdfProp,'proprnd',generator);
Following is the error message:
Error using mhsample (line 117)
Error occurred while trying to evaluate the user-supplied
proppdf function '@(x)priorDist(x,xPrior)'.
Error in metroPolisHastingSampling (line 33)
smpl =
mhsample(x0,nsamples,'pdf',pdfPost,'proppdf',pdfProp,'proprnd',generator);
Caused by:
Error using @(x)priorDist(x,xPrior)
Too many input arguments.
'metroPolisHastingSampling' is the name of the script I have used for implementing Metropolis-Hastings sampling method.
So, the error is in proposal distribution which only has one input argument? So, why am I getting this error? Any useful comment is appreciated?
The code for priorDist is as follows:
function [p] = priorDist(x,xprior)
p=1/prod(xPrior(:,2)-xPrior(:,1));
end
I have provided all other codes below: Here is the code for 'PosteriorDist':
function ptr = posteriorDist(x,C,artificialNetwork)
lh=likelihoodPost(artificialNetwork,x,C); % likelihood of observation given these parameters
priorParam = priorDist(x); % computation of joint prior of model parameters
ptr=lh*priorParam; % computation of unnormalized posterior
end
rest of the definitions are
x0=5*ones(7,1);
nsamples=10000;
xprior=[zeros(7,1) 15*ones(15,1)];
C=rand(7,7);
I have taken a random just for testing purposes. artificialNetwork contains information and relevant data.

4 个评论

You need to give us the code for priorDist(), or at least the line that says
function output = priorDist(......
Evidently priorDist wants only one input, or none, and you're passing it two.
I have edited question to include the priorDist function. The second parameter in the function is a fixed variable like the parameters of the distribution. In fact, I am using a uniform distribution for start, which does not even need x values.
Why are you passing x into distPrior() when you don't even use it?
you are right, I don't need to pass x in this case. But in a general case, I will need x too. Therefore, I kept x in priorDist function. Also, I tried removing x but it gives the same error.

请先登录,再进行评论。

 采纳的回答

"proppdf takes two arguments as inputs with the same type and size as start."
"The proposal distribution q(x,y) gives the probability density for choosing x as the next point when y is the current point. It is sometimes written as q(x|y)."
Your line
pdfProp=@(x)priorDist(x,xPrior);
should probably just be
pdfProp = @priorDist;

8 个评论

It still gives the same error.
I will need more of your code to test with. Definition of the function posteriorDist, and values for x0, nsamples, xPrior, C, and artificialNetwork
I have edited the question to include the codes you asked. I am not sure though if it would be helpful because there are many other functions that are needed to run this routine. It is nearly impossible to include everything here. Let me know if it helps.
Your line
xprior=[zeros(7,1) 15*ones(15,1)];
needs to be changed to use ";" between the two parts.
You are passing xPrior to PosteriorDist as a fourth argument, but you only define the function as taking three arguments.
I apologize. I used xprior as an argument in my PosteriorDist function. After your answer, I completely removed xPrior from all of the code, but the same error persists. I just wanted to let you know that artificialNetwork variable is structure, can that cause a problem?
I am at the limit of what I can test without having more of your code. My test versions are attached. I do not have the likelihoodPost function.
I understand. I will test my code again with some of your suggestions and try to find out the problem.
Walter Roberson, I could figure out the problem finally. mhsample expects two arguments in proposal distribution function: x and y. My proposal distribution was dependent only on one argument so I passed one argument x. But when I pass two argument x and y, it works even though y is not used in my proposal distribution.

请先登录,再进行评论。

更多回答(0 个)

Community Treasure Hunt

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

Start Hunting!

Translated by