juanxo90 commited on
Commit
82d4f51
·
verified ·
1 Parent(s): be58b6b

Update tools/normal_distribution.py

Browse files
Files changed (1) hide show
  1. 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)->matplotlib.figure.Figure:
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
- # Create a figure
76
- fig = plt.figure(figsize=(10, 6))
77
- ax = fig.add_subplot(111) # Create an Axes object on the figure
78
-
79
- # Add histogram as a bar plot to the Axes
80
- ax.bar(x_values, hist_data, width=bin_width, alpha=0.6, color='g', label='Histogram')
81
-
82
- # Add theoretical PDF as a line plot to the same Axes
83
- ax.plot(x_values, pdf_values, 'k', linewidth=2, label='Theoretical PDF')
84
-
85
- # Set title and labels
86
- ax.set_title(f'Histogram of {len(random_numbers)} Random Numbers\nMean: {mean:.2f}, Std Dev: {std_dev:.2f}')
87
- ax.set_xlabel('Value')
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