def __init__(self, opts): if isinstance(opts, dict): self.options = [] self.weights = dict() ordered = [] for opt, prob in opts.items(): if not isinstance(prob, (float, int)): raise RuntimeParseError( f'discrete distribution weight {prob}' ' is not a constant number') if prob < 0: raise RuntimeParseError( f'discrete distribution weight {prob} is negative') if prob == 0: continue opt = toDistribution(opt) self.options.append(opt) self.weights[opt] = prob ordered.append(prob) self.cumulativeWeights = tuple(itertools.accumulate(ordered)) else: self.options = tuple(toDistribution(opt) for opt in opts) self.cumulativeWeights = None if len(self.options) == 0: raise RuntimeParseError( 'tried to make discrete distribution over empty domain!') valueType = type_support.unifyingType(self.options) super().__init__(*self.options, valueType=valueType)
def __init__(self, opts): self.options = opts valueType = type_support.unifyingType(self.options) super().__init__(*self.options, valueType=valueType)
def __init__(self, index, options): self.index = index self.options = tuple(toDistribution(opt) for opt in options) assert len(self.options) > 0 valueType = type_support.unifyingType(self.options) super().__init__(index, *self.options, valueType=valueType)