You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by lu...@apache.org on 2008/12/06 18:11:07 UTC
svn commit: r724014 - in /commons/proper/math/trunk: ./
src/java/org/apache/commons/math/
src/java/org/apache/commons/math/distribution/ src/site/xdoc/
src/test/org/apache/commons/math/distribution/
Author: luc
Date: Sat Dec 6 09:11:07 2008
New Revision: 724014
URL: http://svn.apache.org/viewvc?rev=724014&view=rev
Log:
added support for Zipf distribution
JIRA: MATH-178
Added:
commons/proper/math/trunk/src/java/org/apache/commons/math/distribution/ZipfDistribution.java (with props)
commons/proper/math/trunk/src/java/org/apache/commons/math/distribution/ZipfDistributionImpl.java (with props)
commons/proper/math/trunk/src/test/org/apache/commons/math/distribution/ZipfDistributionTest.java (with props)
Modified:
commons/proper/math/trunk/pom.xml
commons/proper/math/trunk/src/java/org/apache/commons/math/MessagesResources_fr.java
commons/proper/math/trunk/src/site/xdoc/changes.xml
Modified: commons/proper/math/trunk/pom.xml
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/pom.xml?rev=724014&r1=724013&r2=724014&view=diff
==============================================================================
--- commons/proper/math/trunk/pom.xml (original)
+++ commons/proper/math/trunk/pom.xml Sat Dec 6 09:11:07 2008
@@ -100,6 +100,9 @@
<name>C. Scott Ananian</name>
</contributor>
<contributor>
+ <name>Paul Cowan</name>
+ </contributor>
+ <contributor>
<name>Rodrigo di Lorenzo Lopes</name>
</contributor>
<contributor>
Modified: commons/proper/math/trunk/src/java/org/apache/commons/math/MessagesResources_fr.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/java/org/apache/commons/math/MessagesResources_fr.java?rev=724014&r1=724013&r2=724014&view=diff
==============================================================================
--- commons/proper/math/trunk/src/java/org/apache/commons/math/MessagesResources_fr.java (original)
+++ commons/proper/math/trunk/src/java/org/apache/commons/math/MessagesResources_fr.java Sat Dec 6 09:11:07 2008
@@ -304,7 +304,13 @@
{ "statistics constructed from external moments cannot be incremented",
"les statistiques bas\u00e9es sur des moments externes ne peuvent pas \u00eatre incr\u00e9ment\u00e9es" },
{ "statistics constructed from external moments cannot be cleared",
- "les statistiques bas\u00e9es sur des moments externes ne peuvent pas \u00eatre remises \u00e0 z\u00e9ro" }
+ "les statistiques bas\u00e9es sur des moments externes ne peuvent pas \u00eatre remises \u00e0 z\u00e9ro" },
+
+ // org.apache.commons.math.distribution.ZipfDistributionImpl
+ { "invalid number of elements {0} (must be positive)",
+ "nombre d''\u00e9l\u00e9ments {0} invalide (doit \u00eatre positif)" },
+ { "invalid exponent {0} (must be positive)",
+ "exposant {0} invalide (doit \u00eatre positif)" }
};
Added: commons/proper/math/trunk/src/java/org/apache/commons/math/distribution/ZipfDistribution.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/java/org/apache/commons/math/distribution/ZipfDistribution.java?rev=724014&view=auto
==============================================================================
--- commons/proper/math/trunk/src/java/org/apache/commons/math/distribution/ZipfDistribution.java (added)
+++ commons/proper/math/trunk/src/java/org/apache/commons/math/distribution/ZipfDistribution.java Sat Dec 6 09:11:07 2008
@@ -0,0 +1,66 @@
+/*
+ * 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.math.distribution;
+
+/**
+ * The Zipf (or zeta) Distribution.
+ * <p>
+ * References:
+ * <ul>
+ * <li><a href="http://mathworld.wolfram.com/ZipfDistribution.html">Zipf
+ * Distribution</a></li>
+ * </ul>
+ * </p>
+ *
+ * @version $Revision$ $Date$
+ */
+public interface ZipfDistribution extends IntegerDistribution {
+ /**
+ * Get the number of elements (e.g. corpus size) for the distribution.
+ *
+ * @return the number of elements
+ */
+ public int getNumberOfElements();
+
+ /**
+ * Set the number of elements (e.g. corpus size) for the distribution.
+ * The parameter value must be positive; otherwise an
+ * <code>IllegalArgumentException</code> is thrown.
+ *
+ * @param n the number of elements
+ * @throws IllegalArgumentException if n ≤ 0
+ */
+ public void setNumberOfElements(int n);
+
+ /**
+ * Get the exponent characterising the distribution.
+ *
+ * @return the exponent
+ */
+ public double getExponent();
+
+ /**
+ * Set the exponent characterising the distribution.
+ * The parameter value must be positive; otherwise an
+ * <code>IllegalArgumentException</code> is thrown.
+ *
+ * @param s the exponent
+ * @throws IllegalArgumentException if s ≤ 0.0
+ */
+ public void setExponent(double s);
+}
\ No newline at end of file
Propchange: commons/proper/math/trunk/src/java/org/apache/commons/math/distribution/ZipfDistribution.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: commons/proper/math/trunk/src/java/org/apache/commons/math/distribution/ZipfDistribution.java
------------------------------------------------------------------------------
svn:keywords = Author Date Id Revision
Added: commons/proper/math/trunk/src/java/org/apache/commons/math/distribution/ZipfDistributionImpl.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/java/org/apache/commons/math/distribution/ZipfDistributionImpl.java?rev=724014&view=auto
==============================================================================
--- commons/proper/math/trunk/src/java/org/apache/commons/math/distribution/ZipfDistributionImpl.java (added)
+++ commons/proper/math/trunk/src/java/org/apache/commons/math/distribution/ZipfDistributionImpl.java Sat Dec 6 09:11:07 2008
@@ -0,0 +1,178 @@
+/*
+ * 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.math.distribution;
+
+import java.io.Serializable;
+
+import org.apache.commons.math.MathRuntimeException;
+
+/**
+ * Implementation for the {@link ZipfDistribution}.
+ *
+ * @version $Revision$ $Date$
+ */
+public class ZipfDistributionImpl extends AbstractIntegerDistribution
+ implements ZipfDistribution, Serializable {
+ private static final long serialVersionUID = -140627372283420404L;
+
+ private int numberOfElements;
+ private double exponent;
+
+ /**
+ * Create a new Zipf distribution with the given number of elements and
+ * exponent. Both values must be positive; otherwise an
+ * <code>IllegalArgumentException</code> is thrown.
+ *
+ * @param n the number of elements
+ * @param s the exponent
+ * @exception IllegalArgumentException if n ≤ 0 or s ≤ 0.0
+ */
+ public ZipfDistributionImpl(final int numberOfElements, final double exponent)
+ throws IllegalArgumentException {
+ setNumberOfElements(numberOfElements);
+ setExponent(exponent);
+ }
+
+ /**
+ * Get the number of elements (e.g. corpus size) for the distribution.
+ *
+ * @return the number of elements
+ */
+ public int getNumberOfElements() {
+ return numberOfElements;
+ }
+
+ /**
+ * Set the number of elements (e.g. corpus size) for the distribution.
+ * The parameter value must be positive; otherwise an
+ * <code>IllegalArgumentException</code> is thrown.
+ *
+ * @param n the number of elements
+ * @exception IllegalArgumentException if n ≤ 0
+ */
+ public void setNumberOfElements(final int n)
+ throws IllegalArgumentException {
+ if (n <= 0) {
+ throw MathRuntimeException.createIllegalArgumentException("invalid number of elements {0}" +
+ " (must be positive)",
+ new Object[] { n });
+ }
+ this.numberOfElements = n;
+ }
+
+ /**
+ * Get the exponent characterising the distribution.
+ *
+ * @return the exponent
+ */
+ public double getExponent() {
+ return exponent;
+ }
+
+ /**
+ * Set the exponent characterising the distribution.
+ * The parameter value must be positive; otherwise an
+ * <code>IllegalArgumentException</code> is thrown.
+ *
+ * @param s the exponent
+ * @exception IllegalArgumentException if s ≤ 0.0
+ */
+ public void setExponent(final double s)
+ throws IllegalArgumentException {
+ if (s <= 0.0) {
+ throw MathRuntimeException.createIllegalArgumentException("invalid exponent {0} (must be positive)",
+ new Object[] { s });
+ }
+ this.exponent = s;
+ }
+
+ /**
+ * The probability mass function P(X = x) for a Zipf distribution.
+ *
+ * @param x the value at which the probability density function is evaluated.
+ * @return the value of the probability mass function at x
+ */
+ public double probability(final int x) {
+ if (x <= 0 || x > getNumberOfElements()) {
+ return 0.0;
+ }
+
+ return (1.0 / Math.pow(x, exponent)) / generalizedHarmonic(numberOfElements, exponent);
+
+ }
+
+ /**
+ * The probability distribution function P(X <= x) for a Zipf distribution.
+ *
+ * @param x the value at which the PDF is evaluated.
+ * @return Zipf distribution function evaluated at x
+ */
+ public double cumulativeProbability(final int x) {
+ if (x <= 0) {
+ return 0.0;
+ } else if (x >= getNumberOfElements()) {
+ return 1.0;
+ }
+
+ return generalizedHarmonic(x, exponent) / generalizedHarmonic(numberOfElements, exponent);
+
+ }
+
+ /**
+ * Access the domain value lower bound, based on <code>p</code>, used to
+ * bracket a PDF root.
+ *
+ * @param p the desired probability for the critical value
+ * @return domain value lower bound, i.e.
+ * P(X < <i>lower bound</i>) < <code>p</code>
+ */
+ protected int getDomainLowerBound(final double p) {
+ return 0;
+ }
+
+ /**
+ * Access the domain value upper bound, based on <code>p</code>, used to
+ * bracket a PDF root.
+ *
+ * @param p the desired probability for the critical value
+ * @return domain value upper bound, i.e.
+ * P(X < <i>upper bound</i>) > <code>p</code>
+ */
+ protected int getDomainUpperBound(final double p) {
+ return numberOfElements;
+ }
+
+
+ /**
+ * Calculates the Nth generalized harmonic number. See
+ * <a href="http://mathworld.wolfram.com/HarmonicSeries.html">Harmonic
+ * Series</a>.
+ *
+ * @param n the term in the series to calculate (must be ≥ 1)
+ * @param m the exponent; special case m == 1.0 is the harmonic series
+ * @return the nth generalized harmonic number
+ */
+ private double generalizedHarmonic(final int n, final double m) {
+ double value = 0;
+ for (int k = n; k > 0; --k) {
+ value += 1.0 / Math.pow(k, m);
+ }
+ return value;
+ }
+
+}
Propchange: commons/proper/math/trunk/src/java/org/apache/commons/math/distribution/ZipfDistributionImpl.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: commons/proper/math/trunk/src/java/org/apache/commons/math/distribution/ZipfDistributionImpl.java
------------------------------------------------------------------------------
svn:keywords = Author Date Id Revision
Modified: commons/proper/math/trunk/src/site/xdoc/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/site/xdoc/changes.xml?rev=724014&r1=724013&r2=724014&view=diff
==============================================================================
--- commons/proper/math/trunk/src/site/xdoc/changes.xml (original)
+++ commons/proper/math/trunk/src/site/xdoc/changes.xml Sat Dec 6 09:11:07 2008
@@ -39,6 +39,9 @@
</properties>
<body>
<release version="2.0" date="TBD" description="TBD">
+ <action dev="luc" type="add" issue="MATH-178" due-to="Paul Cowan">
+ Added support for the Zipf distribution.
+ </action>
<action dev="psteitz" type="add" issue="MATH-212" due-to="Jason C. HandUber">
Added support for copying statistics. Changes to stats classes
include copy constructor, static copy(-,-) and instance copy()
Added: commons/proper/math/trunk/src/test/org/apache/commons/math/distribution/ZipfDistributionTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/org/apache/commons/math/distribution/ZipfDistributionTest.java?rev=724014&view=auto
==============================================================================
--- commons/proper/math/trunk/src/test/org/apache/commons/math/distribution/ZipfDistributionTest.java (added)
+++ commons/proper/math/trunk/src/test/org/apache/commons/math/distribution/ZipfDistributionTest.java Sat Dec 6 09:11:07 2008
@@ -0,0 +1,71 @@
+/*
+ * 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.math.distribution;
+
+/**
+ * Test cases for {@link ZipfDistribution}.
+ * Extends IntegerDistributionAbstractTest. See class javadoc for
+ * IntegerDistributionAbstractTest for details.
+ *
+ * @version $Revision$ $Date$
+ */
+public class ZipfDistributionTest extends IntegerDistributionAbstractTest {
+ public ZipfDistributionTest(String name) {
+ super(name);
+ }
+
+ //-------------- Implementations for abstract methods -----------------------
+
+ /** Creates the default discrete distribution instance to use in tests. */
+ public IntegerDistribution makeDistribution() {
+ return new ZipfDistributionImpl(10, 1);
+ }
+
+ /** Creates the default probability density test input values */
+ public int[] makeDensityTestPoints() {
+ return new int[] {-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
+ }
+
+ /** Creates the default probability density test expected values */
+ public double[] makeDensityTestValues() {
+ return new double[] {0d, 0d, 0.3414d, 0.1707d, 0.1138d, 0.0854d, 0.0683d,
+ 0.0569d, 0.0488d, 0.0427d, 0.0379d, 0.0341d, 0d};
+ }
+
+ /** Creates the default cumulative probability density test input values */
+ public int[] makeCumulativeTestPoints() {
+ return makeDensityTestPoints();
+ }
+
+ /** Creates the default cumulative probability density test expected values */
+ public double[] makeCumulativeTestValues() {
+ return new double[] {0d, 0.0000d, 0.3414d, 0.5121d, 0.6259d, 0.7113d,
+ 0.7796d, 0.8365d, 0.8852d, 0.9279d, 0.9659d, 1d, 1d};
+ }
+
+ /** Creates the default inverse cumulative probability test input values */
+ public double[] makeInverseCumulativeTestPoints() {
+ return new double[] {0, 0.001d, 0.010d, 0.025d, 0.050d, 0.3414d, 0.3415d, 0.999d,
+ 0.990d, 0.975d, 0.950d, 0.900d, 1};
+ }
+
+ /** Creates the default inverse cumulative probability density test expected values */
+ public int[] makeInverseCumulativeTestValues() {
+ return new int[] {0, 0, 0, 0, 0, 0, 1, 9, 9, 9, 8, 7, 10};
+ }
+}
\ No newline at end of file
Propchange: commons/proper/math/trunk/src/test/org/apache/commons/math/distribution/ZipfDistributionTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: commons/proper/math/trunk/src/test/org/apache/commons/math/distribution/ZipfDistributionTest.java
------------------------------------------------------------------------------
svn:keywords = Author Date Id Revision