0

I am triing to integrate the following function accrding to h:

FUNCTION:

integralpos <- function(h){
  (h)^(m-1)*exp(-x[4]*h-(r[v]-h-x[5]*x[3] + 0.5*(x[6]^2)*x[3])^2/
                   (2*x[6]^2*x[3]))}

CALL:

integrate(integralneg, -Inf, 0, abs.tol = 1e-08)$val

This works fine if i have example values for all variables (exept h).

The problem arises when i try to nest the integrate function in a loop where the variables change. I use bobyqa for max. likelihood estimation according to the x. The function bobyqa change the values of x[.] but since integralpos is not defined as a function of x it does not "enter" there.

Can somebody help me out here? Is there a way to run integrate with changing variables? Is there another function?

Thank you very much

pnuts
  • 58,317
  • 11
  • 87
  • 139
Valegard234
  • 45
  • 1
  • 7
  • Can you include the code with the loop? – Tim Zimmermann Aug 31 '14 at 09:55
  • @ Valegard234: Correct the title. It should be 'integrate' and not 'inegrate'. Otherwise it will not be included for searches for 'integrate' – rnso Aug 31 '14 at 10:10
  • Please note the error. The Call should be: `integrate(integralneg, -Inf, 0, abs.tol = 1e-08)$val` the next function is: `infinitesumn <- function(n,x){ (exp(-x[2])*x[2]^n)/ (factorial(n))* ((x[7]^n)/((factorial(n-1)*sqrt(2*pi*x[3]))*x[6]))* (integrate(integralneg, -Inf, 0, abs.tol = 1e-08)$val)}` but its nestet in several other functions until it reaches the top level with the function `bobyqa() (library('nloptr'))` – Valegard234 Aug 31 '14 at 11:35
  • one level up:\\`source('sumn.R') firstinfinitesum <- function(n,x){ i = 1 repeat{ savenew = infinitesumn(i,x) + saveold[q] +0*n q = q + 1 saveold[q] = savenew i = i + 1 # exit if (2*abs(infinitesumn(i-1,x)) <= FTOL*(abs(savenew - infinitesumn(i-1,x)) + abs(savenew))) {break} #before: x$SCORE } savenew }` – Valegard234 Aug 31 '14 at 11:50
  • another level up: `Fri <- function(x){ log(exp(-1*(x[1]+x[2]))*frzero(x) + exp(-x[1])*firstinfinitesum(n,x) + exp(-x[2])*secondinfinitesum(m,x) + #läuft bis hier secounddoublesum(m,x)) } ` TOP LEVEL: `bobyqa(c(0.05, 0.05, 1,0.05, 0.5, 0.000001,0, 0), Fri, lower = c(0, 0, 0.99,0, 0, 0.0000001,0, 0), upper = c(0.99, 0.99, 1,0.99, 0.99, 0.99,0.99, 0.99))` Please note that there are several other functions but I now included all relevant functions for the posted integarl – Valegard234 Aug 31 '14 at 11:52
  • WHy `integralpos` in first line? Assuming it's a typo, write a wrapper function `foointegrate<-function(h,x) integrate(integralpos(h,x).(etc)))` . You should be passing `h,m` **and** `x` to your `integralpos` function anyway. – Carl Witthoft Aug 31 '14 at 17:52

1 Answers1

0

solved the problem by passing the variables as follows:

integrate(integralneg, -Inf, 0, abs.tol = 1e-08,x=x,r=r,m=m)$val

thx for the help

Valegard234
  • 45
  • 1
  • 7