r - Recursive sum, using Poisson distribution -
i trying build recursive function in r,
h(x,t) = \sum\limits_{d=0}^{x} (pr(d=d)*(h*(x-d)+h(x-d,t-1))) + \sum\limits_{d=x+1}^{\infty} (pr(d=d)*(p(*d-x)+ h(0,t-1)))
where h,p constants, d ~ po(l) , h(x,0) = 0, code have done far, gives obvious error, can't see fix. code
p<- 1000 # unit penalty cost lost sales h<- 10 # unit inventory holding cost pr. time unit l<- 5 # mean of d h <- function(x,t){ if(t==0)(return(0)) fp <- 0 sp <- 0 for(d in 0:x){ fp <- fp + dpois(x=d,l)*(h*(x-d)+h(x-d,t-1)) } for(d in x+1:inf){ sp <- sp + dpois(x=d,l)*(p*(d-x)+h(0,t-1)) } return(fp+sp) }
when run this, error is
error in 1:inf : result long vector
which, seems obvious, question is, can point me in direction redefine problem, can r bring me solution?
thanks in advance.
going x+1:inf
won't work. since you're using poisson's pdf, can add upper bound (why? think shape of pdf , how small values @ right tail):
for(d in x+1:100)
which when ran h(20,2)
gives
[1] 252.806
when increase
for(d in x+1:500)
then h(20,2)
gives
[1] 252.806
Comments
Post a Comment