Search code examples
matlabparametersestimation

fminsearch-likelihood estimation for grid valued function


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);

Solution

    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