Hello everyone'

I am trying to port an R function to estimate some parameters to the rpy2
but the performance in python is appaling. So I was wondering if there is a
best way of doing this. What I have at the moment is:
R code
finbinom <-function(pars,x)
{
p<-pars[1]
mu <- pars[2]
size <- pars[3]
if(any(pars <= 0)) return(NA)
if(p >= 1) return(NA)
 sum(log((1-p) * dnbinom(x, mu = mu, size = size, log = FALSE) + p * (x ==
0)))
}

opNB <- optim(par = c(0.9, 100, 1), fn = finbinom, control = list(fnscale =
-1), x = distances)

Python
def zero_inflated(Pars,k):
    p,mu,size = Pars
    if p<=0 or mu<=0 or size<=0:return rinterface.NA_Real
    if p>=1:return rinterface.NA_Real
    i=0
    result=0
    #return rsum(rlog(rinterface.FloatSexpVector([1-p]) * dnbinom(k, mu =
mu, size = size, log = rinterface.BoolSexpVector([False]))))

    for elem in dnbinom(x=k, mu=mu, size=size,
log=rinterface.BoolSexpVector([False])):
        if k[i] == 0:result+=rlog((1-p)*elem+p)[0]
        else: result+=rlog((1-p)*elem)[0]

    return result
cost_zinb = rinterface.rternalize(zero_inflated)
start_params = [0.9, 100, 1]
res = stats.optim(start_params, cost_zinb, control = rlist(fnscale = -1),
k=vDistances)
print 'Distances between sRNAs:',res[0],#res[1],res[2],res[3]

Thank you very much in advance.
Best,
Bruno
------------------------------------------------------------------------------
Virtualization & Cloud Management Using Capacity Planning
Cloud computing makes use of virtualization - but cloud computing 
also focuses on allowing computing to be delivered as a service.
http://www.accelacomm.com/jaw/sfnl/114/51521223/
_______________________________________________
rpy-list mailing list
rpy-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/rpy-list

Reply via email to