考虑随机变量。假设取值1,…,具有概率1,…,并取值1,…,具有概率1,…、。假设是独立的。实现函数joint_pmf(xvalue、xprobs、yvalue、yprobs),该函数接受值1、…的数组。,作为xvalue,概率1,…的数组。,作为xprobs,与yvalue和yprobs相同。函数应该返回一个字典,其中键是元组(x,y),其中x是某个值,y是,对应的值是联合概率质量函数的值,(,)
def joint_pmf(xvalues, xprobs, yvalues, yprobs):
# your code
testdata = [([1], [1], [2, 3], [0.2, 0.8]),
([1, 2], [0.5, 0.5], [3, 4, 5], [0.3, 0.3, 0.4])]
answers = [{(1, 2): 0.2, (1, 3): 0.8},
{(1, 3): 0.15,
(1, 4): 0.15,
(1, 5): 0.2,
(2, 3): 0.15,
(2, 4): 0.15,
(2, 5): 0.2}]
for data, answer in zip(testdata, answers):
assert joint_pmf(*data) == answer在找到解决方案之前,我无法理解这个任务。例如,对于4x值(1、1、2、3、0.2、0.8),测试数据中只有两个概率?为什么像x=1和y=1这样的答案中没有这样的值?{(1,1):.}?你能解释一下吗?或者你的解决方案?
发布于 2022-05-23 12:51:00
def joint_pmf(xvalues, xprobs, yvalues, yprobs):
jpm = {}
for i in range(len(xvalues)):
for j in range(len(yvalues)):
jpm[(xvalues[i],yvalues[j])] = xprobs[i]*yprobs[j]
return jpm也许是更优雅的解决方案?
https://stackoverflow.com/questions/72346880
复制相似问题