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



##########
File path: 
commons-statistics-distribution/src/test/java/org/apache/commons/statistics/distribution/TruncatedNormalDistributionTest.java
##########
@@ -0,0 +1,239 @@
+/*
+ * 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 {
+    /** 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(1e-7);
+        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.NEGATIVE_INFINITY, 7.1),
+                new double[]{0, 0.00108276414971883, 0.00433032247708514, 
0.0155754809421998, 0.0504271331622245,
+                             0.147106879016387, 0.387159643321778, 
0.920668099879139, 1},
+                new double[]{Double.NEGATIVE_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

Review comment:
       Remove the extra new line before `);`

##########
File path: 
commons-statistics-distribution/src/test/java/org/apache/commons/statistics/distribution/TruncatedNormalDistributionTest.java
##########
@@ -0,0 +1,239 @@
+/*
+ * 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 {
+    /** 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(1e-7);
+        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

Review comment:
       Remove the extra newline before the `) {`

##########
File path: 
commons-statistics-distribution/src/main/java/org/apache/commons/statistics/distribution/TruncatedNormalDistribution.java
##########
@@ -0,0 +1,183 @@
+/*
+ * 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 standardNormal.cumulativeProbability((upper - 
mean) / sd) - cdfAlpha}
+     * for faster computations.
+     */
+    private final double cdfDelta;
+

Review comment:
       Remove the double line break.




----------------------------------------------------------------
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:
[email protected]


Reply via email to