
     i                      4    d Z ddlZddZ	 d	dZdeddfdZdS )
a  
Covariance calculation --- :mod:`encore.covariance`
=====================================================================

The module contains functions to estimate the covariance matrix of
an ensemble of structures.

:Author: Matteo Tiberti, Wouter Boomsma, Tone Bengtsen

.. versionadded:: 0.16.0

.. deprecated:: 2.8.0
   This module is deprecated in favour of the 
   MDAKit `mdaencore <https://mdanalysis.org/mdaencore/>`_ and will be removed
   in MDAnalysis 3.0.0.

    Nc                     || |z
  }n| t          j        | d          z
  }t          j        | j        d         | j        d         f          }|D ]}|t          j        ||          z  }|| j        d         z  }|S )ae  
    Standard maximum likelihood estimator of the covariance matrix.

    Parameters
    ----------

    coordinates : numpy.array
        Flattened array of coordiantes

    reference_coordinates : numpy.array
        Optional reference to use instead of mean

    Returns
    -------

    cov_mat : numpy.array
        Estimate of  covariance matrix

    Nr   axis   )npaveragezerosshapeouter)coordinatesreference_coordinatescoordinates_offsetcoordinates_covframes        o/srv/www/vhosts/g4struct/public_html/venv/lib/python3.11/site-packages/MDAnalysis/analysis/encore/covariance.pyml_covariance_estimatorr   +   s    * ( )+@@ )2:k+J+J+JJ h 1! 4k6G6JKLLO# 2 228E5111{(++O    c           
      x   | }|j         d         }|j         d         }t          j        |d          }||}||z
  }t          j        |d          }t          t          j        ||ddt          j        f         g          d          |dz
  z  t          |          z  }|d||f         }	|||f         }
|d|d|f         }t          j        |	|	          |
z  }t          j        |          |t          j	        
                    t          j        |                    <   |t          j                            ||z
  d          dz  }|dz  }dt          |          z  t          j        t          j        t          j        |          |                    z  t          j        t          j        |dz                      z
  }dt          |          z  t          j        t          j        |dz                      z  t          j        t          j        |          dz            z
  }|t          j        |ddt          j        f         |d          z  }dt          |          z  t          j        t          j        |          |          z  t          j        |	ddt          j        f         |d          |z  z
  }t          j        |t          j        t          j        |	ddt          j        f         |d                    z            |
z  t          j        t          j        |          |	z            |
z  z
  }dt          |          z  t          j        t          j        |          |          z  |
|z  z
  }t          j        |t          j        |	|	          z            |
dz  z  t          j        t          j        |          |	dz  z            |
dz  z  z
  }d|z  |z
  }||z   }||z
  |z  }t%          dt'          d|t          |          z                      }||z  d|z
  |z  z   }|S )a  
    Shrinkage estimator of the covariance matrix using the method described in

    Improved Estimation of the Covariance Matrix of Stock Returns With an
    Application to Portfolio Selection. Ledoit, O.; Wolf, M., Journal of
    Empirical Finance, 10, 5, 2003

    This implementation is based on the matlab code made available by Olivier
    Ledoit on his website:
    http://www.ledoit.net/ole2_abstract.htm

    Parameters
    ----------

    coordinates : numpy.array
        Flattened array of coordinates

    reference_coordinates: numpy.array
        Optional reference to use instead of mean

    shrinkage_parameter: None or float
        Optional shrinkage parameter

    Returns
    --------

    cov_mat : nump.array
        Covariance matrix
    r   r   r   Nfro)ord   )r
   r   r   r   hstacknewaxisfloatr   diagma	make_maskeyelinalgnormsumdot	transposerepeatmaxmin)r   r   shrinkage_parameterxtnmean_xxmktsamplecovmktvarmktpriorcyprdiagzv1roff1v3roff3roffrksigmas                            r   shrinkage_covariance_estimatorr>   R   s   B 	A	
A	
AZ"""F (&	F
A:aa   D 	 	1d111bj=.A*B C CQGGq5	
((	  AaCF^FAqD\FBQBF^F HVV$$v-E(*E"%//"&))
$
$% " INN6E>uN55:qDaL26"&a!"<"<===F619A
 A
 
 E!HHrvbfQTll333bfGFOOq 7
 7
 
 	$qqq"*}-qq9999aL26",q//1555iqqq"*}-qq999FBC 	
 FR\")F111bj=,A11"M"M"MNNN   fRWR[[6)**V3	4 	 q\BF2<??A666&HF200011FAI=fRWR[[619,--	9: 	 5y5 DL UaK!!SAaL%9%9::  %'1/B+Bf*LLELr   zname CAmassc                 b   | j                             |                     |          d          }t          j        ||j        d         df          }d}|5|                    |          }|j        j        }|                                } |||          }|t          |t          t          t          j        f          s0|dk    r*|r|                     |          j        }nX| j        j        }nK|r|                     |          j        }	n| j        j        }	|	t          |          k    rt!          d          t          j        |d          }t          j        t          j        t          |                    |z            }
t          j        |
t          j        ||
                    }|S )	a  
    Calculates (optionally mass weighted) covariance matrix

    Parameters
    ----------
    ensemble : Universe object
        The structural ensemble
    select : str (optional)
        Atom selection string in the MDAnalysis format.
    estimator : function (optional)
        Function that estimates the covariance matrix. It requires at least
        a "coordinates" numpy array (of shape (N,M,3), where N is the number
        of frames and M the number of atoms). See ml_covariance_estimator and
        shrinkage_covariance_estimator for reference.
    weights : str/array_like (optional)
        specify weights. If ``'mass'`` then chose masses of ensemble atoms, if ``None`` chose uniform weights
    reference : MDAnalysis.Universe object (optional)
        Use the distances to a specific reference structure rather than the
        distance to the mean.

    Returns
    -------
    cov_mat : numpy.array
        Covariance matrix

    fac)orderr   Nr?   z;number of weights is unequal to number of atoms in ensemble   )
trajectory
timeseriesselect_atomsr   reshaper
   atoms	positionsflatten
isinstancelisttuplendarraymassesn_atomslen
ValueErrorr$   sqrtidentityr"   )ensembleselect	estimatorweights	referencer   r   reference_atom_selectionr=   req_lenweight_matrixs              r   covariance_matrixr^      s   D %00f%%U 1  K
 *[;+<Q+?*DEEK !#,#9#9&#A#A  8 > H !6 = = ? ?Ik#899E  7T5"*$=>>	6!! 0"//77>"./ 1"//77?".0#g,,&& (   )GQ''CLL 9 9G CDD}bfUM&B&BCCLr   )N)NN)__doc__numpyr   r   r>   r^    r   r   <module>rb      s   . "    $ $ $ $P BFf f f fV ,Q Q Q Q Q Qr   