Python statistics.variance() Examples
The following are 30
code examples of statistics.variance().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
statistics
, or try the search function
.
Example #1
Source File: legibilidad.py From legibilidad with GNU General Public License v3.0 | 6 votes |
def mu(text): ''' Muñoz Baquedano and Muñoz Urra's readability score (2006) ''' n = count_words(text) # Delete all digits text = ''.join(filter(lambda x: not x.isdigit(), text)) # Cleans it all clean = re.compile('\W+') text = clean.sub(' ', text).strip() text = text.split() # word list word_lengths = [] for word in text: word_lengths.append(len(word)) # The mean calculation needs at least 1 value on the list, and the variance, two. If somebody enters only one word or, what is worse, a figure, the calculation breaks, so this is a 'fix' try: mean = statistics.mean(word_lengths) variance = statistics.variance(word_lengths) mu = (n / (n - 1)) * (mean / variance) * 100 return round(mu, 2) except: return 0
Example #2
Source File: PublicAPIService.py From QRL with MIT License | 6 votes |
def GetStats(self, request: qrl_pb2.GetStatsReq, context) -> qrl_pb2.GetStatsResp: response = qrl_pb2.GetStatsResp() response.node_info.CopyFrom(self.qrlnode.get_node_info()) response.epoch = self.qrlnode.epoch response.uptime_network = self.qrlnode.uptime_network response.block_last_reward = self.qrlnode.block_last_reward response.coins_total_supply = int(self.qrlnode.coin_supply_max) response.coins_emitted = int(self.qrlnode.coin_supply) response.block_time_mean = 0 response.block_time_sd = 0 if request.include_timeseries: tmp = list(self.qrlnode.get_block_timeseries(config.dev.block_timeseries_size)) response.block_timeseries.extend(tmp) if len(tmp) > 2: vals = [v.time_last for v in tmp[1:]] response.block_time_mean = int(mean(vals)) response.block_time_sd = int(variance(vals) ** 0.5) return response
Example #3
Source File: test_statistics.py From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 | 5 votes |
def test_iter_list_same(self): # Test that iter data and list data give the same result. # This is an explicit test that iterators and lists are treated the # same; justification for this test over and above the similar test # in UnivariateCommonMixin is that an earlier design had variance and # friends swap between one- and two-pass algorithms, which would # sometimes give different results. data = [random.uniform(-3, 8) for _ in range(1000)] expected = self.func(data) self.assertEqual(self.func(iter(data)), expected)
Example #4
Source File: test_statistics.py From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 | 5 votes |
def test_compare_to_variance(self): # Test that stdev is, in fact, the square root of variance. data = [random.uniform(-17, 24) for _ in range(1000)] expected = math.sqrt(statistics.pvariance(data)) self.assertEqual(self.func(data), expected)
Example #5
Source File: test_statistics.py From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 | 5 votes |
def test_decimals(self): # Test sample variance with Decimal data. D = Decimal data = [D(2), D(2), D(7), D(9)] exact = 4*D('9.5')/D(3) result = self.func(data) self.assertEqual(result, exact) self.assertIsInstance(result, Decimal)
Example #6
Source File: test_statistics.py From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 | 5 votes |
def test_fractions(self): # Test sample variance with Fraction data. F = Fraction data = [F(1, 4), F(1, 4), F(3, 4), F(7, 4)] exact = F(1, 2) result = self.func(data) self.assertEqual(result, exact) self.assertIsInstance(result, Fraction)
Example #7
Source File: test_statistics.py From android_universal with MIT License | 5 votes |
def test_iter_list_same(self): # Test that iter data and list data give the same result. # This is an explicit test that iterators and lists are treated the # same; justification for this test over and above the similar test # in UnivariateCommonMixin is that an earlier design had variance and # friends swap between one- and two-pass algorithms, which would # sometimes give different results. data = [random.uniform(-3, 8) for _ in range(1000)] expected = self.func(data) self.assertEqual(self.func(iter(data)), expected)
Example #8
Source File: test_statistics.py From android_universal with MIT License | 5 votes |
def test_exact_uniform(self): # Test the variance against an exact result for uniform data. data = list(range(10000)) random.shuffle(data) expected = (10000**2 - 1)/12 # Exact value. self.assertEqual(self.func(data), expected)
Example #9
Source File: test_statistics.py From android_universal with MIT License | 5 votes |
def test_ints(self): # Test population variance with int data. data = [4, 7, 13, 16] exact = 22.5 self.assertEqual(self.func(data), exact)
Example #10
Source File: test_statistics.py From android_universal with MIT License | 5 votes |
def test_decimals(self): # Test population variance with Decimal data. D = Decimal data = [D("12.1"), D("12.2"), D("12.5"), D("12.9")] exact = D('0.096875') result = self.func(data) self.assertEqual(result, exact) self.assertIsInstance(result, Decimal)
Example #11
Source File: test_statistics.py From android_universal with MIT License | 5 votes |
def test_ints(self): # Test sample variance with int data. data = [4, 7, 13, 16] exact = 30 self.assertEqual(self.func(data), exact)
Example #12
Source File: test_statistics.py From android_universal with MIT License | 5 votes |
def test_fractions(self): # Test sample variance with Fraction data. F = Fraction data = [F(1, 4), F(1, 4), F(3, 4), F(7, 4)] exact = F(1, 2) result = self.func(data) self.assertEqual(result, exact) self.assertIsInstance(result, Fraction)
Example #13
Source File: test_statistics.py From android_universal with MIT License | 5 votes |
def test_decimals(self): # Test sample variance with Decimal data. D = Decimal data = [D(2), D(2), D(7), D(9)] exact = 4*D('9.5')/D(3) result = self.func(data) self.assertEqual(result, exact) self.assertIsInstance(result, Decimal)
Example #14
Source File: test_statistics.py From android_universal with MIT License | 5 votes |
def test_compare_to_variance(self): # Test that stdev is, in fact, the square root of variance. data = [random.uniform(-17, 24) for _ in range(1000)] expected = math.sqrt(statistics.pvariance(data)) self.assertEqual(self.func(data), expected)
Example #15
Source File: test_statistics.py From android_universal with MIT License | 5 votes |
def test_shift_data(self): # Test that shifting the data by a constant amount does not affect # the variance or stdev. Or at least not much. # Due to rounding, this test should be considered an ideal. We allow # some tolerance away from "no change at all" by setting tol and/or rel # attributes. Subclasses may set tighter or looser error tolerances. raw = [1.03, 1.27, 1.94, 2.04, 2.58, 3.14, 4.75, 4.98, 5.42, 6.78] expected = self.func(raw) # Don't set shift too high, the bigger it is, the more rounding error. shift = 1e5 data = [x + shift for x in raw] self.assertApproxEqual(self.func(data), expected)
Example #16
Source File: test_statistics.py From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 | 5 votes |
def test_ints(self): # Test population variance with int data. data = [4, 7, 13, 16] exact = 22.5 self.assertEqual(self.func(data), exact)
Example #17
Source File: test_statistics.py From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 | 5 votes |
def test_exact_uniform(self): # Test the variance against an exact result for uniform data. data = list(range(10000)) random.shuffle(data) expected = (10000**2 - 1)/12 # Exact value. self.assertEqual(self.func(data), expected)
Example #18
Source File: test_statistics.py From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 | 5 votes |
def test_decimals(self): # Test population variance with Decimal data. D = Decimal data = [D("12.1"), D("12.2"), D("12.5"), D("12.9")] exact = D('0.096875') result = self.func(data) self.assertEqual(result, exact) self.assertIsInstance(result, Decimal)
Example #19
Source File: test_statistics.py From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 | 5 votes |
def test_shift_data(self): # Test that shifting the data by a constant amount does not affect # the variance or stdev. Or at least not much. # Due to rounding, this test should be considered an ideal. We allow # some tolerance away from "no change at all" by setting tol and/or rel # attributes. Subclasses may set tighter or looser error tolerances. raw = [1.03, 1.27, 1.94, 2.04, 2.58, 3.14, 4.75, 4.98, 5.42, 6.78] expected = self.func(raw) # Don't set shift too high, the bigger it is, the more rounding error. shift = 1e5 data = [x + shift for x in raw] self.assertApproxEqual(self.func(data), expected)
Example #20
Source File: test_statistics.py From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 | 5 votes |
def test_domain_error_regression(self): # Regression test for a domain error exception. # (Thanks to Geremy Condra.) data = [0.123456789012345]*10000 # All the items are identical, so variance should be exactly zero. # We allow some small round-off error, but not much. result = self.func(data) self.assertApproxEqual(result, 0.0, tol=5e-17) self.assertGreaterEqual(result, 0) # A negative result must fail.
Example #21
Source File: stats.py From Turing with MIT License | 5 votes |
def variance_sample(lst): return statistics.variance(lst)
Example #22
Source File: stats.py From Turing with MIT License | 5 votes |
def variance(lst): return statistics.pvariance(lst)
Example #23
Source File: statistics.py From mpyc with MIT License | 5 votes |
def _var(data, m, correction): if iter(data) is data: x = list(data) else: x = data n = len(x) if n < 1 + correction: if correction: e = 'variance requires at least two data points' else: e = 'pvariance requires at least one data point' raise statistics.StatisticsError(e) stype = type(x[0]) # all elts of x assumed of same type if issubclass(stype, sectypes.SecureFiniteField): raise TypeError('secure fixed-point or integer type required') if issubclass(stype, sectypes.SecureInteger): if m is None: s = runtime.sum(x) y = [a * n - s for a in x] # TODO: runtime.scalar_mul(n,x) for public (int) n d = n**2 * (n - correction) else: y = runtime.vector_sub(x, [m] * n) # TODO: runtime.vector_sub(x,y) for scalar y d = n - correction return (runtime.in_prod(y, y) + d//2) // d if issubclass(stype, sectypes.SecureFixedPoint): if m is None: m = mean(x) y = runtime.vector_sub(x, [m] * n) d = n - correction return runtime.in_prod(y, y) / d if correction: return statistics.variance(x, m) return statistics.pvariance(x, m)
Example #24
Source File: statistics.py From mpyc with MIT License | 5 votes |
def pstdev(data, mu=None): """Return the population standard deviation (square root of the population variance). See pvariance() for arguments and other details. """ return _std(data, mu, 0)
Example #25
Source File: statistics.py From mpyc with MIT License | 5 votes |
def pvariance(data, mu=None): """Return the population variance of data, an iterable of at least two numbers. If the optional second argument mu is given, it is typically the mean of the data. It can also be used to compute the second moment around a point that is not the mean. If it is missing or None (the default), the arithmetic mean is automatically calculated. Use this function to calculate the variance from the entire population. To estimate the variance from a sample, the variance() function is usually a better choice. Raises StatisticsError if data is empty. """ return _var(data, mu, 0)
Example #26
Source File: statistics.py From mpyc with MIT License | 5 votes |
def stdev(data, xbar=None): """Return the sample standard deviation (square root of the sample variance). See variance() for arguments and other details. """ return _std(data, xbar, 1)
Example #27
Source File: test_statistics.py From mpyc with MIT License | 5 votes |
def test_secfxp(self): secfxp = mpc.SecFxp() x = [1, 1, 2, 2, 3, 4, 4, 4, 6] * 5 random.shuffle(x) x = list(map(secfxp, x)) self.assertAlmostEqual(mpc.run(mpc.output(mean(x))).signed(), 3, delta=1) self.assertAlmostEqual(mpc.run(mpc.output(median(x))).signed(), 3) self.assertAlmostEqual(mpc.run(mpc.output(mode(x))).signed(), 4) x = [1, 1, 1, 1, 2, 2, 3, 4, 4, 4, 4, 5, 6, 6, 6] * 100 random.shuffle(x) x = list(map(lambda a: a * 2**-4, x)) x = list(map(secfxp, x)) self.assertAlmostEqual(mpc.run(mpc.output(mean(x))).signed(), (2**-4) * 10/3, delta=1) y = [1.75, 1.25, -0.25, 0.5, 1.25, -3.5] * 5 random.shuffle(y) x = list(map(secfxp, y)) self.assertAlmostEqual(float(mpc.run(mpc.output(mean(x)))), statistics.mean(y), 4) self.assertAlmostEqual(float(mpc.run(mpc.output(variance(x)))), statistics.variance(y), 2) self.assertAlmostEqual(float(mpc.run(mpc.output(stdev(x)))), statistics.stdev(y), 3) self.assertAlmostEqual(float(mpc.run(mpc.output(pvariance(x)))), statistics.pvariance(y), 2) self.assertAlmostEqual(float(mpc.run(mpc.output(pstdev(x)))), statistics.pstdev(y), 3) self.assertAlmostEqual(float(mpc.run(mpc.output(median(x)))), statistics.median(y), 4) x = list(map(secfxp, [1.0]*10)) self.assertAlmostEqual(mpc.run(mpc.output(mode(x))).signed(), 1) k = mpc.options.sec_param mpc.options.sec_param = 1 # force no privacy case self.assertAlmostEqual(mpc.run(mpc.output(mode(x))).signed(), 1) mpc.options.sec_param = k
Example #28
Source File: test_statistics.py From mpyc with MIT License | 5 votes |
def test_secint(self): secint = mpc.SecInt() y = [1, 3, -2, 3, 1, -2, -2, 4] * 5 random.shuffle(y) x = list(map(secint, y)) self.assertEqual(mpc.run(mpc.output(mean(x))), round(statistics.mean(y))) self.assertEqual(mpc.run(mpc.output(variance(x))), round(statistics.variance(y))) self.assertEqual(mpc.run(mpc.output(variance(x, mean(x)))), round(statistics.variance(y))) self.assertEqual(mpc.run(mpc.output(stdev(x))), round(statistics.stdev(y))) self.assertEqual(mpc.run(mpc.output(pvariance(x))), round(statistics.pvariance(y))) self.assertEqual(mpc.run(mpc.output(pstdev(x))), round(statistics.pstdev(y))) self.assertEqual(mpc.run(mpc.output(mode(x))), round(statistics.mode(y))) self.assertEqual(mpc.run(mpc.output(median(x))), round(statistics.median(y))) self.assertEqual(mpc.run(mpc.output(median_low(x))), round(statistics.median_low(y))) self.assertEqual(mpc.run(mpc.output(median_high(x))), round(statistics.median_high(y)))
Example #29
Source File: test_statistics.py From mpyc with MIT License | 5 votes |
def test_secfld(self): secfld = mpc.SecFld() x = [secfld(0), secfld(0)] self.assertRaises(TypeError, mean, x) self.assertRaises(TypeError, variance, x) self.assertRaises(TypeError, stdev, x) self.assertRaises(TypeError, mode, x) self.assertRaises(TypeError, median, x)
Example #30
Source File: test_statistics.py From mpyc with MIT License | 5 votes |
def test_statistics_error(self): self.assertRaises(statistics.StatisticsError, mean, []) self.assertRaises(statistics.StatisticsError, variance, [0]) self.assertRaises(statistics.StatisticsError, stdev, [0]) self.assertRaises(statistics.StatisticsError, pvariance, []) self.assertRaises(statistics.StatisticsError, pstdev, []) self.assertRaises(statistics.StatisticsError, mode, []) self.assertRaises(statistics.StatisticsError, median, [])