fminsearch-网格值函数的似然估计
我想创建一个带有网格的多变量(这里是四个)函数,然后对任何 X,Y 进行插值。假设这是我的概率密度函数,我计算它们在 X 上的边际。我使用这个函数构建一个数据集。我的每个 M 文件都运行良好,至少我这么认为。但是当我运行估计部分时,我收到一个错误。如果我的错误可以弥补,你能帮帮我吗?
clear all;
%%%Some parameters
m1=0.01;
m2=0.02;
m3=0.001;
Z=zeros(10,20,10,5);
% MFILE1
%Create a four variable function
% function w=anemon(m1,m2,m3,X,Y,k,l)
% w=(m1*k+(m1+m2).*l)+X.*exp(-m3.*Y);
% end
% MFILE2
%Interpolate any (Xstar,Ystar) with a nonsence interpolation code
%I wrote it only for giving an example
% function p=interpanemon(Xstar,Ystar,m1,m2,m3,Z,X,Y,kk,ll)
% p=0;
% for i=1:length(X)-1
% for j=1:length(Y)-1
% if ((Y(j)<=Ystar)&&(Ystar<=Y(j+1)))&&((X(i)<=Xstar)&&(Xstar<=X(i+1)))
% p=(Z(i,j,kk,ll)+Z(i+1,j,kk,ll)+Z(i,j+1,kk,ll)+Z(i+1,j+1,kk,ll))/4;
% end
% end
% end
% return
% end
% MFILE3
%Create a model which I'm going to use for parameter estimation
% function loglik= modelanemon(p)
% global n x m2 m3 kk ll ;
% f =marjinteranemon(Ystar,p,m2,m3,Z,X,Y,kk,ll);
% loglik0=(-1)*(x.*log(f)+(n-x).*log(1-f));%minus likelihood
% loglik=sum(loglik0);
% end
%%
X=linspace(0,10,6);
Y=linspace(0,20,6);
for k=1:10
kk=k-1;
for l=1:5
ll=l-1;
for i=1:length(X)
for j=1:length(Y)
Z(i,j,k,l)=anemon(m1,m2,m3,X(i),Y(j),kk,ll);
end
end
end
end
%%%Now run all M-Files for self control
anemon(m1,m2,m3,X,Y,4,5)
interpanemon(3,4,m1,m2,m3,Z,X,Y,4,5)
marjinteranemon(3,Z,X,Y,m1,m2,m3,4,5)
%STARTING PROBLEM PART
%SCRIPT fminsearch, I will be greatful if you have any advise. This part of % code doesn't work
% global n Ystar x kk ll m1 m2 m3;
% m1=0.1;
% m2=0.2;
% m3=0.3;
% Ystar=[1 3 6 9 12 18]';
% kk=[1 1 2 3 3 4]';
% ll=[0 0 1 3 4 5]';
% y=marjinteranemon(Ystar,Z,X,Y,m1,m2,m3,kk,ll);
% n=100;
% x=y.*n;
% pstart=5;
% [p1,modelvalue]=fminsearch(@modelanemon,pstart);
I want to create a multivariable(here four) function with a grid and than interpolate it for any X,Y. Assuming that is my probablity density function I calculate their marginal on X. I use this function to built a dataset. Each of my M-Files runs well,at least I think so. But I recieve an error, when I let run the estimation part. Could you please help me, if my mistake is mendable.
clear all;
%%%Some parameters
m1=0.01;
m2=0.02;
m3=0.001;
Z=zeros(10,20,10,5);
% MFILE1
%Create a four variable function
% function w=anemon(m1,m2,m3,X,Y,k,l)
% w=(m1*k+(m1+m2).*l)+X.*exp(-m3.*Y);
% end
% MFILE2
%Interpolate any (Xstar,Ystar) with a nonsence interpolation code
%I wrote it only for giving an example
% function p=interpanemon(Xstar,Ystar,m1,m2,m3,Z,X,Y,kk,ll)
% p=0;
% for i=1:length(X)-1
% for j=1:length(Y)-1
% if ((Y(j)<=Ystar)&&(Ystar<=Y(j+1)))&&((X(i)<=Xstar)&&(Xstar<=X(i+1)))
% p=(Z(i,j,kk,ll)+Z(i+1,j,kk,ll)+Z(i,j+1,kk,ll)+Z(i+1,j+1,kk,ll))/4;
% end
% end
% end
% return
% end
% MFILE3
%Create a model which I'm going to use for parameter estimation
% function loglik= modelanemon(p)
% global n x m2 m3 kk ll ;
% f =marjinteranemon(Ystar,p,m2,m3,Z,X,Y,kk,ll);
% loglik0=(-1)*(x.*log(f)+(n-x).*log(1-f));%minus likelihood
% loglik=sum(loglik0);
% end
%%
X=linspace(0,10,6);
Y=linspace(0,20,6);
for k=1:10
kk=k-1;
for l=1:5
ll=l-1;
for i=1:length(X)
for j=1:length(Y)
Z(i,j,k,l)=anemon(m1,m2,m3,X(i),Y(j),kk,ll);
end
end
end
end
%%%Now run all M-Files for self control
anemon(m1,m2,m3,X,Y,4,5)
interpanemon(3,4,m1,m2,m3,Z,X,Y,4,5)
marjinteranemon(3,Z,X,Y,m1,m2,m3,4,5)
%STARTING PROBLEM PART
%SCRIPT fminsearch, I will be greatful if you have any advise. This part of % code doesn't work
% global n Ystar x kk ll m1 m2 m3;
% m1=0.1;
% m2=0.2;
% m3=0.3;
% Ystar=[1 3 6 9 12 18]';
% kk=[1 1 2 3 3 4]';
% ll=[0 0 1 3 4 5]';
% y=marjinteranemon(Ystar,Z,X,Y,m1,m2,m3,kk,ll);
% n=100;
% x=y.*n;
% pstart=5;
% [p1,modelvalue]=fminsearch(@modelanemon,pstart);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
anemon
函数来看,mu 应该是 m3。modelanemon
函数。我通常不会推荐全局变量,但我认为尝试将参数传递给本地函数句柄可能有点太先进了。 :-) 你的代码将是这样的:
anemon
function, mu should be m3.modelanemon
function as parameters or globals.I usually wouldn't recommend globals, but I think it may be a little too advanced to try passing parameters to local function handles. :-) You're code will be something like this: