SPB Git

Repository Public Research preview

test_distributions.py 1.3 KB 32 lines python

1import numpy as np2from arch.univariate import distribution as D34from wp46.models.distributions import expected_shortfall, loglik_z, neg_half_moment, ppf, rvs567def test_loglik_matches_arch():8    z = np.linspace(-5, 5, 41)9    for dist, dp, A in [("normal", [], D.Normal()), ("t", [4.5], D.StudentsT()), ("ged", [1.2], D.GeneralizedError()),10                        ("skewt", [5.0, 0.3], D.SkewStudent())]:11        mine = loglik_z(dist, z, np.array(dp))12        ref = A.loglikelihood(dp, z, np.ones_like(z), individual=True)13        assert np.max(np.abs(mine - ref)) < 1e-12141516def test_simulated_moments_and_es():17    rng = np.random.default_rng(0)18    for dist, dp in [("t", [5.0]), ("ged", [1.3]), ("skewt", [6.0, -0.4])]:19        x = rvs(dist, np.array(dp), 300000, rng)20        assert abs(x.mean()) < 0.02 and abs(x.var() - 1) < 0.0321        es = expected_shortfall(dist, 0.05, np.array(dp))22        emp = np.mean(np.sort(x)[: int(0.05 * len(x))])23        assert abs(es - emp) < 0.0524        q = ppf(dist, 0.05, np.array(dp))[0]25        assert abs(np.mean(x < q) - 0.05) < 0.003262728def test_kappa_symmetric_half():29    assert neg_half_moment("t", np.array([5.0])) == 0.530    k = neg_half_moment("skewt", np.array([6.0, -0.4]))31    assert 0.5 < k < 1.0   # negative skew puts more mass of z^2 below zero32
Files main
Menu

Theme