Need help with statistics interview question
#1
I'm having trouble with one of the statistics interview questions from this resource. The question is:

Given a data set X, calculate the mean and variance of X.

I've tried to solve the problem using the following code:

Code:
mean = 0
for i in range(len(X)):
    mean += X[i]
mean = mean/len(X)
variance = 0
for i in range(len(X)):
    variance += (X[i]-mean)**2
variance = variance/len(X)

But when I run the code, I get an error. Can anyone help me figure out what the problem is? Any help would be greatly appreciated. Thanks!
Reply
#2
I don't see X defined anywhere
Reply
#3
Well, there are some logical issues with your code, try this code to fix the error.

X = [list of your data points]

mean = 0
for i in range(len(X)):
mean += X[i]
mean = mean / len(X)

variance = 0
for i in range(len(X)):
variance += (X[i] - mean) ** 2
variance = variance / len(X)

print("Mean:", mean)
print("Variance:", variance)

Thanks
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Question About the Concept of Abstracts in Java RobertX 4 5,196 Jan 17, 2024, 07:50 am
Last Post: gulshan212
  SAP MM Interview Questions - Solving Challenge Avantika_Sharmaa24 1 6,408 Sep 28, 2023, 10:09 am
Last Post: SectorVector
  Manual Testing Interview Questions Avantika_Sharmaa24 2 6,310 Sep 15, 2023, 06:39 am
Last Post: gulshan212
  Need help with a Selenium coding problem - Clicking a button using XPath Avantika_Sharmaa24 2 7,254 Aug 21, 2023, 05:34 am
Last Post: Avantika_Sharmaa24
  Trouble with Deep Learning Interview Questions Avantika_Sharmaa24 3 9,316 Jul 13, 2023, 12:50 pm
Last Post: gulshan212



Users browsing this thread: 1 Guest(s)