本文整理匯總了Python中statistics.pvariance方法的典型用法代碼示例。如果您正苦於以下問題:Python statistics.pvariance方法的具體用法?Python statistics.pvariance怎麽用?Python statistics.pvariance使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類statistics
的用法示例。
在下文中一共展示了statistics.pvariance方法的14個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: DVOLA
# 需要導入模塊: import statistics [as 別名]
# 或者: from statistics import pvariance [as 別名]
def DVOLA(df, n=30, price='Close'):
"""
Daily Volatility
Returns: list of floats = jhta.DVOLA(df, n=30, price='Close')
Source: https://www.wallstreetmojo.com/volatility-formula/
"""
dvola_list = []
for i in range(len(df[price])):
if i + 1 < n:
dvola = float('NaN')
else:
start = i + 1 - n
end = i + 1
pvariance = statistics.pvariance(df[price][start:end])
dvola = math.sqrt(pvariance)
dvola_list.append(dvola)
return dvola_list
示例2: pvariance
# 需要導入模塊: import statistics [as 別名]
# 或者: from statistics import pvariance [as 別名]
def pvariance(self):
return statistics.pvariance(self.price)
# 方差
示例3: pvariance
# 需要導入模塊: import statistics [as 別名]
# 或者: from statistics import pvariance [as 別名]
def pvariance(self):
'返回DataStruct.price的方差 variance'
res = self.price.groupby(level=1
).apply(lambda x: statistics.pvariance(x))
res.name = 'pvariance'
return res
# 方差
示例4: test_compare_to_variance
# 需要導入模塊: import statistics [as 別名]
# 或者: from statistics import pvariance [as 別名]
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)
示例5: PVARIANCE
# 需要導入模塊: import statistics [as 別名]
# 或者: from statistics import pvariance [as 別名]
def PVARIANCE(df, n, price='Close', mu=None):
"""
Population variance of data
Returns: list of floats = jhta.PVARIANCE(df, n, price='Close', mu=None)
"""
pvariance_list = []
if n == len(df[price]):
start = None
for i in range(len(df[price])):
if df[price][i] != df[price][i]:
pvariance = float('NaN')
else:
if start is None:
start = i
end = i + 1
pvariance = statistics.pvariance(df[price][start:end], mu)
pvariance_list.append(pvariance)
else:
for i in range(len(df[price])):
if i + 1 < n:
pvariance = float('NaN')
else:
start = i + 1 - n
end = i + 1
pvariance = statistics.pvariance(df[price][start:end], mu)
pvariance_list.append(pvariance)
return pvariance_list
示例6: test_plain
# 需要導入模塊: import statistics [as 別名]
# 或者: from statistics import pvariance [as 別名]
def test_plain(self):
f = lambda: (i * j for i in range(-1, 2, 1) for j in range(2, -2, -1))
self.assertEqual(mean(f()), statistics.mean(f()))
self.assertEqual(variance(f()), statistics.variance(f()))
self.assertEqual(stdev(f()), statistics.stdev(f()))
self.assertEqual(pvariance(f()), statistics.pvariance(f()))
self.assertEqual(pstdev(f()), statistics.pstdev(f()))
self.assertEqual(mode(f()), statistics.mode(f()))
self.assertEqual(median(f()), statistics.median(f()))
示例7: test_statistics_error
# 需要導入模塊: import statistics [as 別名]
# 或者: from statistics import pvariance [as 別名]
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, [])
示例8: test_secint
# 需要導入模塊: import statistics [as 別名]
# 或者: from statistics import pvariance [as 別名]
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)))
示例9: test_secfxp
# 需要導入模塊: import statistics [as 別名]
# 或者: from statistics import pvariance [as 別名]
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
示例10: variance
# 需要導入模塊: import statistics [as 別名]
# 或者: from statistics import pvariance [as 別名]
def variance(data, xbar=None):
"""Return the sample variance of data, an iterable of at least two numbers.
If the optional second argument xbar is given, it should be the mean of data.
If it is missing or None (the default), the mean is automatically calculated.
Use this function when your data is a sample from a population. To calculate
the variance from the entire population, see pvariance().
Raises StatisticsError if data has fewer than two values.
"""
return _var(data, xbar, 1)
示例11: pstdev
# 需要導入模塊: import statistics [as 別名]
# 或者: from statistics import pvariance [as 別名]
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)
示例12: _var
# 需要導入模塊: import statistics [as 別名]
# 或者: from statistics import pvariance [as 別名]
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)
示例13: variance
# 需要導入模塊: import statistics [as 別名]
# 或者: from statistics import pvariance [as 別名]
def variance(lst):
return statistics.pvariance(lst)
示例14: time_testcase_statistics
# 需要導入模塊: import statistics [as 別名]
# 或者: from statistics import pvariance [as 別名]
def time_testcase_statistics(
testcase: typing.Callable,
*args: typing.Any,
runs: int = 10,
sleep: float = 0,
**kwargs: typing.Any,
) -> None:
"""
Take multiple measurements about the run-time of a testcase and return/display statistics.
:param testcase: Testcase to call.
:param args,\\ kwargs: Arguments to pass to the testcase.
:param int runs: How many samples to take.
:param float sleep: How much time to sleep in between the runs. Example
use: Maybe the board does not discharge quick enough so it can cause
troubles when the subsecuent testcase run tries to boot again the board
"""
elapsed_times = []
for n in range(runs):
elapsed_time, _ = time_testcase(testcase, *args, **kwargs)
elapsed_times.append(elapsed_time)
time.sleep(sleep)
results = TimingResults(
statistics.mean(elapsed_times),
statistics.harmonic_mean(elapsed_times),
statistics.median(elapsed_times),
statistics.pvariance(elapsed_times),
statistics.pstdev(elapsed_times),
)
tbot.log.message(
f"""\
Timing Results:
{tbot.log.c('mean').green}: {results.mean}
{tbot.log.c('harmonic mean').green}: {results.harmonic_mean}
{tbot.log.c('median').green}: {results.median}
{tbot.log.c('variance').green}: {results.pvariance}
{tbot.log.c('standard deviation').green}: {results.pstdev}
"""
)