fminsearch-网格值函数的似然估计

发布于 2025-01-07 06:18:41 字数 2134 浏览 2 评论 0原文

我想创建一个带有网格的多变量(这里是四个)函数,然后对任何 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

秋日私语 2025-01-14 06:18:41
  1. anemon 函数来看,mu 应该是 m3。
  2. 由于您尝试估计参数 m1,因此需要将所有其他数据作为参数或全局变量传递给 modelanemon 函数。

我通常不会推荐全局变量,但我认为尝试将参数传递给本地函数句柄可能有点太先进了。 :-) 你的代码将是这样的:

    % MFILE3
    %Create a model which I'm going to use for parameter estimation
    function loglik= modelanemon(p)
      global Ystar m2 m3 Z X Y kk ll n x;
      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
  1. From the anemon function, mu should be m3.
  2. Since you're trying to estimate the parameter m1, you'll need to pass all other data to the 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:

    % MFILE3
    %Create a model which I'm going to use for parameter estimation
    function loglik= modelanemon(p)
      global Ystar m2 m3 Z X Y kk ll n x;
      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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文