[mlpack-svn] r13634 - mlpack/trunk/src/mlpack/methods/radical
fastlab-svn at coffeetalk-1.cc.gatech.edu
fastlab-svn at coffeetalk-1.cc.gatech.edu
Mon Oct 1 20:18:24 EDT 2012
Author: rcurtin
Date: 2012-10-01 20:18:24 -0400 (Mon, 01 Oct 2012)
New Revision: 13634
Modified:
mlpack/trunk/src/mlpack/methods/radical/radical.cpp
mlpack/trunk/src/mlpack/methods/radical/radical.hpp
Log:
First cleanup of code: adhere to formatting standards (mostly).
Modified: mlpack/trunk/src/mlpack/methods/radical/radical.cpp
===================================================================
--- mlpack/trunk/src/mlpack/methods/radical/radical.cpp 2012-10-01 20:31:17 UTC (rev 13633)
+++ mlpack/trunk/src/mlpack/methods/radical/radical.cpp 2012-10-02 00:18:24 UTC (rev 13634)
@@ -9,34 +9,24 @@
using namespace std;
using namespace arma;
+using namespace mlpack;
+using namespace mlpack::radical;
-namespace mlpack {
-namespace radical {
-
-
-Radical::Radical(double noiseStdDev, size_t nReplicates, size_t nAngles,
- size_t nSweeps) :
- noiseStdDev(noiseStdDev),
- nReplicates(nReplicates),
- nAngles(nAngles),
- nSweeps(nSweeps),
- m(0)
+// Set the parameters to RADICAL.
+Radical::Radical(const double noiseStdDev,
+ const size_t nReplicates,
+ const size_t nAngles,
+ const size_t nSweeps,
+ const size_t m) :
+ noiseStdDev(noiseStdDev),
+ nReplicates(nReplicates),
+ nAngles(nAngles),
+ nSweeps(nSweeps),
+ m(m)
{
- // nothing to do here
+ // Nothing to do here.
}
-Radical::Radical(double noiseStdDev, size_t nReplicates, size_t nAngles,
- size_t nSweeps, size_t m) :
- noiseStdDev(noiseStdDev),
- nReplicates(nReplicates),
- nAngles(nAngles),
- nSweeps(nSweeps),
- m(m)
-{
- // nothing to do here
-}
-
-
void Radical::CopyAndPerturb(mat& matXNew, const mat& matX)
{
matXNew = repmat(matX, nReplicates, 1) + noiseStdDev *
@@ -58,7 +48,8 @@
// Apparently faster
double sum = 0;
uword range = z.n_elem - m;
- for(uword i = 0; i < range; i++) {
+ for (uword i = 0; i < range; i++)
+ {
sum += log(z(i + m) - z(i));
}
return sum;
@@ -78,13 +69,14 @@
((double) nAngles) * M_PI / 2;
vec values(nAngles);
- for(size_t i = 0; i < nAngles; i++) {
+ for (size_t i = 0; i < nAngles; i++)
+ {
double cosTheta = cos(thetas(i));
double sinTheta = sin(thetas(i));
- matJacobi(0,0) = cosTheta;
- matJacobi(1,0) = -sinTheta;
- matJacobi(0,1) = sinTheta;
- matJacobi(1,1) = cosTheta;
+ matJacobi(0, 0) = cosTheta;
+ matJacobi(1, 0) = -sinTheta;
+ matJacobi(0, 1) = sinTheta;
+ matJacobi(1, 1) = cosTheta;
candidateY = matXMod * matJacobi;
vec candidateY1 = candidateY.unsafe_col(0);
@@ -101,19 +93,16 @@
void Radical::DoRadical(const mat& matXT, mat& matY, mat& matW)
{
-
// matX is nPoints by nDims (although less intuitive than columns being
// points, and although this is the transpose of the ICA literature, this
// choice is for computational efficiency when repeatedly generating
- // two-dimensional coordinate projections for Radical2D
+ // two-dimensional coordinate projections for Radical2D.
mat matX = trans(matXT);
-
- // if m was not specified, initialize m as recommended in
- // (Learned-Miller and Fisher, 2003)
- if (m < 1) {
+ // If m was not specified, initialize m as recommended in
+ // (Learned-Miller and Fisher, 2003).
+ if (m < 1)
m = floor(sqrt((double) matX.n_rows));
- }
const size_t nDims = matX.n_cols;
const size_t nPoints = matX.n_rows;
@@ -123,23 +112,23 @@
WhitenFeatureMajorMatrix(matX, matY, matWhitening);
// matY is now the whitened form of matX
- // in the RADICAL code, they do not copy and perturb initially, although the
+ // In the RADICAL code, they do not copy and perturb initially, although the
// paper does. we follow the code as it should match their reported results
- // and likely does a better job bouncing out of local optima
+ // and likely does a better job bouncing out of local optima.
//GeneratePerturbedX(X, X);
- // initialize the unmixing matrix to the whitening matrix
+ // Initialize the unmixing matrix to the whitening matrix.
matW = matWhitening;
mat matYSubspace(nPoints, 2);
mat matEye = eye(nDims, nDims);
- for(size_t sweepNum = 0; sweepNum < nSweeps; sweepNum++)
+ for (size_t sweepNum = 0; sweepNum < nSweeps; sweepNum++)
{
- for(size_t i = 0; i < nDims - 1; i++)
+ for (size_t i = 0; i < nDims - 1; i++)
{
- for(size_t j = i + 1; j < nDims; j++)
+ for (size_t j = i + 1; j < nDims; j++)
{
matYSubspace.col(0) = matY.col(i);
matYSubspace.col(1) = matY.col(j);
@@ -159,7 +148,8 @@
}
}
- // the final transposes provide W and Y in the typical form from the ICA literature
+ // the final transposes provide W and Y in the typical form from the ICA
+ // literature
//matW = trans(matWhitening * matW);
//matY = trans(matY);
matW = trans(matW);
@@ -167,9 +157,9 @@
}
-void WhitenFeatureMajorMatrix(const mat& matX,
- mat& matXWhitened,
- mat& matWhitening)
+void mlpack::radical::WhitenFeatureMajorMatrix(const mat& matX,
+ mat& matXWhitened,
+ mat& matWhitening)
{
mat matU, matV;
vec s;
@@ -177,7 +167,3 @@
matWhitening = matU * diagmat(1 / sqrt(s)) * trans(matV);
matXWhitened = matX * matWhitening;
}
-
-
-}; // namespace radical
-}; // namespace mlpack
Modified: mlpack/trunk/src/mlpack/methods/radical/radical.hpp
===================================================================
--- mlpack/trunk/src/mlpack/methods/radical/radical.hpp 2012-10-01 20:31:17 UTC (rev 13633)
+++ mlpack/trunk/src/mlpack/methods/radical/radical.hpp 2012-10-02 00:18:24 UTC (rev 13634)
@@ -2,9 +2,8 @@
* @file radical.hpp
* @author Nishant Mehta
*
- * Declaration of Radical class (RADICAL is Robust, Accurate, Direct ICA aLgorithm)
- * Note: upper case variables correspond to matrices. do not convert them to
- * camelCase because that would make them appear to be vectors (which is bad!)
+ * Declaration of Radical class (RADICAL is Robust, Accurate, Direct ICA
+ * aLgorithm).
*/
#ifndef __MLPACK_METHODS_RADICAL_RADICAL_HPP
@@ -18,6 +17,7 @@
/**
* An implementation of RADICAL, an algorithm for independent component
* analysis (ICA).
+ *
* Let X be a matrix where each column is a point and each row a dimension.
* The goal is to find a square unmixing matrix W such that Y = W X and
* the rows of Y are independent components.
@@ -35,7 +35,8 @@
* }
* @endcode
*/
-class Radical {
+class Radical
+{
public:
/**
* Set the parameters to RADICAL.
@@ -48,27 +49,15 @@
* Radical2D
* @param nSweeps Number of sweeps
* Each sweep calls Radical2D once for each pair of dimensions
+ * @param m The variable m from Vasicek's m-spacing estimator of entropy.
*/
- Radical(double noiseStdDev, size_t nReplicates, size_t nAngles,
- size_t nSweeps);
+ Radical(const double noiseStdDev = 0.175,
+ const size_t nReplicates = 30,
+ const size_t nAngles = 150,
+ const size_t nSweeps = 0,
+ const size_t m = 0);
/**
- * Set the parameters to RADICAL.
- *
- * @param noiseStdDev Standard deviation of the Gaussian noise added to the
- * replicates of the data points during Radical2D
- * @param nReplicates Number of Gaussian-perturbed replicates to use
- * (per point) in Radical2D
- * @param nAngles Number of angles to consider in brute-force search during
- * Radical2D
- * @param nSweeps Number of sweeps
- * Each sweep calls Radical2D once for each pair of dimensions
- * @param m The variable m from Vasicek's m-spacing estimator of entropy
- */
- Radical(double noiseStdDev, size_t nReplicates, size_t nAngles,
- size_t nSweeps, size_t m);
-
- /**
* Run RADICAL.
*
* @param matX Input data into the algorithm - a matrix where each column is
@@ -81,10 +70,9 @@
/**
* Vasicek's m-spacing estimator of entropy, with overlap modification from
- * (Learned-Miller and Fisher, 2003)
+ * (Learned-Miller and Fisher, 2003).
*
- * @param x Empirical sample (one-dimensional) over which to estimate entropy
- *
+ * @param x Empirical sample (one-dimensional) over which to estimate entropy.
*/
double Vasicek(arma::vec& x);
@@ -124,7 +112,6 @@
* m to use for Vasicek's m-spacing estimator of entropy
*/
size_t m;
-
};
void WhitenFeatureMajorMatrix(const arma::mat& matX,
More information about the mlpack-svn
mailing list