Arithmetic, geometric, harmonic means calculation
#
# Mean_A is weighted arithmetic mean calculation.
# Mean_G is weighted geometric mean calculation.
# Mean_H is weighted harmonic average calculation.
# f[i] - weight data point x[i]
# x[i] - data point i in dataset
# Nr - lenght dataset
#
Nr = int(input('Lenght dataset is:'))
#
# ********** Initialization list ************
#
def Initialization_list(nr, string_mane):
termx = ['0']*nr
print(string_mane)
i = 0
while i < nr:
termx[i] = input('Term in dataset is:')
i = i + 1
return termx
#
# ********** Harmonic wighted mean calculation ************
#
def Harmonic_mean(nr, termx, termf):
suma_xf = 0
suma_f = 0
i = 0
while i < nr:
suma_xf = suma_xf + float(termf [i])/float(termx [i])
suma_f = suma_f + float(termf [i])
i = i + 1
mean_h = suma_f / suma_xf
return mean_h
#
# ********** Geometric wighted mean calculation ************
#
def Geometric_mean(nr, termx, termf):
Prod_xf = 1.
suma_f = 0
i = 0
while i < nr:
Prod_xf = Prod_xf * float(termx [i])**float(termf [i])
suma_f = suma_f + float(termf [i])
i = i + 1
mean_g = (Prod_xf)** (1./suma_f)
return mean_g
#
# ********** Arithmetic wighted mean calculation ************
#
def Arithmetic_mean(nr, termx, termf):
suma_xf = 0
suma_f = 0
i = 0
while i < nr:
suma_xf = suma_xf + float(termx [i]) * float(termf [i])
suma_f = suma_f + float(termf [i])
i = i + 1
mean_a = suma_xf / suma_f
return mean_a
#
# ***************Means calculation ***********
#
x = Initialization_list(Nr,'xxx')
f = Initialization_list(Nr, 'fff')
Mean_A = Arithmetic_mean(Nr, x, f)
print('Arithmetic mean is:','%.2f' % Mean_A)
Mean_G = Geometric_mean(Nr, x, f)
print('Geometric mean is:','%.2f' % Mean_G)
Mean_H = Harmonic_mean(Nr, x, f)
print('Harmonic mean is:','%.2f' % Mean_H)
# Mean_A is weighted arithmetic mean calculation.
# Mean_G is weighted geometric mean calculation.
# Mean_H is weighted harmonic average calculation.
# f[i] - weight data point x[i]
# x[i] - data point i in dataset
# Nr - lenght dataset
#
Nr = int(input('Lenght dataset is:'))
#
# ********** Initialization list ************
#
def Initialization_list(nr, string_mane):
termx = ['0']*nr
print(string_mane)
i = 0
while i < nr:
termx[i] = input('Term in dataset is:')
i = i + 1
return termx
#
# ********** Harmonic wighted mean calculation ************
#
def Harmonic_mean(nr, termx, termf):
suma_xf = 0
suma_f = 0
i = 0
while i < nr:
suma_xf = suma_xf + float(termf [i])/float(termx [i])
suma_f = suma_f + float(termf [i])
i = i + 1
mean_h = suma_f / suma_xf
return mean_h
#
# ********** Geometric wighted mean calculation ************
#
def Geometric_mean(nr, termx, termf):
Prod_xf = 1.
suma_f = 0
i = 0
while i < nr:
Prod_xf = Prod_xf * float(termx [i])**float(termf [i])
suma_f = suma_f + float(termf [i])
i = i + 1
mean_g = (Prod_xf)** (1./suma_f)
return mean_g
#
# ********** Arithmetic wighted mean calculation ************
#
def Arithmetic_mean(nr, termx, termf):
suma_xf = 0
suma_f = 0
i = 0
while i < nr:
suma_xf = suma_xf + float(termx [i]) * float(termf [i])
suma_f = suma_f + float(termf [i])
i = i + 1
mean_a = suma_xf / suma_f
return mean_a
#
# ***************Means calculation ***********
#
x = Initialization_list(Nr,'xxx')
f = Initialization_list(Nr, 'fff')
Mean_A = Arithmetic_mean(Nr, x, f)
print('Arithmetic mean is:','%.2f' % Mean_A)
Mean_G = Geometric_mean(Nr, x, f)
print('Geometric mean is:','%.2f' % Mean_G)
Mean_H = Harmonic_mean(Nr, x, f)
print('Harmonic mean is:','%.2f' % Mean_H)
Results are:
Python 3.10.2 (v3.10.2:a58ebcc701, Jan 13 2022, 14:50:16) [Clang 13.0.0 (clang-1300.0.29.30)] on darwin
Type "help", "copyright", "credits" or "license()" for more information.
============= RESTART: /Users/ionivan/Documents/Meanscalculation.py ============
Lenght dataset is:3
xxx
Term in dataset is:1
Term in dataset is:2
Term in dataset is:3
fff
Term in dataset is:1
Term in dataset is:1
Term in dataset is:1
Arithmetic mean is: 2.00
Geometric mean is: 1.82
Harmonic mean is: 1.64
(March 24, 2022)
Comments
Post a Comment