Re: [R] Integration of functions with a vector argument

2024-06-24 Thread Levine, Michael
Dear Bert,

Thank you very much for your suggestions!

Yours sincerely,
Michael

Michael Levine
Associate Professor, Statistics

Department of Statistics
Purdue University
250 North University Street
West Lafayette, IN 47907 USA

email: mlev...@purdue.edu
Phone: +1-765-496-7571
Fax:   +1-765-494-0558
URL:   www.stat.purdue.edu/~mlevins

From: Bert Gunter 
Sent: Thursday, June 20, 2024 1:09 PM
To: Ivan Krylov 
Cc: Levine, Michael ; r-help@r-project.org 

Subject: Re: [R] Integration of functions with a vector argument

You don't often get email from bgunter.4...@gmail.com. Learn why this is 
important<https://aka.ms/LearnAboutSenderIdentification>
 External Email: Use caution with attachments, links, or sharing data 

The calculus package might also be relevant (not sure if it was in the Task 
View):

https://cran.r-project.org/web/packages/calculus/index.html

-- Bert

On Thu, Jun 20, 2024 at 10:01 AM Bert Gunter 
mailto:bgunter.4...@gmail.com>> wrote:
If you haven't already done so, you may wish to have a look here:

https://cran.r-project.org/web/views/NumericalMathematics.html#differentiation-and-integration

(Or perhaps in other related subtopics in the Numerical Math task view)

Cheers,
Bert

On Thu, Jun 20, 2024 at 8:37 AM Ivan Krylov via R-help 
mailto:r-help@r-project.org>> wrote:
В Tue, 18 Jun 2024 23:12:03 +
"Levine, Michael" mailto:mlev...@purdue.edu>> пишет:

> I have heard of several packages used for numerical integration in R -
> cubature that you mentioned, mvQuad, and pracma.  My impression is
> that you think that Cubature is the best in your opinion. Is that so?

Yes, but the preference is not very strong. My own numerical
integration experience is limited to using QUADPACK in a project where
everything had to be rewritten in Fortran for speed.

I trust the code of the 'cubature' library by Steven G. Johnson (whose
work includes FFTW and NLopt) and the 'Cuba' library by Thomas Hahn.
The 'cubature' R package wraps these two libraries. Unfortunately, as
you see, its documentation can be lacking.

Like 'cubature', the 'mvQuad' package won't require a significant
rewrite of the integrand because it expects its functions to take
matrices of arguments. Unfortunately, the two packages differ in the
meanings they assign to the matrices: in 'cubature', individual
arguments of the function correspond to the rows of the first argument,
while in 'mvQuad', they must be in the columns.

The 'mvQuad' package requires manual adjustments to the generated
quadratures using rescale(...), which may be not very convenient.

The 'pracma' package contains implementations of a lot of excellent
methods (I've trusted it before for its other functions), but the (two-
or three-argument) integrand will have to be rewritten to accept
separate arrays (of arbitrary shape?) instead of a single matrix.

> If yes, do you know of any detailed discussion of this package beyond
> the two vignettes available on CRAN?

I don't know how much it can help, but more information about the
underlying code for 'cubature' can be found at
<https://github.com/stevengj/cubature> and <https://feynarts.de/cuba/>.

--
Best regards,
Ivan

__
R-help@r-project.org<mailto:R-help@r-project.org> mailing list -- To 
UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide 
http://www.R-project.org/posting-guide.html<http://www.r-project.org/posting-guide.html>
and provide commented, minimal, self-contained, reproducible code.

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.


Re: [R] Integration of functions with a vector argument

2024-06-24 Thread Levine, Michael
Dear Ivan,

Thank you very much again for your suggestions! I will try to use Cubature, 
then.

yours sincerely,
Michael

Michael Levine
Associate Professor, Statistics

Department of Statistics
Purdue University
250 North University Street
West Lafayette, IN 47907 USA

