Integrate in R is failing when upper limit is Inf -


i need numerically approximate variance of logarithm of sum of 2 log-normal random variables. i'd in r, here's example:

################ ## setup data ## ################  sdx1 = 0.33 sdx2 = 0.70 mux1 = log(32765) - 0.5 * sdx1^2 mux2 = log(52650) - 0.5 * sdx2^2  #################################### ## pdf sum of 2 lognormal rvs ## ####################################  d2lnorm = function(z, mux1, mux2, sdx1, sdx2){      #pdfs     l1 = distr::lnorm(meanlog = mux1, sdlog = sdx1)     l2 = distr::lnorm(meanlog = mux2, sdlog = sdx2)      #convlution integral     l1plusl2 = distr::convpow(l1 + l2, 1)      #density function     f.z = distr::d(l1plusl2)      #evaluate     return(f.z(z))  }  ############################################ ## expectation sum of 2 lognormal rvs ## ############################################      ex2lnorm = function(mux1, mux2, sdx1, sdx2){          #e(g(x)) = integral of g(x) * f(x) w.r.t x     integrate(function(z) log(z) * d2lnorm(z, mux1 = mux1, mux2 = mux2, sdx1 = sdx1, sdx2 = sdx2), lower = 0, upper = +inf)$value  }  ############## ## run code ## ##############  ex2lnorm(mux1, mux2, sdx1, sdx2) 

however, integral evaluates 0. if change upper limit of integrate large number works. however, i'm running bunch of simulations , can't fiddle upper limit work every time. there other way work consistently?

what if finding maximum upper value doesn't give 0:

ex2lnorm = function(mux1, mux2, sdx1, sdx2){        f <- function(z) log(z) * d2lnorm(z, mux1 = mux1, mux2 = mux2, sdx1 = sdx1, sdx2 = sdx2)    upper <- 2^10   cond.lower <- false   repeat {     new <- integrate(f, lower = 0, upper = upper)$value     if (new > 0) cond.lower <- true     if (cond.lower && new == 0) break     upper <- upper * 2     prev <- new   }    prev } 

Comments

Popular posts from this blog

javascript - Replicate keyboard event with html button -

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Web audio api 5.1 surround example not working in firefox -