def mixture_rvs(prob, size, dist, kwargs=None):
 if (len(prob) != len(dist)):
  raise ValueError('You must provide as many probabilities as distributions')
 if (not np.allclose(np.sum(prob), 1)):
  raise ValueError('prob does not sum to 1')
 if (kwargs is None):
  kwargs = (({},) * len(prob))
 idx = _make_index(prob, size)
 sample = np.empty(size)
 for i in range(len(prob)):
  sample_idx = idx[..., i]
  sample_size = sample_idx.sum()
  loc = kwargs[i].get('loc', 0)
  scale = kwargs[i].get('scale', 1)
  args = kwargs[i].get('args', ())
  sample[sample_idx] = dist[i].rvs(*args, **dict(loc=loc, scale=scale, size=sample_size))
 return sample

def mixture_rvs ( prob , size , dist , kwargs = None ) : if ( len ( prob ) != len ( dist ) ) : raise ValueError ( 'You must provide as many probabilities as distributions' ) if ( not np . allclose ( np . sum ( prob ) , 1 ) ) : raise ValueError ( 'prob does not sum to 1' ) if ( kwargs is None ) : kwargs = ( ( { } , ) * len ( prob ) ) idx = _make_index ( prob , size ) sample = np . empty ( size ) for i in range ( len ( prob ) ) : sample_idx = idx [ ... , i ] sample_size = sample_idx . sum ( ) loc = kwargs [ i ] . get ( 'loc' , 0 ) scale = kwargs [ i ] . get ( 'scale' , 1 ) args = kwargs [ i ] . get ( 'args' , ( ) ) sample [ sample_idx ] = dist [ i ] . rvs ( * args , ** dict ( loc = loc , scale = scale , size = sample_size ) ) return sample
sample from a mixture of distributions .