U
    Ãmœdÿ  ã                   @   s\  d Z ddlZddlmZ ddlmZ G dd„ dƒZG dd„ deƒZG d	d
„ d
eƒZ	dd„ Z
ddd„Zdd„ Zedk�rXdZe e¡Zej de¡Zdde e¡  dej e¡  Ze e¡Zeed�Zeeejƒƒ ejZeeeƒƒ eed�Zeee e eje¡¡ej  ƒƒ eee !ej ¡ƒƒ eed�Z"ee"j#d ƒ eej#d ƒ e
ee"ƒ dS )a8  Recipes for more efficient work with linalg using classes


intended for use for multivariate normal and linear regression
calculations

x  is the data (nobs, nvars)
m  is the moment matrix (x'x) or a covariance matrix Sigma

examples:
x'sigma^{-1}x
z = Px  where P=Sigma^{-1/2}  or P=Sigma^{1/2}

Initially assume positive definite, then add spectral cutoff and
regularization of moment matrix, and extend to PCA

maybe extend to sparse if some examples work out
(transformation matrix P for random effect and for toeplitz)


Author: josef-pktd
Created on 2010-10-20
é    N)Úlinalg)Úcache_readonlyc                   @   sª   e Zd ZdZd!dd„Zedd„ ƒZdd„ Zd	d
„ Zedd„ ƒZ	edd„ ƒZ
dd„ Zdd„ Zdd„ Zdd„ Zedd„ ƒZedd„ ƒZedd„ ƒZedd„ ƒZedd „ ƒZdS )"ÚPlainMatrixArrayz³Class that defines linalg operation on an array

    simplest version as benchmark

    linear algebra recipes for multivariate normal and linear
    regression calculations

    Nc                 C   sn   |d k	r<|d kr2t  |¡| _t  | jj| j¡| _qjtdƒ‚n.|d k	rbt  |¡| _t j| jjŽ | _ntdƒ‚d S )Nz!data and sym cannot be both givenz#either data or sym need to be given)	ÚnpZasarrayÚxÚdotÚTÚmÚ
ValueErrorÚeyeÚshape©ÚselfÚdataÚsym© r   úd/home/sam/Atlas/atlas_env/lib/python3.8/site-packages/statsmodels/sandbox/archive/linalg_decomp_1.pyÚ__init__(   s    
zPlainMatrixArray.__init__c                 C   s   t j | j¡S ©N)r   r   Úinvr	   ©r   r   r   r   Úminv6   s    zPlainMatrixArray.minvc                 C   s   t  | j|¡S r   )r   r   r	   ©r   Úyr   r   r   Úm_y:   s    zPlainMatrixArray.m_yc                 C   s   t  | j|¡S r   )r   r   r   r   r   r   r   Úminv_y=   s    zPlainMatrixArray.minv_yc                 C   s   t  | j¡S r   )r   Úpinvr	   r   r   r   r   Úmpinv@   s    zPlainMatrixArray.mpinvc                 C   s   t  | j¡S r   )r   r   r   r   r   r   r   ÚxpinvD   s    zPlainMatrixArray.xpinvc                 C   s   t  |jt  | j|¡¡S r   )r   r   r   r	   r   r   r   r   Úyt_m_yH   s    zPlainMatrixArray.yt_m_yc                 C   s   t  |jt  | j|¡¡S r   )r   r   r   r   r   r   r   r   Ú	yt_minv_yK   s    zPlainMatrixArray.yt_minv_yc                 C   s   t  |t  | j|j¡¡S r   )r   r   r	   r   r   r   r   r   Úy_m_ytO   s    zPlainMatrixArray.y_m_ytc                 C   s   t  |t  | j|j¡¡S r   )r   r   r   r   r   r   r   r   Ú	y_minv_ytR   s    zPlainMatrixArray.y_minv_ytc                 C   s   t  | j¡S r   )r   Údetr	   r   r   r   r   ÚmdetU   s    zPlainMatrixArray.mdetc                 C   s   t  t | j¡¡S r   )r   Úlogr   r#   r	   r   r   r   r   ÚmlogdetY   s    zPlainMatrixArray.mlogdetc                 C   s<   t  | j¡\}}t |¡d d d… }|| |d d …|f fS )Néÿÿÿÿ)r   Zeighr	   r   Zargsort)r   ÚevalsÚevecsZsortindr   r   r   Úmeigh]   s    zPlainMatrixArray.meighc                 C   s"   | j \}}t t |d ¡|j¡S )Ng      à?)r*   r   r   Údiagr   ©r   r(   r)   r   r   r   Úmhalfc   s    
zPlainMatrixArray.mhalfc                 C   s&   | j \}}t |dt |¡ |j ¡S )Nç      ð?)r*   r   r   Úsqrtr   r,   r   r   r   Úminvhalfj   s    
zPlainMatrixArray.minvhalf)NN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r   r   r   r    r!   r"   r$   r&   r*   r-   r0   r   r   r   r   r      s0   