email: mlev...@purdue.edu
Phone: +1-765-496-7571
Fax:   +1-765-494-0558
URL:   www.stat.purdue.edu/~mlevins


From: Ivan Krylov 
Sent: Thursday, June 20, 2024 11:37 AM
To: Levine, Michael 
Cc: r-help@r-project.org 
Subject: Re: [R] Integration of functions with a vector argument

 External Email: Use caution with attachments, links, or sharing data 


� Tue, 18 Jun 2024 23:12:03 +
"Levine, Michael"  �:

> I have heard of several packages used for numerical integration in R -
> cubature that you mentioned, mvQuad, and pracma.  My impression is
> that you think that Cubature is the best in your opinion. Is that so?

Yes, but the preference is not very strong. My own numerical
integration experience is limited to using QUADPACK in a project where
everything had to be rewritten in Fortran for speed.

I trust the code of the 'cubature' library by Steven G. Johnson (whose
work includes FFTW and NLopt) and the 'Cuba' library by Thomas Hahn.
The 'cubature' R package wraps these two libraries. Unfortunately, as
you see, its documentation can be lacking.

Like 'cubature', the 'mvQuad' package won't require a significant
rewrite of the integrand because it expects its functions to take
matrices of arguments. Unfortunately, the two packages differ in the
meanings they assign to the matrices: in 'cubature', individual
arguments of the function correspond to the rows of the first argument,
while in 'mvQuad', they must be in the columns.

The 'mvQuad' package requires manual adjustments to the generated
quadratures using rescale(...), which may be not very convenient.

The 'pracma' package contains implementations of a lot of excellent
methods (I've trusted it before for its other functions), but the (two-
or three-argument) integrand will have to be rewritten to accept
separate arrays (of arbitrary shape?) instead of a single matrix.

> If yes, do you know of any detailed discussion of this package beyond
> the two vignettes available on CRAN?

I don't know how much it can help, but more information about the
underlying code for 'cubature' can be found at
<https://nam04.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fstevengj%2Fcubature&data=05%7C02%7Cmlevins%40purdue.edu%7Cebdca9b21fa14410c84308dc913eed65%7C4130bd397c53419cb1e58758d6d63f21%7C0%7C0%7C638544946662774955%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C0%7C%7C%7C&sdata=jflgsS7UUtmkgKyWyYuKxHtJNBpWA0eSbPQRWxEHOTk%3D&reserved=0<https://github.com/stevengj/cubature>>
 and 
<https://nam04.safelinks.protection.outlook.com/?url=https%3A%2F%2Ffeynarts.de%2Fcuba%2F&data=05%7C02%7Cmlevins%40purdue.edu%7Cebdca9b21fa14410c84308dc913eed65%7C4130bd397c53419cb1e58758d6d63f21%7C0%7C0%7C638544946662793433%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C0%7C%7C%7C&sdata=kRblrnUyftzdC7SKqJrVQ9rLjpSX3ySWJK%2FCzhWYAvE%3D&reserved=0<https://feynarts.de/cuba/>>.

--
Best regards,
Ivan

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.


Re: [R] Integration of functions with a vector argument

2024-06-20 Thread Bert Gunter
The calculus package might also be relevant (not sure if it was in the Task
View):

https://cran.r-project.org/web/packages/calculus/index.html

-- Bert

On Thu, Jun 20, 2024 at 10:01 AM Bert Gunter  wrote:

