0

The following MATLAB code is used by Dattatreya et al in the book ‘Angular Statistics’ to find the mle of a certain probability distribution.

function [fun]=w_eiw_fminmle(X)
    th=[8 9 13 13 14 18 22 27 30 34383840 44 45 47 48 48 48 48 50 53 56 57 58 58 61 63 64 64 64 65 65 68 70 73 78 78 78 83 83 88 88 88 90 92 92 93 95 96 98 100 103 106 113 118 138 153 153 155 204 215 223 226 237 238 243 244 250 251 257 268 285 319 343 350];
    th=th.*pi./180;
    th=sort(th); 
    n=76;
    c=X(1) 
    lambda=X(2)
    m=5;

    g=0;
    for k=0:m
        g=g+((lambda.*c).*((th+2*k*pi)).^(-(c+1))).*(exp(-(th+2*k*pi).^(-c)).^lambda); 
    end

    fun=0; 
    for i=1:n
        fun=fun+log(g(i)); 
    end

    fun=-(fun); 

return

I tried to run this code in MATLAB and receive an error stating that X is unidentified. How do I fix this error?

juju89
  • 554
  • 2
  • 5
  • 18
Will
  • 190
  • 7

1 Answers1

0

To use this function and display its output, try adding something like

your_input = [[1,2], [3,4]];
[output] = w_eiw_fminmle(your_input);
disp(output)

before the rest of your code. This may solve your error, as in your code you didn't show what do you use for input of your function. Also, your line 10 starting with g= ends with . which is the first part of an element-wise multiplication operator being .*. This will give you an error, try merging line 10 and 11 into one line.

g=g+((lambda.*c).*((th+2*k*pi)).^(-(c+1))).*(exp(-(th+2*k*pi).^(-c)).^lambda);

In line 8, you define g = 0, then you iterate over k in line 9, where it is changed iteratively. g is still a scalar. Later in line 12, you use g(i) where i goes from 1 to 76 (n = 76). As you can see, g has a single value and you are trying to get the value of its 76th element, which will give you an error.

If this code is taken from a book as you say, there are two likely scenarios I can imagine, most likely there was an error when you typed your function, or the book has a code that won't run (book typos or poor book). I would double check your code transcription. Good luck!

Martin
  • 119
  • 8