r   c                       s~   e Zd ZdZd‡ fdd„	Zdd„ Zedd„ ƒZed	d
„ ƒZedd„ ƒZ	edd„ ƒZ
edd„ ƒZedd„ ƒZedd„ ƒZ‡  ZS )ÚSvdArrayz´Class that defines linalg operation on an array

    svd version, where svd is taken on original data array, if
    or when it matters

    no spectral cutoff in first version
    Nc                    sr   t t| ƒj||d� tjj| jdd�\}}}|||  | _| _| _	tj
|ftjžŽ | _tj
d| ftjžŽ | _d S )N©r   r   é   )Zfull_matricesr.   )Úsuperr5   r   r   r   Zsvdr   ÚuÚsÚvÚdiagsvdr   ÚsdiagÚsinvdiag)r   r   r   r9   r:   r;   ©Ú	__class__r   r   r   z   s
    zSvdArray.__init__c                 C   s   t jt | j|¡ftjžŽ S r   )r   r<   r   Úpowerr:   r   r   )r   Úpr   r   r   Ú	_sdiagpow‚   s    zSvdArray._sdiagpowc                 C   s   t  | j| j¡}t  |j|¡S r   )r   r   r>   r;   r   )r   Zsinvvr   r   r   r   …   s    zSvdArray.minvc                 C   s   | j j}| jd }||fS )Né   )r;   r   r:   )r   r)   r(   r   r   r   r*   Š   s    
zSvdArray.meighc                 C   s   | j d  ¡ S ©Nr   )r*   Úprodr   r   r   r   r$   �   s    zSvdArray.mdetc                 C   s   t  | jd ¡ ¡ S rE   )r   r%   r*   Úsumr   r   r   r   r&   ”   s    zSvdArray.mlogdetc                 C   s   t  t  | j¡| j¡S r   )r   r   r+   r:   r;   r   r   r   r   r-   ˜   s    zSvdArray.mhalfc                 C   s   t  | j| j¡S r   )r   r   r9   r=   r   r   r   r   Úxxthalfœ   s    zSvdArray.xxthalfc                 C   s   t  | j| j¡S r   )r   r   r9   r>   r   r   r   r   Ú
xxtinvhalf    s    zSvdArray.xxtinvhalf)NN)r1   r2   r3   r4   r   rC   r   r   r*   r$   r&   r-   rH   rI   Ú__classcell__r   r   r?   r   r5   q   s"   





r5   c                       s*   e Zd ZdZd‡ fdd„	Zdd„ Z‡  ZS )Ú	CholArrayzÞClass that defines linalg operation on an array

    cholesky version, where svd is taken on original data array, if
    or when it matters

    plan: use cholesky factor and cholesky solve
    nothing implemented yet
    Nc                    s   t t| ƒj||d� d S )Nr6   )r8   r5   r   r   r?   r   r   r   ¯   s    zCholArray.__init__c                 C   s   t  tt t | j¡t¡¡S )z<xSigmainvx
        does not use stored cholesky yet
        )r   r   r   r   Z	cho_solveZ
cho_factorr	   r   r   r   r   r    ³   s    zCholArray.yt_minv_y)NN)r1   r2   r3   r4   r   r    rJ   r   r   r?   r   rK   ¥   s   	rK   c                 C   s  ddl m}m} d}|| j|j|d� t | j d¡¡d d …d f }t |j d¡¡d d …d f }|| }|| j|j| |d� || j|j|d� | j	\}}	|j	\}
}|||
|d� t |	 d¡¡}t | d¡¡}|| }||	|| |d� || j
|j
dd� || j|jdd� d S )Nr   )Úassert_almost_equalÚassert_approx_equalé   )Údecimalr7   é   )Zsignificant)Znumpy.testingrL   rM   r   r   Úsignr-   rG   r0   r*   r$   r&   )Úm1Úm2rL   rM   rO   Ús1Ús2ZscorrZevals1Zevecs1Zevals2Zevecs2r   r   r   Útestcompare¾   s"    

rV   çVçž¯Ò<c                 C   s   t  |  ¡ ¡|k }d| |< | S )z<replace abs values smaller than eps by zero, makes copy
    r   )r   ÚabsÚcopy)r   ZepsÚmaskr   r   r   Ú	tiny2zeroÜ   s    r[   c                 C   s   t  t  | ¡¡S r   )r   ÚmaxrX   )r   r   r   r   Úmaxabsã   s    r]   Ú__main__é   éd   rD   gš™™™™™é?g{®Gáz„?)r   )r   )rW   )$r4   Únumpyr   Zscipyr   Zstatsmodels.tools.decoratorsr   r   r5   rK   rV   r[   r]   r1   ÚnZaranger   ÚrandomZrandnr   ZautocovZtoeplitzÚsigmaÚmatÚprintr-   r0   ZmihZmat2r    r   r   r	   r   Zmat3r*   r   r   r   r   Ú<module>   s6   R4


"


"

