| import numpy as np | |
| from ylff.services.metrology.uncertainty_propagation import monte_carlo_propagate | |
| def test_monte_carlo_propagate_linear_function_matches_analytic(): | |
| # d = x0 + 2*x1, with independent Gaussian noise | |
| # Var(d) = Var(x0) + 4*Var(x1) | |
| mean = np.array([1.0, 2.0]) | |
| sigma = np.array([0.1, 0.2]) | |
| def f(x): | |
| return float(x[0] + 2.0 * x[1]) | |
| res = monte_carlo_propagate(f, mean, sigma, num_samples=2000, rng=np.random.default_rng(0)) | |
| assert abs(res.mean - (1.0 + 2.0 * 2.0)) < 0.02 | |
| expected_sigma = np.sqrt(0.1**2 + 4.0 * 0.2**2) | |
| assert abs(res.sigma - expected_sigma) < 0.02 | |