From c37dba44a8e1e1952cc97de0f9f350375d0831aa Mon Sep 17 00:00:00 2001 From: Daniel Heras Quesada Date: Mon, 24 Nov 2025 22:00:26 +0100 Subject: [PATCH] feat: normal distribution --- src/main.py | 2 +- src/modules/statistics.py | 16 +++++++++++++--- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/main.py b/src/main.py index 30a3445..ce2d640 100644 --- a/src/main.py +++ b/src/main.py @@ -16,5 +16,5 @@ if __name__=="__main__": # t_strings() # test_math_module() # test_probability_module() - # test_statistics_module() + test_statistics_module() # test_exercises_module() diff --git a/src/modules/statistics.py b/src/modules/statistics.py index 43d5b2c..fc6c80a 100644 --- a/src/modules/statistics.py +++ b/src/modules/statistics.py @@ -1,4 +1,5 @@ -from math import sqrt, pi, e +from math import sqrt, pi, e, exp +from scipy.stats import norm def mean(list): return sum(list) / len(list) @@ -51,9 +52,14 @@ def sample_standard_deviation(difference_list): def standard_deviation(difference_list, is_sample): return sample_standard_deviation(difference_list) if is_sample else population_standard_deviation(difference_list) +## Normal distribution # PDF generates the Normal Distribution (symetric arround the mean) -def probability_density_function(x: float, mean: float, standard_deviation: float): - return (1 / (standard_deviation * sqrt(2 * pi))) * (e ** ((-1/2) * (x - (mean ** 2)) / standard_deviation)) +def normal_probability_density_function(x: float, mean: float, standard_deviation: float): + return (1.0 / (2.0 * pi * standard_deviation ** 2) ** 0.5) * exp(-1.0 * ((x - mean) ** 2 / (2.0 * standard_deviation ** 2))) + +def normal_cumulative_density_function(x, mean, difference_list): + std_dev = standard_deviation(difference_list, False) + return norm.cdf(x, mean, std_dev) def test_statistics_module(): @@ -72,3 +78,7 @@ def test_statistics_module(): del sample[1] print("The sample variance for a population is", sample_variance(sample)) print("The standard deviation for a population is", standard_deviation(sample, True)) + + print("== Normal distribution ==") + print(">> The probability_density_function for x = 1 over the example data is {0}".format(normal_probability_density_function(1, sum(differences) / len(differences), standard_deviation(differences, False)))) + print(">> The probability for observing a value smaller than 1 is given by the cumulative density function and it is: {0}".format(normal_cumulative_density_function(1, sum(differences) / len(differences), differences)))