Files
gps-denied-onboard/test_h03_robust_kernels.py
Denys Zaitsev d7e1066c60 Initial commit
2026-04-03 23:25:54 +03:00

26 lines
1.0 KiB
Python

import pytest
import numpy as np
from h03_robust_kernels import RobustKernels
@pytest.fixture
def kernels():
return RobustKernels()
class TestRobustKernels:
def test_huber_kernel_scalar(self, kernels):
# Inlier (r <= threshold): 0.5 * r^2
assert kernels.huber_loss(1.0, 2.0) == 0.5 * (1.0 ** 2)
assert kernels.compute_weight(1.0, "huber", {"threshold": 2.0}) == 1.0
# Outlier (r > threshold): threshold * (|r| - 0.5 * threshold)
assert kernels.huber_loss(3.0, 2.0) == 2.0 * (3.0 - 0.5 * 2.0)
assert kernels.compute_weight(3.0, "huber", {"threshold": 2.0}) == 2.0 / 3.0
def test_cauchy_kernel_scalar(self, kernels):
assert kernels.cauchy_loss(0.0, 1.0) == 0.0
assert np.isclose(kernels.cauchy_loss(1.0, 1.0), 0.5 * np.log(2.0))
assert kernels.compute_weight(0.0, "cauchy", {"k": 1.0}) == 1.0
assert kernels.compute_weight(1.0, "cauchy", {"k": 1.0}) == 0.5
assert kernels.compute_weight(3.0, "cauchy", {"k": 1.0}) == 0.1