B
    ¤ŠãZž  ã               @   s@   d dl mZ d dlZd dlmZ eddgƒZG dd„ deƒZdS )é    )Ú
namedtupleN)ÚBunchÚ_MinimalWLSModelÚweightsc               @   s$   e Zd ZdZd	dd„Zd
dd„ZdS )Ú_MinimalWLSaŠ  
    Minimal implementation of WLS optimized for performance.

    Parameters
    ----------
    endog : array-like
        1-d endogenous response variable. The dependent variable.
    exog : array-like
        A nobs x k array where `nobs` is the number of observations and `k`
        is the number of regressors. An intercept is not included by default
        and should be added by the user. See
        :func:`statsmodels.tools.add_constant`.
    weights : array-like, optional
        1d array of weights.  If you supply 1/W then the variables are pre-
        multiplied by 1/sqrt(W).  If no weights are supplied the default value
        is 1 and WLS reults are the same as OLS.

    Notes
    -----
    Need resid, scale, fittedvalues, model.weights!
        history['scale'].append(tmp_results.scale)
        if conv == 'dev':
            history['deviance'].append(self.deviance(tmp_results))
        elif conv == 'sresid':
            history['sresid'].append(tmp_results.resid/tmp_results.scale)
        elif conv == 'weights':
            history['weights'].append(tmp_results.model.weights)
    Does not perform and checks on the input data
    ç      ð?c             C   sV   || _ || _|| _t |¡}|| | _t |¡r<|| | _n|d d …d f | | _d S )N)ÚendogÚexogr   ÚnpZsqrtÚwendogZisscalarÚwexog)Úselfr   r	   r   Zw_half© r   ú<lib/python3.7/site-packages/statsmodels/regression/_tools.pyÚ__init__'   s    


z_MinimalWLS.__init__Úpinvc             C   sÚ   |dkr$t j | j¡}| | j¡}nT|dkrZt j | j¡\}}t j |t  |j| j¡¡}nt jj	| j| jdd\}}}}| j
 |¡}| j| }| j| j |¡ }	| jjd | jjd  }
t  |	|	¡|
 }t|||| |dS )aK  
        Minimal implementation of WLS optimized for performance.

        Parameters
        ----------
        method : str, optional
            Method to use to estimate parameters.  "pinv", "qr" or "lstsq"

              * "pinv" uses the Moore-Penrose pseudoinverse
                 to solve the least squares problem.
              * "qr" uses the QR factorization.
              * "lstsq" uses the least squares implementation in numpy.linalg

        Returns
        -------
        results : namedtuple
            Named tuple containing the fewest terms needed to implement
            iterative estimation in models. Currently

              * params : Estimated parameters
              * fittedvalues : Fit values using original data
              * resid : Residuals using original data
              * model : namedtuple with one field, weights
              * scale : scale computed using weighted residuals

        Notes
        -----
        Does not perform and checks on the input data

        See Also
        --------
        statsmodels.regression.linear_model.WLS
        r   Úqréÿÿÿÿ)Zrcondr   é   )ÚparamsZfittedvaluesÚresidZmodelÚscale)r
   Zlinalgr   r   Údotr   r   ZsolveÚTZlstsqr	   r   Úshaper   )r   ÚmethodZ
pinv_wexogr   ÚQÚRÚ_Zfitted_valuesr   ZwresidZdf_residr   r   r   r   Úfit3   s    "
z_MinimalWLS.fitN)r   )r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r   r      s   
r   )	Úcollectionsr   Znumpyr
   Zstatsmodels.tools.toolsr   r   Úobjectr   r   r   r   r   Ú<module>   s   