Matrix argument in Matlabs MLE function

2 次查看(过去 30 天)
Dear all,
I want to use Matlabs MLE function to find the maximum of a log-likelihood expression. The expression contains matrix multiplication of the unknown parameters (called theta here), so I want to submit a matrix. The matrix is of variable size, so I don't want to hard code any sizes.
What I've tried so far, with failed results, are seen below. First of all, I create some data that I use for all my trials:
global p b
p = [0.5 1 0.7; 0.4 0.5 0.2]'; %some matrix
b = [2 .6 .3 .1]; %some other matrix
theta0 = [1 1; 1 1; 1 1; 1 1]'; %initial guess of unknowns
y_meas = [2.32 3.17 2.53]; %measured values
Note that the matrix sizes above will not always be the same, they can vary.
Trial 1)
%Log-likelihood function
loglik = @(y_meas,theta) sum(sum(y_meas.*log( b*(p*theta)' ) - b*(p*theta)'));
%Maximum likelihood estimation
est = mle(y_meas,'logpdf',loglik,'start',theta0)
The above expression failes since I think you can only send and recieve individual arguments (not matrices or vectors) in MLE and the custom loglik function.
Trial 2)
%Create string argument with matrix indices like 'theta(1,1),theta(1,2),...'
[ro,co] = size(theta_true);
str = '';
for j = 1:co
for i = 1:ro
str = [str 'theta(' num2str(i) ',' num2str(j) '),'];
end
end
str = str(1:end-1); %Remove last comma
%Log-likelihood function
eval( ['loglik = @(y_meas,' str ') sum(sum(y_meas.*log( b*(p*theta)'' ) - b*(p*theta)''));'] );
%Maximum likelihood estimation
est = mle(y_meas,'logpdf',loglik,'start',theta0(:));
The above fails with unbalanced or unexpected parenthesis. I gather you cannot recieve arguments with indices in the custom function loglik?
Trial 3)
%Create string argument with matrix indices like 'theta11,theta12,...'
[ro,co] = size(theta_true);
str = '';
for j = 1:co
for i = 1:ro
str = [str 'theta' num2str(i) num2str(j) ','];;
end
end
str = str(1:end-1); %Remove last comma
Like the arguments above, I get no errors when passing them to the custom function, but I get the problem of how to perform the matrix multiplication.
Does anyone have an idea of how to use a variable number of arguments in MLE for a custom log-likelihood function containing matrix multiplication with the unknowns?
Thank you!

采纳的回答

Cam Salzberger
Cam Salzberger 2015-9-2
Hello Ida,
I understand that you would like to pass a matrix of parameters into the mle function, and allow the same function call to occur no matter the size of the matrix. The issue is that mle will call the custom logpdf function with the parameters matrix, with each entry in the matrix as a separate input to the function.
I am not certain that it is the best way, but you can use varargin when defining the function handle to capture all of the parameter inputs in one cell array. You'll need a separate parameter for the size of the matrix, and then you can use cell2mat and reshape to turn the cell array back into the matrix you want.
As a side note, you can also pass the values of p and b as "constant" input arguments, rather than having to declare them as global variables.
function testMleWithMat
p = [0.5 1 0.7; 0.4 0.5 0.2]'; %some matrix
b = [2 .6 .3 .1]; %some other matrix
theta0 = [1 1; 1 1; 1 1; 1 1]'; %initial guess of unknowns
y_meas = [2.32 3.17 2.53]; %measured values
est = mle(y_meas,'logpdf',...
@(data,varargin) custlogpdf(data,p,b,size(theta0),varargin),...
'start',theta0);
function tot = custlogpdf(y_meas,p,b,dims,params)
theta = reshape(cell2mat(params),dims);
y_meas = y_meas'; % Gets permuted in mle
tot = sum(sum(y_meas.*log(b*(p*theta)')-b*(p*theta)'));
I hope that this helps with your optimization.
-Cam
  1 个评论
Ida
Ida 2015-9-3
Dear Cam,
thank you! This was just what I was looking for =) Your suggestion works fine, now it's just a matter of optimizing all other stuff... =)
Thank you again!

请先登录,再进行评论。

更多回答(0 个)

Community Treasure Hunt

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

Start Hunting!

Translated by