计算两个骰子熵的python代码
时间: 2023-08-06 19:04:34 浏览: 101
样本熵python代码
5星 · 资源好评率100%
以下是计算两个骰子熵的Python代码:
```python
import math
def entropy(probabilities):
return -sum([p * math.log2(p) for p in probabilities if p != 0])
dice1_probabilities = [1/6] * 6
dice2_probabilities = [0.1, 0.1, 0.1, 0.1, 0.3, 0.1] # example probabilities
joint_probabilities = [[d1 * d2 for d2 in dice2_probabilities] for d1 in dice1_probabilities]
joint_entropy = entropy([p for row in joint_probabilities for p in row])
dice1_entropy = entropy(dice1_probabilities)
dice2_entropy = entropy(dice2_probabilities)
print(f"Joint entropy: {joint_entropy}")
print(f"Dice 1 entropy: {dice1_entropy}")
print(f"Dice 2 entropy: {dice2_entropy}")
```
此代码计算了两个骰子的联合熵,以及每个骰子的独立熵。您可以通过更改`dice1_probabilities`和`dice2_probabilities`列表来使用不同的概率分布。
阅读全文