juanxo90 commited on
Commit
b670886
·
verified ·
1 Parent(s): 083c7e5

Create normal_distribution.py

Browse files
Files changed (1) hide show
  1. tools/normal_distribution.py +80 -0
tools/normal_distribution.py ADDED
@@ -0,0 +1,80 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import random
2
+ import math
3
+ import matplotlib.pyplot as plt
4
+
5
+ def generate_normal_distribution(mean: float, std_dev: float, count: int = 10000)->list:
6
+ """Generate a list of random numbers from a normal distribution.
7
+
8
+ This function generates a list of random numbers drawn from a normal
9
+ distribution specified by the mean and standard deviation.
10
+
11
+ Args:
12
+ mean: The mean (average) of the normal distribution.
13
+ std_dev: The standard deviation of the normal distribution.
14
+ count: The number of random samples to generate (default: 10000).
15
+
16
+ Returns:
17
+ list: A list of samples drawn from the specified normal distribution.
18
+ """
19
+ samples = []
20
+
21
+ for _ in range(count // 2): # Generate pairs of samples
22
+ u1 = random.random()
23
+ u2 = random.random()
24
+
25
+ # Box-Muller transform
26
+ z0 = math.sqrt(-2.0 * math.log(u1)) * math.cos(2.0 * math.pi * u2)
27
+ z1 = math.sqrt(-2.0 * math.log(u1)) * math.sin(2.0 * math.pi * u2)
28
+
29
+ # Scale and shift to the specified mean and standard deviation
30
+ samples.append(z0 * std_dev + mean)
31
+ samples.append(z1 * std_dev + mean)
32
+
33
+ return samples
34
+
35
+ def create_histogram_and_theorical_pdf(mean: float, std_dev:float, random_numbers:list)->None:
36
+ """Generate a histogram of random numbers and overlay the theoretical
37
+ probability density function (PDF) of a normal distribution.
38
+ Return the histogram as a base64-encoded string.
39
+
40
+ Args:
41
+ mean: The mean (average) of the normal distribution.
42
+ std_dev: The standard deviation of the normal distribution.
43
+ random_numbers: A list of random numbers generated from a
44
+ normal distribution.
45
+ """
46
+ # Prepare data for plotting
47
+ hist_data = [0] * 50 # Create a list to hold histogram data
48
+ min_value = min(random_numbers)
49
+ max_value = max(random_numbers)
50
+ bin_width = (max_value - min_value) / len(hist_data)
51
+
52
+ # Fill histogram data
53
+ for number in random_numbers:
54
+ bin_index = int((number - min_value) / bin_width)
55
+ if bin_index >= len(hist_data):
56
+ bin_index = len(hist_data) - 1
57
+ hist_data[bin_index] += 1
58
+
59
+ # Normalize histogram data
60
+ hist_data = [count / len(random_numbers) / bin_width for count in hist_data]
61
+
62
+ # Prepare x values for the theoretical PDF
63
+ x_values = [min_value + i * bin_width for i in range(len(hist_data))]
64
+
65
+ # Calculate the corresponding y values for the theoretical normal distribution
66
+ pdf_values = [
67
+ (1 / (std_dev * math.sqrt(2 * math.pi))) * math.exp(-0.5 * ((x - mean) / std_dev) ** 2) \
68
+ for x in x_values
69
+ ]
70
+
71
+ # Plotting
72
+ plt.figure(figsize=(10, 6))
73
+ plt.bar(x_values, hist_data, width=bin_width, alpha=0.6, color='g', label='Histogram')
74
+ plt.plot(x_values, pdf_values, 'k', linewidth=2, label='Theoretical PDF')
75
+
76
+ plt.title(f'Histogram of {len(random_numbers)} Random Numbers\nMean: {mean}, Std Dev: {std_dev}')
77
+ plt.xlabel('Value')
78
+ plt.ylabel('Density')
79
+ plt.legend()
80
+ plt.grid()