def slice_sampler(px, N=1, x=None):
 values = np.zeros(N, dtype=np.int)
 samples = np.arange(len(px))
 px = (np.array(px) / (1.0 * sum(px)))
 u = uniform(0, max(px))
 for n in xrange(N):
  included = (px >= u)
  choice = random.sample(range(np.sum(included)), 1)[0]
  values[n] = samples[included][choice]
  u = uniform(0, px[included][choice])
 if x:
  if (len(x) == len(px)):
   x = np.array(x)
   values = x[values]
  else:
   print 'px and x are different lengths. Returning index locations for px.'
 return values

def slice_sampler ( px , N = 1 , x = None ) : values = np . zeros ( N , dtype = np . int ) samples = np . arange ( len ( px ) ) px = ( np . array ( px ) / ( 1.0 * sum ( px ) ) ) u = uniform ( 0 , max ( px ) ) for n in xrange ( N ) : included = ( px >= u ) choice = random . sample ( range ( np . sum ( included ) ) , 1 ) [ 0 ] values [ n ] = samples [ included ] [ choice ] u = uniform ( 0 , px [ included ] [ choice ] ) if x : if ( len ( x ) == len ( px ) ) : x = np . array ( x ) values = x [ values ] else : print 'px and x are different lengths. Returning index locations for px.' return values
provides samples from a user-defined distribution .