Skip to content

Numerical Integration in 1D

6 messages · Ravi Varadhan, Max, Brian Ripley

Max
#
Dear UseRs,

I'm curious about the derivative of n!.

We know that Gamma(n+1)=n! So when on takes the derivative of 
Gamma(n+1) we get Int(ln(x)*exp(-x)*x^n,x=0..Inf).

I've tried code like
It seems that R doesn't like to integrate for any n, and I was 
wondering if anyone knew a way around this?

-Max
#
On Fri, 7 Mar 2008, Max wrote:

            
ln(x) e^x x^n is not integrable on (0, Inf).  You presumably slipped over 
a minus sign, but your definition of gamma(n) is wrong -- see ?gamma.

integrate(function(x) exp(-x)*x^n, lower=0, upper=Inf)

will work for gamma(n+1).
#
Hi Max,

The analytic integral \int _0 ^\Inf exp(-t) t^n log(t) might not converge
because the integrand tends to -Inf as t -> 0.

So, here is a numerical approach to estimating the derivative of the gamma
function:

library(numDeriv)

fx <- function(x, n) exp(-x) * x^n

gf <- function(n) {integrate(fx, lower=0, upper=Inf, n=n)$val}
[1] 7.536706
[1] 8534040
Best,
Ravi.


----------------------------------------------------------------------------
-------

Ravi Varadhan, Ph.D.

Assistant Professor, The Center on Aging and Health

Division of Geriatric Medicine and Gerontology 

Johns Hopkins University

Ph: (410) 502-2619

Fax: (410) 614-9625

Email: rvaradhan at jhmi.edu

Webpage:  http://www.jhsph.edu/agingandhealth/People/Faculty/Varadhan.html

 

----------------------------------------------------------------------------
--------

-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On
Behalf Of Max
Sent: Friday, March 07, 2008 1:41 PM
To: r-help at stat.math.ethz.ch
Subject: [R] Numerical Integration in 1D

Dear UseRs,

I'm curious about the derivative of n!.

We know that Gamma(n+1)=n! So when on takes the derivative of 
Gamma(n+1) we get Int(ln(x)*exp(-x)*x^n,x=0..Inf).

I've tried code like
It seems that R doesn't like to integrate for any n, and I was 
wondering if anyone knew a way around this?

-Max

______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
#
Hi max,

Prof. Ripley is right.  Your problem is that you missed a (-) sign in the
exponential.  Here is a demonstration showing the agreement between
numerical and analytical results: 

gx <- function(x, n) exp(-x) * x^n * log(x)

df <- function(n) {integrate(gx, lower=0, upper=Inf, n=n)$val}

library(numDeriv)

fx <- function(x, n) exp(-x) * x^n

gf <- function(n) {integrate(fx, lower=0, upper=Inf, n=n)$val}
[1] 1348.405
[1] 1348.405
Ravi.


----------------------------------------------------------------------------
-------

Ravi Varadhan, Ph.D.

Assistant Professor, The Center on Aging and Health

Division of Geriatric Medicine and Gerontology 

Johns Hopkins University

Ph: (410) 502-2619

Fax: (410) 614-9625

Email: rvaradhan at jhmi.edu

Webpage:  http://www.jhsph.edu/agingandhealth/People/Faculty/Varadhan.html

 

----------------------------------------------------------------------------
--------


-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On
Behalf Of Max
Sent: Friday, March 07, 2008 1:41 PM
To: r-help at stat.math.ethz.ch
Subject: [R] Numerical Integration in 1D

Dear UseRs,

I'm curious about the derivative of n!.

We know that Gamma(n+1)=n! So when on takes the derivative of 
Gamma(n+1) we get Int(ln(x)*exp(-x)*x^n,x=0..Inf).

I've tried code like
It seems that R doesn't like to integrate for any n, and I was 
wondering if anyone knew a way around this?

-Max

______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Max
#
Prof Brian Ripley formulated on Friday :
I did miss a minus sign in the integration, which explains part of my 
problems. The function of interest is the derivative of Gamma(n+1) with 
respect to n, but obviously integrated over x from 0 to Infinity.

What happens now is:
Error in f(x, ...) : object "n" not found

Any ideas on how to get around this error?
#
On Fri, 7 Mar 2008, Max wrote:

            
And you said n!, so n must be integer and you cannot differentiate a 
function of a integer argument.

If you are interested in the derivative of gamma(x), check out ?digamma.
Set 'n' in the evaluation environment.

E.g.
7.536706 with absolute error < 4.7e-06
[1] 7.536706