> If you haven't already done so, you may wish to have a look here:
>
>
> https://cran.r-project.org/web/views/NumericalMathematics.html#differentiation-and-integration
>
> (Or perhaps in other related subtopics in the Numerical Math task view)
>
> Cheers,
> Bert
>
> On Thu, Jun 20, 2024 at 8:37 AM Ivan Krylov via R-help <
> r-help@r-project.org> wrote:
>
>> В Tue, 18 Jun 2024 23:12:03 +
>> "Levine, Michael"  пишет:
>>
>> > I have heard of several packages used for numerical integration in R -
>> > cubature that you mentioned, mvQuad, and pracma.  My impression is
>> > that you think that Cubature is the best in your opinion. Is that so?
>>
>> Yes, but the preference is not very strong. My own numerical
>> integration experience is limited to using QUADPACK in a project where
>> everything had to be rewritten in Fortran for speed.
>>
>> I trust the code of the 'cubature' library by Steven G. Johnson (whose
>> work includes FFTW and NLopt) and the 'Cuba' library by Thomas Hahn.
>> The 'cubature' R package wraps these two libraries. Unfortunately, as
>> you see, its documentation can be lacking.
>>
>> Like 'cubature', the 'mvQuad' package won't require a significant
>> rewrite of the integrand because it expects its functions to take
>> matrices of arguments. Unfortunately, the two packages differ in the
>> meanings they assign to the matrices: in 'cubature', individual
>> arguments of the function correspond to the rows of the first argument,
>> while in 'mvQuad', they must be in the columns.
>>
>> The 'mvQuad' package requires manual adjustments to the generated
>> quadratures using rescale(...), which may be not very convenient.
>>
>> The 'pracma' package contains implementations of a lot of excellent
>> methods (I've trusted it before for its other functions), but the (two-
>> or three-argument) integrand will have to be rewritten to accept
>> separate arrays (of arbitrary shape?) instead of a single matrix.
>>
>> > If yes, do you know of any detailed discussion of this package beyond
>> > the two vignettes available on CRAN?
>>
>> I don't know how much it can help, but more information about the
>> underlying code for 'cubature' can be found at
>>  and .
>>
>> --
>> Best regards,
>> Ivan
>>
>> __
>> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> 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.
>>
>

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.


Re: [R] Integration of functions with a vector argument

2024-06-20 Thread Bert Gunter
If you haven't already done so, you may wish to have a look here:

https://cran.r-project.org/web/views/NumericalMathematics.html#differentiation-and-integration

(Or perhaps in other related subtopics in the Numerical Math task view)

Cheers,
Bert

On Thu, Jun 20, 2024 at 8:37 AM Ivan Krylov via R-help 
wrote:

> В Tue, 18 Jun 2024 23:12:03 +
> "Levine, Michael"  пишет:
>
> > I have heard of several packages used for numerical integration in R -
> > cubature that you mentioned, mvQuad, and pracma.  My impression is
> > that you think that Cubature is the best in your opinion. Is that so?
>
> Yes, but the preference is not very strong. My own numerical
> integration experience is limited to using QUADPACK in a project where
> everything had to be rewritten in Fortran for speed.
>
> I trust the code of the 'cubature' library by Steven G. Johnson (whose
> work includes FFTW and NLopt) and the 'Cuba' library by Thomas Hahn.
> The 'cubature' R package wraps these two libraries. Unfortunately, as
> you see, its documentation can be lacking.
>
> Like 'cubature', the 'mvQuad' package won't require a significant
> rewrite of the integrand because it expects its functions to take
> matrices of arguments. Unfortunately, the two packages differ in the
> meanings they assign to the matrices: in 'cubature', individual
> arguments of the function correspond to the rows of the first argument,
> while in 'mvQuad', they must be in the columns.
>
> The 'mvQuad' package requires manual adjustments to the generated
> quadratures using rescale(...), which may be not very convenient.
>
> The 'pracma' package contains implementations of a lot of excellent
> methods (I've trusted it before for its other functions), but the (two-
> or three-argument) integrand will have to be rewritten to accept
> separate arrays (of arbitrary shape?) instead of a single matrix.
>
> > If yes, do you know of any detailed discussion of this package beyond
> > the two vignettes available on CRAN?
>
> I don't know how much it can help, but more information about the
> underlying code for 'cubature' can be found at
>  and .
>
> --
> Best regards,
> Ivan
>
> __
> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.
>

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.


