[R] using integrate with optimize nested in the integration

2005-07-28 Thread Gregory Gentlemen
Hi guys
im having a problem getting R to numerically integrate for some function, 
say f(bhat)*optimize(G(bhat)), over bhat. Where id like to integrate this over 
some finite range, so that here as we integrate over bhat optimize would return 
a different optimum.
 
For instance consider this simple example for which I cannot get R to return 
the desired result:
 
f - function(bhat) exp(bhat)
g - function(bhat) optimize(f,c(0,15),maximum=TRUE)$maximum*bhat
integrate(g,lower=0,upper=5)
which returns:
187.499393759 with absolute error  2.1e-12

However this is an approximation of : 15*(5^2/2 - 0)=187.5, not what I intended 
on getting. Its not identifying that f is a function of bhat ... any advice or 
ways I can get integrate to not treat this as a constant?

Any help is appreciated.
 
Gregoy Gentlemen

__



[[alternative HTML version deleted]]

__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


Re: [R] using integrate with optimize nested in the integration

2005-07-28 Thread Huntsinger, Reid
In your example, f is a function, but
optimize(f,c(0,15),maximum=TRUE)$maximum is just a number (the point at
which f reaches its maximum value). I'm not sure what you want, but if you
had say

f - function(x,y) x^3 + yx + 1

and defined

g - function(y) optimize(f,c(0,5),maximum=TRUE,y)$maximum

then g(t) is the x-value at which the function f(x,t) (over x in (0,5))
reaches its maximum for this fixed t. That could then be integrated. 

Reid Huntsinger

-Original Message-
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of Gregory Gentlemen
Sent: Thursday, July 28, 2005 3:58 PM
To: r-help@stat.math.ethz.ch
Subject: [R] using integrate with optimize nested in the integration


Hi guys
im having a problem getting R to numerically integrate for some function, 
say f(bhat)*optimize(G(bhat)), over bhat. Where id like to integrate this
over some finite range, so that here as we integrate over bhat optimize
would return a different optimum.
 
For instance consider this simple example for which I cannot get R to return
the desired result:
 
f - function(bhat) exp(bhat)
g - function(bhat) optimize(f,c(0,15),maximum=TRUE)$maximum*bhat
integrate(g,lower=0,upper=5)
which returns:
187.499393759 with absolute error  2.1e-12

However this is an approximation of : 15*(5^2/2 - 0)=187.5, not what I
intended on getting. Its not identifying that f is a function of bhat ...
any advice or ways I can get integrate to not treat this as a constant?

Any help is appreciated.
 
Gregoy Gentlemen

__



[[alternative HTML version deleted]]

__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide!
http://www.R-project.org/posting-guide.html

__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


Re: [R] using integrate with optimize nested in the integration

2005-07-28 Thread Sundar Dorai-Raj


Gregory Gentlemen wrote:
 Hi guys
 im having a problem getting R to numerically integrate for some function, 
 say f(bhat)*optimize(G(bhat)), over bhat. Where id like to integrate this 
 over some finite range, so that here as we integrate over bhat optimize would 
 return a different optimum.
  
 For instance consider this simple example for which I cannot get R to return 
 the desired result:
  
 f - function(bhat) exp(bhat)
 g - function(bhat) optimize(f,c(0,15),maximum=TRUE)$maximum*bhat
 integrate(g,lower=0,upper=5)
 which returns:
 187.499393759 with absolute error  2.1e-12
 
 However this is an approximation of : 15*(5^2/2 - 0)=187.5, not what I 
 intended on getting. Its not identifying that f is a function of bhat ... any 
 advice or ways I can get integrate to not treat this as a constant?
 
 Any help is appreciated.
  
 Gregoy Gentlemen
 

Hi, Gregory,

Your example is not very useful here since the optimize function is 
constant.

I think you want something more like:

f - function(bhat, x) {
   exp(bhat * x)
}
g - function(bhat) {
   o - vector(numeric, length(bhat))
   for(i in seq(along = bhat))
 o[i] - optimize(f, c(0,15), maximum = TRUE, x = bhat[i])$maximum
   bhat * o
}
integrate(g, lower = 0, upper = 5)

Because of the way integrate works, g(bhat) will always return a vector.

HTH,

--sundar

__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


Re: [R] using integrate with optimize nested in the integration

