walterddr commented on a change in pull request #9733: [FLINK-14154][ml] Add 
the class for multivariate Gaussian Distribution.
URL: https://github.com/apache/flink/pull/9733#discussion_r339354278
 
 

 ##########
 File path: 
flink-ml-parent/flink-ml-lib/src/main/java/org/apache/flink/ml/common/statistics/basicstatistic/MultivariateGaussian.java
 ##########
 @@ -0,0 +1,148 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.flink.ml.common.statistics.basicstatistic;
+
+import org.apache.flink.ml.common.linalg.BLAS;
+import org.apache.flink.ml.common.linalg.DenseMatrix;
+import org.apache.flink.ml.common.linalg.DenseVector;
+import org.apache.flink.ml.common.linalg.SparseVector;
+import org.apache.flink.ml.common.linalg.Vector;
+
+import com.github.fommil.netlib.LAPACK;
+import org.netlib.util.intW;
+
+/**
+ * This class provides basic functionality for a Multivariate Gaussian 
(Normal) Distribution.
+ */
+public class MultivariateGaussian {
+
+       private static final LAPACK LAPACK_INST = LAPACK.getInstance();
+       private static final double EPSILON;
+
+       static {
+               double eps = 1.0;
+               while ((1.0 + (eps / 2.0)) != 1.0) {
+                       eps /= 2.0;
+               }
+               EPSILON = eps;
+       }
+
+       private final DenseVector mean;
+       private final DenseMatrix cov;
+
+       private DenseMatrix rootSigmaInv;
+       private double u;
+
+       // data buffers for computing pdf
+       private DenseVector delta;
+       private DenseVector v;
+
+       /**
+        * The constructor.
+        *
+        * @param mean The mean vector of the distribution.
+        * @param cov  The covariance matrix of the distribution.
+        */
+       public MultivariateGaussian(DenseVector mean, DenseMatrix cov) {
+               this.mean = mean;
+               this.cov = cov;
+               this.delta = DenseVector.zeros(mean.size());
+               this.v = DenseVector.zeros(mean.size());
+               calculateCovarianceConstants();
+       }
+
+       /**
+        * Returns density of this multivariate Gaussian at given point, x.
+        */
+       public double pdf(Vector x) {
+               return Math.exp(logpdf(x));
+       }
+
+       /**
+        * Returns the log-density of this multivariate Gaussian at given 
point, x.
+        */
+       public double logpdf(Vector x) {
+               int n = mean.size();
+               System.arraycopy(mean.getData(), 0, delta.getData(), 0, n);
+               BLAS.scal(-1.0, delta);
+               if (x instanceof DenseVector) {
+                       BLAS.axpy(1., (DenseVector) x, delta);
+               } else if (x instanceof SparseVector) {
+                       BLAS.axpy(1., (SparseVector) x, delta);
+               }
+               BLAS.gemv(1.0, rootSigmaInv, false, delta, 0., v);
+               return u - 0.5 * BLAS.dot(v, v);
+       }
+
+       /**
+        * Compute distribution dependent constants.
+        *
+        * <p>The probability density function is calculated as:
+        * pdf(x) = (2*pi)^(-k/2)^ * det(sigma)^(-1/2)^ * exp((-1/2) * (x-mu).t 
* inv(sigma) * (x-mu))
+        *
+        * <p>Here we compute the following distribution dependent constants 
that can be reused in each pdf computation:
+        * A) u = log((2*pi)^(-k/2)^ * det(sigma)^(-1/2)^)
+        * B) rootSigmaInv = sqrt(inv(sigma)) = D^(-1/2)^ * U.t
+        *
+        * <p><ul>
+        * <li> sigma = U * D * U.t
+        * <li> inv(sigma) = U * inv(D) * U.t = (D^{-1/2}^ * U.t).t * 
(D^{-1/2}^ * U.t)
+        * <li> sqrt(inv(sigma)) = D^(-1/2)^ * U.t
+        * </ul></p>
+        */
+       private void calculateCovarianceConstants() {
+               int n = this.mean.size();
+               int lwork = 3 * n - 1;
+               double[] matA = new double[n * n];
+               double[] work = new double[lwork];
+               double[] evs = new double[n];
+               intW info = new intW(0);
+
+               for (int i = 0; i < n; i++) {
+                       System.arraycopy(cov.getData(), i * n, matA, i * n, i + 
1);
+               }
 
 Review comment:
   `System.arraycopy(cov.getData(), 0, matA, 0, n*n);`?

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to