def __init__(self, values, log_weights=None):
self.length = len(values)
if log_weights is None:
log_weights = np.full(len(values), -math.log(len(values))) # assume uniform
else:
log_weights = np.array(log_weights)
weights = np.exp(log_weights - logsumexp(log_weights))
self.distribution = collections.defaultdict(float)
for i in range(self.length):
self.distribution[values[i]] += weights[i]
self.distribution = collections.OrderedDict(sorted(self.distribution.items()))
self.values = np.array(list(self.distribution.keys()))
self.weights = np.array(list(self.distribution.values()))
评论列表
文章目录