There are three methods to find the mean for grouped data, depending on the size of the data. They are:
This method is used when the number of observations is small.
When the data values are large, the step-deviation method is used to find the mean. The formula is given by:
def parityString(string):
while len(string) < 9:
string += " "
return string
def stepDeviationMethod(numberOFClasses,lowerLimitOfFirstClass,upperLimitOfFirstClass):
classWidth = upperLimitOfFirstClass - lowerLimitOfFirstClass
listLowerClassLimits = []
listUpperClassLimits = []
classFrequency = []
midValue = []
assumed_Mean = 0
for i in range(numberOFClasses):
listLowerClassLimits.append(lowerLimitOfFirstClass)
listUpperClassLimits.append(upperLimitOfFirstClass)
print("Enter the frequency of the class:",listLowerClassLimits[i],"-",listUpperClassLimits[i])
classFrequency.append(float(input()))
midValue.append((lowerLimitOfFirstClass+upperLimitOfFirstClass)/2.0)
lowerLimitOfFirstClass += classWidth
upperLimitOfFirstClass += classWidth
print("Select the assumed mean (A) from the following values:",midValue)
assumed_Mean = float(input())
di = []
fidi = []
sum_fi = 0
sum_fidi = 0
print(parityString("CLASSES"),parityString("frequency(fi)"),parityString("mid value(xi)")," ",parityString("di=(xi-A)/h"),end='')
print(" ",parityString("fi*di"))
for i in range(numberOFClasses):
di.append((midValue[i]-assumed_Mean)/classWidth)
fidi.append(classFrequency[i]*di[i])
print(str(listLowerClassLimits[i]),end =' '),
print("-",parityString(str(listUpperClassLimits[i]))," ",end='')
print(parityString(str(classFrequency[i]))," ",parityString(str(midValue[i]))," ",parityString(str(di[i])),end = ' ')
print(parityString(str(fidi[i])))
sum_fi += classFrequency[i]
sum_fidi += fidi[i]
print("--------------------------------------------------------------------------------------------")
print("Assumed mean A =",assumed_Mean," Class width h = ",classWidth)
print(parityString("fi="+str(sum_fi)),parityString(" ")," ",parityString("fidi="+str(sum_fidi)))
print("CALCULATED MEAN BY SHORT-CUT METHOD:",round(assumed_Mean+(sum_fidi/sum_fi)*classWidth,2))
print()
def shortCutMethod(numberOFClasses,lowerLimitOfFirstClass,upperLimitOfFirstClass):
classWidth = upperLimitOfFirstClass - lowerLimitOfFirstClass
listLowerClassLimits = []
listUpperClassLimits = []
classFrequency = []
midValue = []
assumed_Mean = 0
for i in range(numberOFClasses):
listLowerClassLimits.append(lowerLimitOfFirstClass)
listUpperClassLimits.append(upperLimitOfFirstClass)
print("Enter the frequency of the class:",listLowerClassLimits[i],"-",listUpperClassLimits[i])
classFrequency.append(float(input()))
midValue.append((lowerLimitOfFirstClass+upperLimitOfFirstClass)/2.0)
lowerLimitOfFirstClass += classWidth
upperLimitOfFirstClass += classWidth
print("Select the assumed mean (A) from the following values:",midValue)
assumed_Mean = float(input())
di = []
fidi = []
sum_fi = 0
sum_fidi = 0
print(parityString("CLASSES"),parityString("frequency(fi)"),parityString("mid value(xi)")," ",parityString("di=xi-A"),end='')
print(parityString("fi*di"))
for i in range(numberOFClasses):
di.append(midValue[i]-assumed_Mean)
fidi.append(classFrequency[i]*di[i])
print(str(listLowerClassLimits[i]),end =' '),
print("-",parityString(str(listUpperClassLimits[i]))," ",end='')
print(parityString(str(classFrequency[i]))," ",parityString(str(midValue[i]))," ",parityString(str(di[i])),end = ' ')
print(parityString(str(fidi[i])))
sum_fi += classFrequency[i]
sum_fidi += fidi[i]
print("--------------------------------------------------------------------------------------------")
print("Assumed mean A =",assumed_Mean)
print(parityString("fi="+str(sum_fi)),parityString(" ")," ",parityString("fidi="+str(sum_fidi)))
print("CALCULATED MEAN BY SHORT-CUT METHOD:",round(assumed_Mean+(sum_fidi/sum_fi),2))
print()
def directMethod(numberOFClasses,lowerLimitOfFirstClass,upperLimitOfFirstClass):
classWidth = upperLimitOfFirstClass - lowerLimitOfFirstClass
listLowerClassLimits = []
listUpperClassLimits = []
classFrequency = []
midValue = []
for i in range(numberOFClasses):
listLowerClassLimits.append(lowerLimitOfFirstClass)
listUpperClassLimits.append(upperLimitOfFirstClass)
print("Enter the frequency of the class:",listLowerClassLimits[i],"-",listUpperClassLimits[i])
classFrequency.append(float(input()))
midValue.append((lowerLimitOfFirstClass+upperLimitOfFirstClass)/2.0)
lowerLimitOfFirstClass += classWidth
upperLimitOfFirstClass += classWidth
fixi = []
sum_fi = 0
sum_fixi = 0
print(parityString("CLASSES"),parityString("frequency(fi)"),parityString("mid value(xi)")," ",parityString("fi*xi"))
print()
for i in range(numberOFClasses):
fixi.append(classFrequency[i]*midValue[i])
print(str(listLowerClassLimits[i]),end =' '),
print("-",parityString(str(listUpperClassLimits[i]))," ",end='')
print(parityString(str(classFrequency[i]))," ",parityString(str(midValue[i]))," ",parityString(str(fixi[i])))
sum_fi += classFrequency[i]
sum_fixi += fixi[i]
print("--------------------------------------------------------------------------------------------")
print(parityString(" "),parityString("fi="+str(sum_fi)),parityString(" ")," ",parityString("fixi="+str(sum_fixi)))
print("CALCULATED MEAN BY DIRECT METHOD:",round(sum_fixi/sum_fi,2))
print("****** MEAN CALCULATION *******")
key = 0
while key != -99:
print(" WELCOME TO THE MAIN MENU ***** MEAN CALCULATION FOR CONTINUOUS DATA *****")
print(" Press 1 to CALCULATE MEAN BY DIRECT METHOD: ")
print(" Press 2 to CALCULATE MEAN BY SHORT CUT METHOD / ASSUMED MEAN METHOD: ")
print(" Press 3 to CALCULATE MEAN BY STEP DEVIATION METHOD: ")
print(" Type -99 to exit the menu:")
key = int(input("Enter your choice:"))
if key == 1:
numberOFClasses = int(input("Enter the number of classes:"))
lowerLimitOfFirstClass = int(input("Enter the lower limit of the first classes:"))
upperLimitOfFirstClass = int(input("Enter the upper limit of the first classes:"))
directMethod(numberOFClasses,lowerLimitOfFirstClass,upperLimitOfFirstClass)
if key == 2:
numberOFClasses = int(input("Enter the number of classes:"))
lowerLimitOfFirstClass = int(input("Enter the lower limit of the first classes:"))
upperLimitOfFirstClass = int(input("Enter the upper limit of the first classes:"))
shortCutMethod(numberOFClasses,lowerLimitOfFirstClass,upperLimitOfFirstClass)
if key == 3:
numberOFClasses = int(input("Enter the number of classes:"))
lowerLimitOfFirstClass = int(input("Enter the lower limit of the first classes:"))
upperLimitOfFirstClass = int(input("Enter the upper limit of the first classes:"))
stepDeviationMethod(numberOFClasses,lowerLimitOfFirstClass,upperLimitOfFirstClass)
if key == -99:
print("Exited from main menu:")
No comments:
Post a Comment