2005-07-28 Thread Gregory Gentlemen
Thanks for the prompt reply.
Your right, that was a weak example.
Consider this one though:
 
f - function(n,x) (x-2.5)^2*n
g - function(y) optimize(f,c(0,15), maximum=TRUE,x=y)$maximum*y
 
then if you try:
integrate(g,lower=0,upper=5)
it produces:
Error in optimize(f, c(0, 15), maximum = TRUE, x = y) : 
invalid function value in 'optimize'

Any ideas? 
My problem is a similar more complex function in which optimizaiton depends on 
the value of the integrator.

Huntsinger, Reid [EMAIL PROTECTED] wrote:
In your example, f is a function, but
optimize(f,c(0,15),maximum=TRUE)$maximum is just a number (the point at
which f reaches its maximum value). I'm not sure what you want, but if you
had say

f - function(x,y) x^3 + yx + 1

and defined

g - function(y) optimize(f,c(0,5),maximum=TRUE,y)$maximum

then g(t) is the x-value at which the function f(x,t) (over x in (0,5))
reaches its maximum for this fixed t. That could then be integrated. 

Reid Huntsinger

-Original Message-
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of Gregory Gentlemen
Sent: Thursday, July 28, 2005 3:58 PM
To: r-help@stat.math.ethz.ch
Subject: [R] using integrate with optimize nested in the integration


Hi guys
im having a problem getting R to numerically integrate for some function, 
say f(bhat)*optimize(G(bhat)), over bhat. Where id like to integrate this
over some finite range, so that here as we integrate over bhat optimize
would return a different optimum.

For instance consider this simple example for which I cannot get R to return
the desired result:

f - function(bhat) exp(bhat)
g - function(bhat) optimize(f,c(0,15),maximum=TRUE)$maximum*bhat
integrate(g,lower=0,upper=5)
which returns:
187.499393759 with absolute error  2.1e-12

However this is an approximation of : 15*(5^2/2 - 0)=187.5, not what I
intended on getting. Its not identifying that f is a function of bhat ...
any advice or ways I can get integrate to not treat this as a constant?

Any help is appreciated.

Gregoy Gentlemen

__



[[alternative HTML version deleted]]

__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide!
http://www.R-project.org/posting-guide.html





--

--

__



[[alternative HTML version deleted]]

__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


Re: [R] using integrate with optimize nested in the integration

2005-07-28 Thread Sundar Dorai-Raj
Again, not a good example, since f is linear in n so the max will always 
be at 15.

Try this:

f - function(x, n) -(x - 2.5 * n)^2 # max is at 2.5*n
g - function(n) {
o - vector(numeric, length(n))
for(i in seq(along = n))
  o[i] - optimize(f, c(0, 15), maximum = TRUE, n = n[i])$maximum
n * o
}
integrate(g, lower = 0, upper = 5)

# int_0^5 (2.5 * n^2) dn = 2.5/3 * 5^3 = 104.1667

--sundar


