Return-Path: X-Original-To: apmail-mahout-commits-archive@www.apache.org Delivered-To: apmail-mahout-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id BCB9495D3 for ; Fri, 23 Dec 2011 01:02:20 +0000 (UTC) Received: (qmail 69049 invoked by uid 500); 23 Dec 2011 01:02:20 -0000 Delivered-To: apmail-mahout-commits-archive@mahout.apache.org Received: (qmail 69009 invoked by uid 500); 23 Dec 2011 01:02:20 -0000 Mailing-List: contact commits-help@mahout.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@mahout.apache.org Delivered-To: mailing list commits@mahout.apache.org Received: (qmail 69002 invoked by uid 99); 23 Dec 2011 01:02:20 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 23 Dec 2011 01:02:20 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 23 Dec 2011 01:02:19 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id 0B34823888D2; Fri, 23 Dec 2011 01:01:59 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1222524 - /mahout/trunk/core/src/main/java/org/apache/mahout/clustering/dirichlet/models/GaussianCluster.java Date: Fri, 23 Dec 2011 01:01:58 -0000 To: commits@mahout.apache.org From: jeastman@apache.org X-Mailer: svnmailer-1.0.8-patched Message-Id: <20111223010159.0B34823888D2@eris.apache.org> Author: jeastman Date: Fri Dec 23 01:01:58 2011 New Revision: 1222524 URL: http://svn.apache.org/viewvc?rev=1222524&view=rev Log: MAHOUT-846: Cache pdf zProd2piR term constant over life of cluster Modified: mahout/trunk/core/src/main/java/org/apache/mahout/clustering/dirichlet/models/GaussianCluster.java Modified: mahout/trunk/core/src/main/java/org/apache/mahout/clustering/dirichlet/models/GaussianCluster.java URL: http://svn.apache.org/viewvc/mahout/trunk/core/src/main/java/org/apache/mahout/clustering/dirichlet/models/GaussianCluster.java?rev=1222524&r1=1222523&r2=1222524&view=diff ============================================================================== --- mahout/trunk/core/src/main/java/org/apache/mahout/clustering/dirichlet/models/GaussianCluster.java (original) +++ mahout/trunk/core/src/main/java/org/apache/mahout/clustering/dirichlet/models/GaussianCluster.java Fri Dec 23 01:01:58 2011 @@ -48,27 +48,47 @@ public class GaussianCluster extends Abs return new GaussianCluster(getCenter(), getRadius(), getId()); } + // the value of the zProduct(S*2pi) term. Calculated below. + private Double zProd2piR = null; + @Override public double pdf(VectorWritable vw) { - Vector x = vw.get(); - Vector m = getCenter(); - Vector s = getRadius().plus(0.0000001); // add a small prior to avoid divide by zero - return Math.exp(-(divideSquareAndSum(x.minus(m), s) / 2)) / zProdSqt2Pi(s); + if (zProd2piR == null) { + computeProd2piR(); + } + return Math.exp(-(sumXminusCdivRsquared(vw.get()) / 2)) / zProd2piR; } - private double zProdSqt2Pi(Vector s) { - double prod = 1; - for (int i = 0; i < s.size(); i++) { - prod *= s.getQuick(i) * UncommonDistributions.SQRT2PI; + /** + * Compute the product(r[i]*SQRT2PI) over all i. Note that the cluster Radius + * corresponds to the Stdev of a Gaussian and the Center to its Mean. + */ + private void computeProd2piR() { + zProd2piR = 1.0; + for (Iterator it = getRadius().iterateNonZero(); it.hasNext();) { + Element radius = it.next(); + zProd2piR *= radius.get() * UncommonDistributions.SQRT2PI; } - return prod; } - private double divideSquareAndSum(Vector numerator, Vector denominator) { + @Override + public void computeParameters() { + super.computeParameters(); + zProd2piR = null; + } + + /** + * @param x + * a Vector + * @return the zSum(((x[i]-c[i])/r[i])^2) over all i + */ + private double sumXminusCdivRsquared(Vector x) { double result = 0; - for (Iterator it = denominator.iterateNonZero(); it.hasNext();) { - Element denom = it.next(); - double quotient = numerator.getQuick(denom.index()) / denom.get(); + for (Iterator it = getRadius().iterateNonZero(); it.hasNext();) { + Element radiusElem = it.next(); + int index = radiusElem.index(); + double quotient = (x.get(index) - getCenter().get(index)) + / radiusElem.get(); result += quotient * quotient; } return result;