【发布时间】:2021-11-02 10:41:52
【问题描述】:
【问题讨论】:
-
到目前为止您尝试过什么?你遇到过哪些问题?为了让您开始,您可以使用 for 循环来模拟 sigma(即
for i in range(1, n + 1)或带有sum的列表理解sum(i for i in range(1, n + 1))
标签: python python-3.x statistics
【问题讨论】:
for i in range(1, n + 1) 或带有 sum 的列表理解 sum(i for i in range(1, n + 1))
标签: python python-3.x statistics
import math
ys = [1,2,3] # 3 classes
expectation = {
1 : 2.718281828459045,
2 : 7.38905609893065,
3 : 20.085536923187668,
}
#i have given exponent value as expectation in the example, some sample values
D = sum([((y * math.log(y/expectation[y])) - (y - expectation[y])) for y in ys]) * 2
离散和连续随机变量的期望值不同。
P(Y=y) 可以通过Probability mass function计算得到
其中Fy(Y)可以通过Probability density function计算,(a,b)是取值范围
【讨论】:
假设您已经解决了计算像 Y 这样的随机变量的平均值的问题。所以,我假设您有一个名为 mean(sample: list) 的东西,它接收 R.V. 采用的值列表。 (随机变量)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)
【讨论】: