Spaces:
Sleeping
Sleeping
Update tools/normal_distribution.py
Browse files- tools/normal_distribution.py +17 -19
tools/normal_distribution.py
CHANGED
@@ -36,7 +36,7 @@ def generate_normal_distribution(mean: float, std_dev: float, count: int = 10000
|
|
36 |
return samples
|
37 |
|
38 |
@tool
|
39 |
-
def create_histogram_and_theorical_pdf(mean: float, std_dev:float, random_numbers:list)->
|
40 |
"""Generate a histogram of random numbers and overlay the theoretical
|
41 |
probability density function (PDF) of a normal distribution.
|
42 |
Return the histogram as a base64-encoded string.
|
@@ -46,6 +46,9 @@ def create_histogram_and_theorical_pdf(mean: float, std_dev:float, random_number
|
|
46 |
std_dev: The standard deviation of the normal distribution.
|
47 |
random_numbers: A list of random numbers generated from a
|
48 |
normal distribution.
|
|
|
|
|
|
|
49 |
"""
|
50 |
# Prepare data for plotting
|
51 |
hist_data = [0] * 50 # Create a list to hold histogram data
|
@@ -72,21 +75,16 @@ def create_histogram_and_theorical_pdf(mean: float, std_dev:float, random_number
|
|
72 |
for x in x_values
|
73 |
]
|
74 |
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
ax.set_ylabel('Density')
|
89 |
-
ax.legend()
|
90 |
-
ax.grid()
|
91 |
-
|
92 |
-
return fig
|
|
|
36 |
return samples
|
37 |
|
38 |
@tool
|
39 |
+
def create_histogram_and_theorical_pdf(mean: float, std_dev:float, random_numbers:list)->str:
|
40 |
"""Generate a histogram of random numbers and overlay the theoretical
|
41 |
probability density function (PDF) of a normal distribution.
|
42 |
Return the histogram as a base64-encoded string.
|
|
|
46 |
std_dev: The standard deviation of the normal distribution.
|
47 |
random_numbers: A list of random numbers generated from a
|
48 |
normal distribution.
|
49 |
+
|
50 |
+
Returns:
|
51 |
+
str: The graphics for the histogram and probability density function (PDF) on string format
|
52 |
"""
|
53 |
# Prepare data for plotting
|
54 |
hist_data = [0] * 50 # Create a list to hold histogram data
|
|
|
75 |
for x in x_values
|
76 |
]
|
77 |
|
78 |
+
# Scale for ASCII output
|
79 |
+
max_hist = max(hist_data) if hist_data else 1 # Avoid division by zero
|
80 |
+
max_pdf = max(pdf_values) if pdf_values else 1 # Avoid division by zero
|
81 |
+
max_height = 20 # Maximum height of the ASCII histogram
|
82 |
+
|
83 |
+
# Building the ASCII graph as a string
|
84 |
+
ascii_graph = "Histogram (|: Counts, -: PDF)\n"
|
85 |
+
for i in range(len(hist_data)):
|
86 |
+
hist_count = int((hist_data[i] / max_hist) * max_height)
|
87 |
+
pdf_count = int((pdf_values[i] / max_pdf) * max_height)
|
88 |
+
ascii_graph += f"{'|' * hist_count} {'-' * pdf_count} {x_values[i]:.2f}\n"
|
89 |
+
|
90 |
+
return ascii_graph
|
|
|
|
|
|
|
|
|
|