2024-heraklion-testing-debu.../hands_on_solutions/numerical_fuzzing/test_var.py
2024-08-26 13:54:13 +02:00

23 lines
559 B
Python

from math import isclose
import numpy
def test_var_deterministic():
x = numpy.array([-2.0, 2.0])
expected = 4.0
assert isclose(numpy.var(x), expected)
def test_var_fuzzing():
rand_state = numpy.random.RandomState(8393)
N, D = 100000, 5
# Goal variances: [0.1 , 0.45, 0.8 , 1.15, 1.5]
expected = numpy.linspace(0.1, 1.5, D)
# Generate random, D-dimensional data
x = rand_state.randn(N, D) * numpy.sqrt(expected)
variance = numpy.var(x, axis=0)
numpy.testing.assert_allclose(variance, expected, rtol=1e-2)