B
    ¤ŠãZ÷  ã               @   sV   d dl Z d dlZd dlmZ d dlmZ G dd„ deƒZ	G dd„ deƒZ
d
dd	„ZdS )é    N)Úpinv)Ú	utils_oldc               @   s*   e Zd ZdZd	dd„Zdd„ Zdd„ ZdS )
ÚContrastResultsz¸
    Results from looking at a particular contrast of coefficients in
    a parametric model. The class does nothing, it is a container
    for the results from T and F contrasts.
    Nc             C   s8   |d k	r|| _ || _|| _n|| _|| _|| _|| _d S )N)ÚFÚdf_denomÚdf_numÚtÚsdÚeffect)Úselfr   r   r	   r
   r   r   © r   ú?lib/python3.7/site-packages/statsmodels/sandbox/contrast_old.pyÚ__init__   s    zContrastResults.__init__c             C   s   t | dƒr| jS | jS d S )Nr   )Úhasattrr   r   )r   r   r   r   Ú	__array__   s    
zContrastResults.__array__c             C   sJ   t | dƒr"dt| jƒ| j| jf S dt| jƒt| jƒt| jƒ| jf S d S )Nr   z*<F contrast: F=%s, df_denom=%d, df_num=%d>z1<T contrast: effect=%s, sd=%s, t=%s, df_denom=%d>)r   Úreprr   r   r   r
   r	   r   )r   r   r   r   Ú__str__    s
    
zContrastResults.__str__)NNNNNN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r   r   r      s
    
r   c               @   s:   e Zd ZdZddd„Zdd„ Zdd„ Zd	d
„ ZeeƒZ	dS )ÚContrastah  
    This class is used to construct contrast matrices in regression models.
    They are specified by a (term, formula) pair.

    The term, T,  is a linear combination of columns of the design
    matrix D=formula(). The matrix attribute is
    a contrast matrix C so that

    colspan(dot(D, C)) = colspan(dot(D, dot(pinv(D), T)))

    where pinv(D) is the generalized inverse of D. Further, the matrix

    Tnew = dot(C, D)

    is full rank. The rank attribute is the rank of

    dot(D, dot(pinv(D), T))

    In a regression model, the contrast tests that E(dot(Tnew, Y)) = 0
    for each column of Tnew.

    Ú c             C   s*   || _ || _|dkr t|ƒ| _n|| _d S )Nr   )ÚtermÚformulaÚstrÚname)r   r   r   r   r   r   r   r   A   s
    zContrast.__init__c             C   s   dt t| jƒt| jƒdœƒ S )Nz<contrast:%s>)r   r   )r   r   r   r   )r   r   r   r   r   I   s    zContrast.__str__c             O   sœ   t   | j¡}| jj|_t t |||Ž¡¡}|jdkrF|jd df|_t	 
|¡| _| jj||Ž| _t| j| jƒ| _y| jjd | _W n   d| _Y nX dS )a   
        Construct a contrast matrix C so that

        colspan(dot(D, C)) = colspan(dot(D, dot(pinv(D), T)))

        where pinv(D) is the generalized inverse of D=self.D=self.formula().

        If the design, self.D is already set,
        then evaldesign can be set to False.
        é   r   N)Úcopyr   r   Ú	namespaceÚnpZ	transposeZarrayÚndimÚshapeÚutilsZclean0ÚTZdesignÚDÚcontrastfromcolsÚ_matrixÚmatrixÚrank)r   ÚargsÚkwr   r$   r   r   r   Úcompute_matrixM   s    

zContrast.compute_matrixc             C   s   t | dƒs|  ¡  | jS )z`
        This will fail if the formula needs arguments to construct
        the design.
        r'   )r   r,   r'   )r   r   r   r   Ú_get_matrixj   s    
zContrast._get_matrixN)r   )
r   r   r   r   r   r   r,   r-   Úpropertyr(   r   r   r   r   r   )   s   
r   c             C   sè   t  | ¡} t  |¡}|j\}}| jd |krB| jd |krBtdƒ‚|dkrRt|ƒ}| jd |krpt  || ¡j}n| }t  |t  ||j¡¡j}t  ||j¡}t|jƒdkr²|df|_t 	|¡|jd krÞt 
|¡}t  ||¡j}t  |¡S )aV  
    From an n x p design matrix D and a matrix L, tries
    to determine a p x q contrast matrix C which
    determines a contrast of full rank, i.e. the
    n x q matrix

    dot(transpose(C), pinv(D))

    is full rank.

    L must satisfy either L.shape[0] == n or L.shape[1] == p.

    If L.shape[0] == n, then L is thought of as representing
    columns in the column space of D.

    If L.shape[1] == p, then L is thought of as what is known
    as a contrast matrix. In this case, this function returns an estimable
    contrast corresponding to the dot(D, L.T)

    Note that this always produces a meaningful contrast, not always
    with the intended properties because q is always non-zero unless
    L is identically 0. That is, it produces a contrast that spans
    the column space of L (after projection onto the column space of D).

    r   r   zshape of L and D mismatchedN)r    Zasarrayr"   Ú
ValueErrorr   Údotr$   Úlenr#   r)   ZfullrankZsqueeze)ÚLr%   ZpseudoÚnÚpÚCZLpr   r   r   r&   t   s$    




r&   )N)r   Znumpyr    Znumpy.linalgr   Zstatsmodels.sandboxr   r#   Úobjectr   r   r&   r   r   r   r   Ú<module>   s   "K