self.range_list = [[probable_min_val + i * scale, probable_min_val + (i + 1) * scale] for i in range(self.range_num)] # 数值区间表,左小右大,左闭右开 self.range_element_count = [0for _ inrange(self.range_num)] # 落入区间内的数值数,小标与数值区间表对应 self.real_val_probability_preference_list = [0.5for _ inrange(self.range_num)] # 实际值值概率偏向(偏向左边)
defcalculate(self, _val): """根据输入数值计算估计值""" for i inrange(self.range_num): tmp_range = self.range_list[i] if _val in np.arange(tmp_range[0], tmp_range[1]): # 在区间范围内 self.range_element_count[i] += 1# 区间元素个数加一 self.real_val_probability_preference_list[i] = 1 - (_val - tmp_range[0]) / self.scale # 概率偏向更新
max_count_index_list = [i for i, x inenumerate(self.range_element_count) if x == max(self.range_element_count)] probable_real_val_list = [] for i inrange(len(max_count_index_list)): val_range = self.range_list[max_count_index_list[i]] probability_preference = self.real_val_probability_preference_list[max_count_index_list[i]] probable_real_val_list.append( val_range[0] * probability_preference + val_range[1] * (1 - probability_preference))
if probable_real_val_list: returnsum(probable_real_val_list) / len(probable_real_val_list) else: return0
if __name__ == "__main__": real_value = 1 num = 100