简体   繁体   English

独立随机变量:PMF 计算

[英]Independent random variables: PMF calculation

Consider random variables 𝑋 and 𝑌 .考虑随机变量 𝑋 和 𝑌 。 Assume that 𝑋 takes values 𝑥1,…,𝑥𝑛 with probabilities 𝑝1,…,𝑝𝑛 and 𝑌 takes values 𝑦1,…,𝑦𝑚 with probabilities 𝑞1,…,𝑞𝑚 .假设𝑋取值𝑥1,…,𝑥𝑛,概率为𝑝1,…,𝑝𝑛,𝑌取值𝑦1,…,𝑦𝑚,概率为𝑞1,…,𝑞𝑚。 Assume that 𝑋 and 𝑌 are independent.假设𝑋和𝑌是独立的。 Implement function joint_pmf(xvalues, xprobs, yvalues, yprobs) that takes an array of values 𝑥1,…,𝑥𝑛 as xvalues, an array of probabilities 𝑝1,…,𝑝𝑛 as xprobs and the same with yvalues and yprobs.实现函数joint_pmf(xvalues, xprobs, yvalues, yprobs),它将值数组𝑥1,…,𝑥𝑛作为xvalues,概率数组𝑝1,…,𝑝𝑛作为xprobs,yvalues和yprobs也是如此。 The function should return a dictionary which keys are tuples (x, y) where x is some value 𝑥𝑖 and y is 𝑦𝑗 and corresponding values are values of joint probability mass function 𝑝𝑚𝑓𝑋,𝑌(𝑥𝑖,𝑦𝑗)该函数应返回一个字典,其中键是元组 (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

I can't understand the task before going to the solution.在找到解决方案之前,我无法理解任务。 For example, there are only two probabilities in testdata for 4 x values ([1], [1], [2, 3], [0.2, 0.8])?例如,对于 4 个 x 值([1]、[1]、[2、3]、[0.2、0.8]),testdata 中只有两个概率? Why is there no such value in answers like x=1 and y=1?为什么像 x=1 和 y=1 这样的答案中没有这样的值? {(1,1): ...}? {(1,1): ...}? Could you please give an explanation or your solution?你能给出一个解释或你的解决方案吗?

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

may be more elegant solution?可能是更优雅的解决方案?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM