Question
In python! The formula is V ar(X) = E(((X E(X))2 ) You are supposed to complete the function getVariance in the expect.py file. You should
In python!
The formula is V ar(X) = E(((X E(X))2 ) You are supposed to complete the function getVariance in the expect.py file. You should follow the steps commented in the code and use the expect function. The getVariance function accepts a dictionary as input. The dictionary represents the distribution of the random variable. The keys of dictionary are the possible values of random variable X and the values of the dictionary are the corresponding probability p(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) return 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()
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started