You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by tn...@apache.org on 2013/05/07 22:09:52 UTC
svn commit: r1480056 - in /commons/proper/math/trunk/src: changes/changes.xml
main/java/org/apache/commons/math3/distribution/ParetoDistribution.java
test/R/paretoTestCases
test/java/org/apache/commons/math3/distribution/ParetoDistributionTest.java
Author: tn
Date: Tue May 7 20:09:52 2013
New Revision: 1480056
URL: http://svn.apache.org/r1480056
Log:
[MATH-968] Added ParetoDistribution.
Added:
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/ParetoDistribution.java (with props)
commons/proper/math/trunk/src/test/R/paretoTestCases
commons/proper/math/trunk/src/test/java/org/apache/commons/math3/distribution/ParetoDistributionTest.java (with props)
Modified:
commons/proper/math/trunk/src/changes/changes.xml
Modified: commons/proper/math/trunk/src/changes/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/changes/changes.xml?rev=1480056&r1=1480055&r2=1480056&view=diff
==============================================================================
--- commons/proper/math/trunk/src/changes/changes.xml (original)
+++ commons/proper/math/trunk/src/changes/changes.xml Tue May 7 20:09:52 2013
@@ -51,6 +51,9 @@ If the output is not quite correct, chec
</properties>
<body>
<release version="x.y" date="TBD" description="TBD">
+ <action dev="tn" type="add" issue="MATH-968" due-to="Alex Gryzlov">
+ Added "ParetoDistribution" to "o.a.c.m.distribution" package.
+ </action>
<action dev="tn" type="fix" issue="MATH-962">
Added clarification to the javadoc of "VectorFormat" and derived classes
in case "," is used as a separator.
Added: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/ParetoDistribution.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/ParetoDistribution.java?rev=1480056&view=auto
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/ParetoDistribution.java (added)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/ParetoDistribution.java Tue May 7 20:09:52 2013
@@ -0,0 +1,280 @@
+/*
+ * 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.math3.distribution;
+
+import org.apache.commons.math3.exception.NotStrictlyPositiveException;
+import org.apache.commons.math3.exception.NumberIsTooLargeException;
+import org.apache.commons.math3.exception.util.LocalizedFormats;
+import org.apache.commons.math3.util.FastMath;
+import org.apache.commons.math3.random.RandomGenerator;
+import org.apache.commons.math3.random.Well19937c;
+
+/**
+ * Implementation of the Pareto distribution.
+ *
+ * <p>
+ * <strong>Parameters:</strong>
+ * The probability distribution function of {@code X} is given by (for {@code x >= k}):
+ * <pre>
+ * α * k^α / x^(α + 1)
+ * </pre>
+ * <p>
+ * <ul>
+ * <li>{@code k} is the <em>scale</em> parameter: this is the minimum possible value of {@code X},</li>
+ * <li>{@code α} is the <em>shape</em> parameter: this is the Pareto index</li>
+ * </ul>
+ *
+ * @see <a href="http://en.wikipedia.org/wiki/Pareto_distribution">
+ * Pareto distribution (Wikipedia)</a>
+ * @see <a href="http://mathworld.wolfram.com/ParetoDistribution.html">
+ * Pareto distribution (MathWorld)</a>
+ *
+ * @version $Id$
+ * @since 4.0
+ */
+public class ParetoDistribution extends AbstractRealDistribution {
+
+ /** Default inverse cumulative probability accuracy. */
+ public static final double DEFAULT_INVERSE_ABSOLUTE_ACCURACY = 1e-9;
+
+ /** Serializable version identifier. */
+ private static final long serialVersionUID = 20130424;
+
+ /** The scale parameter of this distribution. */
+ private final double scale;
+
+ /** The shape parameter of this distribution. */
+ private final double shape;
+
+ /** Inverse cumulative probability accuracy. */
+ private final double solverAbsoluteAccuracy;
+
+ /**
+ * Create a Pareto distribution with a scale of {@code 1} and a shape of {@code 1}.
+ */
+ public ParetoDistribution() {
+ this(1, 1);
+ }
+
+ /**
+ * Create a Pareto distribution using the specified scale and shape.
+ *
+ * @param scale the scale parameter of this distribution
+ * @param shape the shape parameter of this distribution
+ * @throws NotStrictlyPositiveException if {@code scale <= 0} or {@code shape <= 0}.
+ */
+ public ParetoDistribution(double scale, double shape)
+ throws NotStrictlyPositiveException {
+ this(scale, shape, DEFAULT_INVERSE_ABSOLUTE_ACCURACY);
+ }
+
+ /**
+ * Create a Pareto distribution using the specified scale, shape and
+ * inverse cumulative distribution accuracy.
+ *
+ * @param scale the scale parameter of this distribution
+ * @param shape the shape parameter of this distribution
+ * @param inverseCumAccuracy Inverse cumulative probability accuracy.
+ * @throws NotStrictlyPositiveException if {@code scale <= 0} or {@code shape <= 0}.
+ */
+ public ParetoDistribution(double scale, double shape, double inverseCumAccuracy)
+ throws NotStrictlyPositiveException {
+ this(new Well19937c(), scale, shape, inverseCumAccuracy);
+ }
+
+ /**
+ * Creates a log-normal distribution.
+ *
+ * @param rng Random number generator.
+ * @param scale Scale parameter of this distribution.
+ * @param shape Shape parameter of this distribution.
+ * @param inverseCumAccuracy Inverse cumulative probability accuracy.
+ * @throws NotStrictlyPositiveException if {@code scale <= 0} or {@code shape <= 0}.
+ */
+ public ParetoDistribution(RandomGenerator rng,
+ double scale,
+ double shape,
+ double inverseCumAccuracy)
+ throws NotStrictlyPositiveException {
+ super(rng);
+
+ if (scale <= 0) {
+ throw new NotStrictlyPositiveException(LocalizedFormats.SCALE, scale);
+ }
+
+ if (shape <= 0) {
+ throw new NotStrictlyPositiveException(LocalizedFormats.SHAPE, shape);
+ }
+
+ this.scale = scale;
+ this.shape = shape;
+ this.solverAbsoluteAccuracy = inverseCumAccuracy;
+ }
+
+ /**
+ * Returns the scale parameter of this distribution.
+ *
+ * @return the scale parameter
+ */
+ public double getScale() {
+ return scale;
+ }
+
+ /**
+ * Returns the shape parameter of this distribution.
+ *
+ * @return the shape parameter
+ */
+ public double getShape() {
+ return shape;
+ }
+
+ /**
+ * {@inheritDoc}
+ * <p>
+ * For scale {@code k}, and shape {@code α} of this distribution, the PDF
+ * is given by
+ * <ul>
+ * <li>{@code 0} if {@code x < k},</li>
+ * <li>{@code α * k^α / x^(α + 1)} otherwise.</li>
+ * </ul>
+ */
+ public double density(double x) {
+ if (x < scale) {
+ return 0;
+ }
+ return FastMath.pow(scale, shape) / FastMath.pow(x, shape + 1) * shape;
+ }
+
+ /**
+ * {@inheritDoc}
+ * <p>
+ * For scale {@code k}, and shape {@code α} of this distribution, the CDF is given by
+ * <ul>
+ * <li>{@code 0} if {@code x < k},</li>
+ * <li>{@code 1 - (k / x)^α} otherwise.</li>
+ * </ul>
+ */
+ public double cumulativeProbability(double x) {
+ if (x <= scale) {
+ return 0;
+ }
+ return 1 - FastMath.pow(scale / x, shape);
+ }
+
+ /**
+ * {@inheritDoc}
+ *
+ * @deprecated See {@link RealDistribution#cumulativeProbability(double,double)}
+ */
+ @Override
+ @Deprecated
+ public double cumulativeProbability(double x0, double x1)
+ throws NumberIsTooLargeException {
+ return probability(x0, x1);
+ }
+
+ /** {@inheritDoc} */
+ @Override
+ protected double getSolverAbsoluteAccuracy() {
+ return solverAbsoluteAccuracy;
+ }
+
+ /**
+ * {@inheritDoc}
+ * <p>
+ * For scale {@code k} and shape {@code α}, the mean is given by
+ * <ul>
+ * <li>{@code â} if {@code α <= 1},</li>
+ * <li>{@code α * k / (α - 1)} otherwise.</li>
+ * </ul>
+ */
+ public double getNumericalMean() {
+ if (shape <= 1) {
+ return Double.POSITIVE_INFINITY;
+ }
+ return shape * scale / (shape - 1);
+ }
+
+ /**
+ * {@inheritDoc}
+ * <p>
+ * For scale {@code k} and shape {@code α}, the variance is given by
+ * <ul>
+ * <li>{@code â} if {@code 1 < α <= 2},</li>
+ * <li>{@code k^2 * α / ((α - 1)^2 * (α - 2))} otherwise.</li>
+ * </ul>
+ */
+ public double getNumericalVariance() {
+ if (shape <= 2) {
+ return Double.POSITIVE_INFINITY;
+ }
+ double s = shape - 1;
+ return scale * scale * shape / (s * s) / (shape - 2);
+ }
+
+ /**
+ * {@inheritDoc}
+ * <p>
+ * The lower bound of the support is equal to the scale parameter {@code k}.
+ *
+ * @return lower bound of the support
+ */
+ public double getSupportLowerBound() {
+ return scale;
+ }
+
+ /**
+ * {@inheritDoc}
+ * <p>
+ * The upper bound of the support is always positive infinity no matter the parameters.
+ *
+ * @return upper bound of the support (always {@code Double.POSITIVE_INFINITY})
+ */
+ public double getSupportUpperBound() {
+ return Double.POSITIVE_INFINITY;
+ }
+
+ /** {@inheritDoc} */
+ public boolean isSupportLowerBoundInclusive() {
+ return true;
+ }
+
+ /** {@inheritDoc} */
+ public boolean isSupportUpperBoundInclusive() {
+ return false;
+ }
+
+ /**
+ * {@inheritDoc}
+ * <p>
+ * The support of this distribution is connected.
+ *
+ * @return {@code true}
+ */
+ public boolean isSupportConnected() {
+ return true;
+ }
+
+ /** {@inheritDoc} */
+ @Override
+ public double sample() {
+ final double n = random.nextDouble();
+ return scale / FastMath.pow(n, 1 / shape);
+ }
+}
Propchange: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/ParetoDistribution.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/ParetoDistribution.java
------------------------------------------------------------------------------
svn:keywords = Id Revision HeadURL
Propchange: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/distribution/ParetoDistribution.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: commons/proper/math/trunk/src/test/R/paretoTestCases
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/R/paretoTestCases?rev=1480056&view=auto
==============================================================================
--- commons/proper/math/trunk/src/test/R/paretoTestCases (added)
+++ commons/proper/math/trunk/src/test/R/paretoTestCases Tue May 7 20:09:52 2013
@@ -0,0 +1,108 @@
+# 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.
+#
+#------------------------------------------------------------------------------
+# R source file to validate LogNormal distribution tests in
+# org.apache.commons.math.distribution.LogNormalDistributionTest
+#
+# To run the test, install R, put this file and testFunctions
+# into the same directory, launch R from this directory and then enter
+# source("<name-of-this-file>")
+#
+# R functions used
+# ppareto(q, mean=0, sd=1, lower.tail = TRUE, log.p = FALSE) <-- distribution
+#-----------------------------------------------------------------------------
+tol <- 1E-9
+
+# Function definitions
+
+library(VGAM)
+source("testFunctions") # utility test functions
+
+# function to verify distribution computations
+
+verifyDistribution <- function(points, expected, mu, sigma, tol) {
+ rDistValues <- rep(0, length(points))
+ i <- 0
+ for (point in points) {
+ i <- i + 1
+ rDistValues[i] <- ppareto(point, mu, sigma)
+ }
+ output <- c("Distribution test mu = ",mu,", sigma = ", sigma)
+ if (assertEquals(expected, rDistValues, tol, "Distribution Values")) {
+ displayPadded(output, SUCCEEDED, WIDTH)
+ } else {
+ displayPadded(output, FAILED, WIDTH)
+ }
+}
+
+# function to verify density computations
+
+verifyDensity <- function(points, expected, mu, sigma, tol) {
+ rDensityValues <- rep(0, length(points))
+ i <- 0
+ for (point in points) {
+ i <- i + 1
+ rDensityValues[i] <- dpareto(point, mu, sigma, log = FALSE)
+ }
+ output <- c("Density test mu = ",mu,", sigma = ", sigma)
+ if (assertEquals(expected, rDensityValues, tol, "Density Values")) {
+ displayPadded(output, SUCCEEDED, WIDTH)
+ } else {
+ displayPadded(output, FAILED, WIDTH)
+ }
+}
+
+#--------------------------------------------------------------------------
+cat("Pareto test cases\n")
+
+mu <- 2.1
+sigma <- 1.4
+distributionValues <- c(0, 0, 0, 0, 0, 0.791089998892, 0.730456085931, 0.689667290488, 0.645278794701, 0.578763688757)
+densityValues <- c(0, 0, 0, 0, 0, 0.0455118580441, 0.070444173646, 0.0896924681582, 0.112794186114, 0.151439332084)
+distributionPoints <- c(-2.226325228634938, -1.156887023657177, -0.643949578356075, -0.2027950777320613, 0.305827808237559,
+ 6.42632522863494, 5.35688702365718, 4.843949578356074, 4.40279507773206, 3.89417219176244)
+verifyDistribution(distributionPoints, distributionValues, mu, sigma, tol)
+verifyDensity(distributionPoints, densityValues, mu, sigma, tol)
+
+distributionValues <- c(0, 0, 0, 0.510884134236, 0.694625688662, 0.785201995008, 0.837811522357, 0.871634279326)
+densityValues <- c(0, 0, 0, 0.195646346305, 0.0872498032394, 0.0477328899983, 0.0294888141169, 0.0197485724114)
+distributionPoints <- c(mu - 2 *sigma, mu - sigma, mu, mu + sigma,
+ mu + 2 * sigma, mu + 3 * sigma, mu + 4 * sigma,
+ mu + 5 * sigma)
+verifyDistribution(distributionPoints, distributionValues, mu, sigma, tol)
+verifyDensity(distributionPoints, densityValues, mu, sigma, tol)
+
+mu <- 1
+sigma <- 1
+distributionPoints <- c(mu - 2 *sigma, mu - sigma, mu, mu + sigma,
+ mu + 2 * sigma, mu + 3 * sigma, mu + 4 * sigma,
+ mu + 5 * sigma)
+distributionValues <- c(0, 0, 0, 0.5, 0.666666666667, 0.75, 0.8, 0.833333333333)
+densityValues <- c(0, 0, 0, 0.25, 0.111111111111, 0.0625, 0.04, 0.0277777777778)
+verifyDistribution(distributionPoints, distributionValues, mu, sigma, tol)
+verifyDensity(distributionPoints, densityValues, mu, sigma, tol)
+
+mu <- 0.1
+sigma <- 0.1
+distributionPoints <- c(mu - 2 *sigma, mu - sigma, mu, mu + sigma,
+ mu + 2 * sigma, mu + 3 * sigma, mu + 4 * sigma,
+ mu + 5 * sigma)
+distributionValues <- c(0, 0, 0, 0.0669670084632, 0.104041540159, 0.129449436704, 0.148660077479, 0.164041197922)
+densityValues <- c(0, 0, 0, 0.466516495768, 0.298652819947, 0.217637640824, 0.170267984504, 0.139326467013)
+verifyDistribution(distributionPoints, distributionValues, mu, sigma, tol)
+verifyDensity(distributionPoints, densityValues, mu, sigma, tol)
+
+displayDashes(WIDTH)
Added: commons/proper/math/trunk/src/test/java/org/apache/commons/math3/distribution/ParetoDistributionTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math3/distribution/ParetoDistributionTest.java?rev=1480056&view=auto
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math3/distribution/ParetoDistributionTest.java (added)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math3/distribution/ParetoDistributionTest.java Tue May 7 20:09:52 2013
@@ -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.math3.distribution;
+
+import org.apache.commons.math3.exception.NotStrictlyPositiveException;
+import org.junit.Assert;
+import org.junit.Test;
+
+/**
+ * Test cases for {@link ParetoDistribution}.
+ * <p>
+ * Extends {@link RealDistributionAbstractTest}. See class javadoc of that class for details.
+ *
+ * @version $Id$
+ * @since 4.0
+ */
+public class ParetoDistributionTest extends RealDistributionAbstractTest {
+
+ //-------------- Implementations for abstract methods -----------------------
+
+ /** Creates the default real distribution instance to use in tests. */
+ @Override
+ public ParetoDistribution makeDistribution() {
+ return new ParetoDistribution(2.1, 1.4);
+ }
+
+ /** Creates the default cumulative probability distribution test input values */
+ @Override
+ public double[] makeCumulativeTestPoints() {
+ // quantiles computed using R
+ return new double[] { -2.226325228634938, -1.156887023657177, -0.643949578356075, -0.2027950777320613, 0.305827808237559,
+ +6.42632522863494, 5.35688702365718, 4.843949578356074, 4.40279507773206, 3.89417219176244 };
+ }
+
+ /** Creates the default cumulative probability density test expected values */
+ @Override
+ public double[] makeCumulativeTestValues() {
+ return new double[] { 0, 0, 0, 0, 0, 0.791089998892, 0.730456085931, 0.689667290488, 0.645278794701, 0.578763688757 };
+ }
+
+ /** Creates the default probability density test expected values */
+ @Override
+ public double[] makeDensityTestValues() {
+ return new double[] { 0, 0, 0, 0, 0, 0.0455118580441, 0.070444173646, 0.0896924681582, 0.112794186114, 0.151439332084 };
+ }
+
+ /**
+ * Creates the default inverse cumulative probability distribution test input values.
+ */
+ @Override
+ public double[] makeInverseCumulativeTestPoints() {
+ // Exclude the test points less than zero, as they have cumulative
+ // probability of zero, meaning the inverse returns zero, and not the
+ // points less than zero.
+ double[] points = makeCumulativeTestValues();
+ double[] points2 = new double[points.length - 5];
+ System.arraycopy(points, 5, points2, 0, points.length - 5);
+ return points2;
+ }
+
+ /**
+ * Creates the default inverse cumulative probability test expected values.
+ */
+ @Override
+ public double[] makeInverseCumulativeTestValues() {
+ // Exclude the test points less than zero, as they have cumulative
+ // probability of zero, meaning the inverse returns zero, and not the
+ // points less than zero.
+ double[] points = makeCumulativeTestPoints();
+ double[] points2 = new double[points.length - 5];
+ System.arraycopy(points, 5, points2, 0, points.length - 5);
+ return points2;
+ }
+
+ // --------------------- Override tolerance --------------
+ @Override
+ public void setUp() {
+ super.setUp();
+ setTolerance(ParetoDistribution.DEFAULT_INVERSE_ABSOLUTE_ACCURACY);
+ }
+
+ //---------------------------- Additional test cases -------------------------
+
+ private void verifyQuantiles() {
+ ParetoDistribution distribution = (ParetoDistribution)getDistribution();
+ double mu = distribution.getScale();
+ double sigma = distribution.getShape();
+ setCumulativeTestPoints( new double[] { mu - 2 *sigma, mu - sigma,
+ mu, mu + sigma,
+ mu + 2 * sigma, mu + 3 * sigma,
+ mu + 4 * sigma, mu + 5 * sigma });
+ verifyCumulativeProbabilities();
+ }
+
+ @Test
+ public void testQuantiles() {
+ setCumulativeTestValues(new double[] {0, 0, 0, 0.510884134236, 0.694625688662, 0.785201995008, 0.837811522357, 0.871634279326});
+ setDensityTestValues(new double[] {0, 0, 0.666666666, 0.195646346305, 0.0872498032394, 0.0477328899983, 0.0294888141169, 0.0197485724114});
+ verifyQuantiles();
+ verifyDensities();
+
+ setDistribution(new ParetoDistribution(1, 1));
+ setCumulativeTestValues(new double[] {0, 0, 0, 0.5, 0.666666666667, 0.75, 0.8, 0.833333333333});
+ setDensityTestValues(new double[] {0, 0, 1.0, 0.25, 0.111111111111, 0.0625, 0.04, 0.0277777777778});
+ verifyQuantiles();
+ verifyDensities();
+
+ setDistribution(new ParetoDistribution(0.1, 0.1));
+ setCumulativeTestValues(new double[] {0, 0, 0, 0.0669670084632, 0.104041540159, 0.129449436704, 0.148660077479, 0.164041197922});
+ setDensityTestValues(new double[] {0, 0, 1.0, 0.466516495768, 0.298652819947, 0.217637640824, 0.170267984504, 0.139326467013});
+ verifyQuantiles();
+ verifyDensities();
+ }
+
+ @Test
+ public void testInverseCumulativeProbabilityExtremes() {
+ setInverseCumulativeTestPoints(new double[] {0, 1});
+ setInverseCumulativeTestValues(new double[] {2.1, Double.POSITIVE_INFINITY});
+ verifyInverseCumulativeProbabilities();
+ }
+
+ @Test
+ public void testGetScale() {
+ ParetoDistribution distribution = (ParetoDistribution)getDistribution();
+ Assert.assertEquals(2.1, distribution.getScale(), 0);
+ }
+
+ @Test
+ public void testGetShape() {
+ ParetoDistribution distribution = (ParetoDistribution)getDistribution();
+ Assert.assertEquals(1.4, distribution.getShape(), 0);
+ }
+
+ @Test(expected=NotStrictlyPositiveException.class)
+ public void testPreconditions() {
+ new ParetoDistribution(1, 0);
+ }
+
+ @Test
+ public void testDensity() {
+ double [] x = new double[]{-2, -1, 0, 1, 2};
+ // R 2.14: print(dpareto(c(-2,-1,0,1,2), scale=1, shape=1), digits=10)
+ checkDensity(1, 1, x, new double[] { 0.00, 0.00, 0.00, 1.00, 0.25 });
+ // R 2.14: print(dpareto(c(-2,-1,0,1,2), scale=1.1, shape=1), digits=10)
+ checkDensity(1.1, 1, x, new double[] { 0.000, 0.000, 0.000, 0.000, 0.275 });
+ }
+
+ private void checkDensity(double scale, double shape, double[] x,
+ double[] expected) {
+ ParetoDistribution d = new ParetoDistribution(scale, shape);
+ for (int i = 0; i < x.length; i++) {
+ Assert.assertEquals(expected[i], d.density(x[i]), 1e-9);
+ }
+ }
+
+ /**
+ * Check to make sure top-coding of extreme values works correctly.
+ */
+ @Test
+ public void testExtremeValues() {
+ ParetoDistribution d = new ParetoDistribution(1, 1);
+ for (int i = 0; i < 1e5; i++) { // make sure no convergence exception
+ double upperTail = d.cumulativeProbability(i);
+ if (i <= 1000) { // make sure not top-coded
+ Assert.assertTrue(upperTail < 1.0d);
+ }
+ else { // make sure top coding not reversed
+ Assert.assertTrue(upperTail > 0.999);
+ }
+ }
+
+ Assert.assertEquals(d.cumulativeProbability(Double.MAX_VALUE), 1, 0);
+ Assert.assertEquals(d.cumulativeProbability(-Double.MAX_VALUE), 0, 0);
+ Assert.assertEquals(d.cumulativeProbability(Double.POSITIVE_INFINITY), 1, 0);
+ Assert.assertEquals(d.cumulativeProbability(Double.NEGATIVE_INFINITY), 0, 0);
+ }
+
+ @Test
+ public void testMeanVariance() {
+ final double tol = 1e-9;
+ ParetoDistribution dist;
+
+ dist = new ParetoDistribution(1, 1);
+ Assert.assertEquals(dist.getNumericalMean(), Double.POSITIVE_INFINITY, tol);
+ Assert.assertEquals(dist.getNumericalVariance(), Double.POSITIVE_INFINITY, tol);
+
+ dist = new ParetoDistribution(2.2, 2.4);
+ Assert.assertEquals(dist.getNumericalMean(), 3.771428571428, tol);
+ Assert.assertEquals(dist.getNumericalVariance(), 14.816326530, tol);
+ }
+}
Propchange: commons/proper/math/trunk/src/test/java/org/apache/commons/math3/distribution/ParetoDistributionTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: commons/proper/math/trunk/src/test/java/org/apache/commons/math3/distribution/ParetoDistributionTest.java
------------------------------------------------------------------------------
svn:keywords = Id Revision HeadURL
Propchange: commons/proper/math/trunk/src/test/java/org/apache/commons/math3/distribution/ParetoDistributionTest.java
------------------------------------------------------------------------------
svn:mime-type = text/plain