1
$\begingroup$

I am looking for a function in Python testing the hypothesis that the variance of a Gaussian sample is equal to a given value, to validate my own function.

I talk about this test: https://www.itl.nist.gov/div898/handbook/eda/section3/eda358.htm

I could find the $\chi^2$ test for categorical variance, and the Levene and Bartlett tests to compare sample variances, but not this simple test. Anybody aware of such a function in Python?

$\endgroup$

1 Answer 1

1
$\begingroup$
import numpy as np
from scipy.stats import chi2

def var_test(x, va0, direction = "two-tailed", alpha = 0.05):
    n = len(x)
    Q = (n - 1) * np.var(x) / va0 
    if direction == "lower":
        q = chi2.ppf(alpha, n - 1)
            if Q <= q:
            return "H_0 rejected"
        else:
            return "H_0 not rejected"
    elif direction == "upper":
        q = chi2.ppf(1 - alpha, n - 1)
        if Q >= q:
            return "H_0 rejected"
        else:
            return "H_0 not rejected"
    else:
        q1 = chi2.ppf(alpha / 2, n - 1)
        q2 = chi2.ppf(1 - (alpha / 2), n - 1)
        if Q <= q1 or Q >= q2:
            return "H_0 rejected"
        else:
            return "H_0 not rejected"

n = 25    

x = np.random.normal(0, 3, n)

var_test(x, va0 = 1)
$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.