Page 1 of 2 12 LastLast
Results 1 to 15 of 17

Math Help - NMaximize in Mathematica

  1. #1
    xdu
    xdu is offline
    Newbie
    Joined
    Nov 2009
    Posts
    19

    NMaximize in Mathematica

    Hi all,

    When I use NMaximize in mathematica, I always get the error like this:
    NMaximize::nrnum: The function value 10613.354-3.142 I is not a real number at {r,x,\[Alpha]} = {0.1909383,0.7839172,1.676922}.

    My function is complicated because it has hypergeometric function, so does anyone know how to solve this problem or avoid it?

    Any suggestions are very appreciated,

    xdu
    Follow Math Help Forum on Facebook and Google+

  2. #2
    A Plied Mathematician
    Joined
    Jun 2010
    From
    CT, USA
    Posts
    6,318
    Thanks
    4
    Awards
    2
    Well, I can't say I'm an expert with hypergeometric functions, though I have seen them from time to time. I do know something about optimization, and I know something about Mathematica. Could you post a little more background? What's the original problem you're trying to solve?
    Follow Math Help Forum on Facebook and Google+

  3. #3
    xdu
    xdu is offline
    Newbie
    Joined
    Nov 2009
    Posts
    19
    Hi Adrian,

    Many thanks for your reply.

    I made a model and tried to using maximize log likelihood method to estimate the parameters of it. The function of my model is:f (r,x,a)=(r*x)^n *Gamma(a+1)*Gamma(n)*Hpergeometric2F1[n,n,n+a,x-x*r]/(x*n*Gamma(n+a)*(HypergeometricPFQ[1,1,1,2,1+a,x]-(1-r)*HypergeometricPFQ[1,1,1,2,1+a,x-x*r])).
    where n is a vector, e.g.n=[1,1,2,3,4,4,...150,532]. and then using maximize log likelihood method to estimate the parameters' values, that are r, x, a.
    the log likelihood function is
    L(x,r,a)=sum(log(f(r,x,a))) that is sum all the elements of vector n.
    So I use the NMaximize command:
    NMaximize[{L(x,r,a),0<x<1,0<r<1,a>0},{x,r,a}]
    The problem is that I got the error "NMaximize::nrnum: The function value 10613.354-3.142 I is not a real number at", sometime I could not get the answer.
    Also I got different answers when I used different WorkingPrecision and MaxIterations.
    I think there are some values are indeterminate or complex number, do you know how to delete these points when it is running NMaximize? Or you have other method to solve it?

    Many thanks and Look forward to your reply.

    Cheers,
    Xiaoguang
    Follow Math Help Forum on Facebook and Google+

  4. #4
    A Plied Mathematician
    Joined
    Jun 2010
    From
    CT, USA
    Posts
    6,318
    Thanks
    4
    Awards
    2
    So, just to be clear, you've got a vector \vec{n}.

    1. Are the elements of \vec{n} all integers?

    2. You've got a model function

    \displaystyle{f(r,x,a)=\frac{(rx)^{\vec{n}}\,\Gamm  a(a+1)\,\Gamma(\vec{n})\, _{2}F_{1}(\vec{n},\vec{n},\vec{n}+a,x-rx)}{x\,\vec{n}\,\Gamma(\vec{n}+a)\,( _{p}F_{q}(1,1,1,2,1+a,x) -(1-r)\, _{p}F_{q}(1,1,1,2,1+a,x-rx))}}. Is this correct?

    3. I'm assuming that notations like (rx)^{\vec{n}} indicate the vector that is the same length as \vec{n} and whose jth components are equal to (rx)^{n_{j}}. Is that correct?

    4. Same with the gamma function?

    5. How about the _{2}F_{1} function: same there?

    6. How are we supposed to interpret \vec{n}\,\Gamma(\vec{n})? Is that a dot product?

    7. Are r, x, a all scalars?

    8. Your goal is to estimate the parameters r, x, a using the log likelihood method. Must you use the log likelihood method? Or could you use least squares?

    9. Of what physical system is this a model?
    Follow Math Help Forum on Facebook and Google+

  5. #5
    xdu
    xdu is offline
    Newbie
    Joined
    Nov 2009
    Posts
    19
    Hi Adrian,

    Thanks, and here are your questions:
    1. Are the elements of all integers?
    Yes, they are positive integers.
    2. You've got a model function f(x,r,a) ...
    It is correct. but here the vector n should be the element of n, that is n(i). so the function f(x,r,a) should be a vaule rather than a vector.
    Thus the questions number 3,4,5 are clear. all of them are not vector. the parameters x,r,a are also numbers rather than scalars.

    The only place which is used the vector n is the log likelihood function which is the sum of all the elements of the vector n.
    It's better use maximize log likelihood method because I've done something before this. Of course if it does not work, we can also use least squares method.

    The model I am doing is discrete markov chains model

    I am not sure if it is clear now?

    Cheers,
    Xiaoguang
    Follow Math Help Forum on Facebook and Google+

  6. #6
    xdu
    xdu is offline
    Newbie
    Joined
    Nov 2009
    Posts
    19
    Hi Adrian,

    Thanks, and here are your questions:
    1. Are the elements of all integers?
    Yes, they are all positive integers.
    2. You've got a model function f(x,r,a) ...
    It is correct. but here the vector n should be the element of n, that is n(i). so the function f(x,r,a) should be a value rather than a vector.
    Thus the questions number 3,4,5 are clear. all of them are not vector. the parameters x,r,a are also numbers rather than scalars.

    The only place which is used the vector n is the log likelihood function which is the sum of all the elements of the vector n.
    It's better use maximize log likelihood method because I've done something before this. Of course if it does not work, we can also use least squares method.

    The model I am doing is discrete markov chains model

    I am not sure if it is clear now?

    Cheers,
    Xiaoguang
    Follow Math Help Forum on Facebook and Google+

  7. #7
    A Plied Mathematician
    Joined
    Jun 2010
    From
    CT, USA
    Posts
    6,318
    Thanks
    4
    Awards
    2
    So you've really got

    \displaystyle{f_{j}(r,x,a)=\frac{(rx)^{n_{j}}\,\Ga  mma(a+1)\,\Gamma(n_{j})\, _{2}F_{1}(n_{j},n_{j},n_{j}+a,x-rx)}{x\,n_{j}\,\Gamma(n_{j}+a)\,( _{p}F_{q}(1,1,1,2,1+a,x) -(1-r)\, _{p}F_{q}(1,1,1,2,1+a,x-rx))}}.

    Is that correct?

    Then your log likelihood function is

    \displaystyle{L(r,x,a)=\sum_{j=1}^{\text{length}(\  vec{n})}\ln(f_{j}(r,x,a)).} Is that correct?

    It's better use maximize log likelihood method because I've done something before this.
    What is the "something" you've done before this? Are you saying you've done this method before, and know it better?
    Follow Math Help Forum on Facebook and Google+

  8. #8
    xdu
    xdu is offline
    Newbie
    Joined
    Nov 2009
    Posts
    19
    Yes, It's correct.
    I do not know which method is better, I said I have done something, which means I did something simple model using Maximize log likelihood method.
    Follow Math Help Forum on Facebook and Google+

  9. #9
    A Plied Mathematician
    Joined
    Jun 2010
    From
    CT, USA
    Posts
    6,318
    Thanks
    4
    Awards
    2
    Ok. Thanks for the clarifications.

    I'm still needing to ask questions, though, so please bear with me.

    1. Is the vector \vec{n} known, or are you trying to find it? Or, perhaps, maybe you are given a number of different vectors \vec{n} and you have to solve this problem for each one?

    2. Going along with question #1 in this post, are you sure you have the right likelihood function? That's a genuine question to which I don't know the answer.

    2. The number of arguments for the generalized hypergeometric function _{p}F_{q} is equal to p+q+1. Do you know what p and q are? They would appear to sum to 5. Is that correct?

    Thanks for your patience in answering these questions!
    Follow Math Help Forum on Facebook and Google+

  10. #10
    xdu
    xdu is offline
    Newbie
    Joined
    Nov 2009
    Posts
    19
    Hi Adrian,

    I should say thanks to you. That's ok to make it clear.

    1.The vector n is known, actually it is the real data set. the data is the species abundance which larger than 0, i.e. 1,2,....or more than one thousand.

    2. I'm pretty sure the function is correct, I did that using my hand and also got the this result by Mathematica. I know it's complicate.

    3 the general hypergeometric function is pFq[a(1),a(2),...a(p);b(1)b(2)...b(q)]. you can have a look at Hypergeometric Function -- from Wolfram MathWorld
    you are right, the number of the arguments in this function is equal to p+q+1 where p=3,q=2 that is a(1)=1,a(2)=1
    a(3)=1 and b(1)=2, b(2)=1+a.
    Is it clear now?

    Thanks for your help

    Cheers
    xiaoguang
    Follow Math Help Forum on Facebook and Google+

  11. #11
    A Plied Mathematician
    Joined
    Jun 2010
    From
    CT, USA
    Posts
    6,318
    Thanks
    4
    Awards
    2
    Ah. Thanks. So the most simplified version of your function is this:

    \displaystyle{f_{j}(r,x,a)=\frac{(rx)^{n_{j}}\,\Ga  mma(a+1)\,\Gamma(n_{j})\, _{2}F_{1}(n_{j},n_{j},n_{j}+a,x-rx)}{x\,n_{j}\,\Gamma(n_{j}+a)\,( _{3}F_{2}(1,1,1,2,1+a,x) -(1-r)\, _{3}F_{2}(1,1,1,2,1+a,x-rx))}}.

    You then defined, as mentioned above,

    \displaystyle{L(r,x,a)=\sum_{j=1}^{\text{length}(\  vec{n})}\ln(f_{j}(r,x,a)).}

    And your goal is to maximize L, correct?

    Looking back at your post # 3, I think may have seen some of your problems. I think they might be in your syntax. When defining your functions, you have to use correct Mathematica syntax. Here's the generalized hypergeometric function in Mathematica:

    Code:
    HypergeometricPFQ[{1,1,1},{2,1+a},x-r x].
    The extra braces, I would guess, are important.

    In addition, I don't think Mathematica understands elided constraints of the form

    Code:
    ...0<x<1...
    .

    Instead, list that as two separate constraints:

    Code:
    ...,0<x, x<1,...
    Unfortunately, I do not have a version of Mathematica that includes the NMaximize command, and I really don't see how to do this problem on WolframAlpha, because of the need to define several things before actually maximizing. However, I will give you the exact syntax that I believe should work, assuming this problem is doable in this manner:

    Code:
    f[j_,r_,x_,a_] := ((r x)^(n[[j]])Gamma[a + 1]Gamma[n[[j]]] Hypergeometric2F1[n[[j]],n[[j]], n[[j]] + a, x - r x])/(x n[[j]] Gamma[n[[j]] + 
                a](HypergeometricPFQ[{1, 1, 1}, {2, 1 + a},x] - 
    (1 - r)HypergeometricPFQ[{1, 1, 1}, {2, 1 + a}, x - r x]))
    
    L[r_,x_,a_] := Sum[Log[f[j, r, x, a]], {j, 1, Length[n]}]
    
    n = {1, 1, 2, 3, 4, 4, 150, 532}
    
    NMaximize[{L(x,r,a),0<x,x<1,0<r,r<1,a>0},{x,r,a}]
    Here the third line definition of n should be whatever your data actually is.

    Try that and let me know how it goes.
    Follow Math Help Forum on Facebook and Google+

  12. #12
    xdu
    xdu is offline
    Newbie
    Joined
    Nov 2009
    Posts
    19
    Thank you. I tried it but for some data it did not work,
    I got something like this:
    SystemException["MemoryAllocationFailure", {\!\(\*
    TagBox[
    RowBox[{"NMaximize", "[",
    RowBox[{
    RowBox[{"{",
    RowBox[{
    RowBox[{
    RowBox[{"135", " ",
    RowBox[{"Log", "[",
    RowBox[{
    RowBox[{"(",
    RowBox[{"x", " ",
    Follow Math Help Forum on Facebook and Google+

  13. #13
    A Plied Mathematician
    Joined
    Jun 2010
    From
    CT, USA
    Posts
    6,318
    Thanks
    4
    Awards
    2
    Did it work for any data sets?
    Follow Math Help Forum on Facebook and Google+

  14. #14
    xdu
    xdu is offline
    Newbie
    Joined
    Nov 2009
    Posts
    19
    Yes, for some data sets, It works.
    Follow Math Help Forum on Facebook and Google+

  15. #15
    A Plied Mathematician
    Joined
    Jun 2010
    From
    CT, USA
    Posts
    6,318
    Thanks
    4
    Awards
    2
    Are the data sets for which it works the bigger sets? Or is there no discernible pattern?
    Follow Math Help Forum on Facebook and Google+

Page 1 of 2 12 LastLast

Similar Math Help Forum Discussions

  1. Help about Mathematica
    Posted in the Math Software Forum
    Replies: 2
    Last Post: June 1st 2010, 12:46 PM
  2. help me in Mathematica
    Posted in the Math Software Forum
    Replies: 2
    Last Post: May 18th 2010, 02:26 AM
  3. Mathematica
    Posted in the Math Software Forum
    Replies: 2
    Last Post: March 24th 2010, 08:13 AM
  4. problem with NMaximize
    Posted in the Math Software Forum
    Replies: 0
    Last Post: December 7th 2009, 08:14 AM
  5. mathematica
    Posted in the Math Software Forum
    Replies: 0
    Last Post: October 27th 2009, 02:03 PM

Search Tags


/mathhelpforum @mathhelpforum