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 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 results') 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) def test_confidenceintervals(self): self.assertEqual( self.gaussian.calculate_confidence_intervals(0.95), [23.205529189316458, 132.97628899250174], 'calculate_confidence_intervals does not give' 'expected result') self.assertEqual( self.gaussian.calculate_confidence_intervals(0.99), [6.123854832188144, 150.05796334963003], 'calculate_confidence_intervals does not give' 'expected result')
class TestGaussianClass(unittest.TestCase): def setUp(self): self.gaussian = Gaussian(25, 2) def test_initialization(self): self.assertEqual(self.gaussian.mean, 25, 'incorrect mean') self.assertEqual(self.gaussian.stdev, 2, 'incorrect standard deviation') def test_pdf(self): self.assertEqual(round(self.gaussian.pdf(25), 5), 0.19947, 'pdf function does not give expected result') def test_meancalculation(self): self.gaussian.read_data_file('numbers.txt', True) 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.gaussian.read_data_file('numbers.txt', True) self.assertEqual(round(self.gaussian.stdev, 2), 92.87, 'sample standard deviation incorrect') self.gaussian.read_data_file('numbers.txt', False) self.assertEqual(round(self.gaussian.stdev, 2), 88.55, 'population standard deviation incorrect')
class TestGaussianClass(unittest.TestCase): def setUp(self): self.gaussian = Gaussian('numbers_gaussian.txt') def test_readdata(self): self.assertEqual(self.gaussian.data[0:7], [36, 37, 38, 38, 39, 39, 40], 'data not read in correctly') def test_meancalculation(self): self.assertEqual(round(self.gaussian.calculate_mean(),2), round(sum(self.gaussian.data) / float(len(self.gaussian.data)),2),\ 'calculated mean not as expected') def test_stdevcalculation(self): self.assertEqual(round(self.gaussian.calculate_stdev(), 2), 3.92,\ 'sample standard deviation incorrect') self.assertEqual(round(self.gaussian.calculate_stdev(0), 2), 3.90,\ 'population standard deviation incorrect') def test_pdf(self): self.assertEqual(round(self.gaussian.pdf(40), 5), 0.03803,\ 'propability density function does not give expected result') def test_def(self): self.assertEqual( str(self.gaussian), "mean {}, variance {}".format(45.5, 15.36), 'parameters of the Gaussian\ not as expected')
class TestGaussianClass(unittest.TestCase): def setUp(self): self.gaussian = Gaussian(25, 2) self.gaussian.read_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(), 78.0909090909091, 'calculated mean not as expected') def test_stdevcalculation(self): self.assertEqual(round(self.gaussian.calculate_stdev(True), 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.density(25), 5), 0.19947,\ 'pdf function does not give expected result') self.gaussian.replace_stats_with_data() self.assertEqual(round(self.gaussian.density(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 TestGaussianClass(unittest.TestCase): def setUp(self): self.gaussian = Gaussian(25, 2) self.gaussian.read_data_file('numbers.txt') 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.gaussian.read_data_file('numbers.txt', True) self.assertEqual(round(self.gaussian.stdev, 2), 92.87, 'sample standard deviation incorrect') self.gaussian.read_data_file('numbers.txt', False) self.assertEqual(round(self.gaussian.stdev, 2), 88.55, 'population standard deviation incorrect') def test_pdf(self): self.gaussian = Gaussian(25, 2) self.assertEqual(round(self.gaussian.pdf(25), 5), 0.19947,\ 'pdf function does not give expected result') self.gaussian.read_data_file('numbers.txt') 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)
from distributions import Gaussian # initialize two gaussian distributions gaussian_one = Gaussian(25, 3) gaussian_two = Gaussian(30, 2) # initialize a third gaussian distribution reading in a data efile gaussian_three = Gaussian() gaussian_three.read_data_file('numbers.txt') gaussian_three.calculate_mean() gaussian_three.calculate_stdev() print(gaussian_one.mean) print(gaussian_two.mean) print(gaussian_one.stdev) print(gaussian_two.stdev) print(gaussian_three.mean) print(gaussian_three.stdev) # plot histogram of gaussian three gaussian_three.plot_histogram_pdf() # add gaussian_one and gaussian_two together gaussian_one + gaussian_two