Github user vectorijk commented on a diff in the pull request:

    https://github.com/apache/spark/pull/13248#discussion_r65798896
  
    --- Diff: python/pyspark/ml/stat/distribution.py ---
    @@ -0,0 +1,267 @@
    +#
    +# 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.
    +#
    +
    +from pyspark.ml.linalg import DenseVector, DenseMatrix, Vector
    +import numpy as np
    +
    +__all__ = ['MultivariateGaussian']
    +
    +
    +
    +class MultivariateGaussian():
    +    """
    +    This class provides basic functionality for a Multivariate Gaussian 
(Normal) Distribution. In
    +     the event that the covariance matrix is singular, the density will be 
computed in a
    +    reduced dimensional subspace under which the distribution is supported.
    +    (see 
[[http://en.wikipedia.org/wiki/Multivariate_normal_distribution#Degenerate_case]])
    +
    +    mu The mean vector of the distribution
    +    sigma The covariance matrix of the distribution
    +
    +
    +    >>> mu = Vectors.dense([0.0, 0.0])
    +    >>> sigma= DenseMatrix(2, 2, [1.0, 1.0, 1.0, 1.0])
    +    >>> x = Vectors.dense([1.0, 1.0])
    +    >>> m = MultivariateGaussian(mu, sigma)
    +    >>> m.pdf(x)
    +    0.0682586811486
    +
    +    """
    +
    +    def __init__(self, mu, sigma):
    +        """
    +        __init__(self, mu, sigma)
    +
    +        mu The mean vector of the distribution
    +        sigma The covariance matrix of the distribution
    +
    +        mu and sigma must be instances of DenseVector and DenseMatrix 
respectively.
    +
    +        """
    +
    +
    +        assert (isinstance(mu, DenseVector)), "mu must be a DenseVector 
Object"
    +        assert (isinstance(sigma, DenseMatrix)), "sigma must be a 
DenseMatrix Object"
    +
    +        sigma_shape=sigma.toArray().shape
    +        assert (sigma_shape[0]==sigma_shape[1]) , "Covariance matrix must 
be square"
    +        assert (sigma_shape[0]==mu.size) , "Mean vector length must match 
covariance matrix size"
    +
    +        # initialize eagerly precomputed attributes
    +        
    +        self.mu=mu
    +
    +        # storing sigma as numpy.ndarray
    +        # furthur calculations are done ndarray only
    +        self.sigma=sigma.toArray()
    +        
    +
    +        # initialize attributes to be computed later
    +
    +        self.prec_U = None
    +        self.log_det_cov = None
    +
    +        # compute distribution dependent constants
    +        self.__calculateCovarianceConstants()
    +
    +
    +    def pdf(self,x):
    +        """
    +        Returns density of this multivariate Gaussian at a point given by 
Vector x
    +        """
    +        assert (isinstance(x, Vector)), "x must be of Vector Type"
    +        return float(self.__pdf(x))
    +
    +    def logpdf(self,x):
    +        """
    +        Returns the log-density of this multivariate Gaussian at a point 
given by Vector x
    +        """
    +        assert (isinstance(x, Vector)), "x must be of Vector Type"
    +        return float(self.__logpdf(x))
    +
    +    def __calculateCovarianceConstants(self):
    +        """
    +        Calculates distribution dependent components used for the density 
function
    +        based on scipy multivariate library
    +        refer 
https://github.com/scipy/scipy/blob/master/scipy/stats/_multivariate.py
    --- End diff --
    
    same here.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org
For additional commands, e-mail: reviews-h...@spark.apache.org

Reply via email to