# NMaximize in Mathematica

Printable View

Show 40 post(s) from this thread on one page
• August 25th 2010, 01:54 AM
xdu
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
• August 30th 2010, 05:15 AM
Ackbeet
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?
• September 2nd 2010, 02:55 AM
xdu
Hi Adrian,

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?

Cheers,
Xiaoguang
• September 2nd 2010, 05:15 AM
Ackbeet
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 $j$th 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?
• September 2nd 2010, 05:39 AM
xdu
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
• September 2nd 2010, 05:41 AM
xdu
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
• September 2nd 2010, 06:18 AM
Ackbeet
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?

Quote:

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?
• September 2nd 2010, 06:22 AM
xdu
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.
• September 2nd 2010, 06:40 AM
Ackbeet
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!
• September 2nd 2010, 09:55 AM
xdu
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?

Cheers
xiaoguang
• September 2nd 2010, 11:13 AM
Ackbeet
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.
• September 4th 2010, 06:35 AM
xdu
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", " ",
• September 4th 2010, 09:52 AM
Ackbeet
Did it work for any data sets?
• September 4th 2010, 02:04 PM
xdu
Yes, for some data sets, It works.
• September 6th 2010, 02:47 PM
Ackbeet
Are the data sets for which it works the bigger sets? Or is there no discernible pattern?
Show 40 post(s) from this thread on one page
All times are GMT -8. The time now is 06:43 AM.
{"extraction_info": {"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 0, "img_math": 0, "codecogs_latex": 22, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0}, "config": {"markdown_headings": true, "markdown_code": true, "boilerplate_config": {"ratio_threshold": 0.18, "absolute_threshold": 10, "end_threshold": 15, "enable": true}, "remove_buttons": true, "remove_image_figures": true, "remove_link_clusters": true, "table_config": {"min_rows": 2, "min_cols": 3, "format": "github"}, "remove_chinese": true, "remove_edit_buttons": true, "extract_latex": true}, "english_confidence": 0.9193274974822998, "perplexity_flag": "middle"}