You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by GitBox <gi...@apache.org> on 2021/02/19 13:19:24 UTC

[GitHub] [commons-statistics] aherbert commented on a change in pull request #26: Add truncated normal distribution.

aherbert commented on a change in pull request #26:
URL: https://github.com/apache/commons-statistics/pull/26#discussion_r579159518



##########
File path: commons-statistics-distribution/src/test/java/org/apache/commons/statistics/distribution/TruncatedNormalDistributionTest.java
##########
@@ -0,0 +1,283 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.statistics.distribution;
+
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+/**
+ * Test class for {@link TruncatedNormalDistribution}.
+ * All test values were computed using Python with SciPy v1.6.0.
+ */
+class TruncatedNormalDistributionTest extends ContinuousDistributionAbstractTest {
+    /**
+     * Default tolerance.
+     */
+    private static final double TOLERANCE = 1e-7;
+
+    /**
+     * Distribution to test with.
+     */
+    private TruncatedNormalDistribution distribution = new TruncatedNormalDistribution(1.9, 1.3, -1.1, 3.4);
+    /**
+     * Percentiles to test with.
+     */
+    private double[] ppfValues = new double[]{0, 0.0001, 0.001, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 0.75, 0.9, 0.95,
+                                              0.975, 0.99, 0.999, 0.9999, 1};
+    /**
+     * Expected cumulative values for percentiles.
+     */
+    private double[] cdfValues = new double[]{-1.1, -1.09597275767544, -1.0609616183922, -0.79283350106842,
+                                              -0.505331829887808, -0.192170173599874, 0.21173317261645,
+                                              0.925791281910463, 1.71399518338879, 2.43413009451536, 2.94473113856785,
+                                              3.15310057075828, 3.27036798398733, 3.34641874981679, 3.39452729074341,
+                                              3.39945153287941, 3.4};
+    /**
+     * Expected density values for percentiles.
+     */
+    private double[] pdfValues = new double[]{0.0247422752302618, 0.0249196707321102, 0.0265057408263321,
+                                              0.0415071096500185, 0.0640403254340905, 0.0971457789636,
+                                              0.152622492901864, 0.267853863255995, 0.35107475879338, 0.325977522502844,
+                                              0.25680502248913, 0.222886115806507, 0.203494915087054, 0.190997946666992,
+                                              0.183167918885238, 0.182370706542209, 0.182281965373914};
+    /**
+     * Expected distribution mean.
+     */
+    private double mean = 1.63375792365723;
+    /**
+     * Expected distribution variance.
+     */
+    private double variance = 1.03158703914439;
+
+    /**
+     * Overrides tolerance and sets up distribution.
+     */
+    @BeforeEach
+    void setUp() {
+        setTolerance(TOLERANCE);
+        super.setUp();
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public ContinuousDistribution makeDistribution() {
+        return distribution;
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public double[] makeCumulativeTestPoints() {
+        return cdfValues;
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public double[] makeCumulativeTestValues() {
+        return ppfValues;
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public double[] makeDensityTestValues() {
+        return pdfValues;
+    }
+
+    /**
+     * Configures new test values and runs relevant tests in this class and {@link ContinuousDistributionAbstractTest}.
+     * @param testDistribution distribution to test with.
+     * @param expectedPpfValues expected percentiles to test with.
+     * @param expectedCdfValues expected cumulative values for percentiles.
+     * @param expectedPdfValues expected density values for percentiles.
+     * @param expectedMean expected mean.
+     * @param expectedVariance expected variance.
+     */
+    private void testAdditionalDistribution(
+            TruncatedNormalDistribution testDistribution,
+            double[] expectedPpfValues,
+            double[] expectedCdfValues,
+            double[] expectedPdfValues,
+            double expectedMean,
+            double expectedVariance
+    ) {
+        this.distribution = testDistribution;
+        this.ppfValues = expectedPpfValues;
+        this.cdfValues = expectedCdfValues;
+        this.pdfValues = expectedPdfValues;
+        this.mean = expectedMean;
+        this.variance = expectedVariance;
+
+        setUp();
+
+        testMoments();
+
+        testConsistency();
+        testSampler();
+        testOutsideSupport();
+        testDensities();
+        testLogDensities();
+        testOutsideSupport();
+        testInverseCumulativeProbabilities();
+        testDensityIntegrals();
+        testCumulativeProbabilities();
+        testIsSupportConnected();
+        testPrecondition1();
+        testPrecondition2();
+        testPrecondition3();
+    }
+
+    /**
+     * Test a one-sided truncation with a lower tail.
+     */
+    @Test
+    void testOneSidedLowerTail() {
+        testAdditionalDistribution(
+                new TruncatedNormalDistribution(12, 2.4, -Double.POSITIVE_INFINITY, 7.1),

Review comment:
       Use `Double.NEGATIVE_INFINITY` in place of `-Double.POSITIVE_INFINITY` here and through the rest of the tests.
   

##########
File path: commons-statistics-distribution/src/main/java/org/apache/commons/statistics/distribution/TruncatedNormalDistribution.java
##########
@@ -0,0 +1,206 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.statistics.distribution;
+
+/**
+ * Implementation of the truncated normal distribution.
+ *
+ * @see <a href="http://en.wikipedia.org/wiki/Truncated_normal_distribution">
+ * Truncated normal distribution (Wikipedia)</a>.

Review comment:
       Remove the period after the `</a>`

##########
File path: commons-statistics-distribution/src/main/java/org/apache/commons/statistics/distribution/TruncatedNormalDistribution.java
##########
@@ -0,0 +1,206 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.statistics.distribution;
+
+/**
+ * Implementation of the truncated normal distribution.
+ *
+ * @see <a href="http://en.wikipedia.org/wiki/Truncated_normal_distribution">
+ * Truncated normal distribution (Wikipedia)</a>.
+ */
+public class TruncatedNormalDistribution extends AbstractContinuousDistribution {
+    /**
+     * Mean of parent normal distribution.
+     */
+    private final double parentMean;
+    /**
+     * Standard deviation of parent normal distribution.
+     */
+    private final double parentSd;
+    /**
+     * Mean of this distribution.
+     */
+    private final double mean;
+    /**
+     * Variance of this distribution.
+     */
+    private final double variance;
+    /**
+     * Lower bound of this distribution.
+     */
+    private final double lower;
+    /**
+     * Upper bound of this distribution.
+     */
+    private final double upper;
+
+    /**
+     * A standard normal distribution used for calculations.
+     */
+    private final NormalDistribution standardNormal;
+    /**
+     * Stored value of @{code standardNormal.cumulativeProbability((lower - mean) / sd)} for faster computations.
+     */
+    private final double cdfAlpha;
+    /**
+     * Stored value of @{code (upper - mean) / sd) - cdfAlpha} for faster computations.
+     */
+    private final double cdfDelta;
+
+    /**
+     * Creates a truncated normal distribution.
+     * Note that the {@code mean} and {@code sd} is of the parent normal distribution,
+     * and not the true mean and standard deviation of the truncated normal distribution.
+     *
+     * @param mean mean for this distribution.
+     * @param sd standard deviation for this distribution.
+     * @param lower lower bound (inclusive) of the distribution, can be {@link Double#NEGATIVE_INFINITY}.
+     * @param upper upper bound (inclusive) of the distribution, can be {@link Double#POSITIVE_INFINITY}.
+     * @throws IllegalArgumentException if {@code sd <= 0} or if {@code upper <= lower}.
+     */
+    public TruncatedNormalDistribution(double mean, double sd, double lower, double upper) {
+        if (sd <= 0) {
+            throw new DistributionException(DistributionException.NEGATIVE, sd);
+        }
+        if (upper <= lower) {
+            throw new DistributionException(DistributionException.TOO_LARGE, lower, upper);
+        }
+
+        this.lower = lower;
+        this.upper = upper;
+
+        parentMean = mean;
+        parentSd = sd;
+        standardNormal = new NormalDistribution(0, 1);
+
+        double alpha = (lower - mean) / sd;
+        double beta = (upper - mean) / sd;
+
+        cdfAlpha = standardNormal.cumulativeProbability(alpha);
+        double cdfBeta = standardNormal.cumulativeProbability(beta);
+        cdfDelta = cdfBeta - cdfAlpha;
+
+        // Calculation of variance and mean.
+        double pdfAlpha = standardNormal.density(alpha);
+        double pdfBeta = standardNormal.density(beta);
+        double pdfCdfDelta = (pdfAlpha - pdfBeta) / cdfDelta;
+        double alphaBetaDelta = (alpha * pdfAlpha - beta * pdfBeta) / cdfDelta;
+
+        if (lower == Double.NEGATIVE_INFINITY && upper != Double.POSITIVE_INFINITY) {
+            // One-sided lower tail truncation
+            double betaRatio = pdfBeta / cdfBeta;
+            this.mean = mean - sd * betaRatio;
+            variance = sd * sd * (1 - beta * betaRatio - betaRatio * betaRatio);
+        } else if (lower != Double.NEGATIVE_INFINITY && upper == Double.POSITIVE_INFINITY) {
+            // One-sided upper tail truncation
+            double alphaRatio =  pdfAlpha / cdfDelta;
+            this.mean = mean + sd * alphaRatio;
+            variance = sd * sd * (1 + alpha * alphaRatio - alphaRatio * alphaRatio);
+        } else if (lower != Double.NEGATIVE_INFINITY && upper != Double.POSITIVE_INFINITY) {
+            // Two-sided truncation
+            this.mean = mean + pdfCdfDelta * parentSd;
+            variance = sd * sd * (1 + alphaBetaDelta - pdfCdfDelta * pdfCdfDelta);
+        } else {
+            // No truncation
+            this.mean = mean;
+            variance = sd * sd;
+        }
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public double density(double x) {
+        if (x < lower || x > upper) {
+            return 0;
+        }
+        return standardNormal.density((x - parentMean) / parentSd) / (parentSd * cdfDelta);
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public double cumulativeProbability(double x) {
+        if (x <= lower) {
+            return 0;
+        } else if (x >= upper) {
+            return 1;
+        } else {
+            return (standardNormal.cumulativeProbability((x - parentMean) / parentSd) - cdfAlpha) / cdfDelta;
+        }
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public double inverseCumulativeProbability(double p) {
+        if (p < 0 || p > 1) {
+            throw new DistributionException(DistributionException.INVALID_PROBABILITY, p);
+        }
+        return standardNormal.inverseCumulativeProbability(cdfAlpha + p * cdfDelta) * parentSd + parentMean;
+    }
+
+    /**
+     * {@inheritDoc}
+     *
+     * Represents the true mean of the truncated normal distribution rather
+     * than the parent normal distribution mean.
+     */
+    @Override
+    public double getMean() {
+        return mean;
+    }
+
+    /**
+     * {@inheritDoc}
+     *
+     * Represents the true variance of the truncated normal distribution rather
+     * than the parent normal distribution variance.
+     */
+    @Override
+    public double getVariance() {
+        return variance;
+    }
+
+    /**

Review comment:
       In this case where nothing is added to the javadoc the rest of the codebase uses a single line `/** {@inheritDoc} */`

##########
File path: commons-statistics-distribution/src/main/java/org/apache/commons/statistics/distribution/TruncatedNormalDistribution.java
##########
@@ -0,0 +1,206 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.statistics.distribution;
+
+/**
+ * Implementation of the truncated normal distribution.
+ *
+ * @see <a href="http://en.wikipedia.org/wiki/Truncated_normal_distribution">
+ * Truncated normal distribution (Wikipedia)</a>.
+ */
+public class TruncatedNormalDistribution extends AbstractContinuousDistribution {
+    /**
+     * Mean of parent normal distribution.
+     */
+    private final double parentMean;
+    /**
+     * Standard deviation of parent normal distribution.
+     */
+    private final double parentSd;
+    /**
+     * Mean of this distribution.
+     */
+    private final double mean;
+    /**
+     * Variance of this distribution.
+     */
+    private final double variance;
+    /**
+     * Lower bound of this distribution.
+     */
+    private final double lower;
+    /**
+     * Upper bound of this distribution.
+     */
+    private final double upper;
+
+    /**
+     * A standard normal distribution used for calculations.
+     */
+    private final NormalDistribution standardNormal;
+    /**
+     * Stored value of @{code standardNormal.cumulativeProbability((lower - mean) / sd)} for faster computations.
+     */
+    private final double cdfAlpha;
+    /**
+     * Stored value of @{code (upper - mean) / sd) - cdfAlpha} for faster computations.

Review comment:
       Missing `standardNormal.cumulativeProbability(` inside the the `@code` tag as there is an unmatched close parenthesis.

##########
File path: commons-statistics-distribution/src/test/java/org/apache/commons/statistics/distribution/TruncatedNormalDistributionTest.java
##########
@@ -0,0 +1,283 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.statistics.distribution;
+
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+/**
+ * Test class for {@link TruncatedNormalDistribution}.
+ * All test values were computed using Python with SciPy v1.6.0.
+ */
+class TruncatedNormalDistributionTest extends ContinuousDistributionAbstractTest {
+    /**
+     * Default tolerance.
+     */
+    private static final double TOLERANCE = 1e-7;

Review comment:
       This can be removed and the `1e-7` placed in the call to `setTolerance(...)`

##########
File path: commons-statistics-distribution/src/main/java/org/apache/commons/statistics/distribution/TruncatedNormalDistribution.java
##########
@@ -0,0 +1,206 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.statistics.distribution;
+
+/**
+ * Implementation of the truncated normal distribution.
+ *
+ * @see <a href="http://en.wikipedia.org/wiki/Truncated_normal_distribution">
+ * Truncated normal distribution (Wikipedia)</a>.
+ */
+public class TruncatedNormalDistribution extends AbstractContinuousDistribution {
+    /**
+     * Mean of parent normal distribution.
+     */
+    private final double parentMean;
+    /**
+     * Standard deviation of parent normal distribution.
+     */
+    private final double parentSd;
+    /**
+     * Mean of this distribution.
+     */
+    private final double mean;
+    /**
+     * Variance of this distribution.
+     */
+    private final double variance;
+    /**
+     * Lower bound of this distribution.
+     */
+    private final double lower;
+    /**
+     * Upper bound of this distribution.
+     */
+    private final double upper;
+
+    /**
+     * A standard normal distribution used for calculations.
+     */
+    private final NormalDistribution standardNormal;
+    /**
+     * Stored value of @{code standardNormal.cumulativeProbability((lower - mean) / sd)} for faster computations.
+     */
+    private final double cdfAlpha;
+    /**
+     * Stored value of @{code (upper - mean) / sd) - cdfAlpha} for faster computations.
+     */
+    private final double cdfDelta;
+
+    /**
+     * Creates a truncated normal distribution.
+     * Note that the {@code mean} and {@code sd} is of the parent normal distribution,
+     * and not the true mean and standard deviation of the truncated normal distribution.
+     *
+     * @param mean mean for this distribution.
+     * @param sd standard deviation for this distribution.
+     * @param lower lower bound (inclusive) of the distribution, can be {@link Double#NEGATIVE_INFINITY}.
+     * @param upper upper bound (inclusive) of the distribution, can be {@link Double#POSITIVE_INFINITY}.
+     * @throws IllegalArgumentException if {@code sd <= 0} or if {@code upper <= lower}.
+     */
+    public TruncatedNormalDistribution(double mean, double sd, double lower, double upper) {
+        if (sd <= 0) {
+            throw new DistributionException(DistributionException.NEGATIVE, sd);

Review comment:
       I am thinking that we should have a `DistributionException.NOT_STRICTLY_POSITIVE` constant here. However the same message is used in NormalDistribution (and others) for zero so if you introduce this new constant then I can propagate it where appropriate through the rest of the code. Or you can leave it and I can do this after this PR is merged.

##########
File path: commons-statistics-distribution/src/test/java/org/apache/commons/statistics/distribution/TruncatedNormalDistributionTest.java
##########
@@ -0,0 +1,283 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.statistics.distribution;
+
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+/**
+ * Test class for {@link TruncatedNormalDistribution}.
+ * All test values were computed using Python with SciPy v1.6.0.
+ */
+class TruncatedNormalDistributionTest extends ContinuousDistributionAbstractTest {
+    /**
+     * Default tolerance.
+     */
+    private static final double TOLERANCE = 1e-7;
+
+    /**
+     * Distribution to test with.
+     */
+    private TruncatedNormalDistribution distribution = new TruncatedNormalDistribution(1.9, 1.3, -1.1, 3.4);
+    /**
+     * Percentiles to test with.
+     */
+    private double[] ppfValues = new double[]{0, 0.0001, 0.001, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 0.75, 0.9, 0.95,
+                                              0.975, 0.99, 0.999, 0.9999, 1};
+    /**
+     * Expected cumulative values for percentiles.
+     */
+    private double[] cdfValues = new double[]{-1.1, -1.09597275767544, -1.0609616183922, -0.79283350106842,
+                                              -0.505331829887808, -0.192170173599874, 0.21173317261645,
+                                              0.925791281910463, 1.71399518338879, 2.43413009451536, 2.94473113856785,
+                                              3.15310057075828, 3.27036798398733, 3.34641874981679, 3.39452729074341,
+                                              3.39945153287941, 3.4};
+    /**
+     * Expected density values for percentiles.
+     */
+    private double[] pdfValues = new double[]{0.0247422752302618, 0.0249196707321102, 0.0265057408263321,
+                                              0.0415071096500185, 0.0640403254340905, 0.0971457789636,
+                                              0.152622492901864, 0.267853863255995, 0.35107475879338, 0.325977522502844,
+                                              0.25680502248913, 0.222886115806507, 0.203494915087054, 0.190997946666992,
+                                              0.183167918885238, 0.182370706542209, 0.182281965373914};
+    /**
+     * Expected distribution mean.
+     */
+    private double mean = 1.63375792365723;
+    /**
+     * Expected distribution variance.
+     */
+    private double variance = 1.03158703914439;
+
+    /**
+     * Overrides tolerance and sets up distribution.
+     */
+    @BeforeEach
+    void setUp() {
+        setTolerance(TOLERANCE);
+        super.setUp();
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public ContinuousDistribution makeDistribution() {
+        return distribution;
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public double[] makeCumulativeTestPoints() {
+        return cdfValues;
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public double[] makeCumulativeTestValues() {
+        return ppfValues;
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public double[] makeDensityTestValues() {
+        return pdfValues;
+    }
+
+    /**
+     * Configures new test values and runs relevant tests in this class and {@link ContinuousDistributionAbstractTest}.
+     * @param testDistribution distribution to test with.
+     * @param expectedPpfValues expected percentiles to test with.
+     * @param expectedCdfValues expected cumulative values for percentiles.
+     * @param expectedPdfValues expected density values for percentiles.
+     * @param expectedMean expected mean.
+     * @param expectedVariance expected variance.
+     */
+    private void testAdditionalDistribution(
+            TruncatedNormalDistribution testDistribution,
+            double[] expectedPpfValues,
+            double[] expectedCdfValues,
+            double[] expectedPdfValues,
+            double expectedMean,
+            double expectedVariance
+    ) {
+        this.distribution = testDistribution;
+        this.ppfValues = expectedPpfValues;
+        this.cdfValues = expectedCdfValues;
+        this.pdfValues = expectedPdfValues;
+        this.mean = expectedMean;
+        this.variance = expectedVariance;
+
+        setUp();
+
+        testMoments();
+
+        testConsistency();
+        testSampler();
+        testOutsideSupport();
+        testDensities();
+        testLogDensities();
+        testOutsideSupport();
+        testInverseCumulativeProbabilities();
+        testDensityIntegrals();
+        testCumulativeProbabilities();
+        testIsSupportConnected();
+        testPrecondition1();
+        testPrecondition2();
+        testPrecondition3();
+    }
+
+    /**
+     * Test a one-sided truncation with a lower tail.
+     */
+    @Test
+    void testOneSidedLowerTail() {
+        testAdditionalDistribution(
+                new TruncatedNormalDistribution(12, 2.4, -Double.POSITIVE_INFINITY, 7.1),
+                new double[]{0, 0.00108276414971883, 0.00433032247708514, 0.0155754809421998, 0.0504271331622245,
+                             0.147106879016387, 0.387159643321778, 0.920668099879139, 1},
+                new double[]{-Double.POSITIVE_INFINITY, 2.20249292901062, 3.00511196424565, 3.80773099948069,
+                             4.61035003471573, 5.41296906995077, 6.21558810518581, 7.01820714042084, 7.1},
+                new double[]{0, 0.00194181137319567, 0.00719165311538403, 0.0238165586714952, 0.0705273999981105,
+                             0.186752027463317, 0.442182309739316, 0.936194292830215, 1.00423817618302},
+                6.21558810518581,
+                0.644197315721623
+        );
+    }
+
+    /**
+     * Test a one-sided truncation with an upper tail.
+     */
+    @Test
+    void testOneSidedUpperTail() {
+        testAdditionalDistribution(
+                new TruncatedNormalDistribution(-9.6, 17, -15, Double.POSITIVE_INFINITY),
+                new double[]{0, 0.164539974698729, 0.564800349576255, 0.836443289017693, 0.957226746540945,
+                             0.992394081771774, 0.999093968560336, 0.999928403010774, 1},
+                new double[]{-15, -10.5314720401464, 0.723583450712814, 11.978638941572, 23.2336944324312,
+                             34.4887499232902, 45.7438054141485, 56.9988609050074, Double.POSITIVE_INFINITY},
+                new double[]{0.035721742043989, 0.0375137766818179, 0.0312438063187719, 0.0167870518464031,
+                             0.00581865051705663, 0.00130109036611494, 0.000187685186297558, 1.74658560715427e-05, 0},
+                0.723583450712812,
+                126.676274102319
+        );
+    }
+
+    /**
+     * Test no truncation.
+     */
+    @Test
+    void testNoTruncation() {
+        testAdditionalDistribution(
+                new TruncatedNormalDistribution(3, 1.1, -Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY),
+                new double[]{0, 2.86651571879193e-07, 3.16712418331199e-05, 0.00134989803163009, 0.0227501319481792,
+                             0.158655253931457, 0.5, 0.841344746068543, 0.977249868051821, 0.99865010196837,
+                             0.999968328758167, 0.999999713348428, 1},
+                new double[]{-Double.POSITIVE_INFINITY, -2.5, -1.4, -0.300000000000001, 0.799999999999999, 1.9, 3,
+                             4.1, 5.2, 6.3, 7.4, 8.49999999996719, Double.POSITIVE_INFINITY},
+                new double[]{0, 1.35156319521299e-06, 0.000121663841604441, 0.00402895310176182, 0.0490826968301709,
+                             0.219973385926494, 0.362674800364939, 0.219973385926494, 0.0490826968301709,
+                             0.00402895310176184, 0.000121663841604441, 1.35156319541454e-06, 0},
+                3,
+                1.21
+        );
+    }
+
+    /**
+     * Test a truncation range that is completely below the mean.
+     */
+    @Test
+    void testLowerTailOnly() {
+        testAdditionalDistribution(
+                new TruncatedNormalDistribution(0, 1, -Double.POSITIVE_INFINITY, -5),
+                new double[]{0, 0.00196196451357246, 0.00597491488512203, 0.0176247203066899, 0.0503595643590926,
+                             0.139390045971621, 0.373761183487683, 0.970943041215359, 1},
+                new double[]{-Double.POSITIVE_INFINITY, -6.09061174025149, -5.90979018562636, -5.72896863100123,
+                             -5.54814707637611, -5.36732552175098, -5.18650396712585, -5.00568241250073, -5},
+                new double[]{0, 0.0122562922051934, 0.0362705138555484, 0.103883943928261, 0.287967362544455,
+                             0.772570689127439, 2.00601097433085, 5.04113700754108, 5.18650396712585},
+                -5.18650396712585,
+                0.0326964346170475
+        );
+    }
+
+    /**
+     * Test a truncation range that is completely above the mean.
+     */
+    @Test
+    void testUpperTailOnly() {
+        testAdditionalDistribution(
+                new TruncatedNormalDistribution(0, 1, 5, Double.POSITIVE_INFINITY),
+                new double[]{0, 0.0290569590230917, 0.626238816822898, 0.860609954247549, 0.949640435971243,
+                             0.982375279755296, 0.994025085266282, 0.998038035551444, 1},
+                new double[]{5, 5.00568241254803, 5.18650396728068, 5.36732552203467, 5.54814707752324,
+                             5.72896863159791, 5.90979018980065, 6.09061174555624, Double.POSITIVE_INFINITY},
+                new double[]{5.18650396712585, 5.04113700634745, 2.00601097272001, 0.772570687951075,
+                             0.287967360711704, 0.103883943573147, 0.0362705129607846, 0.0122562918092027, 0},
+                5.18650396712585,
+                0.0326964346170475
+        );
+    }
+
+    /**
+     * Test a narrow truncation range.
+     */
+    @Test
+    void testNarrowTruncatedRange() {
+        testAdditionalDistribution(
+                new TruncatedNormalDistribution(7.1, 9.9, 7.0999999, 7.1000001),
+                new double[]{0, 0.5, 1},
+                new double[]{7.0999999, 7.1, 7.1000001},
+                new double[]{5000000.00238838, 5000000.00238838, 5000000.00238838},
+                7.1,
+                1.13584123966337e-07
+        );
+    }
+
+    /**
+     * Test mean and variance moments.
+     */
+    @Test
+    void testMoments() {
+        Assertions.assertEquals(mean, distribution.getMean(), TOLERANCE);

Review comment:
       Can use `getTolerance()` here and avoid a class level constant

##########
File path: commons-statistics-distribution/src/main/java/org/apache/commons/statistics/distribution/TruncatedNormalDistribution.java
##########
@@ -0,0 +1,206 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.statistics.distribution;
+
+/**
+ * Implementation of the truncated normal distribution.
+ *
+ * @see <a href="http://en.wikipedia.org/wiki/Truncated_normal_distribution">
+ * Truncated normal distribution (Wikipedia)</a>.
+ */
+public class TruncatedNormalDistribution extends AbstractContinuousDistribution {
+    /**
+     * Mean of parent normal distribution.
+     */
+    private final double parentMean;
+    /**
+     * Standard deviation of parent normal distribution.
+     */
+    private final double parentSd;
+    /**
+     * Mean of this distribution.
+     */
+    private final double mean;
+    /**
+     * Variance of this distribution.
+     */
+    private final double variance;
+    /**
+     * Lower bound of this distribution.
+     */
+    private final double lower;
+    /**
+     * Upper bound of this distribution.
+     */
+    private final double upper;
+
+    /**
+     * A standard normal distribution used for calculations.
+     */
+    private final NormalDistribution standardNormal;
+    /**
+     * Stored value of @{code standardNormal.cumulativeProbability((lower - mean) / sd)} for faster computations.
+     */
+    private final double cdfAlpha;
+    /**
+     * Stored value of @{code (upper - mean) / sd) - cdfAlpha} for faster computations.
+     */
+    private final double cdfDelta;
+
+    /**
+     * Creates a truncated normal distribution.
+     * Note that the {@code mean} and {@code sd} is of the parent normal distribution,
+     * and not the true mean and standard deviation of the truncated normal distribution.
+     *
+     * @param mean mean for this distribution.
+     * @param sd standard deviation for this distribution.
+     * @param lower lower bound (inclusive) of the distribution, can be {@link Double#NEGATIVE_INFINITY}.
+     * @param upper upper bound (inclusive) of the distribution, can be {@link Double#POSITIVE_INFINITY}.
+     * @throws IllegalArgumentException if {@code sd <= 0} or if {@code upper <= lower}.
+     */
+    public TruncatedNormalDistribution(double mean, double sd, double lower, double upper) {
+        if (sd <= 0) {
+            throw new DistributionException(DistributionException.NEGATIVE, sd);
+        }
+        if (upper <= lower) {
+            throw new DistributionException(DistributionException.TOO_LARGE, lower, upper);
+        }
+
+        this.lower = lower;
+        this.upper = upper;
+
+        parentMean = mean;
+        parentSd = sd;
+        standardNormal = new NormalDistribution(0, 1);
+
+        double alpha = (lower - mean) / sd;
+        double beta = (upper - mean) / sd;
+
+        cdfAlpha = standardNormal.cumulativeProbability(alpha);
+        double cdfBeta = standardNormal.cumulativeProbability(beta);
+        cdfDelta = cdfBeta - cdfAlpha;
+
+        // Calculation of variance and mean.
+        double pdfAlpha = standardNormal.density(alpha);
+        double pdfBeta = standardNormal.density(beta);
+        double pdfCdfDelta = (pdfAlpha - pdfBeta) / cdfDelta;
+        double alphaBetaDelta = (alpha * pdfAlpha - beta * pdfBeta) / cdfDelta;
+
+        if (lower == Double.NEGATIVE_INFINITY && upper != Double.POSITIVE_INFINITY) {
+            // One-sided lower tail truncation
+            double betaRatio = pdfBeta / cdfBeta;
+            this.mean = mean - sd * betaRatio;
+            variance = sd * sd * (1 - beta * betaRatio - betaRatio * betaRatio);
+        } else if (lower != Double.NEGATIVE_INFINITY && upper == Double.POSITIVE_INFINITY) {
+            // One-sided upper tail truncation
+            double alphaRatio =  pdfAlpha / cdfDelta;
+            this.mean = mean + sd * alphaRatio;
+            variance = sd * sd * (1 + alpha * alphaRatio - alphaRatio * alphaRatio);
+        } else if (lower != Double.NEGATIVE_INFINITY && upper != Double.POSITIVE_INFINITY) {
+            // Two-sided truncation
+            this.mean = mean + pdfCdfDelta * parentSd;
+            variance = sd * sd * (1 + alphaBetaDelta - pdfCdfDelta * pdfCdfDelta);
+        } else {
+            // No truncation
+            this.mean = mean;
+            variance = sd * sd;
+        }
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public double density(double x) {
+        if (x < lower || x > upper) {
+            return 0;
+        }
+        return standardNormal.density((x - parentMean) / parentSd) / (parentSd * cdfDelta);
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public double cumulativeProbability(double x) {
+        if (x <= lower) {
+            return 0;
+        } else if (x >= upper) {
+            return 1;
+        } else {

Review comment:
       Remove the unnecessary `else` 

##########
File path: commons-statistics-distribution/src/main/java/org/apache/commons/statistics/distribution/TruncatedNormalDistribution.java
##########
@@ -0,0 +1,206 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.statistics.distribution;
+
+/**
+ * Implementation of the truncated normal distribution.
+ *
+ * @see <a href="http://en.wikipedia.org/wiki/Truncated_normal_distribution">
+ * Truncated normal distribution (Wikipedia)</a>.
+ */
+public class TruncatedNormalDistribution extends AbstractContinuousDistribution {
+    /**
+     * Mean of parent normal distribution.
+     */
+    private final double parentMean;

Review comment:
       Single line javadoc can be left on one line: `/** Something short. */`




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org