Re: [R] Integration of functions with a vector argument

2024-06-20 Thread Ivan Krylov via R-help
В Tue, 18 Jun 2024 23:12:03 +
"Levine, Michael"  пишет:

> I have heard of several packages used for numerical integration in R -
> cubature that you mentioned, mvQuad, and pracma.  My impression is
> that you think that Cubature is the best in your opinion. Is that so?

Yes, but the preference is not very strong. My own numerical
integration experience is limited to using QUADPACK in a project where
everything had to be rewritten in Fortran for speed.

I trust the code of the 'cubature' library by Steven G. Johnson (whose
work includes FFTW and NLopt) and the 'Cuba' library by Thomas Hahn.
The 'cubature' R package wraps these two libraries. Unfortunately, as
you see, its documentation can be lacking.

Like 'cubature', the 'mvQuad' package won't require a significant
rewrite of the integrand because it expects its functions to take
matrices of arguments. Unfortunately, the two packages differ in the
meanings they assign to the matrices: in 'cubature', individual
arguments of the function correspond to the rows of the first argument,
while in 'mvQuad', they must be in the columns.

The 'mvQuad' package requires manual adjustments to the generated
quadratures using rescale(...), which may be not very convenient.

The 'pracma' package contains implementations of a lot of excellent
methods (I've trusted it before for its other functions), but the (two-
or three-argument) integrand will have to be rewritten to accept
separate arrays (of arbitrary shape?) instead of a single matrix.

> If yes, do you know of any detailed discussion of this package beyond
> the two vignettes available on CRAN?

I don't know how much it can help, but more information about the
underlying code for 'cubature' can be found at
 and .

-- 
Best regards,
Ivan

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.


Re: [R] Integration of functions with a vector argument

2024-06-19 Thread Levine, Michael
Dear Ivan,

Sorry for my slow response. Your advice works well; however, as you predicted, 
the time it takes to obtain any results is completely unacceptable (for 
example, a sequence of integrations we discussed is done once per iteration and 
even one iteration takes about 2 hours to complete).

I have one more question for you - hope you don't mind. I have heard of several 
packages used for numerical integration in R - cubature that you mentioned, 
mvQuad, and pracma.  My impression is that you think that Cubature is the best 
in your opinion. Is that so? If yes, do you know of any detailed discussion of 
this package beyond the two vignettes available on CRAN?

Thank you very much again for your help!

Yours sincerely,
Michael

Michael Levine
Associate Professor, Statistics

Department of Statistics
Purdue University
250 North University Street
West Lafayette, IN 47907 USA

email: mlev...@purdue.edu
Phone: +1-765-496-7571
Fax:   +1-765-494-0558
URL:   www.stat.purdue.edu/~mlevins

From: Ivan Krylov 
Sent: Thursday, June 13, 2024 6:21 AM
To: Levine, Michael 
Cc: r-help@r-project.org 
Subject: Re: [R] Integration of functions with a vector argument

