-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmoment (math_library).py
65 lines (52 loc) · 3.14 KB
/
moment (math_library).py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# You can use the program to calculate the (n_th Order Moment).
# n_th Order Moment formula in mathematics is written as follows: (1/N) * Σ((xi - mean)^n)
# ---> N is the number of data points in the dataset
# ---> xi is the ith data point
# ---> mean is the mean of the dataset
# ---> n is the order of the moment
import math
class Moment:
def __init__(self, data):
self.data = data
# Calculations
def calculate_nth_order_moment(self, n):
mean = sum(self.data) / len(self.data) # Calculate the mean
moment = sum((x - mean) ** n for x in self.data) / len(self.data) # Calculate the moment
return moment
def print_moment(self, n):
moment = self.calculate_nth_order_moment(n)
print(" --->", n, "th order moment:", moment)
# This part of the code is written as an example to show the output of the code.
# According to your needs, you can change or delete this part.
def banner():
print("""
################################################################################
# *** Welcome *** #
# #
# You can use the program to calculate the (n_th Order Moment). #
# _____________________________________________ #
# | | #
# | he formula for variance is as follows: | #
# | (1/N) * Σ((xi - mean)^n) | #
# |_____________________________________________| #
# #
# ---> N = is the number of data points in the dataset #
# ---> Σ = sum of #
# ---> xi = is the ith data point #
# ---> mean = is the mean of the dataset #
# ---> n = is the order of the moment #
# #
################################################################################
""")
def main():
banner()
data = input(" ---> Enter a list of data (separated by spaces): ").split()
data = [float(x) for x in data]
n = int(input(" ---> Enter the order of the moment: "))
moment_calculator = Moment(data)
print("\n**********************************************************************************")
moment_calculator.print_moment(n)
print("**********************************************************************************\n")
if __name__ == "__main__":
main()
# An example of how to use the program is shown.