# Thread: NMaximize in Mathematica

1. ## 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

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?

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

4. So, just to be clear, you've got a vector $\displaystyle \vec{n}$.

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

2. You've got a model function

$\displaystyle \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 $\displaystyle (rx)^{\vec{n}}$ indicate the vector that is the same length as $\displaystyle \vec{n}$ and whose $\displaystyle j$th components are equal to $\displaystyle (rx)^{n_{j}}.$ Is that correct?

4. Same with the gamma function?

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

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

7. Are $\displaystyle r, x, a$ all scalars?

8. Your goal is to estimate the parameters $\displaystyle 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?

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

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

7. So you've really got

$\displaystyle \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 \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?

8. 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.

9. Ok. Thanks for the clarifications.

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

1. Is the vector $\displaystyle \vec{n}$ known, or are you trying to find it? Or, perhaps, maybe you are given a number of different vectors $\displaystyle \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 $\displaystyle _{p}F_{q}$ is equal to $\displaystyle p+q+1$. Do you know what $\displaystyle p$ and $\displaystyle q$ are? They would appear to sum to 5. Is that correct?

Thanks for your patience in answering these questions!

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

11. Ah. Thanks. So the most simplified version of your function is this:

$\displaystyle \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 \displaystyle{L(r,x,a)=\sum_{j=1}^{\text{length}(\ vec{n})}\ln(f_{j}(r,x,a)).}$

And your goal is to maximize $\displaystyle 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.

12. 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", " ",

13. Did it work for any data sets?

14. Yes, for some data sets, It works.

15. Are the data sets for which it works the bigger sets? Or is there no discernible pattern?

Page 1 of 2 12 Last