python - 需要帮助将公式 D=2∑ni=1{Ylog[YE(Y)]−[Y−E(Y)]} 转换为 python 代码
解决方案
假设您已经解决了计算像 Y 这样的随机变量的平均值的问题。所以,我假设您有一个名为的东西mean(sample: list)
,它接收 RV(随机变量)Y 所采用的值列表。
现在,要将公式转换为代码,可能有很多方法。我个人更喜欢那些更容易阅读和理解的。
import math
sample = [] # Whatever values your RV takes
mean_of_Y = mean(sample)
D = 0
# This part will make the summation - obs stands for observation
for obs in sample:
D += obs*math.log(obs/mean_of_Y, 10) - (obs - mean_of_Y)
# I made the assumption that the log's base is 10
# Now we need to multiply it by 2
D = D*2
# If you want to see it
print(D)
推荐阅读
- r - R倾向得分与百万行数据匹配
- dictionary - 如何根据 .bmp 文件中的 RGB 代码定位特定像素坐标?
- python - 如何在虚拟机中将python3.5升级到python3.6?
- flexbox - 我正在尝试在我的文件中创建一个 H1 元素,它只是作为文本发布而不是所需的结果
- android - MutableLiveData 未发布正确的值 - Android
- arrays - 数组.map与.map中的Ruby整数元素!?以及如何检查哪个代码最好?
- c# - 如何在没有 Mono.Posix 的情况下使用 p/invoke 在 .NET 5 / .NET 6 中获取 Linux 文件权限?
- c# - 当右侧为空时,Linq Left Join 在列表中失败
- c# - Azure VM - TCP 侦听器不适用于自定义端口
- flask - Flask-SocketIo | 登录 Chrome 后不会返回 session['name'] 值,但 Firefox 会