Python statistics.pvariance() Examples

The following are 17 code examples of statistics.pvariance(). 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: volatility_indicators.py    From jhTAlib with GNU General Public License v3.0 6 votes vote down vote up
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 
Example #2
Source File: test_statistics.py    From mpyc with MIT License 5 votes vote down vote up
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 #3
Source File: test_statistics.py    From android_universal with MIT License 5 votes vote down vote up
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 #4
Source File: test_statistics.py    From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 5 votes vote down vote up
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: stats.py    From Turing with MIT License 5 votes vote down vote up
def variance(lst):
    return statistics.pvariance(lst) 
Example #6
Source File: statistics.py    From mpyc with MIT License 5 votes vote down vote up
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 #7
Source File: statistics.py    From mpyc with MIT License 5 votes vote down vote up
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 #8
Source File: statistics.py    From mpyc with MIT License 5 votes vote down vote up
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) 
Example #9
Source File: test_statistics.py    From mpyc with MIT License 5 votes vote down vote up
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 #10
Source File: QAAnalysis_dataframe.py    From QUANTAXIS with MIT License 5 votes vote down vote up
def pvariance(self):
        return statistics.pvariance(self.price)

    # 方差 
Example #11
Source File: test_statistics.py    From mpyc with MIT License 5 votes vote down vote up
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, []) 
Example #12
Source File: test_statistics.py    From mpyc with MIT License 5 votes vote down vote up
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())) 
Example #13
Source File: statistic_functions.py    From jhTAlib with GNU General Public License v3.0 5 votes vote down vote up
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 
Example #14
Source File: test_statistics.py    From ironpython3 with Apache License 2.0 5 votes vote down vote up
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 Fluid-Designer with GNU General Public License v3.0 5 votes vote down vote up
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 #16
Source File: base_datastruct.py    From QUANTAXIS with MIT License 5 votes vote down vote up
def pvariance(self):
        '返回DataStruct.price的方差 variance'
        res = self.price.groupby(level=1
                                 ).apply(lambda x: statistics.pvariance(x))
        res.name = 'pvariance'
        return res

    # 方差 
Example #17
Source File: __init__.py    From tbot with GNU General Public License v3.0 4 votes vote down vote up
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}
    """
    )