Skip to content

numerical integration and 'non-finite function value' error

5 messages · Ravi Varadhan, Adan_Seb, David Winsemius

#
Dear R users,

I have a question about numerical integration in R.
I am facing the 'non-finite function value' error while integrating the
function
                               xf(x)
using 'integrate'. f(x) is a probability density function and assumed to
follow the three parameter (min = 0) beta distribution for which I have
estimated the parameters. The function is integrated over two ranges, say
(0, a) and (a, Inf). The error 'non-finite function value' happens when I do
the integration for the second range. I have 10 different density functions
for 10 different data series and for some of them replacing Inf by the Max
of f(x) gives me an estimate of the area while for others this doesn't work.

What does the error mean and how can I address it? Is replacing a max for
Inf appropriate?

I really appreciate any clues.




--
View this message in context: http://r.789695.n4.nabble.com/numerical-integration-and-non-finite-function-value-error-tp3618486p3618486.html
Sent from the R help mailing list archive at Nabble.com.
#
In the limit as x goes to infinity, the integrand x f(x) should go to 0 sufficiently fast in order for the integral to be finite.  The error indicates that the integrand becomes infinite for large x. Check to ensure that the integrand is correctly specified.

I don't understand how you can replace the upper limit of integration, Inf, by max f(x). This does not make much sense.

Can you produce a self-contained example? 

Ravi.
#
Here is a self-contained example of my problem. 

set.seed(100)
x = rbeta(100, 10.654, 10.439)
# So the shape parameters and the exteremes are
a = 10.654
b = 10.439
xmax = 1
xmin = 0
# Using the non-standardized form (as in my application and this shouldn't
make any difference) of the 
# Beta density function, I specify the  integrand (i.e., xf(x)) as
integrand = function(x) {x*((1/beta(a,b))*((x^(a - 1)*(xmax - x)^(b-1)) /
xmax^(a + b - 1)))}
 
# Say I want to integrate in the range (0, 0.45) and then in (0.45, Inf)
# In (0, 0.45)
 
integrate(integrand, lower = 0, upper = 0.45)$val
[1] 0.1176079

# In (0.45, Inf)
 
integrate(integrand, lower = 0.45, upper = Inf)$val
Error in integrate(integrand, lower = 0.45, upper = Inf) : 
  non-finite function value

The same thing happens when I integrate f(x) only.
Thanks again.


--
View this message in context: http://r.789695.n4.nabble.com/numerical-integration-and-non-finite-function-value-error-tp3618486p3619761.html
Sent from the R help mailing list archive at Nabble.com.
#
On Jun 23, 2011, at 8:55 AM, Adan_Seb wrote:

            
Right. integrand() is NaN outside [0,1]

 > integrand(1:100)
   [1]   0 NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN  
NaN NaN NaN NaN NaN
  [22] NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN  
NaN NaN NaN NaN NaN
  [43] NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN  
NaN NaN NaN NaN NaN
  [64] NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN  
NaN NaN NaN NaN NaN
  [85] NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN NaN
David Winsemius, MD
West Hartford, CT
#
The domain of the beta distribution as defined in R is 0 <= x <= 1 and as
shown by David Winsemius it is undefined outside [0,1]. But thats sort of
the question I have. 
To elaborate, I have a variable with 0 as its natural lower limit but can
assume any positive number as an upper limit. So its domain is [0, Inf]. In
stead of the the two-parameter beta distribution with domain [0,1], I need
to estimate a three parameter beta distribution with domain [0, max]. Given
the parameter estimates for the 3-parameter distribution, I want to do the
integration for two ranges say [0,d] and [d, Inf] for 0 < d < Inf. But the
estimated distribution has a max < Inf and hence the same problem as in the
example arises when I do the integration for [d, Inf]. Replacing the Inf
with the estimated Max seem to work in some cases but not in others. Ravi
Varadhan said such replacement doesn't make sense and in that case what can
I do to implement the numerical integration in [d, Inf] using the estimated
Beta distribution?

Many thanks. 

--
View this message in context: http://r.789695.n4.nabble.com/numerical-integration-and-non-finite-function-value-error-tp3618486p3622163.html
Sent from the R help mailing list archive at Nabble.com.