[beast-mcmc] r6730 committed - Added timing tests and debug option to the KDE distributions.

1 view
Skip to first unread message

beast...@googlecode.com

unread,
Apr 10, 2015, 5:27:52 AM4/10/15
to beast-...@googlegroups.com
Revision: 6730
Author: bael...@gmail.com
Date: Fri Apr 10 09:27:25 2015 UTC
Log: Added timing tests and debug option to the KDE distributions.
https://code.google.com/p/beast-mcmc/source/detail?r=6730

Modified:
/trunk/src/dr/math/distributions/GammaKDEDistribution.java
/trunk/src/dr/math/distributions/LogTransformedNormalKDEDistribution.java
/trunk/src/dr/math/distributions/NormalKDEDistribution.java

=======================================
--- /trunk/src/dr/math/distributions/GammaKDEDistribution.java Mon Feb 14
21:08:27 2011 UTC
+++ /trunk/src/dr/math/distributions/GammaKDEDistribution.java Fri Apr 10
09:27:25 2015 UTC
@@ -2,6 +2,8 @@

import dr.stats.DiscreteStatistics;

+import java.util.Random;
+

/**
* @author Jennifer Tom
@@ -68,6 +70,28 @@
return cern.jet.stat.Gamma.gamma(value);

}
+
+ public static void main(String[] args) {
+
+ long start = System.currentTimeMillis();
+
+ Random random = new Random(1234);
+
+ Double[] samples = new Double[10000000];
+ for (int i = 0; i < samples.length; i++) {
+ samples[i] = random.nextDouble();
+ }
+ GammaKDEDistribution nKDE = new GammaKDEDistribution(samples);
+
+ for (int i = 0; i < 100; i++) {
+ nKDE.evaluateKernel(random.nextDouble());
+ }
+
+ long end = System.currentTimeMillis();
+
+ System.out.println("Time: " + (end-start));
+
+ }

// private double sampleMean() {return DiscreteStatistics.mean(sample);}

=======================================
---
/trunk/src/dr/math/distributions/LogTransformedNormalKDEDistribution.java
Mon Oct 1 20:31:38 2012 UTC
+++
/trunk/src/dr/math/distributions/LogTransformedNormalKDEDistribution.java
Fri Apr 10 09:27:25 2015 UTC
@@ -11,6 +11,7 @@
public class LogTransformedNormalKDEDistribution extends
KernelDensityEstimatorDistribution {

public static final int MINIMUM_GRID_SIZE = 2048;
+ public static final boolean DEBUG = false;

//the samples should not already be log transformed (the log
transformation is done in this class)
public LogTransformedNormalKDEDistribution(Double[] sample) {
@@ -42,11 +43,15 @@
processBounds(lowerBound, upperBound);
setBandWidth(bandWidth);
*/
+
super(sample, lowerBound, upperBound, bandWidth);
//transform the data to the log scale and store in logSample
- System.out.println("Creating the KDE in log space");
- System.out.println("lowerBound = " + lowerBound);
- System.out.println("upperBound = " + upperBound);
+ if (DEBUG) {
+ System.out.println("Creating the KDE in log space");
+ System.out.println("lowerBound = " + lowerBound);
+ System.out.println("upperBound = " + upperBound);
+ }
+
this.logSample = new double[sample.length];
for (int i = 0; i < logSample.length; i++) {
this.logSample[i] = Math.log(sample[i]);
@@ -70,16 +75,20 @@
from = DiscreteStatistics.min(this.sample) - this.cut *
this.bandWidth;
to = DiscreteStatistics.max(this.sample) + this.cut *
this.bandWidth;

- System.out.println("bandWidth = " + this.bandWidth);
- System.out.println("cut = " + this.cut);
- System.out.println("from = " + from);
- System.out.println("to = " + to);
+ if (DEBUG) {
+ System.out.println("bandWidth = " + this.bandWidth);
+ System.out.println("cut = " + this.cut);
+ System.out.println("from = " + from);
+ System.out.println("to = " + to);
+ }

lo = from - 4.0 * this.bandWidth;
up = to + 4.0 * this.bandWidth;
-
- System.out.println("lo = " + lo);
- System.out.println("up = " + up);
+
+ if (DEBUG) {
+ System.out.println("lo = " + lo);
+ System.out.println("up = " + up);
+ }

densityKnown = false;

@@ -212,9 +221,12 @@

private void transformEstimator() {

- System.out.println("\nCreating the KDE in normal space");
- System.out.println("lowerBound = " + lowerBound);
- System.out.println("upperBound = " + upperBound);
+ if (DEBUG) {
+ System.out.println("\nCreating the KDE in normal space");
+ System.out.println("lowerBound = " + lowerBound);
+ System.out.println("upperBound = " + upperBound);
+ }
+
this.sample = backupSample;
//processBounds(lowerBound, upperBound);
setBandWidth(null);
@@ -222,16 +234,20 @@
from = DiscreteStatistics.min(this.sample) - this.cut *
this.bandWidth;
to = DiscreteStatistics.max(this.sample) + this.cut *
this.bandWidth;

- System.out.println("bandWidth = " + this.bandWidth);
- System.out.println("cut = " + this.cut);
- System.out.println("from = " + from);
- System.out.println("to = " + to);
+ if (DEBUG) {
+ System.out.println("bandWidth = " + this.bandWidth);
+ System.out.println("cut = " + this.cut);
+ System.out.println("from = " + from);
+ System.out.println("to = " + to);
+ }

lo = from - 4.0 * this.bandWidth;
up = to + 4.0 * this.bandWidth;

- System.out.println("lo = " + lo);
- System.out.println("up = " + up);
+ if (DEBUG) {
+ System.out.println("lo = " + lo);
+ System.out.println("up = " + up);
+ }

/*if (from < 0.0) {
from = 0.0;
@@ -247,24 +263,30 @@
makeOrdinates();

int numberOfNegatives = 0;
- System.out.println("\nxPoints length = " + xPoints.length);
+ if (DEBUG) {
+ System.out.println("\nxPoints length = " + xPoints.length);
+ }
for (int i = 0; i < xPoints.length; i++) {
if (xPoints[i] < 0.0) {
numberOfNegatives++;
}
//System.out.println(xPoints[i]);
}
- System.out.println("number of negative xPoints = " +
numberOfNegatives);
+ if (DEBUG) {
+ System.out.println("number of negative xPoints = " +
numberOfNegatives);
+ }

//the KDE on log scale is contained in the xPoints and
densityPoints arrays
//copy them to finalXPoints and finalDensityPoints
this.finalXPoints = new double[xPoints.length - numberOfNegatives];
System.arraycopy(xPoints, numberOfNegatives, finalXPoints, 0,
xPoints.length - numberOfNegatives);
- for (int i = 0; i < xPoints.length; i++) {
- System.out.println(backupXPoints[i] + " : " + densityPoints[i]);
- }
-
- System.out.println("\nfinalXPoints length = " + finalXPoints.length);
+ if (DEBUG) {
+ for (int i = 0; i < xPoints.length; i++) {
+ System.out.println(backupXPoints[i] + " : " +
densityPoints[i]);
+ }
+ System.out.println("\nfinalXPoints length = " +
finalXPoints.length);
+ }
+
this.finalDensityPoints = new double[densityPoints.length -
numberOfNegatives];

for (int i = 0; i < finalXPoints.length; i++) {
=======================================
--- /trunk/src/dr/math/distributions/NormalKDEDistribution.java Mon Feb 14
21:08:27 2011 UTC
+++ /trunk/src/dr/math/distributions/NormalKDEDistribution.java Fri Apr 10
09:27:25 2015 UTC
@@ -5,6 +5,8 @@
import dr.stats.DiscreteStatistics;
import dr.util.HeapSort;

+import java.util.Random;
+
/**
* @author Marc A. Suchard
*/
@@ -260,4 +262,27 @@
private double up;

private boolean densityKnown = false;
+
+ public static void main(String[] args) {
+
+ long start = System.currentTimeMillis();
+
+ Random random = new Random(1234);
+
+ Double[] samples = new Double[10000000];
+ for (int i = 0; i < samples.length; i++) {
+ samples[i] = random.nextDouble();
+ }
+ NormalKDEDistribution nKDE = new NormalKDEDistribution(samples);
+
+ for (int i = 0; i < 100; i++) {
+ nKDE.evaluateKernel(random.nextDouble());
+ }
+
+ long end = System.currentTimeMillis();
+
+ System.out.println("Time: " + (end-start));
+
+ }
+
}
Reply all
Reply to author
Forward
0 new messages