Example usage for org.apache.commons.math3.analysis.function Exp Exp

List of usage examples for org.apache.commons.math3.analysis.function Exp Exp

Introduction

In this page you can find the example usage for org.apache.commons.math3.analysis.function Exp Exp.

Prototype

Exp

Source Link

Usage

From source file:edu.stanford.cfuller.imageanalysistools.clustering.GaussianLikelihoodObjectiveFunction.java

/**
 * Evaluates the function with the specified parameters.
 *
 * The parameters describe a set of gaussian generators (which are the Clusters).
 *
 * @param parameters    A RealVector containing the values of all the parameters of each Gaussian, ordered so that all the parameters of a single gaussian are together, then the next gaussian, etc.
 * @return              The negative log likelihood of having observed the ClusterObjects at their locations, given the parameters describing the Gaussian clusters.
 *///from  w  w w  . j  av  a  2 s  . c o  m
public double evaluate(RealVector parameters) {

    int nClusters = parameters.getDimension() / nParametersEach;

    //java.util.logging.Logger.getLogger("edu.stanford.cfuller.imageanalysistools").info("nClusters: " + nClusters + "  abdMatrices_size: " + abdMatrices.size() + "  det_dim: " + det.getDimension());

    if (det.getDimension() != nClusters) {

        clusterProbs = new Array2DRowRealMatrix(this.objects.size(), nClusters);
        det = new ArrayRealVector(nClusters);
        pk = new ArrayRealVector(nClusters);

        if (abdMatrices.size() < nClusters) {
            int originalSize = abdMatrices.size();
            for (int i = 0; i < nClusters - originalSize; i++) {
                abdMatrices.add(new Array2DRowRealMatrix(numDim, numDim));
            }
        } else {
            abdMatrices.setSize(nClusters);
        }

    }

    pk.mapMultiplyToSelf(0.0);

    //java.util.logging.Logger.getLogger("edu.stanford.cfuller.imageanalysistools").info("nClusters: " + nClusters + "  abdMatrices_size: " + abdMatrices.size() + "  det_dim: " + det.getDimension());

    for (int i = 0; i < nClusters; i++) {
        /*
        double ct = Math.cos(parameters.getEntry(nParametersEach*i+3));
        double st = Math.sin(parameters.getEntry(nParametersEach*i+3));
        double sin2t = Math.sin(2*parameters.getEntry(nParametersEach*i+3));
        double a = (ct*ct/(2*parameters.getEntry(nParametersEach*i+2)) + st*st/(2*parameters.getEntry(nParametersEach*i+4)));
        double b = (sin2t/(4*parameters.getEntry(nParametersEach*i+4)) - sin2t/(4*parameters.getEntry(nParametersEach*i+2)));
        double d = (st*st/(2*parameters.getEntry(nParametersEach*i+2)) + ct*ct/(2*parameters.getEntry(nParametersEach*i+4)));
        */

        double a = parameters.getEntry(nParametersEach * i + 2);
        double d = parameters.getEntry(nParametersEach * i + 4);
        double b = Math.sqrt(a * d) * parameters.getEntry(nParametersEach * i + 3);

        abdMatrices.get(i).setEntry(0, 0, a);
        abdMatrices.get(i).setEntry(1, 0, b);
        abdMatrices.get(i).setEntry(0, 1, b);
        abdMatrices.get(i).setEntry(1, 1, d);

        LUDecomposition abdLU = (new LUDecomposition(abdMatrices.get(i)));

        det.setEntry(i, (abdLU).getDeterminant());
        //det.setEntry(i, a*d-b*b);
        try {
            abdMatrices.set(i, abdLU.getSolver().getInverse());
        } catch (org.apache.commons.math3.linear.SingularMatrixException e) {
            return Double.MAX_VALUE;
        }

    }

    for (int n = 0; n < this.objects.size(); n++) {

        ClusterObject c = this.objects.get(n);

        double max = -1.0 * Double.MAX_VALUE;
        int maxIndex = 0;

        for (int k = 0; k < nClusters; k++) {

            mean.setEntry(0, c.getCentroid().getX() - parameters.getEntry(nParametersEach * k));
            mean.setEntry(1, c.getCentroid().getY() - parameters.getEntry(nParametersEach * k + 1));

            x = abdMatrices.get(k).operate(mean);

            double dot = x.dotProduct(mean);

            //                java.util.logging.Logger.getLogger("edu.stanford.cfuller.imageanalysistools").info("k, n: " + k + ", " + this.objects.size());
            //                java.util.logging.Logger.getLogger("edu.stanford.cfuller.imageanalysistools").info("parameters: " + parameters.toString());
            //                java.util.logging.Logger.getLogger("edu.stanford.cfuller.imageanalysistools").info("abd matrix: " + abdMatrices.get(k).toString());
            //                java.util.logging.Logger.getLogger("edu.stanford.cfuller.imageanalysistools").info("det: " + det.getEntry(k));
            //                java.util.logging.Logger.getLogger("edu.stanford.cfuller.imageanalysistools").info("mean: " + mean.toString());
            //                java.util.logging.Logger.getLogger("edu.stanford.cfuller.imageanalysistools").info("dot: " + dot);

            double logN = negLog2PI - 0.5 * Math.log(det.getEntry(k)) - 0.5 * dot;

            //                java.util.logging.Logger.getLogger("edu.stanford.cfuller.imageanalysistools").info("logN: " + logN);

            clusterProbs.setEntry(n, k, logN);
            if (logN > max) {
                max = logN;
                maxIndex = k;
            }

            if (Double.isInfinite(logN) || Double.isNaN(logN)) {
                return Double.MAX_VALUE;
            }

        }

        c.setMostProbableCluster(maxIndex);

    }

    for (int k = 0; k < nClusters; k++) {

        double tempMax = -1.0 * Double.MAX_VALUE;

        for (int n = 0; n < this.objects.size(); n++) {
            if (clusterProbs.getEntry(n, k) > tempMax)
                tempMax = clusterProbs.getEntry(n, k);
        }

        pk.setEntry(k,
                tempMax + Math.log(
                        clusterProbs.getColumnVector(k).mapSubtract(tempMax).mapToSelf(new Exp()).getL1Norm())
                        - Math.log(this.objects.size()));

    }

    double pkMax = -1.0 * Double.MAX_VALUE;

    for (int k = 0; k < nClusters; k++) {
        if (pk.getEntry(k) > pkMax)
            pkMax = pk.getEntry(k);
    }

    double logSumPk = pkMax + Math.log(pk.mapSubtract(pkMax).mapToSelf(new Exp()).getL1Norm());

    pk.mapSubtractToSelf(logSumPk);

    //java.util.logging.Logger.getLogger("edu.stanford.cfuller.imageanalysistools").info("pk: " + pk.toString());

    double L = 0;

    for (int n = 0; n < this.objects.size(); n++) {

        RealVector toSum = clusterProbs.getRowVector(n).add(pk);

        double tempMax = -1.0 * Double.MAX_VALUE;

        for (int k = 0; k < nClusters; k++) {
            if (toSum.getEntry(k) > tempMax)
                tempMax = toSum.getEntry(k);
        }

        double pn = tempMax + Math.log(toSum.mapSubtract(tempMax).mapToSelf(new Exp()).getL1Norm());

        //java.util.logging.Logger.getLogger("edu.stanford.cfuller.imageanalysistools").info("pn: " + pn);

        L += pn;

    }

    return -1.0 * L;
}

From source file:org.deidentifier.arx.criteria.EDDifferentialPrivacy.java

/**
 * Calculates beta_max/*from  www  .  j av  a  2  s  .  co  m*/
 * @param epsilon
 * @return
 */
private double calculateBeta(double epsilon) {
    return 1.0d - (new Exp()).value(-1.0d * epsilon);
}

From source file:org.deidentifier.arx.criteria.EDDifferentialPrivacy.java

/**
 * Calculates c_n/*from  w  w  w .j a v a 2s .c o m*/
 * @param n
 * @param epsilon
 * @param beta
 * @return
 */
private double calculateC(int n, double epsilon, double beta) {
    double gamma = calculateGamma(epsilon, beta);
    return (new Exp()).value(-1.0d * n * (gamma * (new Log()).value(gamma / beta) - (gamma - beta)));
}

From source file:org.deidentifier.arx.criteria.EDDifferentialPrivacy.java

/**
 * Calculates gamma// w  w  w.jav  a  2 s .  c om
 * @param epsilon
 * @param beta
 * @return
 */
private double calculateGamma(double epsilon, double beta) {
    double power = (new Exp()).value(epsilon);
    return (power - 1.0d + beta) / power;
}