Example #1
0
    def __init__(self, mytable):
        self.probs = numpy.array(mytable, float)
        s = self.probs.sum()
        if s > 0:
            self.probs /= s
        else:
            raise ValueError, "sum of table frequencies must be > 0"

        self.sampler = _intsampler(self.probs)
Example #2
0
def rdiscrete(pvals, numsamples=1):
    """Sample from discrete distribution with probabilities given by pvals.
    
    If size is given, a matrix of the given size is filled with samples, 
    otherwise a single value (between 0 and len(pvals) is returned.
    """
    if HAVE_INTSAMPLER and numsamples > 100:
        sampler = _intsampler(pvals)
        return sampler.sample(numsamples)
    else:
        cdf = cumsum(pvals)
        unif = R.random_sample(size=numsamples)
        return cdf.searchsorted(unif)