Menu

Python Formula E F X Xn 1 P X F X Q43833401

In python!

The formula is E(f(X)) = Xn i=1 p(x)f(x) , <-(summation ofp(x)f(x))You are supposed to complete the function expect in theexpect.py file. There are two input parameters of the functionexpect. (1) A dictionary representing the distribution of therandom variable. The keys of dictionary are the possible values ofrandom variable X and the values of the dictionary are thecorresponding probability p(x). (2) A function. The function faccepts a scalar as input and returns a scalar. The return value ofthe function expect is a scalar, representing the expected value off(X).

def main(): xDistributionExample1={1: 1/5, 2: 2/5, 3: 2/5} functionExample1=lambda x: x ** 2 print(expect(xDistributionExample1, functionExample1)) xDistributionExample2={1: 1/6, -1/2: 1/3, 1/3: 1/4, -1/4: 1/12, 1/5: 1/6} functionExample2=lambda x: 1/x print(expect(xDistributionExample2, functionExample2))if __name__ == ‘__main__’: main()

Expert Answer


Answer to In python! The formula is E(f(X)) = Xn i=1 p(x)f(x) , …

OR