[You don't often get email from ikry...@disroot.org. Learn why this is 
important at https://aka.ms/LearnAboutSenderIdentification ]

 External Email: Use caution with attachments, links, or sharing data 


�� Wed, 12 Jun 2024 23:42:18 +
"Levine, Michael"  ��ڧ�֧�:

> f.int1 <- function(x,y) {Vectorize (function(y) f(c(x,y),H=H,j=j))}

Vectorize returns a callable function(y), so wrapping it in a
function(x,y) will not work. Since you'd like to integrate over y, we
can perform the same transformation manually using vapply:

# A version of f(...) that can be integrated over y
f.int1 <- function(y, x, H, j)
 vapply(y, function(y) f(c(x,y), H = H, j = j), numeric(1))

The same transformation can be performed using Vectorize as follows:

f.int1 <- Vectorize(
 function(y, x, H, j) f(c(x,y), H = H, j = j),
 vectorize.args = 'y'
)

> mrg.d1 <- function(x){
> integrate(f.int1,lower=-3,upper=3)$value
> }

We can then integrate f.int1 over y, making sure to pass the remaining
scalar parameters to the function:

# mrg.d1(x, H, j) = �� f(y,x,H,j) dy from -3 to 3
mrg.d1 <- function(x, H, j)
 integrate(f.int1, lower=-3, upper=3, x = x, H = H, j = j)$value

> mrg.cdf1 <- function(x){
> integrate(mrg.d1,lower=-Inf,upper=x)$value
> }

Unfortunately, mrg.d1 is once again not vectorised and may give wrong
answers or raise exceptions with non-scalar x. We can now perform the
same transformation so that the function would work with integrate():

# Version of mrg.d1 integratable over x
mrg.d1.int <- function(x, H, j)
 vapply(x, mrg.d1, numeric(1), H = H, j = j)

Here, instead of having to combine arguments like in f.int1, we can
just use vapply() to call the original mrg.d1 for every scalar element
inside the vector x given to mrg.d1.int. Alternatively,

mrg.d1.int <- Vectorize(mrg.d1, 'x')

A vectorised function can then be integrated:

# mrg.cdf1(x, H, j) = �� mrg.d1(x', H, j) dx' from -�� to x
mrg.cdf1 <- function(x, H, j)
 integrate(mrg.d1.int, lower=-Inf, upper=x, H = H, j = j)$value

This double nested loop (produced by Vectorize() and mapply() or
manually with vapply()) may be not very fast. If you rewrite your
function f to accept matrices containing values of (x, y) for many
evaluations of the function and to return vectors of the resulting
values, you'll be able to use the CRAN package 'cubature'
<https://nam04.safelinks.protection.outlook.com/?url=https%3A%2F%2Fcran.r-project.org%2Fpackage%3Dcubature&data=05%7C02%7Cmlevins%40purdue.edu%7C9753b609c1d34745311908dc8b9296b9%7C4130bd397c53419cb1e58758d6d63f21%7C0%7C0%7C638538708933465815%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C0%7C%7C%7C&sdata=uCwVWSrsLemT4j1nT9UiEt%2BQxH99Z8erBorg0LEYmWI%3D&reserved=0<https://cran.r-project.org/package=cubature>>
 to integrate it over
multiple variables at once.

--
Best regards,
Ivan

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.


Re: [R] Integration of functions with a vector argument

2024-06-13 Thread Ivan Krylov via R-help
В Wed, 12 Jun 2024 23:42:18 +
"Levine, Michael"  пишет:

> f.int1 <- function(x,y) {Vectorize (function(y) f(c(x,y),H=H,j=j))}

Vectorize returns a callable function(y), so wrapping it in a
function(x,y) will not work. Since you'd like to integrate over y, we
can perform the same transformation manually using vapply:

# A version of f(...) that can be integrated over y
f.int1 <- function(y, x, H, j)
 vapply(y, function(y) f(c(x,y), H = H, j = j), numeric(1))

The same transformation can be performed using Vectorize as follows:

f.int1 <- Vectorize(
 function(y, x, H, j) f(c(x,y), H = H, j = j),
 vectorize.args = 'y'
)

> mrg.d1 <- function(x){
> integrate(f.int1,lower=-3,upper=3)$value
> }

We can then integrate f.int1 over y, making sure to pass the remaining
scalar parameters to the function:

# mrg.d1(x, H, j) = ∫ f(y,x,H,j) dy from -3 to 3
mrg.d1 <- function(x, H, j)
 integrate(f.int1, lower=-3, upper=3, x = x, H = H, j = j)$value

> mrg.cdf1 <- function(x){
> integrate(mrg.d1,lower=-Inf,upper=x)$value
> }

Unfortunately, mrg.d1 is once again not vectorised and may give wrong
answers or raise exceptions with non-scalar x. We can now perform the
same transformation so that the function would work with integrate():

# Version of mrg.d1 integratable over x
mrg.d1.int <- function(x, H, j)
 vapply(x, mrg.d1, numeric(1), H = H, j = j)

Here, instead of having to combine arguments like in f.int1, we can
just use vapply() to call the original mrg.d1 for every scalar element
inside the vector x given to mrg.d1.int. Alternatively,

mrg.d1.int <- Vectorize(mrg.d1, 'x')

A vectorised function can then be integrated:

# mrg.cdf1(x, H, j) = ∫ mrg.d1(x', H, j) dx' from -∞ to x
mrg.cdf1 <- function(x, H, j)
 integrate(mrg.d1.int, lower=-Inf, upper=x, H = H, j = j)$value

This double nested loop (produced by Vectorize() and mapply() or
manually with vapply()) may be not very fast. If you rewrite your
function f to accept matrices containing values of (x, y) for many
evaluations of the function and to return vectors of the resulting
values, you'll be able to use the CRAN package 'cubature'
 to integrate it over
multiple variables at once.

-- 
Best regards,
Ivan

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.


Re: [R] Integration of functions with a vector argument

2024-06-13 Thread Levine, Michael
Dear Ivan,

Thank you very much for your suggestions. I understand what you are saying. I 
am afraid I didn't explain well enough what was needed, however.  Let us again 
restrict ourselves to the case of two dimensions. Then, what I have is a 
bivariate density function f(x,y) and my intention is to obtain f(x) by 
integrating out y. However, I don't only need to know the value of the marginal 
density f(x) at one point - I need to integrate it again to obtain the marginal 
cumulative density function F(x) as a next step. Thus, I also need to pass x as 
an additional parameter before integration, and this is what makes the problem 
more difficult.

I tried to do the following (here H and j are additional arguments needed to 
obtain the estimated bivariate density function):

f.int1 <- function(x,y) {Vectorize (function(y) f(c(x,y),H=H,j=j))}
mrg.d1 <- function(x){
integrate(f.int1,lower=-3,upper=3)$value
}
mrg.cdf1 <- function(x){
integrate(mrg.d1,lower=-Inf,upper=x)$value
}

I received the following error message:


Error in integrate(f.int1, lower = -3, upper = 3) :
  evaluation of function gave a result of wrong length


How would you go about performing this task?

Yours sincerely,
Michael

Michael Levine
Associate Professor, Statistics

Department of Statistics
Purdue University
250 North University Street
West Lafayette, IN 47907 USA

email: mlev...@purdue.edu
Phone: +1-765-496-7571
Fax:   +1-765-494-0558
URL:   www.stat.purdue.edu/~mlevins

From: Ivan Krylov 
Sent: Wednesday, June 12, 2024 6:41 AM
To: Levine, Michael 
Cc: r-help@r-project.org 
Subject: Re: [R] Integration of functions with a vector argument

[You don't often get email from ikry...@disroot.org. Learn why this is 
important at https://aka.ms/LearnAboutSenderIdentification ]

 External Email: Use caution with attachments, links, or sharing data 


� Tue, 11 Jun 2024 18:44:08 +
"Levine, Michael"  �:

> Let us say we have a function
>
> F <- function(x){ body of the function}
>
> Where x is, in general, a d by 1 vector with d>1.  Now I want to
> integrate out some of the coordinates of x, e.g. x[1] or x[2] or both
> of them etc. I'm well aware of how to integrate out e.g. y if a
> function is defined as f <- function (x,y) {body of the function}
> where y is a scalar.

The reason integrate() wants a separate function argument for the
integration coordinate is so that it could give the function a vector
of different values of the variable and receive a vector of the same
length containing the corresponding values of the function.

If the problem is small enough to make performance considerations
irrelevant, you can use Vectorize to make a function compatible with
integrate() from your function F:

x <- x0
z <- z0
Fiy <- Vectorize(function(y) F(c(x, y, z)))
integrate(Fiy, ymin, ymax)

The resulting function Fiy will accept a vector of values for y and
translate it into multiple calls to F with a three-element vector
argument as it expects.

Achieving better performance will require rewriting the function F to
be "vectorised", i.e. to accept vectors for arguments and return a
vector of the same length.

--
Best regards,
Ivan

[[alternative HTML version deleted]]

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.


Re: [R] Integration of functions with a vector argument

2024-06-13 Thread Jeff Newmiller via R-help
How do you integrate perpendicular to a plane if you don't have a concept of 
3d? In that direction the plane is just a point.

Vectors in R do not have any concept of "n by 1"... they are like a string of 
beads. When you take a column out of a matrix `a` using indexing b <- a[,j] the 
resulting vector can only be indexed along its length as b[i]. Such a vector is 
indistinguishable in structure from a[i,] which originated from a row.

So I would say you are tilting at windmills. If you want you can write a[, j, 
drop=FALSE] to end up with an n by 1 matrix... but that is not a vector, it is 
a matrix. I have reservations about the value of "integrating out" along a row 
in an n by 1 matrix... seems like a degenerate case and you would be better off 
not creating the n by 1 matrix in the first place. But then I left Matlab 
behind a long time ago.


On June 11, 2024 11:44:08 AM PDT, "Levine, Michael"  wrote:
>Hello all,
>
>I have a question concerning integration of a function of a multivariate 
>argument with respect to one or more variables in r.  Let us say we have a 
>function
>
>F <- function(x){ body of the function}
>
>Where x is, in general, a d by 1 vector with d>1.  Now I want to integrate out 
>some of the coordinates of x, e.g. x[1] or x[2] or both of them etc. I'm well 
>aware of how to integrate out e.g. y if a function is defined as f <- function 
>(x,y) {body of the function} where y is a scalar.
>However, it seems to be quite difficult to do the same if the function is 
>defined with a vector argument x. At the very least, I haven't seen any good 
>examples of this being done.
>Any suggestions?
>
>Yours sincerely,
>Michael
>
>Michael Levine
>Associate Professor, Statistics
>
>Department of Statistics
>Purdue University
>250 North University Street
>West Lafayette, IN 47907 USA
>
>email: mlev...@purdue.edu
>Phone: +1-765-496-7571
>Fax:   +1-765-494-0558
>URL:   www.stat.purdue.edu/~mlevins
>
>   [[alternative HTML version deleted]]
>
>__
>R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
>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.

-- 
Sent from my phone. Please excuse my brevity.

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.


Re: [R] Integration of functions with a vector argument

2024-06-12 Thread Ivan Krylov via R-help
В Tue, 11 Jun 2024 18:44:08 +
"Levine, Michael"  пишет:

> Let us say we have a function
> 
> F <- function(x){ body of the function}
> 
> Where x is, in general, a d by 1 vector with d>1.  Now I want to
> integrate out some of the coordinates of x, e.g. x[1] or x[2] or both
> of them etc. I'm well aware of how to integrate out e.g. y if a
> function is defined as f <- function (x,y) {body of the function}
> where y is a scalar.

The reason integrate() wants a separate function argument for the
integration coordinate is so that it could give the function a vector
of different values of the variable and receive a vector of the same
length containing the corresponding values of the function.

If the problem is small enough to make performance considerations
irrelevant, you can use Vectorize to make a function compatible with
integrate() from your function F:

x <- x0
z <- z0
Fiy <- Vectorize(function(y) F(c(x, y, z)))
integrate(Fiy, ymin, ymax)

The resulting function Fiy will accept a vector of values for y and
translate it into multiple calls to F with a three-element vector
argument as it expects.

Achieving better performance will require rewriting the function F to
be "vectorised", i.e. to accept vectors for arguments and return a
vector of the same length.

-- 
Best regards,
Ivan

__
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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.