U
    Kvf                     @   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 )"PlainMatrixArrayzClass 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
ValueErrorZeyeshapeselfdatasym r   O/tmp/pip-unpacked-wheel-2v6byqio/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,   r/   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 )SvdArrayzClass 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-   )superr4   r   r   r   Zsvdr   usvdiagsvdr   sdiagsinvdiag)r   r   r   r8   r9   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   powerr9   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   r9   )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 rD   )r   r$   r)   sumr   r   r   r   r%      s    zSvdArray.mlogdetc                 C   s   t t | j| jS r   )r   r   r*   r9   r:   r   r   r   r   r,      s    zSvdArray.mhalfc                 C   s   t | j| jS r   )r   r   r8   r<   r   r   r   r   xxthalf   s    zSvdArray.xxthalfc                 C   s   t | j| jS r   )r   r   r8   r=   r   r   r   r   
xxtinvhalf   s    zSvdArray.xxtinvhalf)NN)r0   r1   r2   r3   r   rB   r   r   r)   r#   r%   r,   rG   rH   __classcell__r   r   r>   r   r4   q   s"   





r4   c                       s*   e Zd ZdZd fdd	Zdd Z  ZS )	CholArrayzClass 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 )Nr5   )r7   r4   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)r0   r1   r2   r3   r   r   rI   r   r   r>   r   rJ      s   	rJ   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   )decimalr6      )Zsignificant)Znumpy.testingrK   rL   r   r   signr,   rF   r/   r)   r#   r%   )m1m2rK   rL   rN   s1s2ZscorrZevals1Zevecs1Zevals2Zevecs2r   r   r   testcompare   s"    

rU   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    rZ   c                 C   s   t t | S r   )r   maxrW   )r   r   r   r   maxabs   s    r\   __main__   d   rC   g?g{Gz?)r   )r   )rV   )$r3   Znumpyr   Zscipyr   Zstatsmodels.tools.decoratorsr   r   r4   rJ   rU   rZ   r\   r0   nZaranger   randomZrandnr   ZautocovZtoeplitzsigmamatprintr,   r/   ZmihZmat2r   r   r   r	   r   Zmat3r)   r   r   r   r   <module>   s6   R4


"


"

