-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLaplacian.py
83 lines (64 loc) · 4.07 KB
/
Laplacian.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# You can use this program to calculate the (Laplacian Distribution) and (display its Graph).
# Laplacian Distribution formula in mathematics is written as follows: f(x) = 0.5 * e^(-|x - μ| / b) / b
# ---> μ = location parameter
# ---> b = scale parameter
# * The scale parameter is calculated as the square root of 0.5 times the median absolute deviation of the data from the median. *
import numpy as np
import matplotlib.pyplot as plt
class LaplacianDistribution:
def __init__(self, data):
self.data = data
def plot(self):
b = np.sqrt(0.5) * np.median(np.abs(self.data - np.median(self.data))) # Calculate the scale parameter of the Laplacian distribution based on the median of the dataset.
x = np.linspace(min(self.data), max(self.data), 100) # Generate 100 evenly spaced values between the minimum and maximum value in the dataset.
y = 0.5 * np.exp(-np.abs(x - np.median(self.data)) / b) / b # Calculate the probability density function of the Laplacian distribution for each value in x.
plt.plot(x, y)
plt.xlabel('Values')
plt.ylabel('Probability Density Function')
plt.title('Laplacian Distribution')
plt.grid(True)
plt.show()
class DataAnalyzer:
def __init__(self):
self.data = []
def read_data(self):
data_input = input("---> Enter a list of numbers (comma-separated): ")
self.data = [float(x.strip()) for x in data_input.split(',')]
def calculate_statistics(self):
mean = np.mean(self.data)
print("---> Mean:", mean)
return mean
# 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 this program to calculate the (Laplacian Distribution) and display its Graph. *
* *
****************************************************************************************************
* *
* Laplacian Distribution formula in mathematics is written as follows: *
* *
* f(x) = 0.5 * e^(-|x - μ| / b) / b *
* *
* ---> μ = location parameter *
* ---> b = scale parameter *
* *
* The scale parameter is calculated as the square root of 0.5 times the *
* median absolute deviation of the data from the median. *
* *
****************************************************************************************************
""")
def main():
banner()
analyzer = DataAnalyzer()
analyzer.read_data()
mean = analyzer.calculate_statistics()
plotter = LaplacianDistribution(analyzer.data)
plotter.plot()
print("*********************************************************************\n")
if __name__ == '__main__':
main()
# An example of how to use the program is shown.