feat: normal distribution

This commit is contained in:
2025-11-24 22:00:26 +01:00
parent 6a86e0ab9d
commit c37dba44a8
2 changed files with 14 additions and 4 deletions

View File

@@ -16,5 +16,5 @@ if __name__=="__main__":
# t_strings() # t_strings()
# test_math_module() # test_math_module()
# test_probability_module() # test_probability_module()
# test_statistics_module() test_statistics_module()
# test_exercises_module() # test_exercises_module()

View File

@@ -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): def mean(list):
return sum(list) / len(list) return sum(list) / len(list)
@@ -51,9 +52,14 @@ def sample_standard_deviation(difference_list):
def standard_deviation(difference_list, is_sample): def standard_deviation(difference_list, is_sample):
return sample_standard_deviation(difference_list) if is_sample else population_standard_deviation(difference_list) 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) # PDF generates the Normal Distribution (symetric arround the mean)
def probability_density_function(x: float, mean: float, standard_deviation: float): def normal_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)) 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(): def test_statistics_module():
@@ -72,3 +78,7 @@ def test_statistics_module():
del sample[1] del sample[1]
print("The sample variance for a population is", sample_variance(sample)) print("The sample variance for a population is", sample_variance(sample))
print("The standard deviation for a population is", standard_deviation(sample, True)) 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)))