public BernoulliDistribution(double p) {
        if (p < 0 || p > 1) {
            throw new IllegalArgumentException("Invalid p: " + p);
        }

        this.p = p;
        q = 1 - p;

        entropy = -p * MathEx.log2(p) - q * MathEx.log2(q);
    }
@Test
    public void testBernoulliDistribution() {
        System.out.println("BernoulliDistribution");
        MathEx.setSeed(19650218); // to get repeatable results.
        BernoulliDistribution instance = new BernoulliDistribution(0.4);
        int[] data = instance.randi(1000);
        BernoulliDistribution est = BernoulliDistribution.fit(data);
        assertEquals(0.4, est.p, 1E-2);
    }