You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mahout.apache.org by sr...@apache.org on 2011/01/08 14:49:11 UTC
svn commit: r1056703 [2/4] - in /mahout/trunk:
core/src/main/java/org/apache/mahout/cf/taste/hadoop/item/
core/src/main/java/org/apache/mahout/classifier/
core/src/main/java/org/apache/mahout/classifier/bayes/common/
core/src/main/java/org/apache/mahou...
Modified: mahout/trunk/math/src/main/java/org/apache/mahout/math/function/Functions.java
URL: http://svn.apache.org/viewvc/mahout/trunk/math/src/main/java/org/apache/mahout/math/function/Functions.java?rev=1056703&r1=1056702&r2=1056703&view=diff
==============================================================================
--- mahout/trunk/math/src/main/java/org/apache/mahout/math/function/Functions.java (original)
+++ mahout/trunk/math/src/main/java/org/apache/mahout/math/function/Functions.java Sat Jan 8 13:49:07 2011
@@ -36,8 +36,8 @@ import java.util.Date;
* in a generic manner. Essentially, a function object is an object that can perform a function on some arguments. It
* has a minimal interface: a method <tt>apply</tt> that takes the arguments, computes something and returns some result
* value. Function objects are comparable to function pointers in C used for call-backs. <p>Unary functions are of type
- * {@link org.apache.mahout.math.function.UnaryFunction}, binary functions of type {@link
- * org.apache.mahout.math.function.BinaryFunction}. All can be retrieved via <tt>public static final</tt>
+ * {@link org.apache.mahout.math.function.DoubleFunction}, binary functions of type {@link
+ * org.apache.mahout.math.function.DoubleDoubleFunction}. All can be retrieved via <tt>public static final</tt>
* variables named after the function. Unary predicates are of type
* {@link org.apache.mahout.math.function.DoubleProcedure},
* binary predicates of type {@link org.apache.mahout.math.function.DoubleDoubleProcedure}. All can be retrieved via
@@ -48,29 +48,29 @@ import java.util.Date;
* <li><tt>Functions.pow</tt> gives the function <tt>a<sup>b</sup></tt>. <li><tt>Functions.pow.apply(2,3)==8</tt>.
* <li><tt>Functions.pow(3)</tt> gives the function <tt>a<sup>3</sup></tt>. <li><tt>Functions.pow(3).apply(2)==8</tt>.
* </ul> More general, any binary function can be made an unary functions by fixing either the first or the second
- * argument. See methods {@link #bindArg1(org.apache.mahout.math.function.BinaryFunction ,double)} and {@link
- * #bindArg2(org.apache.mahout.math.function.BinaryFunction ,double)}. The order of arguments can
+ * argument. See methods {@link #bindArg1(org.apache.mahout.math.function.DoubleDoubleFunction ,double)} and {@link
+ * #bindArg2(org.apache.mahout.math.function.DoubleDoubleFunction ,double)}. The order of arguments can
* be swapped so that the first argument becomes the
- * second and vice-versa. See method {@link #swapArgs(org.apache.mahout.math.function.BinaryFunction)}.
+ * second and vice-versa. See method {@link #swapArgs(org.apache.mahout.math.function.DoubleDoubleFunction)}.
* Example: <ul> <li><tt>Functions.pow</tt>
* gives the function <tt>a<sup>b</sup></tt>. <li><tt>Functions.bindArg2(Functions.pow,3)</tt> gives the function
* <tt>x<sup>3</sup></tt>. <li><tt>Functions.bindArg1(Functions.pow,3)</tt> gives the function <tt>3<sup>x</sup></tt>.
* <li><tt>Functions.swapArgs(Functions.pow)</tt> gives the function <tt>b<sup>a</sup></tt>. </ul> <p> Even more
* general, functions can be chained (composed, assembled). Assume we have two unary functions <tt>g</tt> and
* <tt>h</tt>. The unary function <tt>g(h(a))</tt> applying both in sequence can be generated via {@link
- * #chain(org.apache.mahout.math.function.UnaryFunction , org.apache.mahout.math.function.UnaryFunction)}:
+ * #chain(org.apache.mahout.math.function.DoubleFunction , org.apache.mahout.math.function.DoubleFunction)}:
* <ul> <li><tt>Functions.chain(g,h);</tt> </ul> Assume further we have a binary
* function <tt>f</tt>. The binary function <tt>g(f(a,b))</tt> can be generated via {@link
- * #chain(org.apache.mahout.math.function.UnaryFunction , org.apache.mahout.math.function.BinaryFunction)}:
+ * #chain(org.apache.mahout.math.function.DoubleFunction , org.apache.mahout.math.function.DoubleDoubleFunction)}:
* <ul> <li><tt>Functions.chain(g,f);</tt> </ul> The binary function
* <tt>f(g(a),h(b))</tt> can be generated via
- * {@link #chain(org.apache.mahout.math.function.BinaryFunction , org.apache.mahout.math.function.UnaryFunction ,
- * org.apache.mahout.math.function.UnaryFunction)}: <ul>
+ * {@link #chain(org.apache.mahout.math.function.DoubleDoubleFunction , org.apache.mahout.math.function.DoubleFunction ,
+ * org.apache.mahout.math.function.DoubleFunction)}: <ul>
* <li><tt>Functions.chain(f,g,h);</tt> </ul> Arbitrarily complex functions can be composed from these building blocks.
* For example <tt>sin(a) + cos<sup>2</sup>(b)</tt> can be specified as follows: <ul>
* <li><tt>chain(plus,sin,chain(square,cos));</tt> </ul> or, of course, as
* <pre>
- * new BinaryFunction() {
+ * new DoubleDoubleFunction() {
* public final double apply(double a, double b) { return Math.sin(a) + Math.pow(Math.cos(b),2); }
* }
* </pre>
@@ -82,9 +82,9 @@ import java.util.Date;
* double v = Math.sin(a) + Math.pow(Math.cos(b),2);
* log.info(v);
* Functions F = Functions.functions;
- * BinaryFunction f = F.chain(F.plus,F.sin,F.chain(F.square,F.cos));
+ * DoubleDoubleFunction f = F.chain(F.plus,F.sin,F.chain(F.square,F.cos));
* log.info(f.apply(a,b));
- * BinaryFunction g = new BinaryFunction() {
+ * DoubleDoubleFunction g = new DoubleDoubleFunction() {
* public double apply(double a, double b) { return Math.sin(a) + Math.pow(Math.cos(b),2); }
* };
* log.info(g.apply(a,b));
@@ -116,35 +116,35 @@ public final class Functions {
* <H3>Unary functions</H3>
*/
/** Function that returns <tt>Math.abs(a)</tt>. */
- public static final UnaryFunction ABS = new UnaryFunction() {
+ public static final DoubleFunction ABS = new DoubleFunction() {
public double apply(double a) {
return Math.abs(a);
}
};
/** Function that returns <tt>Math.acos(a)</tt>. */
- public static final UnaryFunction ACOS = new UnaryFunction() {
+ public static final DoubleFunction ACOS = new DoubleFunction() {
public double apply(double a) {
return Math.acos(a);
}
};
/** Function that returns <tt>Math.asin(a)</tt>. */
- public static final UnaryFunction ASIN = new UnaryFunction() {
+ public static final DoubleFunction ASIN = new DoubleFunction() {
public double apply(double a) {
return Math.asin(a);
}
};
/** Function that returns <tt>Math.atan(a)</tt>. */
- public static final UnaryFunction ATAN = new UnaryFunction() {
+ public static final DoubleFunction ATAN = new DoubleFunction() {
public double apply(double a) {
return Math.atan(a);
}
};
/** Function that returns <tt>Math.ceil(a)</tt>. */
- public static final UnaryFunction CEIL = new UnaryFunction() {
+ public static final DoubleFunction CEIL = new DoubleFunction() {
public double apply(double a) {
return Math.ceil(a);
@@ -152,7 +152,7 @@ public final class Functions {
};
/** Function that returns <tt>Math.cos(a)</tt>. */
- public static final UnaryFunction COS = new UnaryFunction() {
+ public static final DoubleFunction COS = new DoubleFunction() {
public double apply(double a) {
return Math.cos(a);
@@ -160,7 +160,7 @@ public final class Functions {
};
/** Function that returns <tt>Math.exp(a)</tt>. */
- public static final UnaryFunction EXP = new UnaryFunction() {
+ public static final DoubleFunction EXP = new DoubleFunction() {
public double apply(double a) {
return Math.exp(a);
@@ -168,7 +168,7 @@ public final class Functions {
};
/** Function that returns <tt>Math.floor(a)</tt>. */
- public static final UnaryFunction FLOOR = new UnaryFunction() {
+ public static final DoubleFunction FLOOR = new DoubleFunction() {
public double apply(double a) {
return Math.floor(a);
@@ -176,7 +176,7 @@ public final class Functions {
};
/** Function that returns its argument. */
- public static final UnaryFunction IDENTITY = new UnaryFunction() {
+ public static final DoubleFunction IDENTITY = new DoubleFunction() {
public double apply(double a) {
return a;
@@ -184,7 +184,7 @@ public final class Functions {
};
/** Function that returns <tt>1.0 / a</tt>. */
- public static final UnaryFunction INV = new UnaryFunction() {
+ public static final DoubleFunction INV = new DoubleFunction() {
public double apply(double a) {
return 1.0 / a;
@@ -192,7 +192,7 @@ public final class Functions {
};
/** Function that returns <tt>Math.log(a)</tt>. */
- public static final UnaryFunction LOGARITHM = new UnaryFunction() {
+ public static final DoubleFunction LOGARITHM = new DoubleFunction() {
public double apply(double a) {
return Math.log(a);
@@ -200,7 +200,7 @@ public final class Functions {
};
/** Function that returns <tt>Math.log(a) / Math.log(2)</tt>. */
- public static final UnaryFunction LOG2 = new UnaryFunction() {
+ public static final DoubleFunction LOG2 = new DoubleFunction() {
public double apply(double a) {
return Math.log(a) * 1.4426950408889634;
@@ -208,7 +208,7 @@ public final class Functions {
};
/** Function that returns <tt>-a</tt>. */
- public static final UnaryFunction NEGATE = new UnaryFunction() {
+ public static final DoubleFunction NEGATE = new DoubleFunction() {
public double apply(double a) {
return -a;
@@ -216,7 +216,7 @@ public final class Functions {
};
/** Function that returns <tt>Math.rint(a)</tt>. */
- public static final UnaryFunction RINT = new UnaryFunction() {
+ public static final DoubleFunction RINT = new DoubleFunction() {
public double apply(double a) {
return Math.rint(a);
@@ -224,7 +224,7 @@ public final class Functions {
};
/** Function that returns <tt>a < 0 ? -1 : a > 0 ? 1 : 0</tt>. */
- public static final UnaryFunction SIGN = new UnaryFunction() {
+ public static final DoubleFunction SIGN = new DoubleFunction() {
public double apply(double a) {
return a < 0 ? -1 : a > 0 ? 1 : 0;
@@ -232,7 +232,7 @@ public final class Functions {
};
/** Function that returns <tt>Math.sin(a)</tt>. */
- public static final UnaryFunction SIN = new UnaryFunction() {
+ public static final DoubleFunction SIN = new DoubleFunction() {
public double apply(double a) {
return Math.sin(a);
@@ -240,7 +240,7 @@ public final class Functions {
};
/** Function that returns <tt>Math.sqrt(a)</tt>. */
- public static final UnaryFunction SQRT = new UnaryFunction() {
+ public static final DoubleFunction SQRT = new DoubleFunction() {
public double apply(double a) {
return Math.sqrt(a);
@@ -248,7 +248,7 @@ public final class Functions {
};
/** Function that returns <tt>a * a</tt>. */
- public static final UnaryFunction SQUARE = new UnaryFunction() {
+ public static final DoubleFunction SQUARE = new DoubleFunction() {
public double apply(double a) {
return a * a;
@@ -256,7 +256,7 @@ public final class Functions {
};
/** Function that returns <tt>Math.tan(a)</tt>. */
- public static final UnaryFunction TAN = new UnaryFunction() {
+ public static final DoubleFunction TAN = new DoubleFunction() {
public double apply(double a) {
return Math.tan(a);
@@ -269,7 +269,7 @@ public final class Functions {
*/
/** Function that returns <tt>Math.atan2(a,b)</tt>. */
- public static final BinaryFunction ATAN2 = new BinaryFunction() {
+ public static final DoubleDoubleFunction ATAN2 = new DoubleDoubleFunction() {
public double apply(double a, double b) {
return Math.atan2(a, b);
@@ -277,7 +277,7 @@ public final class Functions {
};
/** Function that returns <tt>a < b ? -1 : a > b ? 1 : 0</tt>. */
- public static final BinaryFunction COMPARE = new BinaryFunction() {
+ public static final DoubleDoubleFunction COMPARE = new DoubleDoubleFunction() {
public double apply(double a, double b) {
return a < b ? -1 : a > b ? 1 : 0;
@@ -285,7 +285,7 @@ public final class Functions {
};
/** Function that returns <tt>a / b</tt>. */
- public static final BinaryFunction DIV = new BinaryFunction() {
+ public static final DoubleDoubleFunction DIV = new DoubleDoubleFunction() {
public double apply(double a, double b) {
return a / b;
@@ -293,7 +293,7 @@ public final class Functions {
};
/** Function that returns <tt>a == b ? 1 : 0</tt>. */
- public static final BinaryFunction EQUALS = new BinaryFunction() {
+ public static final DoubleDoubleFunction EQUALS = new DoubleDoubleFunction() {
public double apply(double a, double b) {
return a == b ? 1 : 0;
@@ -301,7 +301,7 @@ public final class Functions {
};
/** Function that returns <tt>a > b ? 1 : 0</tt>. */
- public static final BinaryFunction GREATER = new BinaryFunction() {
+ public static final DoubleDoubleFunction GREATER = new DoubleDoubleFunction() {
public double apply(double a, double b) {
return a > b ? 1 : 0;
@@ -309,7 +309,7 @@ public final class Functions {
};
/** Function that returns <tt>Math.IEEEremainder(a,b)</tt>. */
- public static final BinaryFunction IEEE_REMAINDER = new BinaryFunction() {
+ public static final DoubleDoubleFunction IEEE_REMAINDER = new DoubleDoubleFunction() {
public double apply(double a, double b) {
return Math.IEEEremainder(a, b);
@@ -341,7 +341,7 @@ public final class Functions {
};
/** Function that returns <tt>a < b ? 1 : 0</tt>. */
- public static final BinaryFunction LESS = new BinaryFunction() {
+ public static final DoubleDoubleFunction LESS = new DoubleDoubleFunction() {
public double apply(double a, double b) {
return a < b ? 1 : 0;
@@ -349,7 +349,7 @@ public final class Functions {
};
/** Function that returns <tt>Math.log(a) / Math.log(b)</tt>. */
- public static final BinaryFunction LG = new BinaryFunction() {
+ public static final DoubleDoubleFunction LG = new DoubleDoubleFunction() {
public double apply(double a, double b) {
return Math.log(a) / Math.log(b);
@@ -357,7 +357,7 @@ public final class Functions {
};
/** Function that returns <tt>Math.max(a,b)</tt>. */
- public static final BinaryFunction MAX = new BinaryFunction() {
+ public static final DoubleDoubleFunction MAX = new DoubleDoubleFunction() {
public double apply(double a, double b) {
return Math.max(a, b);
@@ -365,7 +365,7 @@ public final class Functions {
};
/** Function that returns <tt>Math.min(a,b)</tt>. */
- public static final BinaryFunction MIN = new BinaryFunction() {
+ public static final DoubleDoubleFunction MIN = new DoubleDoubleFunction() {
public double apply(double a, double b) {
return Math.min(a, b);
@@ -373,15 +373,15 @@ public final class Functions {
};
/** Function that returns <tt>a - b</tt>. */
- public static final BinaryFunction MINUS = plusMult(-1);
+ public static final DoubleDoubleFunction MINUS = plusMult(-1);
/*
- new BinaryFunction() {
+ new DoubleDoubleFunction() {
public final double apply(double a, double b) { return a - b; }
};
*/
/** Function that returns <tt>a % b</tt>. */
- public static final BinaryFunction MOD = new BinaryFunction() {
+ public static final DoubleDoubleFunction MOD = new DoubleDoubleFunction() {
public double apply(double a, double b) {
return a % b;
@@ -389,7 +389,7 @@ public final class Functions {
};
/** Function that returns <tt>a * b</tt>. */
- public static final BinaryFunction MULT = new BinaryFunction() {
+ public static final DoubleDoubleFunction MULT = new DoubleDoubleFunction() {
public double apply(double a, double b) {
return a * b;
@@ -397,7 +397,7 @@ public final class Functions {
};
/** Function that returns <tt>a + b</tt>. */
- public static final BinaryFunction PLUS = new BinaryFunction() {
+ public static final DoubleDoubleFunction PLUS = new DoubleDoubleFunction() {
public double apply(double a, double b) {
return a + b;
@@ -405,7 +405,7 @@ public final class Functions {
};
/** Function that returns <tt>Math.abs(a) + Math.abs(b)</tt>. */
- public static final BinaryFunction PLUS_ABS = new BinaryFunction() {
+ public static final DoubleDoubleFunction PLUS_ABS = new DoubleDoubleFunction() {
public double apply(double a, double b) {
return Math.abs(a) + Math.abs(b);
@@ -413,7 +413,7 @@ public final class Functions {
};
/** Function that returns <tt>Math.pow(a,b)</tt>. */
- public static final BinaryFunction POW = new BinaryFunction() {
+ public static final DoubleDoubleFunction POW = new DoubleDoubleFunction() {
public double apply(double a, double b) {
return Math.pow(a, b);
@@ -427,8 +427,8 @@ public final class Functions {
* Constructs a function that returns <tt>(from<=a && a<=to) ? 1 : 0</tt>. <tt>a</tt> is a variable, <tt>from</tt> and
* <tt>to</tt> are fixed.
*/
- public static UnaryFunction between(final double from, final double to) {
- return new UnaryFunction() {
+ public static DoubleFunction between(final double from, final double to) {
+ return new DoubleFunction() {
public double apply(double a) {
return (from <= a && a <= to) ? 1 : 0;
@@ -443,8 +443,8 @@ public final class Functions {
* @param function a binary function taking operands in the form <tt>function.apply(c,var)</tt>.
* @return the unary function <tt>function(c,var)</tt>.
*/
- public static UnaryFunction bindArg1(final BinaryFunction function, final double c) {
- return new UnaryFunction() {
+ public static DoubleFunction bindArg1(final DoubleDoubleFunction function, final double c) {
+ return new DoubleFunction() {
public double apply(double var) {
return function.apply(c, var);
@@ -459,8 +459,8 @@ public final class Functions {
* @param function a binary function taking operands in the form <tt>function.apply(var,c)</tt>.
* @return the unary function <tt>function(var,c)</tt>.
*/
- public static UnaryFunction bindArg2(final BinaryFunction function, final double c) {
- return new UnaryFunction() {
+ public static DoubleFunction bindArg2(final DoubleDoubleFunction function, final double c) {
+ return new DoubleFunction() {
public double apply(double var) {
return function.apply(var, c);
@@ -476,9 +476,9 @@ public final class Functions {
* @param h a unary function.
* @return the binary function <tt>f( g(a), h(b) )</tt>.
*/
- public static BinaryFunction chain(final BinaryFunction f, final UnaryFunction g,
- final UnaryFunction h) {
- return new BinaryFunction() {
+ public static DoubleDoubleFunction chain(final DoubleDoubleFunction f, final DoubleFunction g,
+ final DoubleFunction h) {
+ return new DoubleDoubleFunction() {
public double apply(double a, double b) {
return f.apply(g.apply(a), h.apply(b));
@@ -493,8 +493,8 @@ public final class Functions {
* @param h a binary function.
* @return the unary function <tt>g( h(a,b) )</tt>.
*/
- public static BinaryFunction chain(final UnaryFunction g, final BinaryFunction h) {
- return new BinaryFunction() {
+ public static DoubleDoubleFunction chain(final DoubleFunction g, final DoubleDoubleFunction h) {
+ return new DoubleDoubleFunction() {
public double apply(double a, double b) {
return g.apply(h.apply(a, b));
@@ -509,8 +509,8 @@ public final class Functions {
* @param h a unary function.
* @return the unary function <tt>g( h(a) )</tt>.
*/
- public static UnaryFunction chain(final UnaryFunction g, final UnaryFunction h) {
- return new UnaryFunction() {
+ public static DoubleFunction chain(final DoubleFunction g, final DoubleFunction h) {
+ return new DoubleFunction() {
public double apply(double a) {
return g.apply(h.apply(a));
@@ -522,8 +522,8 @@ public final class Functions {
* Constructs a function that returns <tt>a < b ? -1 : a > b ? 1 : 0</tt>. <tt>a</tt> is a variable, <tt>b</tt> is
* fixed.
*/
- public static UnaryFunction compare(final double b) {
- return new UnaryFunction() {
+ public static DoubleFunction compare(final double b) {
+ return new DoubleFunction() {
public double apply(double a) {
return a < b ? -1 : a > b ? 1 : 0;
@@ -532,8 +532,8 @@ public final class Functions {
}
/** Constructs a function that returns the constant <tt>c</tt>. */
- public static UnaryFunction constant(final double c) {
- return new UnaryFunction() {
+ public static DoubleFunction constant(final double c) {
+ return new DoubleFunction() {
public double apply(double a) {
return c;
@@ -543,13 +543,13 @@ public final class Functions {
/** Constructs a function that returns <tt>a / b</tt>. <tt>a</tt> is a variable, <tt>b</tt> is fixed. */
- public static UnaryFunction div(double b) {
+ public static DoubleFunction div(double b) {
return mult(1 / b);
}
/** Constructs a function that returns <tt>a == b ? 1 : 0</tt>. <tt>a</tt> is a variable, <tt>b</tt> is fixed. */
- public static UnaryFunction equals(final double b) {
- return new UnaryFunction() {
+ public static DoubleFunction equals(final double b) {
+ return new DoubleFunction() {
public double apply(double a) {
return a == b ? 1 : 0;
@@ -558,8 +558,8 @@ public final class Functions {
}
/** Constructs a function that returns <tt>a > b ? 1 : 0</tt>. <tt>a</tt> is a variable, <tt>b</tt> is fixed. */
- public static UnaryFunction greater(final double b) {
- return new UnaryFunction() {
+ public static DoubleFunction greater(final double b) {
+ return new DoubleFunction() {
public double apply(double a) {
return a > b ? 1 : 0;
@@ -571,8 +571,8 @@ public final class Functions {
* Constructs a function that returns <tt>Math.IEEEremainder(a,b)</tt>. <tt>a</tt> is a variable, <tt>b</tt> is
* fixed.
*/
- public static UnaryFunction IEEEremainder(final double b) {
- return new UnaryFunction() {
+ public static DoubleFunction IEEEremainder(final double b) {
+ return new DoubleFunction() {
public double apply(double a) {
return Math.IEEEremainder(a, b);
@@ -624,8 +624,8 @@ public final class Functions {
}
/** Constructs a function that returns <tt>a < b ? 1 : 0</tt>. <tt>a</tt> is a variable, <tt>b</tt> is fixed. */
- public static UnaryFunction less(final double b) {
- return new UnaryFunction() {
+ public static DoubleFunction less(final double b) {
+ return new DoubleFunction() {
public double apply(double a) {
return a < b ? 1 : 0;
@@ -637,8 +637,8 @@ public final class Functions {
* Constructs a function that returns <tt><tt>Math.log(a) / Math.log(b)</tt></tt>. <tt>a</tt> is a variable,
* <tt>b</tt> is fixed.
*/
- public static UnaryFunction lg(final double b) {
- return new UnaryFunction() {
+ public static DoubleFunction lg(final double b) {
+ return new DoubleFunction() {
private final double logInv = 1 / Math.log(b); // cached for speed
@@ -649,8 +649,8 @@ public final class Functions {
}
/** Constructs a function that returns <tt>Math.max(a,b)</tt>. <tt>a</tt> is a variable, <tt>b</tt> is fixed. */
- public static UnaryFunction max(final double b) {
- return new UnaryFunction() {
+ public static DoubleFunction max(final double b) {
+ return new DoubleFunction() {
public double apply(double a) {
return Math.max(a, b);
@@ -659,8 +659,8 @@ public final class Functions {
}
/** Constructs a function that returns <tt>Math.min(a,b)</tt>. <tt>a</tt> is a variable, <tt>b</tt> is fixed. */
- public static UnaryFunction min(final double b) {
- return new UnaryFunction() {
+ public static DoubleFunction min(final double b) {
+ return new DoubleFunction() {
public double apply(double a) {
return Math.min(a, b);
@@ -669,7 +669,7 @@ public final class Functions {
}
/** Constructs a function that returns <tt>a - b</tt>. <tt>a</tt> is a variable, <tt>b</tt> is fixed. */
- public static UnaryFunction minus(double b) {
+ public static DoubleFunction minus(double b) {
return plus(-b);
}
@@ -677,13 +677,13 @@ public final class Functions {
* Constructs a function that returns <tt>a - b*constant</tt>. <tt>a</tt> and <tt>b</tt> are variables,
* <tt>constant</tt> is fixed.
*/
- public static BinaryFunction minusMult(double constant) {
+ public static DoubleDoubleFunction minusMult(double constant) {
return plusMult(-constant);
}
/** Constructs a function that returns <tt>a % b</tt>. <tt>a</tt> is a variable, <tt>b</tt> is fixed. */
- public static UnaryFunction mod(final double b) {
- return new UnaryFunction() {
+ public static DoubleFunction mod(final double b) {
+ return new DoubleFunction() {
public double apply(double a) {
return a % b;
@@ -692,18 +692,18 @@ public final class Functions {
}
/** Constructs a function that returns <tt>a * b</tt>. <tt>a</tt> is a variable, <tt>b</tt> is fixed. */
- public static UnaryFunction mult(double b) {
+ public static DoubleFunction mult(double b) {
return new Mult(b);
/*
- return new UnaryFunction() {
+ return new DoubleFunction() {
public final double apply(double a) { return a * b; }
};
*/
}
/** Constructs a function that returns <tt>a + b</tt>. <tt>a</tt> is a variable, <tt>b</tt> is fixed. */
- public static UnaryFunction plus(final double b) {
- return new UnaryFunction() {
+ public static DoubleFunction plus(final double b) {
+ return new DoubleFunction() {
public double apply(double a) {
return a + b;
@@ -715,18 +715,18 @@ public final class Functions {
* Constructs a function that returns <tt>a + b*constant</tt>. <tt>a</tt> and <tt>b</tt> are variables,
* <tt>constant</tt> is fixed.
*/
- public static BinaryFunction plusMult(double constant) {
+ public static DoubleDoubleFunction plusMult(double constant) {
return new PlusMult(constant);
/*
- return new BinaryFunction() {
+ return new DoubleDoubleFunction() {
public final double apply(double a, double b) { return a + b*constant; }
};
*/
}
/** Constructs a function that returns <tt>Math.pow(a,b)</tt>. <tt>a</tt> is a variable, <tt>b</tt> is fixed. */
- public static UnaryFunction pow(final double b) {
- return new UnaryFunction() {
+ public static DoubleFunction pow(final double b) {
+ return new DoubleFunction() {
public double apply(double a) {
return Math.pow(a, b);
@@ -743,7 +743,7 @@ public final class Functions {
* interfaces. Thus, if you are not happy with the default, just pass your favourite random generator to function
* evaluating methods.
*/
- public static UnaryFunction random() {
+ public static DoubleFunction random() {
return new MersenneTwister(new Date());
}
@@ -755,8 +755,8 @@ public final class Functions {
* precision = 10 rounds 123 --> 120 , 127 --> 130
* </pre>
*/
- public static UnaryFunction round(final double precision) {
- return new UnaryFunction() {
+ public static DoubleFunction round(final double precision) {
+ return new DoubleFunction() {
public double apply(double a) {
return Math.rint(a / precision) * precision;
}
@@ -770,8 +770,8 @@ public final class Functions {
* @param function a function taking operands in the form <tt>function.apply(a,b)</tt>.
* @return the binary function <tt>function(b,a)</tt>.
*/
- public static BinaryFunction swapArgs(final BinaryFunction function) {
- return new BinaryFunction() {
+ public static DoubleDoubleFunction swapArgs(final DoubleDoubleFunction function) {
+ return new DoubleDoubleFunction() {
public double apply(double a, double b) {
return function.apply(b, a);
}
Modified: mahout/trunk/math/src/main/java/org/apache/mahout/math/function/Mult.java
URL: http://svn.apache.org/viewvc/mahout/trunk/math/src/main/java/org/apache/mahout/math/function/Mult.java?rev=1056703&r1=1056702&r2=1056703&view=diff
==============================================================================
--- mahout/trunk/math/src/main/java/org/apache/mahout/math/function/Mult.java (original)
+++ mahout/trunk/math/src/main/java/org/apache/mahout/math/function/Mult.java Sat Jan 8 13:49:07 2011
@@ -37,7 +37,7 @@ package org.apache.mahout.math.function;
* Intended to be passed to <tt>matrix.assign(function)</tt> methods.
*/
-public final class Mult implements UnaryFunction {
+public final class Mult implements DoubleFunction {
private double multiplicator;
Modified: mahout/trunk/math/src/main/java/org/apache/mahout/math/function/PlusMult.java
URL: http://svn.apache.org/viewvc/mahout/trunk/math/src/main/java/org/apache/mahout/math/function/PlusMult.java?rev=1056703&r1=1056702&r2=1056703&view=diff
==============================================================================
--- mahout/trunk/math/src/main/java/org/apache/mahout/math/function/PlusMult.java (original)
+++ mahout/trunk/math/src/main/java/org/apache/mahout/math/function/PlusMult.java Sat Jan 8 13:49:07 2011
@@ -39,7 +39,7 @@ package org.apache.mahout.math.function;
* Intended to be passed to <tt>matrix.assign(otherMatrix,function)</tt> methods.
*/
-public final class PlusMult implements BinaryFunction {
+public final class PlusMult implements DoubleDoubleFunction {
private double multiplicator;
@@ -52,21 +52,11 @@ public final class PlusMult implements B
return a + b * multiplicator;
}
- /** <tt>a - b/constant</tt>. */
- public static PlusMult minusDiv(double constant) {
- return new PlusMult(-1 / constant);
- }
-
/** <tt>a - b*constant</tt>. */
public static PlusMult minusMult(double constant) {
return new PlusMult(-constant);
}
- /** <tt>a + b/constant</tt>. */
- public static PlusMult plusDiv(double constant) {
- return new PlusMult(1 / constant);
- }
-
/** <tt>a + b*constant</tt>. */
public static PlusMult plusMult(double constant) {
return new PlusMult(constant);
Modified: mahout/trunk/math/src/main/java/org/apache/mahout/math/function/SquareRootFunction.java
URL: http://svn.apache.org/viewvc/mahout/trunk/math/src/main/java/org/apache/mahout/math/function/SquareRootFunction.java?rev=1056703&r1=1056702&r2=1056703&view=diff
==============================================================================
--- mahout/trunk/math/src/main/java/org/apache/mahout/math/function/SquareRootFunction.java (original)
+++ mahout/trunk/math/src/main/java/org/apache/mahout/math/function/SquareRootFunction.java Sat Jan 8 13:49:07 2011
@@ -17,7 +17,7 @@
package org.apache.mahout.math.function;
-public class SquareRootFunction implements UnaryFunction {
+public final class SquareRootFunction implements DoubleFunction {
public double apply(double arg1) {
return Math.sqrt(arg1);
Modified: mahout/trunk/math/src/main/java/org/apache/mahout/math/function/TimesFunction.java
URL: http://svn.apache.org/viewvc/mahout/trunk/math/src/main/java/org/apache/mahout/math/function/TimesFunction.java?rev=1056703&r1=1056702&r2=1056703&view=diff
==============================================================================
--- mahout/trunk/math/src/main/java/org/apache/mahout/math/function/TimesFunction.java (original)
+++ mahout/trunk/math/src/main/java/org/apache/mahout/math/function/TimesFunction.java Sat Jan 8 13:49:07 2011
@@ -16,7 +16,7 @@
*/
package org.apache.mahout.math.function;
-public class TimesFunction implements BinaryFunction {
+public final class TimesFunction implements DoubleDoubleFunction {
/**
* Computes the product of two numbers.
Modified: mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/math/Arithmetic.java
URL: http://svn.apache.org/viewvc/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/math/Arithmetic.java?rev=1056703&r1=1056702&r2=1056703&view=diff
==============================================================================
--- mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/math/Arithmetic.java (original)
+++ mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/math/Arithmetic.java Sat Jan 8 13:49:07 2011
@@ -29,26 +29,7 @@ package org.apache.mahout.math.jet.math;
/**
* Arithmetic functions.
*/
-public class Arithmetic extends Constants {
- // for method stirlingCorrection(...)
- private static final double[] STIRLING_CORRECTION_TABLE = {
- 0.0,
- 8.106146679532726e-02, 4.134069595540929e-02,
- 2.767792568499834e-02, 2.079067210376509e-02,
- 1.664469118982119e-02, 1.387612882307075e-02,
- 1.189670994589177e-02, 1.041126526197209e-02,
- 9.255462182712733e-03, 8.330563433362871e-03,
- 7.573675487951841e-03, 6.942840107209530e-03,
- 6.408994188004207e-03, 5.951370112758848e-03,
- 5.554733551962801e-03, 5.207655919609640e-03,
- 4.901395948434738e-03, 4.629153749334029e-03,
- 4.385560249232324e-03, 4.166319691996922e-03,
- 3.967954218640860e-03, 3.787618068444430e-03,
- 3.622960224683090e-03, 3.472021382978770e-03,
- 3.333155636728090e-03, 3.204970228055040e-03,
- 3.086278682608780e-03, 2.976063983550410e-03,
- 2.873449362352470e-03, 2.777674929752690e-03,
- };
+public final class Arithmetic {
// for method logFactorial(...)
// log(k!) for k = 0, ..., 29
@@ -244,37 +225,7 @@ public class Arithmetic extends Constant
7.257415615308004E306
};
- /** Makes this class non instantiable, but still let's others inherit from it. */
- protected Arithmetic() {
- }
-
- /**
- * Efficiently returns the binomial coefficient, often also referred to as "n over k" or "n choose k". The binomial
- * coefficient is defined as <tt>(n * n-1 * ... * n-k+1 ) / ( 1 * 2 * ... * k )</tt>. <ul> <li>k<0<tt>: <tt>0</tt>.
- * <li>k==0<tt>: <tt>1</tt>. <li>k==1<tt>: <tt>n</tt>. <li>else: <tt>(n * n-1 * ... * n-k+1 ) / ( 1 * 2 * ... * k
- * )</tt>. </ul>
- *
- * @return the binomial coefficient.
- */
- public static double binomial(double n, long k) {
- if (k < 0) {
- return 0;
- }
- if (k == 0) {
- return 1;
- }
- if (k == 1) {
- return n;
- }
-
- // binomial(n,k) = (n * n-1 * ... * n-k+1 ) / ( 1 * 2 * ... * k )
- double a = n - k + 1;
- double b = 1;
- double binomial = 1;
- for (long i = k; i-- > 0;) {
- binomial *= (a++) / (b++);
- }
- return binomial;
+ private Arithmetic() {
}
/**
@@ -325,53 +276,6 @@ public class Arithmetic extends Constant
}
/**
- * Returns the smallest <code>long >= value</code>. <dt>Examples: <code>1.0 -> 1, 1.2 -> 2, 1.9 -> 2</code>. This
- * method is safer than using (long) Math.ceil(value), because of possible rounding error.
- */
- public static long ceil(double value) {
- return Math.round(Math.ceil(value));
- }
-
- /**
- * Evaluates the series of Chebyshev polynomials Ti at argument x/2. The series is given by
- * <pre>
- * N-1
- * - '
- * y = > coef[i] T (x/2)
- * - i
- * i=0
- * </pre>
- * Coefficients are stored in reverse order, i.e. the zero order term is last in the array. Note N is the number of
- * coefficients, not the order. <p> If coefficients are for the interval a to b, x must have been transformed to x ->
- * 2(2x - b - a)/(b-a) before entering the routine. This maps x from (a, b) to (-1, 1), over which the Chebyshev
- * polynomials are defined. <p> If the coefficients are for the inverted interval, in which (a, b) is mapped to (1/b,
- * 1/a), the transformation required is x -> 2(2ab/x - b - a)/(b-a). If b is infinity, this becomes x -> 4a/x - 1.
- * <p> SPEED: <p> Taking advantage of the recurrence properties of the Chebyshev polynomials, the routine requires one
- * more addition per loop than evaluating a nested polynomial of the same degree.
- *
- * @param x argument to the polynomial.
- * @param coef the coefficients of the polynomial.
- * @param n the number of coefficients.
- */
- public static double chbevl(double x, double[] coef, int n) {
-
- int p = 0;
-
- double b0 = coef[p++];
- double b1 = 0.0;
- int i = n - 1;
-
- double b2;
- do {
- b2 = b1;
- b1 = b0;
- b0 = x * b1 - b2 + coef[p++];
- } while (--i > 0);
-
- return 0.5 * (b0 - b2);
- }
-
- /**
* Instantly returns the factorial <tt>k!</tt>.
*
* @param k must hold <tt>k >= 0</tt>.
@@ -395,32 +299,6 @@ public class Arithmetic extends Constant
}
/**
- * Returns the largest <code>long <= value</code>. <dt>Examples: <code> 1.0 -> 1, 1.2 -> 1, 1.9 -> 1 <dt> 2.0 -> 2,
- * 2.2 -> 2, 2.9 -> 2 </code><dt> This method is safer than using (long) Math.floor(value), because of possible
- * rounding error.
- */
- public static long floor(double value) {
- return Math.round(Math.floor(value));
- }
-
- /** Returns <tt>log<sub>base</sub>value</tt>. */
- public static double log(double base, double value) {
- return Math.log(value) / Math.log(base);
- }
-
- /** Returns <tt>log<sub>10</sub>value</tt>. */
- public static double log10(double value) {
- // 1.0 / Math.log(10) == 0.43429448190325176
- return Math.log(value) * 0.43429448190325176;
- }
-
- /** Returns <tt>log<sub>2</sub>value</tt>. */
- public static double log2(double value) {
- // 1.0 / Math.log(2) == 1.4426950408889634
- return Math.log(value) * 1.4426950408889634;
- }
-
- /**
* Returns <tt>log(k!)</tt>. Tries to avoid overflows. For <tt>k<30</tt> simply looks up a table in O(1). For
* <tt>k>=30</tt> uses stirlings approximation.
*
@@ -442,41 +320,4 @@ public class Arithmetic extends Constant
}
}
- /**
- * Instantly returns the factorial <tt>k!</tt>.
- *
- * @param k must hold <tt>k >= 0 && k < 21</tt>.
- */
- public static long longFactorial(int k) {
- if (k < 0) {
- throw new IllegalArgumentException("Negative k");
- }
-
- if (k < FACTORIAL_TABLE.length) {
- return FACTORIAL_TABLE[k];
- }
- throw new IllegalArgumentException("Overflow");
- }
-
- /**
- * Returns the StirlingCorrection. <p> Correction term of the Stirling approximation for <tt>log(k!)</tt> (series in
- * 1/k, or table values for small k) with int parameter k. <p> <tt> log k! = (k + 1/2)log(k + 1) - (k + 1) +
- * (1/2)log(2Pi) + stirlingCorrection(k + 1) <p> log k! = (k + 1/2)log(k) - k + (1/2)log(2Pi) +
- * stirlingCorrection(k) </tt>
- */
- public static double stirlingCorrection(int k) {
-
- if (k > 30) {
- double r = 1.0 / (double) k;
- double rr = r * r;
- double c7 = -5.95238095238095238e-04; // -1/1680
- double c5 = 7.93650793650793651e-04; // +1/1260
- double c3 = -2.77777777777777778e-03; // -1/360
- double c1 = 8.33333333333333333e-02; // +1/12
- return r * (c1 + rr * (c3 + rr * (c5 + rr * c7)));
- } else {
- return STIRLING_CORRECTION_TABLE[k];
- }
- }
-
}
Modified: mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/math/Constants.java
URL: http://svn.apache.org/viewvc/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/math/Constants.java?rev=1056703&r1=1056702&r2=1056703&view=diff
==============================================================================
--- mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/math/Constants.java (original)
+++ mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/math/Constants.java Sat Jan 8 13:49:07 2011
@@ -30,21 +30,18 @@ package org.apache.mahout.math.jet.math;
/**
* Defines some useful constants.
*/
-public class Constants {
- /*
- * machine constants
- */
- protected static final double MACHEP = 1.11022302462515654042E-16;
- protected static final double MAXLOG = 7.09782712893383996732E2;
- protected static final double MINLOG = -7.451332191019412076235E2;
- protected static final double MAXGAM = 171.624376956302725;
- protected static final double SQTPI = 2.50662827463100050242E0;
- protected static final double SQRTH = 7.07106781186547524401E-1;
- protected static final double LOGPI = 1.14472988584940017414;
+public final class Constants {
- protected static final double BIG = 4.503599627370496e15;
- protected static final double BIG_INVERSE = 2.22044604925031308085e-16;
+ public static final double MACHEP = 1.11022302462515654042E-16;
+ public static final double MAXLOG = 7.09782712893383996732E2;
+ public static final double MINLOG = -7.451332191019412076235E2;
+ public static final double MAXGAM = 171.624376956302725;
+ public static final double SQTPI = 2.50662827463100050242E0;
+ public static final double LOGPI = 1.14472988584940017414;
- protected Constants() {
+ public static final double BIG = 4.503599627370496e15;
+ public static final double BIG_INVERSE = 2.22044604925031308085e-16;
+
+ private Constants() {
}
}
Modified: mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/math/Polynomial.java
URL: http://svn.apache.org/viewvc/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/math/Polynomial.java?rev=1056703&r1=1056702&r2=1056703&view=diff
==============================================================================
--- mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/math/Polynomial.java (original)
+++ mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/math/Polynomial.java Sat Jan 8 13:49:07 2011
@@ -28,11 +28,9 @@ package org.apache.mahout.math.jet.math;
/**
* Polynomial functions.
*/
+public final class Polynomial {
-public class Polynomial extends Constants {
-
- /** Makes this class non instantiable, but still let's others inherit from it. */
- protected Polynomial() {
+ private Polynomial() {
}
/**
Modified: mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/AbstractDistribution.java
URL: http://svn.apache.org/viewvc/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/AbstractDistribution.java?rev=1056703&r1=1056702&r2=1056703&view=diff
==============================================================================
--- mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/AbstractDistribution.java (original)
+++ mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/AbstractDistribution.java Sat Jan 8 13:49:07 2011
@@ -25,13 +25,12 @@ It is provided "as is" without expressed
*/
package org.apache.mahout.math.jet.random;
-import org.apache.mahout.math.PersistentObject;
-import org.apache.mahout.math.function.UnaryFunction;
+import org.apache.mahout.math.function.DoubleFunction;
import org.apache.mahout.math.function.IntFunction;
import java.util.Random;
-public abstract class AbstractDistribution extends PersistentObject implements UnaryFunction, IntFunction {
+public abstract class AbstractDistribution implements DoubleFunction, IntFunction {
protected Random randomGenerator;
/** Makes this class non instantiable, but still let's others inherit from it. */
@@ -68,14 +67,6 @@ public abstract class AbstractDistributi
public abstract int nextInt();
/**
- * Returns the used uniform random number generator;
- * @return The current PRNG underlying this distribution.
- */
- public Random getRandomGenerator() {
- return randomGenerator;
- }
-
- /**
* Sets the uniform random generator internally used.
* @param randomGenerator the new PRNG
*/
Modified: mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/Gamma.java
URL: http://svn.apache.org/viewvc/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/Gamma.java?rev=1056703&r1=1056702&r2=1056703&view=diff
==============================================================================
--- mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/Gamma.java (original)
+++ mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/Gamma.java Sat Jan 8 13:49:07 2011
@@ -265,10 +265,39 @@ public class Gamma extends AbstractConti
if (alpha == 1.0) {
return rate * Math.exp(-x * rate);
}
- return rate * Math.exp((alpha - 1.0) * Math.log(x * rate) - x * rate - Fun.logGamma(alpha));
+ return rate * Math.exp((alpha - 1.0) * Math.log(x * rate) - x * rate - logGamma(alpha));
}
public String toString() {
return this.getClass().getName() + '(' + rate + ',' + alpha + ')';
}
+
+ /** Returns a quick approximation of <tt>log(gamma(x))</tt>. */
+ public static double logGamma(double x) {
+
+ if (x <= 0.0 /* || x > 1.3e19 */) {
+ return -999;
+ }
+
+ double z;
+ for (z = 1.0; x < 11.0; x++) {
+ z *= x;
+ }
+
+ double r = 1.0 / (x * x);
+ double c6 = -1.9175269175269175e-03;
+ double c5 = 8.4175084175084175e-04;
+ double c4 = -5.9523809523809524e-04;
+ double c3 = 7.9365079365079365e-04;
+ double c2 = -2.7777777777777777e-03;
+ double c1 = 8.3333333333333333e-02;
+ double g = c1 + r * (c2 + r * (c3 + r * (c4 + r * (c5 + r + c6))));
+ double c0 = 9.1893853320467274e-01;
+ g = (x - 0.5) * Math.log(x) - x + c0 + g / x;
+ if (z == 1.0) {
+ return g;
+ }
+ return g - Math.log(z);
+ }
+
}
Modified: mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/NegativeBinomial.java
URL: http://svn.apache.org/viewvc/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/NegativeBinomial.java?rev=1056703&r1=1056702&r2=1056703&view=diff
==============================================================================
--- mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/NegativeBinomial.java (original)
+++ mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/NegativeBinomial.java Sat Jan 8 13:49:07 2011
@@ -31,13 +31,13 @@ import org.apache.mahout.math.jet.stat.P
import java.util.Random;
/** Mostly deprecated until unit tests are in place. Until this time, this class/interface is unsupported. */
-public class NegativeBinomial extends AbstractDiscreteDistribution {
+public final class NegativeBinomial extends AbstractDiscreteDistribution {
- private int r;
- private double p;
+ private final int r;
+ private final double p;
- private Gamma gamma;
- private Poisson poisson;
+ private final Gamma gamma;
+ private final Poisson poisson;
/**
* Constructs a Negative Binomial distribution which describes the probability of getting
@@ -70,27 +70,6 @@ public class NegativeBinomial extends Ab
return Arithmetic.binomial(k + r - 1, r - 1) * Math.pow(p, r) * Math.pow(1.0 - p, k);
}
- /**
- * Returns a deep copy of the receiver; the copy will produce identical sequences. After this call has returned, the
- * copy and the receiver have equal but separate state.
- *
- * @return a copy of the receiver.
- */
- @Deprecated
- @Override
- public Object clone() {
- NegativeBinomial copy = (NegativeBinomial) super.clone();
- if (this.poisson != null) {
- copy.poisson = (Poisson) this.poisson.clone();
- }
- copy.poisson.setRandomGenerator(copy.getRandomGenerator());
- if (this.gamma != null) {
- copy.gamma = (Gamma) this.gamma.clone();
- }
- copy.gamma.setRandomGenerator(copy.getRandomGenerator());
- return copy;
- }
-
@Override
public int nextInt() {
return nextInt(r, p);
@@ -117,17 +96,6 @@ public class NegativeBinomial extends Ab
}
/**
- * Sets the parameters number of trials and the probability of success.
- *
- * @param r the number of trials
- * @param p the probability of success.
- */
- public void setRandP(int r, double p) {
- this.r = r;
- this.p = p;
- }
-
- /**
* Returns a String representation of the receiver.
*/
public String toString() {
Modified: mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/Poisson.java
URL: http://svn.apache.org/viewvc/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/Poisson.java?rev=1056703&r1=1056702&r2=1056703&view=diff
==============================================================================
--- mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/Poisson.java (original)
+++ mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/Poisson.java Sat Jan 8 13:49:07 2011
@@ -9,14 +9,13 @@ It is provided "as is" without expressed
package org.apache.mahout.math.jet.random;
import org.apache.mahout.math.jet.math.Arithmetic;
-import org.apache.mahout.math.jet.stat.Probability;
import java.util.Random;
/** Partially deprecated until unit tests are in place. Until this time, this class/interface is unsupported. */
-public class Poisson extends AbstractDiscreteDistribution {
+public final class Poisson extends AbstractDiscreteDistribution {
- private double mean;
+ private final double mean;
// precomputed and cached values (for performance only)
// cache for < SWITCH_MEAN
@@ -24,7 +23,7 @@ public class Poisson extends AbstractDis
private double p;
private double q;
private double p0;
- private double[] pp = new double[36];
+ private final double[] pp = new double[36];
private int llll;
// cache for >= SWITCH_MEAN
@@ -68,28 +67,6 @@ public class Poisson extends AbstractDis
this.mean = mean;
}
- /** Returns the cumulative distribution function. */
- @Deprecated
- public double cdf(int k) {
- return Probability.poisson(k, this.mean);
- }
-
- /**
- * Returns a deep copy of the receiver; the copy will produce identical sequences. After this call has returned, the
- * copy and the receiver have equal but separate state.
- *
- * @return a copy of the receiver.
- */
- @Override
- @Deprecated
- public Object clone() {
- Poisson copy = (Poisson) super.clone();
- if (this.pp != null) {
- copy.pp = this.pp.clone();
- }
- return copy;
- }
-
private static double f(int k, double lNu, double cPm) {
return Math.exp(k * lNu - Arithmetic.logFactorial(k) - cPm);
}
@@ -99,13 +76,6 @@ public class Poisson extends AbstractDis
return nextInt(mean);
}
- /** Returns a random number from the distribution.
- @Override
- public int nextInt() {
- return nextInt(this.mean);
- }
-
-
/** Returns a random number from the distribution; bypasses the internal state. */
public int nextInt(double theMean) {
/******************************************************************
@@ -323,25 +293,4 @@ public class Poisson extends AbstractDis
}
}
- /** Returns the probability distribution function. */
- @Deprecated
- public double pdf(int k) {
- return Math.exp(k * Math.log(this.mean) - Arithmetic.logFactorial(k) - this.mean);
-
- // Overflow sensitive:
- // return (Math.pow(mean,k) / cephes.Arithmetic.factorial(k)) * Math.exp(-this.mean);
- }
-
- /** Sets the mean. */
- @Deprecated
- public void setMean(double mean) {
- this.mean = mean;
- }
-
- /** Returns a String representation of the receiver. */
- @Deprecated
- public String toString() {
- return this.getClass().getName() + '(' + mean + ')';
- }
-
}
Modified: mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/Uniform.java
URL: http://svn.apache.org/viewvc/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/Uniform.java?rev=1056703&r1=1056702&r2=1056703&view=diff
==============================================================================
--- mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/Uniform.java (original)
+++ mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/Uniform.java Sat Jan 8 13:49:07 2011
@@ -17,9 +17,6 @@ public class Uniform extends AbstractCon
private double min;
private double max;
- // The uniform random number generated shared by all <b>static</b> methods.
- protected static final Uniform shared = new Uniform(RandomUtils.getRandom());
-
/**
* Constructs a uniform distribution with the given minimum and maximum, using a {@link
* org.apache.mahout.math.jet.random.engine.MersenneTwister} seeded with the given seed.
@@ -158,62 +155,6 @@ public class Uniform extends AbstractCon
this.max = max;
}
- /** Returns a uniformly distributed random <tt>boolean</tt>. */
- public static boolean staticNextBoolean() {
- synchronized (shared) {
- return shared.nextBoolean();
- }
- }
-
- /**
- * Returns a uniformly distributed random number in the open interval <tt>(0,1)</tt> (excluding <tt>0</tt> and
- * <tt>1</tt>).
- */
- public static double staticNextDouble() {
- synchronized (shared) {
- return shared.nextDouble();
- }
- }
-
- /**
- * Returns a uniformly distributed random number in the open interval <tt>(from,to)</tt> (excluding <tt>from</tt> and
- * <tt>to</tt>). Pre conditions: <tt>from <= to</tt>.
- */
- public static double staticNextDoubleFromTo(double from, double to) {
- synchronized (shared) {
- return shared.nextDoubleFromTo(from, to);
- }
- }
-
- /**
- * Returns a uniformly distributed random number in the open interval <tt>(from,to)</tt> (excluding <tt>from</tt> and
- * <tt>to</tt>). Pre conditions: <tt>from <= to</tt>.
- */
- public static float staticNextFloatFromTo(float from, float to) {
- synchronized (shared) {
- return shared.nextFloatFromTo(from, to);
- }
- }
-
- /**
- * Returns a uniformly distributed random number in the closed interval <tt>[from,to]</tt> (including <tt>from</tt>
- * and <tt>to</tt>). Pre conditions: <tt>from <= to</tt>.
- */
- public static int staticNextIntFromTo(int from, int to) {
- synchronized (shared) {
- return shared.nextIntFromTo(from, to);
- }
- }
-
- /**
- * Returns a uniformly distributed random number in the closed interval <tt>[from,to]</tt> (including <tt>from</tt>
- * and <tt>to</tt>). Pre conditions: <tt>from <= to</tt>.
- */
- public static long staticNextLongFromTo(long from, long to) {
- synchronized (shared) {
- return shared.nextLongFromTo(from, to);
- }
- }
/** Returns a String representation of the receiver. */
public String toString() {
Modified: mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/engine/MersenneTwister.java
URL: http://svn.apache.org/viewvc/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/engine/MersenneTwister.java?rev=1056703&r1=1056702&r2=1056703&view=diff
==============================================================================
--- mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/engine/MersenneTwister.java (original)
+++ mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/engine/MersenneTwister.java Sat Jan 8 13:49:07 2011
@@ -165,7 +165,7 @@ public final class MersenneTwister exten
private static final int DEFAULT_SEED = 4357;
private int mti;
- private int[] mt = new int[N]; /* set initial seeds: N = 624 words */
+ private final int[] mt = new int[N]; /* set initial seeds: N = 624 words */
/**
* Constructs and returns a random number generator with a default seed, which is a <b>constant</b>. Thus using this
@@ -192,19 +192,6 @@ public final class MersenneTwister exten
this((int) d.getTime());
}
- /**
- * Returns a copy of the receiver; the copy will produce identical sequences. After this call has returned, the copy
- * and the receiver have equal but separate state.
- *
- * @return a copy of the receiver.
- */
- @Override
- public Object clone() {
- MersenneTwister clone = (MersenneTwister) super.clone();
- clone.mt = this.mt.clone();
- return clone;
- }
-
/** Generates N words at one time */
void nextBlock() {
int y;
Modified: mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/engine/RandomEngine.java
URL: http://svn.apache.org/viewvc/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/engine/RandomEngine.java?rev=1056703&r1=1056702&r2=1056703&view=diff
==============================================================================
--- mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/engine/RandomEngine.java (original)
+++ mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/engine/RandomEngine.java Sat Jan 8 13:49:07 2011
@@ -26,8 +26,7 @@ It is provided "as is" without expressed
*/
package org.apache.mahout.math.jet.random.engine;
-import org.apache.mahout.math.PersistentObject;
-import org.apache.mahout.math.function.UnaryFunction;
+import org.apache.mahout.math.function.DoubleFunction;
import org.apache.mahout.math.function.IntFunction;
/**
@@ -55,7 +54,7 @@ import org.apache.mahout.math.function.I
* @see MersenneTwister
* @see java.util.Random
*/
-public abstract class RandomEngine extends PersistentObject implements UnaryFunction, IntFunction {
+public abstract class RandomEngine implements DoubleFunction, IntFunction {
/**
* Equivalent to <tt>raw()</tt>. This has the effect that random engines can now be used as function objects,
@@ -74,14 +73,6 @@ public abstract class RandomEngine exten
}
/**
- * @return a new uniform random number engine seeded with the current time. Currently this is {@link
- * org.apache.mahout.math.jet.random.engine.MersenneTwister}.
- */
- public static RandomEngine makeDefault() {
- return new MersenneTwister((int) System.currentTimeMillis());
- }
-
- /**
* @return a 64 bit uniformly distributed random number in the open unit interval <code>(0.0,1.0)</code> (excluding
* 0.0 and 1.0).
*/
Modified: mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/sampling/RandomSampler.java
URL: http://svn.apache.org/viewvc/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/sampling/RandomSampler.java?rev=1056703&r1=1056702&r2=1056703&view=diff
==============================================================================
--- mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/sampling/RandomSampler.java (original)
+++ mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/sampling/RandomSampler.java Sat Jan 8 13:49:07 2011
@@ -9,7 +9,6 @@ It is provided "as is" without expressed
package org.apache.mahout.math.jet.random.sampling;
import org.apache.mahout.common.RandomUtils;
-import org.apache.mahout.math.PersistentObject;
import java.util.Random;
@@ -128,7 +127,7 @@ import java.util.Random;
/** @deprecated until unit tests are in place. Until this time, this class/interface is unsupported. */
@Deprecated
-public class RandomSampler extends PersistentObject {
+public class RandomSampler {
//public class RandomSampler extends Object implements java.io.Serializable {
private long n;
Modified: mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/sampling/RandomSamplingAssistant.java
URL: http://svn.apache.org/viewvc/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/sampling/RandomSamplingAssistant.java?rev=1056703&r1=1056702&r2=1056703&view=diff
==============================================================================
--- mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/sampling/RandomSamplingAssistant.java (original)
+++ mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/random/sampling/RandomSamplingAssistant.java Sat Jan 8 13:49:07 2011
@@ -8,18 +8,16 @@ It is provided "as is" without expressed
*/
package org.apache.mahout.math.jet.random.sampling;
-import org.apache.mahout.math.PersistentObject;
-
import java.util.Random;
/** @deprecated until unit tests are in place. Until this time, this class/interface is unsupported. */
@Deprecated
-public final class RandomSamplingAssistant extends PersistentObject {
+public final class RandomSamplingAssistant {
private static final int MAX_BUFFER_SIZE = 200;
//public class RandomSamplingAssistant extends Object implements java.io.Serializable {
- private RandomSampler sampler;
+ private final RandomSampler sampler;
private final long[] buffer;
private int bufferPosition;
@@ -45,14 +43,6 @@ public final class RandomSamplingAssista
fetchNextBlock();
}
- /** Returns a deep copy of the receiver. */
- @Override
- public Object clone() {
- RandomSamplingAssistant copy = (RandomSamplingAssistant) super.clone();
- copy.sampler = (RandomSampler) this.sampler.clone();
- return copy;
- }
-
/** Not yet commented. */
void fetchNextBlock() {
if (n > 0) {
Modified: mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/stat/Gamma.java
URL: http://svn.apache.org/viewvc/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/stat/Gamma.java?rev=1056703&r1=1056702&r2=1056703&view=diff
==============================================================================
--- mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/stat/Gamma.java (original)
+++ mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/stat/Gamma.java Sat Jan 8 13:49:07 2011
@@ -25,13 +25,13 @@ It is provided "as is" without expressed
*/
package org.apache.mahout.math.jet.stat;
+import org.apache.mahout.math.jet.math.Constants;
import org.apache.mahout.math.jet.math.Polynomial;
/** Partially deprecated until unit tests are in place. Until this time, this class/interface is unsupported. */
-public class Gamma extends org.apache.mahout.math.jet.math.Constants {
+public final class Gamma {
- /** Makes this class non instantiable, but still let's others inherit from it. */
- protected Gamma() {
+ private Gamma() {
}
/**
@@ -217,7 +217,7 @@ public class Gamma extends org.apache.ma
if (flag && (b * x) <= 1.0 && x <= 0.95) {
t = powerSeries(a, b, x);
- t = t <= MACHEP ? 1.0 - MACHEP : 1.0 - t;
+ t = t <= Constants.MACHEP ? 1.0 - Constants.MACHEP : 1.0 - t;
return t;
}
@@ -231,24 +231,24 @@ public class Gamma extends org.apache.ma
y = a * Math.log(x);
t = b * Math.log(xc);
- if ((a + b) < MAXGAM && Math.abs(y) < MAXLOG && Math.abs(t) < MAXLOG) {
+ if ((a + b) < Constants.MAXGAM && Math.abs(y) < Constants.MAXLOG && Math.abs(t) < Constants.MAXLOG) {
t = Math.pow(xc, b);
t *= Math.pow(x, a);
t /= a;
t *= w;
t *= gamma(a + b) / (gamma(a) * gamma(b));
if (flag) {
- t = t <= MACHEP ? 1.0 - MACHEP : 1.0 - t;
+ t = t <= Constants.MACHEP ? 1.0 - Constants.MACHEP : 1.0 - t;
}
return t;
}
/* Resort to logarithms. */
y += t + logGamma(a + b) - logGamma(a) - logGamma(b);
y += Math.log(w / a);
- t = y < MINLOG ? 0.0 : Math.exp(y);
+ t = y < Constants.MINLOG ? 0.0 : Math.exp(y);
if (flag) {
- t = t <= MACHEP ? 1.0 - MACHEP : 1.0 - t;
+ t = t <= Constants.MACHEP ? 1.0 - Constants.MACHEP : 1.0 - t;
}
return t;
}
@@ -272,7 +272,7 @@ public class Gamma extends org.apache.ma
double ans = 1.0;
double r = 1.0;
int n = 0;
- double thresh = 3.0 * MACHEP;
+ double thresh = 3.0 * Constants.MACHEP;
do {
double xk = -(x * k1 * k2) / (k3 * k4);
double pk = pkm1 + pkm2 * xk;
@@ -314,17 +314,17 @@ public class Gamma extends org.apache.ma
k7 += 2.0;
k8 += 2.0;
- if ((Math.abs(qk) + Math.abs(pk)) > BIG) {
- pkm2 *= BIG_INVERSE;
- pkm1 *= BIG_INVERSE;
- qkm2 *= BIG_INVERSE;
- qkm1 *= BIG_INVERSE;
- }
- if ((Math.abs(qk) < BIG_INVERSE) || (Math.abs(pk) < BIG_INVERSE)) {
- pkm2 *= BIG;
- pkm1 *= BIG;
- qkm2 *= BIG;
- qkm1 *= BIG;
+ if ((Math.abs(qk) + Math.abs(pk)) > Constants.BIG) {
+ pkm2 *= Constants.BIG_INVERSE;
+ pkm1 *= Constants.BIG_INVERSE;
+ qkm2 *= Constants.BIG_INVERSE;
+ qkm1 *= Constants.BIG_INVERSE;
+ }
+ if ((Math.abs(qk) < Constants.BIG_INVERSE) || (Math.abs(pk) < Constants.BIG_INVERSE)) {
+ pkm2 *= Constants.BIG;
+ pkm1 *= Constants.BIG;
+ qkm2 *= Constants.BIG;
+ qkm1 *= Constants.BIG;
}
} while (++n < 300);
@@ -351,7 +351,7 @@ public class Gamma extends org.apache.ma
double ans = 1.0;
double r = 1.0;
int n = 0;
- double thresh = 3.0 * MACHEP;
+ double thresh = 3.0 * Constants.MACHEP;
do {
double xk = -(z * k1 * k2) / (k3 * k4);
double pk = pkm1 + pkm2 * xk;
@@ -393,17 +393,17 @@ public class Gamma extends org.apache.ma
k7 += 2.0;
k8 += 2.0;
- if ((Math.abs(qk) + Math.abs(pk)) > BIG) {
- pkm2 *= BIG_INVERSE;
- pkm1 *= BIG_INVERSE;
- qkm2 *= BIG_INVERSE;
- qkm1 *= BIG_INVERSE;
- }
- if ((Math.abs(qk) < BIG_INVERSE) || (Math.abs(pk) < BIG_INVERSE)) {
- pkm2 *= BIG;
- pkm1 *= BIG;
- qkm2 *= BIG;
- qkm1 *= BIG;
+ if ((Math.abs(qk) + Math.abs(pk)) > Constants.BIG) {
+ pkm2 *= Constants.BIG_INVERSE;
+ pkm1 *= Constants.BIG_INVERSE;
+ qkm2 *= Constants.BIG_INVERSE;
+ qkm1 *= Constants.BIG_INVERSE;
+ }
+ if ((Math.abs(qk) < Constants.BIG_INVERSE) || (Math.abs(pk) < Constants.BIG_INVERSE)) {
+ pkm2 *= Constants.BIG;
+ pkm1 *= Constants.BIG;
+ qkm2 *= Constants.BIG;
+ qkm1 *= Constants.BIG;
}
} while (++n < 300);
@@ -428,7 +428,7 @@ public class Gamma extends org.apache.ma
/* Compute x**a * exp(-x) / gamma(a) */
double ax = alpha * Math.log(x) - x - logGamma(alpha);
- if (ax < -MAXLOG) {
+ if (ax < -Constants.MAXLOG) {
return 0.0;
}
@@ -444,7 +444,7 @@ public class Gamma extends org.apache.ma
c *= x / r;
ans += c;
}
- while (c / ans > MACHEP);
+ while (c / ans > Constants.MACHEP);
return ans * ax / alpha;
@@ -467,7 +467,7 @@ public class Gamma extends org.apache.ma
}
double ax = alpha * Math.log(x) - x - logGamma(alpha);
- if (ax < -MAXLOG) {
+ if (ax < -Constants.MAXLOG) {
return 0.0;
}
@@ -503,13 +503,13 @@ public class Gamma extends org.apache.ma
pkm1 = pk;
qkm2 = qkm1;
qkm1 = qk;
- if (Math.abs(pk) > BIG) {
- pkm2 *= BIG_INVERSE;
- pkm1 *= BIG_INVERSE;
- qkm2 *= BIG_INVERSE;
- qkm1 *= BIG_INVERSE;
+ if (Math.abs(pk) > Constants.BIG) {
+ pkm2 *= Constants.BIG_INVERSE;
+ pkm1 *= Constants.BIG_INVERSE;
+ qkm2 *= Constants.BIG_INVERSE;
+ qkm1 *= Constants.BIG_INVERSE;
}
- } while (t > MACHEP);
+ } while (t > Constants.MACHEP);
return ans * ax;
}
@@ -562,7 +562,7 @@ public class Gamma extends org.apache.ma
throw new
ArithmeticException("lgamma: Overflow");
}
- z = LOGPI - Math.log(z) - w;
+ z = Constants.LOGPI - Math.log(z) - w;
return z;
}
@@ -624,7 +624,7 @@ public class Gamma extends org.apache.ma
double t = u;
double n = 2.0;
double s = 0.0;
- double z = MACHEP * ai;
+ double z = Constants.MACHEP * ai;
while (Math.abs(v) > z) {
u = (n - b) * x / n;
t *= u;
@@ -636,12 +636,12 @@ public class Gamma extends org.apache.ma
s += ai;
u = a * Math.log(x);
- if ((a + b) < MAXGAM && Math.abs(u) < MAXLOG) {
+ if ((a + b) < Constants.MAXGAM && Math.abs(u) < Constants.MAXLOG) {
t = gamma(a + b) / (gamma(a) * gamma(b));
s = s * t * Math.pow(x, a);
} else {
t = logGamma(a + b) - logGamma(a) - logGamma(b) + u + Math.log(s);
- s = t < MINLOG ? 0.0 : Math.exp(t);
+ s = t < Constants.MINLOG ? 0.0 : Math.exp(t);
}
return s;
}
@@ -672,7 +672,7 @@ public class Gamma extends org.apache.ma
} else {
y = Math.pow(x, x - 0.5) / y;
}
- y = SQTPI * y * w;
+ y = Constants.SQTPI * y * w;
return y;
}
}
Modified: mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/stat/Probability.java
URL: http://svn.apache.org/viewvc/mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/stat/Probability.java?rev=1056703&r1=1056702&r2=1056703&view=diff
==============================================================================
--- mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/stat/Probability.java (original)
+++ mahout/trunk/math/src/main/java/org/apache/mahout/math/jet/stat/Probability.java Sat Jan 8 13:49:07 2011
@@ -25,94 +25,14 @@ It is provided "as is" without expressed
*/
package org.apache.mahout.math.jet.stat;
-import org.apache.mahout.math.jet.math.Constants;
-import org.apache.mahout.math.jet.math.Polynomial;
import org.apache.mahout.math.jet.random.Normal;
/** Partially deprecated until unit tests are in place. Until this time, this class/interface is unsupported. */
-public class Probability extends Constants {
- private static final Normal UNIT_NORMAL = new Normal(0, 1, null);
-
- /**
- * ********************************************** COEFFICIENTS FOR METHOD normalInverse() *
- * ***********************************************
- */
- /* approximation for 0 <= |y - 0.5| <= 3/8 */
- private static final double[] P0 = {
- -5.99633501014107895267E1,
- 9.80010754185999661536E1,
- -5.66762857469070293439E1,
- 1.39312609387279679503E1,
- -1.23916583867381258016E0,
- };
- private static final double[] Q0 = {
- /* 1.00000000000000000000E0,*/
- 1.95448858338141759834E0,
- 4.67627912898881538453E0,
- 8.63602421390890590575E1,
- -2.25462687854119370527E2,
- 2.00260212380060660359E2,
- -8.20372256168333339912E1,
- 1.59056225126211695515E1,
- -1.18331621121330003142E0,
- };
-
-
- /* Approximation for interval z = sqrt(-2 log y ) between 2 and 8
- * i.e., y between exp(-2) = .135 and exp(-32) = 1.27e-14.
- */
- private static final double[] P1 = {
- 4.05544892305962419923E0,
- 3.15251094599893866154E1,
- 5.71628192246421288162E1,
- 4.40805073893200834700E1,
- 1.46849561928858024014E1,
- 2.18663306850790267539E0,
- -1.40256079171354495875E-1,
- -3.50424626827848203418E-2,
- -8.57456785154685413611E-4,
- };
-
- private static final double[] Q1 = {
- /* 1.00000000000000000000E0,*/
- 1.57799883256466749731E1,
- 4.53907635128879210584E1,
- 4.13172038254672030440E1,
- 1.50425385692907503408E1,
- 2.50464946208309415979E0,
- -1.42182922854787788574E-1,
- -3.80806407691578277194E-2,
- -9.33259480895457427372E-4,
- };
+public final class Probability {
- /* Approximation for interval z = sqrt(-2 log y ) between 8 and 64
- * i.e., y between exp(-32) = 1.27e-14 and exp(-2048) = 3.67e-890.
- */
- private static final double[] P2 = {
- 3.23774891776946035970E0,
- 6.91522889068984211695E0,
- 3.93881025292474443415E0,
- 1.33303460815807542389E0,
- 2.01485389549179081538E-1,
- 1.23716634817820021358E-2,
- 3.01581553508235416007E-4,
- 2.65806974686737550832E-6,
- 6.23974539184983293730E-9,
- };
- private static final double[] Q2 = {
- /* 1.00000000000000000000E0,*/
- 6.02427039364742014255E0,
- 3.67983563856160859403E0,
- 1.37702099489081330271E0,
- 2.16236993594496635890E-1,
- 1.34204006088543189037E-2,
- 3.28014464682127739104E-4,
- 2.89247864745380683936E-6,
- 6.79019408009981274425E-9,
- };
+ private static final Normal UNIT_NORMAL = new Normal(0, 1, null);
- /** Makes this class non instantiable, but still let's others inherit from it. */
- protected Probability() {
+ private Probability() {
}
/**
@@ -137,289 +57,6 @@ public class Probability extends Constan
}
/**
- * Returns the area under the right hand tail (from <tt>x</tt> to infinity) of the beta density function.
- *
- * This function is identical to the incomplete beta integral function <tt>Gamma.incompleteBeta(b, a, x)</tt>.
- */
- @Deprecated
- public static double betaComplemented(double a, double b, double x) {
- return Gamma.incompleteBeta(b, a, x);
- }
-
- /**
- * Returns the sum of the terms <tt>0</tt> through <tt>k</tt> of the Binomial probability density.
- * <pre>
- * k
- * -- ( n ) j n-j
- * > ( ) p (1-p)
- * -- ( j )
- * j=0
- * </pre>
- * The terms are not summed directly; instead the incomplete beta integral is employed, according to the formula <p>
- * <tt>y = binomial( k, n, p ) = Gamma.incompleteBeta( n-k, k+1, 1-p )</tt>. <p> All arguments must be positive,
- *
- * @param k end term.
- * @param n the number of trials.
- * @param p the probability of success (must be in <tt>(0.0,1.0)</tt>).
- */
- @Deprecated
- public static double binomial(int k, int n, double p) {
- if ((p < 0.0) || (p > 1.0)) {
- throw new IllegalArgumentException();
- }
- if ((k < 0) || (n < k)) {
- throw new IllegalArgumentException();
- }
-
- if (k == n) {
- return 1.0;
- }
- if (k == 0) {
- return Math.pow(1.0 - p, n);
- }
-
- return Gamma.incompleteBeta(n - k, k + 1, 1.0 - p);
- }
-
- /**
- * Returns the sum of the terms <tt>k+1</tt> through <tt>n</tt> of the Binomial probability density.
- * <pre>
- * n
- * -- ( n ) j n-j
- * > ( ) p (1-p)
- * -- ( j )
- * j=k+1
- * </pre>
- * The terms are not summed directly; instead the incomplete beta integral is employed, according to the formula <p>
- * <tt>y = binomialComplemented( k, n, p ) = Gamma.incompleteBeta( k+1, n-k, p )</tt>. <p> All arguments must be
- * positive,
- *
- * @param k end term.
- * @param n the number of trials.
- * @param p the probability of success (must be in <tt>(0.0,1.0)</tt>).
- */
- @Deprecated
- public static double binomialComplemented(int k, int n, double p) {
- if ((p < 0.0) || (p > 1.0)) {
- throw new IllegalArgumentException();
- }
- if ((k < 0) || (n < k)) {
- throw new IllegalArgumentException();
- }
-
- if (k == n) {
- return 0.0;
- }
- if (k == 0) {
- return 1.0 - Math.pow(1.0 - p, n);
- }
-
- return Gamma.incompleteBeta(k + 1, n - k, p);
- }
-
- /**
- * Returns the area under the left hand tail (from 0 to <tt>x</tt>) of the Chi square probability density function
- * with <tt>v</tt> degrees of freedom.
- * <pre>
- * inf.
- * -
- * 1 | | v/2-1 -t/2
- * P( x | v ) = ----------- | t e dt
- * v/2 - | |
- * 2 | (v/2) -
- * x
- * </pre>
- * where <tt>x</tt> is the Chi-square variable. <p> The incomplete gamma integral is used, according to the formula
- * <p> <tt>y = chiSquare( v, x ) = incompleteGamma( v/2.0, x/2.0 )</tt>. <p> The arguments must both be positive.
- *
- * @param v degrees of freedom.
- * @param x integration end point.
- */
- @Deprecated
- public static double chiSquare(double v, double x) {
- if (x < 0.0 || v < 1.0) {
- return 0.0;
- }
- return Gamma.incompleteGamma(v / 2.0, x / 2.0);
- }
-
- /**
- * Returns the area under the right hand tail (from <tt>x</tt> to infinity) of the Chi square probability density
- * function with <tt>v</tt> degrees of freedom.
- * <pre>
- * inf.
- * -
- * 1 | | v/2-1 -t/2
- * P( x | v ) = ----------- | t e dt
- * v/2 - | |
- * 2 | (v/2) -
- * x
- * </pre>
- * where <tt>x</tt> is the Chi-square variable.
- *
- * The incomplete gamma integral is used, according to the formula
- *
- * <tt>y = chiSquareComplemented( v, x ) = incompleteGammaComplement( v/2.0, x/2.0 )</tt>.
- *
- *
- * The arguments must both be positive.
- *
- * @param v degrees of freedom.
- */
- @Deprecated
- public static double chiSquareComplemented(double v, double x) {
- if (x < 0.0 || v < 1.0) {
- return 0.0;
- }
- return Gamma.incompleteGammaComplement(v / 2.0, x / 2.0);
- }
-
- /**
- * Returns the error function of the normal distribution; formerly named <tt>erf</tt>. The integral is
- * <pre>
- * x
- * -
- * 2 | | 2
- * erf(x) = -------- | exp( - t ) dt.
- * sqrt(pi) | |
- * -
- * 0
- * </pre>
- * <b>Implementation:</b> For <tt>0 <= |x| < 1, erf(x) = x * P4(x**2)/Q5(x**2)</tt>; otherwise
- * <tt>erf(x) = 1 - erfc(x)</tt>. <p> Code adapted from the
- * <A HREF="http://www.sci.usq.edu.au/staff/leighb/graph/Top.html">Java 2D
- * Graph Package 2.4</A>, which in turn is a port from the
- * <A HREF="http://people.ne.mediaone.net/moshier/index.html#Cephes">Cephes
- * 2.2</A> Math Library (C).
- *
- * @param x the argument to the function.
- */
- @Deprecated
- public static double errorFunction(double x) {
- double[] t = {
- 9.60497373987051638749E0,
- 9.00260197203842689217E1,
- 2.23200534594684319226E3,
- 7.00332514112805075473E3,
- 5.55923013010394962768E4
- };
- double[] u = {
- //1.00000000000000000000E0,
- 3.35617141647503099647E1,
- 5.21357949780152679795E2,
- 4.59432382970980127987E3,
- 2.26290000613890934246E4,
- 4.92673942608635921086E4
- };
-
- if (Math.abs(x) > 1.0) {
- return 1.0 - errorFunctionComplemented(x);
- }
- double z = x * x;
- return x * Polynomial.polevl(z, t, 4) / Polynomial.p1evl(z, u, 5);
- }
-
- /**
- * Returns the complementary Error function of the normal distribution; formerly named <tt>erfc</tt>.
- * <pre>
- * 1 - erf(x) =
- *
- * inf.
- * -
- * 2 | | 2
- * erfc(x) = -------- | exp( - t ) dt
- * sqrt(pi) | |
- * -
- * x
- * </pre>
- * <b>Implementation:</b> For small x, <tt>erfc(x) = 1 - erf(x)</tt>; otherwise rational approximations are computed.
- * <p> Code adapted from the <A HREF="http://www.sci.usq.edu.au/staff/leighb/graph/Top.html">Java 2D Graph Package
- * 2.4</A>, which in turn is a port from the <A HREF="http://people.ne.mediaone.net/moshier/index.html#Cephes">Cephes
- * 2.2</A> Math Library (C).
- *
- * @param a the argument to the function.
- */
- @Deprecated
- public static double errorFunctionComplemented(double a) {
- double[] p = {
- 2.46196981473530512524E-10,
- 5.64189564831068821977E-1,
- 7.46321056442269912687E0,
- 4.86371970985681366614E1,
- 1.96520832956077098242E2,
- 5.26445194995477358631E2,
- 9.34528527171957607540E2,
- 1.02755188689515710272E3,
- 5.57535335369399327526E2
- };
- double[] q = {
- //1.0
- 1.32281951154744992508E1,
- 8.67072140885989742329E1,
- 3.54937778887819891062E2,
- 9.75708501743205489753E2,
- 1.82390916687909736289E3,
- 2.24633760818710981792E3,
- 1.65666309194161350182E3,
- 5.57535340817727675546E2
- };
-
- double[] r = {
- 5.64189583547755073984E-1,
- 1.27536670759978104416E0,
- 5.01905042251180477414E0,
- 6.16021097993053585195E0,
- 7.40974269950448939160E0,
- 2.97886665372100240670E0
- };
- double[] s = {
- //1.00000000000000000000E0,
- 2.26052863220117276590E0,
- 9.39603524938001434673E0,
- 1.20489539808096656605E1,
- 1.70814450747565897222E1,
- 9.60896809063285878198E0,
- 3.36907645100081516050E0
- };
-
- double x = a < 0.0 ? -a : a;
-
- if (x < 1.0) {
- return 1.0 - errorFunction(a);
- }
-
- double z = -a * a;
-
- if (z < -MAXLOG) {
- return a < 0 ? 2.0 : 0.0;
- }
-
- z = Math.exp(z);
-
- double qval;
- double pval;
- if (x < 8.0) {
- pval = Polynomial.polevl(x, p, 8);
- qval = Polynomial.p1evl(x, q, 8);
- } else {
- pval = Polynomial.polevl(x, r, 5);
- qval = Polynomial.p1evl(x, s, 6);
- }
-
- double y = (z * pval) / qval;
-
- if (a < 0) {
- y = 2.0 - y;
- }
-
- if (y == 0.0) {
- return a < 0 ? 2.0 : 0.0;
- }
-
- return y;
- }
-
- /**
* Returns the integral from zero to <tt>x</tt> of the gamma probability density function.
* <pre>
*
@@ -447,31 +84,6 @@ public class Probability extends Constan
}
/**
- * Returns the integral from <tt>x</tt> to infinity of the gamma probability density function:
- * <pre>
- * alpha - infinity
- * beta | alpha-1 -beta t
- * y = --------- | t e dt
- * - |
- * | (alpha) - x
- * </pre>
- * The incomplete gamma integral is used, according to the relation <p> y = Gamma.incompleteGammaComplement( b, a*x
- * ).
- *
- * TODO this method is inconsistent with gamma(alpha, beta, x)
- *
- * @param alpha the shape parameter of the gamma distribution.
- * @param beta the rate parameter of the gamma distribution.
- * @param x integration end point.
- */
- public static double gammaComplemented(double alpha, double beta, double x) {
- if (x < 0.0) {
- return 0.0;
- }
- return Gamma.incompleteGammaComplement(alpha, beta * x);
- }
-
- /**
* Returns the sum of the terms <tt>0</tt> through <tt>k</tt> of the Negative Binomial Distribution.
* <pre>
* k
@@ -503,36 +115,6 @@ public class Probability extends Constan
}
/**
- * Returns the sum of the terms <tt>k+1</tt> to infinity of the Negative Binomial distribution.
- * <pre>
- * inf
- * -- ( n+j-1 ) n j
- * > ( ) p (1-p)
- * -- ( j )
- * j=k+1
- * </pre>
- * The terms are not computed individually; instead the incomplete beta integral is employed, according to the formula
- * <p> y = negativeBinomialComplemented( k, n, p ) = Gamma.incompleteBeta( k+1, n, 1-p ).
- *
- * All arguments must be positive,
- *
- * @param k end term.
- * @param n the number of trials.
- * @param p the probability of success (must be in <tt>(0.0,1.0)</tt>).
- */
- @Deprecated
- public static double negativeBinomialComplemented(int k, int n, double p) {
- if ((p < 0.0) || (p > 1.0)) {
- throw new IllegalArgumentException();
- }
- if (k < 0) {
- return 0.0;
- }
-
- return Gamma.incompleteBeta(k + 1, n, 1.0 - p);
- }
-
- /**
* Returns the area under the Normal (Gaussian) probability density function, integrated from minus infinity to
* <tt>x</tt> (assumes mean is zero, variance is one).
* <pre>
@@ -592,59 +174,6 @@ public class Probability extends Constan
}
/**
- * Returns the value, <tt>x</tt>, for which the area under the Normal (Gaussian) probability density function
- * (integrated from minus infinity to <tt>x</tt>) is equal to the argument <tt>y</tt> (assumes mean is zero, variance
- * is one); formerly named <tt>ndtri</tt>. <p> For small arguments <tt>0 < y < exp(-2)</tt>, the program computes
- * <tt>z = sqrt( -2.0 * log(y) )</tt>; then the approximation is <tt>x = z - log(z)/z - (1/z) P(1/z) / Q(1/z)</tt>.
- * There are two rational functions P/Q, one for <tt>0 < y < exp(-32)</tt> and the other for <tt>y</tt> up to
- * <tt>exp(-2)</tt>. For larger arguments, <tt>w = y - 0.5</tt>, and <tt>x/sqrt(2pi) = w + w**3
- * R(w**2)/S(w**2))</tt>.
- */
- @Deprecated
- public static double normalInverse(double y0) {
-
- double s2pi = Math.sqrt(2.0 * Math.PI);
-
- if (y0 <= 0.0) {
- throw new IllegalArgumentException();
- }
- if (y0 >= 1.0) {
- throw new IllegalArgumentException();
- }
- int code = 1;
- double y = y0;
- if (y > (1.0 - 0.13533528323661269189)) { /* 0.135... = exp(-2) */
- y = 1.0 - y;
- code = 0;
- }
-
- double x;
- if (y > 0.13533528323661269189) {
- y -= 0.5;
- double y2 = y * y;
- x = y + y * (y2 * Polynomial.polevl(y2, P0, 4) / Polynomial.p1evl(y2, Q0, 8));
- x *= s2pi;
- return x;
- }
-
- x = Math.sqrt(-2.0 * Math.log(y));
- double x0 = x - Math.log(x) / x;
-
- double z = 1.0 / x;
- double x1;
- if (x < 8.0) /* y > exp(-32) = 1.2664165549e-14 */ {
- x1 = z * Polynomial.polevl(z, P1, 8) / Polynomial.p1evl(z, Q1, 8);
- } else {
- x1 = z * Polynomial.polevl(z, P2, 8) / Polynomial.p1evl(z, Q2, 8);
- }
- x = x0 - x1;
- if (code != 0) {
- x = -x;
- }
- return x;
- }
-
- /**
* Returns the sum of the first <tt>k</tt> terms of the Poisson distribution.
* <pre>
* k j
@@ -671,135 +200,4 @@ public class Probability extends Constan
return Gamma.incompleteGammaComplement((double) (k + 1), mean);
}
- /**
- * Returns the sum of the terms <tt>k+1</tt> to <tt>Infinity</tt> of the Poisson distribution.
- * <pre>
- * inf. j
- * -- -m m
- * > e --
- * -- j!
- * j=k+1
- * </pre>
- * The terms are not summed directly; instead the incomplete gamma integral is employed, according to the formula <p>
- * <tt>y = poissonComplemented( k, m ) = Gamma.incompleteGamma( k+1, m )</tt>.
- *
- * The arguments must both be positive.
- *
- * @param k start term.
- * @param mean the mean of the poisson distribution.
- */
- @Deprecated
- public static double poissonComplemented(int k, double mean) {
- if (mean < 0) {
- throw new IllegalArgumentException();
- }
- if (k < -1) {
- return 0.0;
- }
- return Gamma.incompleteGamma((double) (k + 1), mean);
- }
-
- /**
- * Returns the integral from minus infinity to <tt>t</tt> of the Student-t distribution with <tt>k > 0</tt> degrees
- * of freedom.
- * <pre>
- * t
- * -
- * | |
- * - | 2 -(k+1)/2
- * | ( (k+1)/2 ) | ( x )
- * ---------------------- | ( 1 + --- ) dx
- * - | ( k )
- * sqrt( k pi ) | ( k/2 ) |
- * | |
- * -
- * -inf.
- * </pre>
- * Relation to incomplete beta integral: <p> <tt>1 - studentT(k,t) = 0.5 * Gamma.incompleteBeta( k/2, 1/2, z )</tt>
- * where <tt>z = k/(k + t**2)</tt>. <p> Since the function is symmetric about <tt>t=0</tt>, the area under the right
- * tail of the density is found by calling the function with <tt>-t</tt> instead of <tt>t</tt>.
- *
- * @param k degrees of freedom.
- * @param t integration end point.
- */
- @Deprecated
- public static double studentT(double k, double t) {
- if (k <= 0) {
- throw new IllegalArgumentException();
- }
- if (t == 0) {
- return 0.5;
- }
-
- double cdf = 0.5 * Gamma.incompleteBeta(0.5 * k, 0.5, k / (k + t * t));
-
- if (t >= 0) {
- cdf = 1.0 - cdf;
- } // fixes bug reported by stefan.bentink@molgen.mpg.de
-
- return cdf;
- }
-
- /**
- * Returns the value, <tt>t</tt>, for which the area under the Student-t probability density function (integrated from
- * minus infinity to <tt>t</tt>) is equal to <tt>1-alpha/2</tt>. The value returned corresponds to usual Student
- * t-distribution lookup table for <tt>t<sub>alpha[size]</sub></tt>. <p> The function uses the studentT function to
- * determine the return value iteratively.
- *
- * @param alpha probability
- * @param size size of data set
- */
- @Deprecated
- public static double studentTInverse(double alpha, int size) {
- double cumProb = 1 - alpha / 2; // Cumulative probability
-
- double x1 = normalInverse(cumProb);
-
- // Return inverse of normal for large size
- if (size > 200) {
- return x1;
- }
-
- // Find a pair of x1,x2 that bracket zero
- double f1 = studentT(size, x1) - cumProb;
- double x2 = x1;
- double f2;
- do {
- if (f1 > 0) {
- x2 /= 2;
- } else {
- x2 += x1;
- }
- f2 = studentT(size, x2) - cumProb;
- } while (f1 * f2 > 0);
-
- // Find better approximation
- // Pegasus-method
- do {
- // Calculate slope of secant and t value for which it is 0.
- double s12 = (f2 - f1) / (x2 - x1);
- double x3 = x2 - f2 / s12;
-
- // Calculate function value at x3
- double f3 = studentT(size, x3) - cumProb;
- if (Math.abs(f3) < 1.0e-8) { // This criteria needs to be very tight!
- // We found a perfect value -> return
- return x3;
- }
-
- if (f3 * f2 < 0) {
- x1 = x2;
- f1 = f2;
- x2 = x3;
- f2 = f3;
- } else {
- double g = f2 / (f2 + f3);
- f1 = g * f1;
- x2 = x3;
- f2 = f3;
- }
- } while (Math.abs(x2 - x1) > 0.001);
-
- return Math.abs(f2) <= Math.abs(f1) ? x2 : x1;
- }
}