def test_sqrt_beta_analytical(n, m, rng, allclose): """Tests pdf, cdf, and ppf of SqrtBeta distribution.""" pytest.importorskip("scipy") # beta and betainc dt = 0.001 x = np.arange(dt, 1 + dt, dt) dist = SqrtBeta(n, m) pdf = dist.pdf(x) cdf = dist.cdf(x) ppf = dist.ppf(cdf) # The pdf should reflect the samples num_samples = 2500 num_bins = 5 samples = dist.sample(num_samples, rng=rng) act_hist, _ = np.histogram(samples, bins=num_bins) bin_points = np.linspace(0, 1, num_bins + 1) bin_cdf = dist.cdf(bin_points) exp_freq = bin_cdf[1:] - bin_cdf[:-1] assert np.all(np.abs(np.asfarray(act_hist) / num_samples - exp_freq) < 0.1) # The cdf should be the accumulated pdf assert allclose(cdf, np.cumsum(pdf) * dt, atol=0.01) # The ppf should give back x assert allclose(x, ppf, atol=0.01)
def test_sqrt_beta(n, m, rng): num_samples = 1000 num_bins = 5 vectors = rng.randn(num_samples, n + m) vectors /= npext.norm(vectors, axis=1, keepdims=True) expectation, _ = np.histogram(npext.norm(vectors[:, :m], axis=1), bins=num_bins) dist = SqrtBeta(n, m) samples = dist.sample(num_samples, 1, rng=rng) histogram, _ = np.histogram(samples, bins=num_bins) assert np.all(np.abs(np.asfarray(histogram - expectation) / num_samples) < 0.16)