Python Formula V Ar X E X E X 2 Supposed Complete Function Getvariance Expectpy File Follo Q43833405
In python!
The formula is V ar(X) = E(((X − E(X))2 ) You are supposed tocomplete the function getVariance in the expect.py file. You shouldfollow the steps commented in the code and use the expect function.The getVariance function accepts a dictionary as input. Thedictionary represents the distribution of the random variable. Thekeys of dictionary are the possible values of random variable X andthe values of the dictionary are the corresponding probabilityp(x). The return value of the function getVariance is a scalar,representing the variance of X.
expect.py file:def expect(xDistribution, function): ################################################### Your code here################################################## return expect################################################### Your code below each comment################################################## def getVariance(xDistribution): #Step 1 – Calculate the expected value E(X) def getSquaredDistanceToMu(x): #Step 2 – Calculate (X-E(X))^2 return #Step 3 – Calculate Variance: Var(X)=E((X-E(X))^2) returndef 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 V ar(X) = E(((X − E(X))2 ) You are supposed to complete the function getVariance in the expe…
OR