Gregory Gentlemen wrote:
 Thanks for the prompt reply.
 Your right, that was a weak example.
 Consider this one though:
  
 f - function(n,x) (x-2.5)^2*n
 g - function(y) optimize(f,c(0,15), maximum=TRUE,x=y)$maximum*y
  
 then if you try:
 integrate(g,lower=0,upper=5)
 it produces:
 Error in optimize(f, c(0, 15), maximum = TRUE, x = y) : 
 invalid function value in 'optimize'
 
 Any ideas? 
 My problem is a similar more complex function in which optimizaiton depends 
 on the value of the integrator.
 
 Huntsinger, Reid [EMAIL PROTECTED] wrote:
 In your example, f is a function, but
 optimize(f,c(0,15),maximum=TRUE)$maximum is just a number (the point at
 which f reaches its maximum value). I'm not sure what you want, but if you
 had say
 
 f - function(x,y) x^3 + yx + 1
 
 and defined
 
 g - function(y) optimize(f,c(0,5),maximum=TRUE,y)$maximum
 
 then g(t) is the x-value at which the function f(x,t) (over x in (0,5))
 reaches its maximum for this fixed t. That could then be integrated. 
 
 Reid Huntsinger
 
 -Original Message-
 From: [EMAIL PROTECTED]
 [mailto:[EMAIL PROTECTED] On Behalf Of Gregory Gentlemen
 Sent: Thursday, July 28, 2005 3:58 PM
 To: r-help@stat.math.ethz.ch
 Subject: [R] using integrate with optimize nested in the integration
 
 
 Hi guys
 im having a problem getting R to numerically integrate for some function, 
 say f(bhat)*optimize(G(bhat)), over bhat. Where id like to integrate this
 over some finite range, so that here as we integrate over bhat optimize
 would return a different optimum.
 
 For instance consider this simple example for which I cannot get R to return
 the desired result:
 
 f - function(bhat) exp(bhat)
 g - function(bhat) optimize(f,c(0,15),maximum=TRUE)$maximum*bhat
 integrate(g,lower=0,upper=5)
 which returns:
 187.499393759 with absolute error  2.1e-12
 
 However this is an approximation of : 15*(5^2/2 - 0)=187.5, not what I
 intended on getting. Its not identifying that f is a function of bhat ...
 any advice or ways I can get integrate to not treat this as a constant?
 
 Any help is appreciated.
 
 Gregoy Gentlemen
 
 __
 
 
 
 [[alternative HTML version deleted]]
 
 __
 R-help@stat.math.ethz.ch mailing list
 https://stat.ethz.ch/mailman/listinfo/r-help
 PLEASE do read the posting guide!
 http://www.R-project.org/posting-guide.html
 
 
 
 
 
 --
 
 --
 
 __
 
 
 
   [[alternative HTML version deleted]]
 
 __
 R-help@stat.math.ethz.ch mailing list
 https://stat.ethz.ch/mailman/listinfo/r-help
 PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html

__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html


Re: [R] using integrate with optimize nested in the integration

2005-07-28 Thread Huntsinger, Reid
Integrate needs a vectorized function. For your example,
 
 integrate(function(x) sapply(x,g),lower=0,upper=5)
187.4994 with absolute error  2.1e-12

Reid Huntsinger
-Original Message-
From: Gregory Gentlemen [mailto:[EMAIL PROTECTED] 
Sent: Thursday, July 28, 2005 4:37 PM
To: Huntsinger, Reid; r-help@stat.math.ethz.ch
Subject: RE: [R] using integrate with optimize nested in the integration


Thanks for the prompt reply.
Your right, that was a weak example.
Consider this one though:
 
f - function(n,x) (x-2.5)^2*n
g - function(y) optimize(f,c(0,15), maximum=TRUE,x=y)$maximum*y
 
then if you try:
integrate(g,lower=0,upper=5)
it produces:
Error in optimize(f, c(0, 15), maximum = TRUE, x = y) : 
invalid function value in 'optimize'

Any ideas? 
My problem is a similar more complex function in which optimizaiton depends
on the value of the integrator.

Huntsinger, Reid [EMAIL PROTECTED] wrote:

In your example, f is a function, but
optimize(f,c(0,15),maximum=TRUE)$maximum is just a number (the point at
which f reaches its maximum value). I'm not sure what you want, but if you
had say

f - function(x,y) x^3 + yx + 1

and defined

g - function(y) optimize(f,c(0,5),maximum=TRUE,y)$maximum

then g(t) is the x-value at which the function f(x,t) (over x in (0,5))
reaches its maximum for this fixed t. That could then be integrated. 

Reid Huntsinger

-Original Message-
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of Gregory Gentlemen
Sent: Thursday, July 28, 2005 3:58 PM
To: r-help@stat.math.ethz.ch
Subject: [R] using integrate with optimize nested in the integration


Hi guys
im having a problem getting R to numerically integrate for some function, 
say f(bhat)*optimize(G(bhat)), over bhat. Where id like to integrate this
over some finite range, so that here as we integrate over bhat optimize
would return a different optimum.

For instance consider this simple example for which I cannot get R to return
the desired result:

f - function(bhat) exp(bhat)
g - function(bhat) optimize(f,c(0,15),maximum=TRUE)$maximum*bhat
integrate(g,lower=0,upper=5)
which returns:
187.499393759 with absolute error  2.1e-12

However this is an approximation of : 15*(5^2/2 - 0)=187.5, not what I
intended on getting. Its not identifying that f is a function of bhat ...
any advice or ways I can get integrate to not treat this as a constant?

Any help is appreciated.

Gregoy Gentlemen

__



[[alternative HTML version deleted]]

__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide!
http://www.R-project.org/posting-guide.html






--
Notice: This e-mail message, together with any attachments, ...{{dropped}}

__
R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html