Gets the gaussian pdf value. - Java java.lang

Java examples for java.lang:Math Value

Description

Gets the gaussian pdf value.

Demo Code

/**//from w  w  w  .  j  a va2s .co  m
 * Copyright 2004-2006 DFKI GmbH.
 * All Rights Reserved.  Use is subject to license terms.
 * 
 * Permission is hereby granted, free of charge, to use and distribute
 * this software and its documentation without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of this work, and to
 * permit persons to whom this work is furnished to do so, subject to
 * the following conditions:
 * 
 * 1. The code must retain the above copyright notice, this list of
 *    conditions and the following disclaimer.
 * 2. Any modifications must be clearly marked as such.
 * 3. Original authors' names are not deleted.
 * 4. The authors' names are not used to endorse or promote products
 *    derived from this software without specific prior written
 *    permission.
 *
 * DFKI GMBH AND THE CONTRIBUTORS TO THIS WORK DISCLAIM ALL WARRANTIES WITH
 * REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL DFKI GMBH NOR THE
 * CONTRIBUTORS BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
 * THIS SOFTWARE.
 */
import java.util.Arrays;
import java.util.Collections;
import java.util.Vector;

public class Main{
    /**
     * Gets the gaussian pdf value.
     *
     * @param x the x
     * @param meanVector the mean vector
     * @param covarianceMatrix the covariance matrix
     * @return the gaussian pdf value
     */
    public static double getGaussianPdfValue(double[] x,
            double[] meanVector, double[] covarianceMatrix) {
        double constantTerm = getGaussianPdfValueConstantTerm(x.length,
                determinant(covarianceMatrix));

        return getGaussianPdfValue(x, meanVector, covarianceMatrix,
                constantTerm);
    }
    /**
     * Gets the gaussian pdf value.
     *
     * @param x the x
     * @param meanVector the mean vector
     * @param covarianceMatrix the covariance matrix
     * @param constantTerm the constant term
     * @return the gaussian pdf value
     */
    public static double getGaussianPdfValue(double[] x,
            double[] meanVector, double[] covarianceMatrix,
            double constantTerm) {
        double P = 0.0;
        int i;

        for (i = 0; i < x.length; i++)
            P += (x[i] - meanVector[i]) * (x[i] - meanVector[i])
                    / covarianceMatrix[i];

        P *= -0.5;

        P = constantTerm * Math.exp(P);

        return P;
    }
    /**
     * Gets the gaussian pdf value.
     *
     * @param x the x
     * @param meanVector the mean vector
     * @param detCovarianceMatrix the det covariance matrix
     * @param inverseCovarianceMatrix the inverse covariance matrix
     * @return the gaussian pdf value
     */
    public static double getGaussianPdfValue(double[] x,
            double[] meanVector, double detCovarianceMatrix,
            double[][] inverseCovarianceMatrix) {
        double constantTerm = Math.pow(2 * Math.PI, 0.5 * x.length);
        constantTerm *= Math.sqrt(detCovarianceMatrix);
        constantTerm = 1.0 / constantTerm;

        return getGaussianPdfValue(x, meanVector, inverseCovarianceMatrix,
                constantTerm);
    }
    /**
     * Gets the gaussian pdf value.
     *
     * @param x the x
     * @param meanVector the mean vector
     * @param inverseCovarianceMatrix the inverse covariance matrix
     * @param constantTerm the constant term
     * @return the gaussian pdf value
     */
    public static double getGaussianPdfValue(double[] x,
            double[] meanVector, double[][] inverseCovarianceMatrix,
            double constantTerm) {
        double[][] z = new double[1][x.length];
        z[0] = MathUtils.substract(x, meanVector);
        double[][] zT = MathUtils.transpoze(z);

        double[][] prod = MathUtils.matrixProduct(
                MathUtils.matrixProduct(z, inverseCovarianceMatrix), zT);

        double P = -0.5 * prod[0][0];

        P = constantTerm * Math.exp(P);

        return P;
    }
    /**
     * Gets the gaussian pdf value constant term.
     *
     * @param featureDimension the feature dimension
     * @param detCovarianceMatrix the det covariance matrix
     * @return the gaussian pdf value constant term
     */
    public static double getGaussianPdfValueConstantTerm(
            int featureDimension, double detCovarianceMatrix) {
        double constantTerm = Math.pow(2 * Math.PI, 0.5 * featureDimension);
        constantTerm *= Math.sqrt(detCovarianceMatrix);
        constantTerm = 1.0 / constantTerm;

        return constantTerm;
    }
    /**
     * Determinant.
     *
     * @param diagonal the diagonal
     * @return the double
     */
    public static double determinant(double[] diagonal) {
        double det = 1.0;
        for (int i = 0; i < diagonal.length; i++)
            det *= diagonal[i];

        return det;
    }
    /**
     * Determinant.
     *
     * @param matrix the matrix
     * @return the double
     */
    public static double determinant(double[][] matrix) {
        double result = 0;

        if (matrix.length == 1)
            return determinant(matrix[0]);

        if (matrix.length == 1 && matrix[0].length == 1)
            return matrix[0][0];

        if (matrix.length == 2) {
            result = matrix[0][0] * matrix[1][1] - matrix[0][1]
                    * matrix[1][0];
            return result;
        }

        for (int i = 0; i < matrix[0].length; i++) {
            double temp[][] = new double[matrix.length - 1][matrix[0].length - 1];
            for (int j = 1; j < matrix.length; j++) {
                for (int k = 0; k < matrix[0].length; k++) {
                    if (k < i)
                        temp[j - 1][k] = matrix[j][k];
                    else if (k > i)
                        temp[j - 1][k - 1] = matrix[j][k];
                }
            }

            result += matrix[0][i] * Math.pow(-1, (double) i)
                    * determinant(temp);
        }

        return result;
    }
    /**
     * Exp.
     *
     * @param a the a
     * @return the double[]
     */
    public static double[] exp(double[] a) {
        double[] c = new double[a.length];
        for (int i = 0; i < a.length; i++) {
            c[i] = Math.exp(a[i]);
        }
        return c;
    }
    /**
     * Substract.
     *
     * @param a the a
     * @param b the b
     * @return the double[]
     */
    public static double[] substract(double[] a, double[] b) {
        if (a.length != b.length) {
            throw new IllegalArgumentException(
                    "Arrays must be equal length");
        }
        double[] c = new double[a.length];
        for (int i = 0; i < a.length; i++) {
            c[i] = a[i] - b[i];
        }
        return c;
    }
    /**
     * Substract.
     *
     * @param a the a
     * @param b the b
     * @return the double[]
     */
    public static double[] substract(double[] a, double b) {
        double[] c = new double[a.length];
        for (int i = 0; i < a.length; i++) {
            c[i] = a[i] - b;
        }
        return c;
    }
    /**
     * Substract.
     *
     * @param x the x
     * @param y the y
     * @return the double[][]
     */
    public static double[][] substract(double[][] x, double[][] y) {
        double[][] z = null;

        if (x != null && y != null) {
            int i, j;
            assert x.length == y.length;
            for (i = 0; i < x.length; i++) {
                assert x[i].length == x[0].length;
                assert x[i].length == y[i].length;
            }

            z = new double[x.length][x[0].length];

            for (i = 0; i < x.length; i++) {
                for (j = 0; j < x[i].length; j++)
                    z[i][j] = x[i][j] - y[i][j];
            }
        }

        return z;
    }
    /**
     * Substract.
     *
     * @param a the a
     * @param b the b
     * @return the float[]
     */
    public static float[] substract(float[] a, float[] b) {
        if (a.length != b.length) {
            throw new IllegalArgumentException(
                    "Arrays must be equal length");
        }
        float[] c = new float[a.length];
        for (int i = 0; i < a.length; i++) {
            c[i] = a[i] - b[i];
        }
        return c;
    }
    /**
     * Substract.
     *
     * @param a the a
     * @param b the b
     * @return the float[]
     */
    public static float[] substract(float[] a, float b) {
        float[] c = new float[a.length];
        for (int i = 0; i < a.length; i++) {
            c[i] = a[i] - b;
        }
        return c;
    }
    /**
     * Transpoze.
     *
     * @param x the x
     * @return the double[][]
     */
    public static double[][] transpoze(double[] x) {
        double[][] y = new double[x.length][1];
        for (int i = 0; i < x.length; i++)
            y[i][0] = x[i];

        return y;
    }
    /**
     * Transpoze.
     *
     * @param x the x
     * @return the double[][]
     */
    public static double[][] transpoze(double[][] x) {
        double[][] y = null;

        if (x != null) {
            int i, j;
            int rowSizex = x.length;
            int colSizex = x[0].length;
            for (i = 1; i < rowSizex; i++)
                assert x[i].length == colSizex;

            y = new double[colSizex][rowSizex];
            for (i = 0; i < rowSizex; i++) {
                for (j = 0; j < colSizex; j++)
                    y[j][i] = x[i][j];
            }
        }

        return y;
    }
    /**
     * Matrix product.
     *
     * @param x the x
     * @param y the y
     * @return the double[]
     */
    public static double[] matrixProduct(double[][] x, double[] y) {
        double[][] y2 = new double[y.length][1];
        int i;
        for (i = 0; i < y.length; i++)
            y2[i][0] = y[i];

        y2 = matrixProduct(x, y2);

        double[] y3 = new double[y2.length];
        for (i = 0; i < y2.length; i++)
            y3[i] = y2[i][0];

        return y3;
    }
    /**
     * Matrix product.
     *
     * @param x the x
     * @param y the y
     * @return the double[][]
     */
    public static double[][] matrixProduct(double[] x, double[][] y) {
        double[][] x2 = new double[x.length][1];
        int i;
        for (i = 0; i < x.length; i++)
            x2[i][0] = x[i];

        return matrixProduct(x2, y);
    }
    /**
     * Matrix product.
     *
     * @param x the x
     * @param y the y
     * @return the double[][]
     */
    public static double[][] matrixProduct(double[][] x, double[][] y) {
        double[][] z = null;

        if (x != null && y != null) {
            if (x.length == 1 && y.length == 1) //Special case -- diagonal matrix multiplication, returns a diagonal matrix
            {
                assert x[0].length == y[0].length;
                z = new double[1][x[0].length];
                for (int i = 0; i < x[0].length; i++)
                    z[0][i] = x[0][i] * y[0][i];
            } else {
                int i, j, m;
                int rowSizex = x.length;
                int colSizex = x[0].length;
                int rowSizey = y.length;
                int colSizey = y[0].length;
                for (i = 1; i < x.length; i++)
                    assert x[i].length == colSizex;
                for (i = 1; i < y.length; i++)
                    assert y[i].length == colSizey;
                assert colSizex == rowSizey;

                z = new double[rowSizex][colSizey];
                double tmpSum;
                for (i = 0; i < rowSizex; i++) {
                    for (j = 0; j < colSizey; j++) {
                        tmpSum = 0.0;
                        for (m = 0; m < x[i].length; m++)
                            tmpSum += x[i][m] * y[m][j];

                        z[i][j] = tmpSum;
                    }
                }
            }
        }

        return z;
    }
}

Related Tutorials