Median,Mode, Percentile and Percentile Rank Calculator for CBSE,ICSE,Statistics, Psychology and Education students using Python programming. Run this program on Google Colab.
def parityString(string):
while len(string) < 9:
string += " "
return string
def modeContinuousData(numberOFClasses,lowerLimitOfFirstClass,upperLimitOfFirstClass):
classWidth = upperLimitOfFirstClass - lowerLimitOfFirstClass
listLowerClassLimits = []
listUpperClassLimits = []
classFrequency = []
lower_limit_modalclass = 0
f0 = 0
f1 = 0
f2 = 0
modal_index = 0
max_frequency = 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()))
if max_frequency < classFrequency[i]:
max_frequency = classFrequency[i]
modal_index = i
lowerLimitOfFirstClass += classWidth
upperLimitOfFirstClass += classWidth
f1 = classFrequency[modal_index]
f0 = classFrequency[modal_index-1]
f2 = classFrequency[modal_index+1]
lower_limit_modalclass = listLowerClassLimits[modal_index]
print(parityString("CLASSES"),parityString("frequency(fi)"))
for i in range(numberOFClasses):
if i == modal_index or i == modal_index+1:
print("----------------------------------------------------------")
print(str(listLowerClassLimits[i]),end =' '),
print("-",parityString(str(listUpperClassLimits[i]))," ",end='')
print(parityString(str(classFrequency[i])))
print("=================================================================")
print("lower limit of the modal class l =",lower_limit_modalclass," Class width h = ",classWidth)
print("frequency of the modal class f1 =",f1)
print("frequency of the class preceeding the modal class f0 =",f0)
print("frequency of the class succeeding the modal class f2 =",f2)
mode = lower_limit_modalclass + ((f1-f0)/(2*f1-f0-f2))*classWidth
print("MODE FOR THE GIVEN DATA SET:",round(mode,2))
print()
def medianContinuousData(numberOFClasses,lowerLimitOfFirstClass,upperLimitOfFirstClass):
classWidth = upperLimitOfFirstClass - lowerLimitOfFirstClass
listLowerClassLimits = []
listUpperClassLimits = []
classFrequency = []
cumulativeFrequency = []
lower_limit_medianclass = 0
f = 0 #frequency of median class
cf = 0 #cf of the class preceeding the median class
n = 0 # total frequency
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()))
n += classFrequency[i]
lowerLimitOfFirstClass += classWidth
upperLimitOfFirstClass += classWidth
for j in range(numberOFClasses):
if j == 0:
cumulativeFrequency.append(classFrequency[j])
else:
cumulativeFrequency.append(cumulativeFrequency[j-1]+classFrequency[j])
counter = 0
#print(classFrequency,cumulativeFrequency)
while cumulativeFrequency[counter] < (n/2.0):
counter +=1
lower_limit_medianclass = listLowerClassLimits[counter]
f = classFrequency[counter]
cf = cumulativeFrequency[counter-1]
print(parityString("CLASSES"),parityString("frequency(fi)")+" "+parityString("cf"))
for i in range(numberOFClasses):
if i == counter or i == counter+1:
print("----------------------------------------------------------")
print(str(listLowerClassLimits[i]),end =' '),
print("-",parityString(str(listUpperClassLimits[i]))," ",end='')
print(parityString(str(classFrequency[i])),parityString(str(cumulativeFrequency[i])))
print("=================================================================")
print("lower limit of the modal class l =",lower_limit_medianclass," Class width h = ",classWidth)
print("frequency of the median class f =",f)
print("cumulative frequency of the class preceeding the median class cf =",cf)
median = lower_limit_medianclass + ((n/2 - cf)/f)*classWidth
print("Median FOR THE GIVEN DATA SET:",round(median,2))
print()
def percentileRankContinuousData(numberOFClasses,lowerLimitOfFirstClass,upperLimitOfFirstClass,score):
listLowerClassLimits = []
listUpperClassLimits = []
classFrequency = []
cumulativeFrequency = []
classWidth = upperLimitOfFirstClass - lowerLimitOfFirstClass
X = score
f = 0 #frequency of class containing the score
cf = 0 #cf of the class preceeding the class containing the score
n = 0 # total frequency
L = 0 #lower limit of the class containing the score
positionOfScore = 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()))
if score >= listLowerClassLimits[i] and i <= listUpperClassLimits[i]:
L = listLowerClassLimits[i]
positionOfScore = i
f= classFrequency[i]
n += classFrequency[i]
lowerLimitOfFirstClass += classWidth
upperLimitOfFirstClass += classWidth
for j in range(numberOFClasses):
if j == 0:
cumulativeFrequency.append(classFrequency[j])
else:
cumulativeFrequency.append(cumulativeFrequency[j-1]+classFrequency[j])
counter = 0
cf = cumulativeFrequency[positionOfScore-1]
print(parityString("CLASSES"),parityString("frequency(fi)")+" "+parityString("cf"))
for i in range(numberOFClasses):
if i == positionOfScore+1 or i == positionOfScore:
print("----------------------------------------------------------")
print(str(listLowerClassLimits[i]),end =' '),
print("-",parityString(str(listUpperClassLimits[i]))," ",end='')
print(parityString(str(classFrequency[i])),parityString(str(cumulativeFrequency[i])))
print("=================================================================")
print("Score for which the percentile rank is being calculated X =",X)
print("cumulative frequency of the class preceeding the class containing the score F/cf =",cf)
print("lower limit of the class containing the score L =",L)
print("size/width of the class interval i = ",classWidth)
print("frequency of the class f =",f)
print("Total number of cases in the given frequency distribution N =",n)
print(" Percentile Rank PR = (100/N)(cf + ((X-L)/classWidth)*f)")
PR = (cf + ((X-L)/classWidth)*f)
PR = float((100*PR)/n)
print("Percentile rank for the given score ",X," is ",round(PR,2))
print("In other words the person who scored ",X," is above ",round(PR,2),"% of the sample")
print()
def percentileContinuousData(numberOFClasses,lowerLimitOfFirstClass,upperLimitOfFirstClass):
classWidth = upperLimitOfFirstClass - lowerLimitOfFirstClass
listLowerClassLimits = []
listUpperClassLimits = []
classFrequency = []
cumulativeFrequency = []
lower_limit_percentileclass = 0
p = int(input("Enter the percentile which you want to calculate:"))
f = 0 #frequency of percentile class
cf = 0 #cf of the class preceeding the percentile class
n = 0 # total frequency
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()))
n += classFrequency[i]
lowerLimitOfFirstClass += classWidth
upperLimitOfFirstClass += classWidth
X = (p*n)/100.0
for j in range(numberOFClasses):
if j == 0:
cumulativeFrequency.append(classFrequency[j])
else:
cumulativeFrequency.append(cumulativeFrequency[j-1]+classFrequency[j])
counter = 0
#print(classFrequency,cumulativeFrequency)
while cumulativeFrequency[counter] < (X):
counter +=1
lower_limit_percentileclass = listLowerClassLimits[counter]
f = classFrequency[counter]
cf = cumulativeFrequency[counter-1]
print(parityString("CLASSES"),parityString("frequency(fi)")+" "+parityString("cf"))
for i in range(numberOFClasses):
if i == counter or i == counter+1:
print("----------------------------------------------------------")
print(str(listLowerClassLimits[i]),end =' '),
print("-",parityString(str(listUpperClassLimits[i]))," ",end='')
print(parityString(str(classFrequency[i])),parityString(str(cumulativeFrequency[i])))
print("=================================================================")
print("lower limit of the percentile class l =",lower_limit_percentileclass," Class width h = ",classWidth)
print("frequency of the percentile class f =",f)
print("cumulative frequency of the class preceeding the percentile class cf =",cf)
percentile = lower_limit_percentileclass + ((X - cf)/f)*classWidth
print("Percentile P",p," = ",round(percentile,2))
print(p,"% of data lies below the score of ",round(percentile,2))
print()
def medianDiscreetData():
n = int(input("Enter the number of values: "))
dataList = []
for i in range(n):
print("Enter a number: ")
dataList.append(float(input()))
dataList.sort()
print("Sorted numbers:",dataList)
if n % 2 != 0:
print("Median is the ",int((n+1)/2), "th observation")
print("Median =",dataList[int((n+1)/2)])
else:
print("Median is the average of ",int((n)/2)," and ",int((n/2)+1), " th observation")
print("Median =",(dataList[int(n/2)]+dataList[int((n/2)+1)])/2)
print("****** MEDIAN AND MODE CALCULATION *******")
key = 0
while key != -99:
print(" WELCOME TO THE MAIN MENU ***** STATISTICS CALCULATION FOR CONTINUOUS AND DISCREET DATA *****")
print(" Press 1 to CALCULATE MODE FOR CONTINUOUS DATA SET: ")
print(" Press 2 to CALCULATE MEDIAN FOR CONTINUOUS DATA SET: ")
print(" Press 3 to CALCULATE MEDIAN FOR DISCREET DATA: ")
print(" Press 4 to CALCULATE PERCENTILE RANK FOR CONTINUOUS DATA SET: ")
print(" Press 5 to CALCULATE PERCENTILES FOR CONTINUOUS DATA SET")
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 = float(input("Enter the lower limit of the first classes:"))
upperLimitOfFirstClass = float(input("Enter the upper limit of the first classes:"))
modeContinuousData(numberOFClasses,lowerLimitOfFirstClass,upperLimitOfFirstClass)
if key == 2:
numberOFClasses = int(input("Enter the number of classes:"))
lowerLimitOfFirstClass = float(input("Enter the lower limit of the first classes:"))
upperLimitOfFirstClass = float(input("Enter the upper limit of the first classes:"))
medianContinuousData(numberOFClasses,lowerLimitOfFirstClass,upperLimitOfFirstClass)
if key == 3:
medianDiscreetData()
if key == 4:
numberOFClasses = int(input("Enter the number of classes:"))
lowerLimitOfFirstClass = float(input("Enter the lower limit of the first classes:"))
upperLimitOfFirstClass = float(input("Enter the upper limit of the first classes:"))
score = float(input("Enter the score for which you want to get the percentile rank:"))
percentileRankContinuousData(numberOFClasses,lowerLimitOfFirstClass,upperLimitOfFirstClass,score)
if key == 5:
numberOFClasses = int(input("Enter the number of classes:"))
lowerLimitOfFirstClass = float(input("Enter the lower limit of the first classes:"))
upperLimitOfFirstClass = float(input("Enter the upper limit of the first classes:"))
percentileContinuousData(numberOFClasses,lowerLimitOfFirstClass,upperLimitOfFirstClass)
if key == -99:
print("Exited from main menu:")
Median,Mode, Percentile and Percentile Rank Calculator for CBSE,ICSE,Statistics, Psychology and Education students using Python programming. Run this program on Google Colab.
No comments:
Post a Comment