Thank you Laurent for your quick reply.

The if k[i] == 0 in R is implement as being  + p * (x == 0) which will add
p if k[i]==0 or zero if not. I was trying to came up with a closer
implementation but I cannot do sums or multiplications between Robjects is
that right?

2012/2/23 Laurent Gautier <lgaut...@gmail.com>

>  On 2012-02-23 17:57, Bruno Santos wrote:
>
> 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]
>
>
> if size is large, this is potentially a bottleneck. Note that when
> comparing the performances with pure R one should consider comparing
> identical code: the R bit doesn't seem to have the conditional branch if
> k[i] == 0 (or did I miss something).
>
>
>     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 
> listrpy-list@lists.sourceforge.nethttps://lists.sourceforge.net/lists/listinfo/rpy-list
>
>
>
------------------------------------------------------------------------------
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