public GammaDistribution(double shape, double scale) {
        if (shape <= 0) {
            throw new IllegalArgumentException("Invalid shape: " + shape);
        }

        if (scale <= 0) {
            throw new IllegalArgumentException("Invalid scale: " + scale);
        }

        theta = scale;
        k = shape;

        logTheta = Math.log(theta);
        thetaGammaK = theta * Gamma.gamma(k);
        logGammaK = Gamma.lgamma(k);
        entropy = k + Math.log(theta) + Gamma.lgamma(k) + (1 - k) * Gamma.digamma(k);
    }
@Test
    public void testGammaDistribution() {
        System.out.println("GammaDistribution");
        MathEx.setSeed(19650218); // to get repeatable results.
        GammaDistribution instance = new GammaDistribution(3, 2.1);
        double[] data = instance.rand(1000);
        GammaDistribution est = GammaDistribution.fit(data);
        assertEquals(2.09, est.theta, 1E-2);
        assertEquals(2.95, est.k, 1E-2);
    }