[R] Gaussian Quadrature Numerical Integration In R

Ravi Varadhan rvaradhan at jhmi.edu
Thu Apr 15 18:09:46 CEST 2010


Here is an example showing how to use variable transformation:

	k <- 5

	myfn <- function(x, k) exp(-k * x^2)  # this is the integrand over
(-Inf, +Inf)

	# Integration using `integrate' function

	ans1 <- integrate(myfn, lower=-Inf, upper=Inf, k=k)$val

	# Now we integrate using Gauss-Hermite quadrature

	require(statmod)

	n <- 10

	gherm <- gauss.quad(n, kind="hermite")

	myfn.new <- function(x) 1

	mu <- 0

	sigma <- 1/sqrt(k)

	# following line shows how to use variable transformation 

	ans2 <- sum(gherm$weights * sigma * myfn.new(sigma*gherm$nodes +
mu))  

	ans2

	all.equal(ans1, ans2)
	 
Hope this helps,
Ravi.

-----Original Message-----
From: Ines Azaiez [mailto:iazaiez at MIT.EDU] 
Sent: Thursday, April 15, 2010 11:32 AM
To: Ravi Varadhan; r-help-bounces at stat.math.ethz.ch; r-help at r-project.org
Subject: RE: [R] Gaussian Quadrature Numerical Integration In R

Hi Ravi,
Thanks for your response. I already considered the change of variables but I
was wondering if there were already a function in R which do the same thing
in an optimized way. My function f(x) is complicated so adding a change of
variable makes the algorithm takes a lot of time to give outputs.
Thanks,
Iazaiez.
________________________________________
From: Ravi Varadhan [rvaradhan at jhmi.edu]
Sent: Wednesday, April 14, 2010 12:50 PM
To: Ines Azaiez; r-help-bounces at stat.math.ethz.ch; r-help at r-project.org
Subject: RE: [R] Gaussian Quadrature Numerical Integration In R

Just do a variable transformation. If your function is f(x), your new
function would be:
        f'(x) = sigma * f(sigma * x + mu).  You can integrate f'(x) using
the Hermite quadrature.

Ravi.

-----Original Message-----
From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On
Behalf Of Ines Azaiez
Sent: Tuesday, April 13, 2010 9:25 PM
To: r-help-bounces at stat.math.ethz.ch; r-help at r-project.org
Subject: [R] Gaussian Quadrature Numerical Integration In R

Hi All,

I am trying to use A Gaussian quadrature over the interval (-infty,infty)
with weighting function W(x)=exp(-(x-mu)^2/sigma) to estimate an integral.
Is there a way to do it in R? Is there a function already implemented which
uses such weighting function.
I have been searching in the statmode package and I found the function
"gauss.quad(100, kind="hermite")" which uses the weighting function
W(x)=exp(-x^2). Is there a more general version of this weighting function
(using mu and sigma)?

Thanks for your help

Iazaiez
______________________________________________
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.



More information about the R-help mailing list