From 3cca610142f58d025f32ff7a382320bf81660cb1 Mon Sep 17 00:00:00 2001 From: Ayush Modi Date: Sat, 24 Oct 2020 10:26:42 +0530 Subject: [PATCH] Added sample data, updated readme --- README.md | 10 +++-- numbers.txt | 11 ++++++ numbers_binomial.txt | 13 +++++++ test.py | 87 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 118 insertions(+), 3 deletions(-) create mode 100644 numbers.txt create mode 100644 numbers_binomial.txt create mode 100644 test.py diff --git a/README.md b/README.md index 5f1dc5e..febd2b8 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,7 @@ # Prob-Dists-GB -Summary of the package +Python package to model probability distributions.
+Currently supports Gaussian and Binomial Distributions # Files @@ -8,8 +9,11 @@ Explanation of files in the package # Installation -Steps to install +``` +pip install Prob-Dists-GB +``` +You can find the project on PyPi [here](https://pypi.org/project/Prob-Dists-GB/) # Licence -MIT Licence +[MIT Licence](LICENCE.txt) diff --git a/numbers.txt b/numbers.txt new file mode 100644 index 0000000..0d78776 --- /dev/null +++ b/numbers.txt @@ -0,0 +1,11 @@ +1 +3 +99 +100 +120 +32 +330 +23 +76 +44 +31 \ No newline at end of file diff --git a/numbers_binomial.txt b/numbers_binomial.txt new file mode 100644 index 0000000..71658d9 --- /dev/null +++ b/numbers_binomial.txt @@ -0,0 +1,13 @@ +0 +1 +1 +1 +1 +1 +0 +1 +0 +1 +0 +1 +0 \ No newline at end of file diff --git a/test.py b/test.py new file mode 100644 index 0000000..096344a --- /dev/null +++ b/test.py @@ -0,0 +1,87 @@ +import unittest + +from distributions import Gaussian +from distributions import Binomial + +class TestGaussianClass(unittest.TestCase): + def setUp(self): + self.gaussian = Gaussian(25, 2) + self.gaussian.read_data_file('numbers.txt') + + def test_initialization(self): + self.assertEqual(self.gaussian.mean, 25, 'incorrect mean') + self.assertEqual(self.gaussian.stdev, 2, 'incorrect standard deviation') + + def test_readdata(self): + self.assertEqual(self.gaussian.data,\ + [1, 3, 99, 100, 120, 32, 330, 23, 76, 44, 31], 'data not read in correctly') + + def test_meancalculation(self): + self.assertEqual(self.gaussian.calculate_mean(),\ + sum(self.gaussian.data) / float(len(self.gaussian.data)), 'calculated mean not as expected') + + def test_stdevcalculation(self): + self.assertEqual(round(self.gaussian.calculate_stdev(), 2), 92.87, 'sample standard deviation incorrect') + self.assertEqual(round(self.gaussian.calculate_stdev(0), 2), 88.55, 'population standard deviation incorrect') + + def test_pdf(self): + self.assertEqual(round(self.gaussian.pdf(25), 5), 0.19947,\ + 'pdf function does not give expected result') + self.gaussian.calculate_mean() + self.gaussian.calculate_stdev() + self.assertEqual(round(self.gaussian.pdf(75), 5), 0.00429,\ + 'pdf function after calculating mean and stdev does not give expected result') + + def test_add(self): + gaussian_one = Gaussian(25, 3) + gaussian_two = Gaussian(30, 4) + gaussian_sum = gaussian_one + gaussian_two + + self.assertEqual(gaussian_sum.mean, 55) + self.assertEqual(gaussian_sum.stdev, 5) + +class TestBinomialClass(unittest.TestCase): + def setUp(self): + self.binomial = Binomial(0.4, 20) + self.binomial.read_data_file('numbers_binomial.txt') + + def test_initialization(self): + self.assertEqual(self.binomial.p, 0.4, 'p value incorrect') + self.assertEqual(self.binomial.n, 20, 'n value incorrect') + + def test_readdata(self): + self.assertEqual(self.binomial.data,\ + [0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0], 'data not read in correctly') + + def test_calculatemean(self): + mean = self.binomial.calculate_mean() + self.assertEqual(mean, 8) + + def test_calculatestdev(self): + stdev = self.binomial.calculate_stdev() + self.assertEqual(round(stdev,2), 2.19) + + def test_replace_stats_with_data(self): + p, n = self.binomial.replace_stats_with_data() + self.assertEqual(round(p,3), .615) + self.assertEqual(n, 13) + + def test_pdf(self): + self.assertEqual(round(self.binomial.pdf(5), 5), 0.07465) + self.assertEqual(round(self.binomial.pdf(3), 5), 0.01235) + + self.binomial.replace_stats_with_data() + self.assertEqual(round(self.binomial.pdf(5), 5), 0.05439) + self.assertEqual(round(self.binomial.pdf(3), 5), 0.00472) + + def test_add(self): + binomial_one = Binomial(.4, 20) + binomial_two = Binomial(.4, 60) + binomial_sum = binomial_one + binomial_two + + self.assertEqual(binomial_sum.p, .4) + self.assertEqual(binomial_sum.n, 80) + + +if __name__ == '__main__': + unittest.main()