B
    {‹d/  ã               @   sL   d Z ddlmZmZmZ ddlmZ e ¡  ddlZ	ddd„Z
dd	d
„ZdS )z\
Part of awkde package
---------------------

Helper tools for standardizing a data sample.
é    )Úprint_functionÚdivisionÚabsolute_import)Ústandard_libraryNTFc             C   s>  t | jƒdkrtdƒ‚|dkrJ|dkrJtj| dd}t tj| dd¡}nZ|dk	r¤|dk	r¤t |¡}t |¡}t |ƒ| jd krˆtd	ƒ‚|jd | jd kr¤td
ƒ‚|rÂt |¡t 	|jd ¡ }|rÞtj
 tj
 |¡¡j}n4tj
 tj
 |¡¡\}}t |¡}	t |	| |j¡}t || | j¡j}
|r6|
||fS |
S dS )aæ  
    Standardizes a n-dimensional sample using the Mahalanobis distance.

    .. math:: x' = \Sigma^{-1/2} (x - y)

    The resulting sample :math:`x'` has a zero mean vector and an identity
    covariance.

    Parameters
    ----------
    sam : array-like, shape (n_samples, n_features)
        Data points in the sample, each column is a feature, each row a point.
    mean : array-like, shape (n_features), optional
        If explicitely given, use this mean vector for the transformation. If
        None, the estimated mean from data is used. (default: None)
    cov : array-like, shape (n_features, n_features), optional
        If explicitely given, use this covariance matrix for the transformation.
        If None, the estimated cov from data is used. (default: None)
    cholesky : bool, optional
        If true, use fast Cholesky decomposition to calculate the sqrt of the
        inverse covariance matrix. Else use eigenvalue decomposition (Can be
        numerically unstable, not recommended). (default: True)
    ret_stats : bool, optional
        If True, the mean vector and covariance matrix of the input sample are
        returned, too. (default: False)
    diag : bool
        If True, only scale by variance, diagonal cov matrix. (default: False)

    Returns
    -------
    stand_sam : array-like, shape (n_samples, n_features)
        Standardized sample, with mean = [0., ..., 0.] and cov = identity.

    Optional Returns
    ----------------
    mean : array-like, shape(n_features)
        Mean vector of the input data, only if ret_stats is True.
    cov : array-like, shape(n_features, n_features)
        Covariance matrix of the input data, only if ret_stats is True.

    Example
    -------
    >>> mean = [10, -0.01, 1]
    >>> cov = [[14, -.2, 0], [-.2, .1, -0.1], [0, -0.1, 1]]
    >>> sam = np.random.multivariate_normal(mean, cov, size=1000)
    >>> std_sam = standardize_nd_sample(sam)
    >>> print(np.mean(std_sam, axis=0))
    >>> print(np.cov(std_sam, rowvar=False))
    é   z/Shape of `sam` must be (n_samples, n_features).Nr   )ZaxisF)Zrowvaré   z*Dimensions of mean and sample don't match.z)Dimensions of cov and sample don't match.)ÚlenÚshapeÚ
ValueErrorÚ_npÚmeanÚ
atleast_2dÚcovÚ
atleast_1dÚdiagÚeyeÚlinalgÚcholeskyÚinvÚTZeigÚsqrtÚdot)Úsamr   r   r   Z	ret_statsr   Ú
sqrtinvcovZlamÚQZsqrtlamZ	stand_sam© r   úX/work/yifan.wang/ringdown/master-ringdown-env/lib/python3.7/site-packages/awkde/tools.pyÚstandardize_nd_sample   s.    3



r   c             C   s–   t | jƒdkrtdƒ‚t |¡}t |¡}t |ƒ| jd krDtdƒ‚|jd | jd kr`tdƒ‚tj tj |¡¡j	}tj |¡}t 
|| j	¡j	| S )a‹  
    Shift and scale a nD sample by given mean and covariance matrix.

    This is the inverse operation of `standardize_nd_sample`. If a
    standardized sample :math:`x'` with zero mean vector and identity covariance
    matrix is given, it is rescaled and shifted using

    .. math:: x = (\Sigma^{1/2} x) + y

    then having a mean vector `mean` and a covariance matrix `cov`.

    Parameters
    ----------
    sam : array-like, shape (n_samples, n_features)
        Data points in the sample, each column is a feature, each row a point.
    mean : array-like, shape (n_features)
        Mean vector used for the transformation.
    cov : array-like, shape (n_features, n_features)
        Covariance matrix used for the transformation.

    Returns
    -------
    scaled_sam : array-like, shape (n_samples, n_features)
        Scaled sample using the transformation with the given mean and cov.
    r   z/Shape of `sam` must be (n_samples, n_features).r   z*Dimensions of mean and sample don't match.r   z)Dimensions of cov and sample don't match.)r   r	   r
   r   r   r   r   r   r   r   r   )r   r   r   r   r   Zsqrtcovr   r   r   Úshift_and_scale_nd_samplej   s    

r   )NNTFF)T)Ú__doc__Ú
__future__r   r   r   Úfuturer   Zinstall_aliasesÚnumpyr   r   r   r   r   r   r   Ú<module>   s    
X