U
    KvfZ  ã                   @   sT   d Z ddlZddlmZ ddlmZ ddlmZ G dd„ deƒZ	G d	d
„ d
e	ƒZ
dS )a   
This script contains empirical likelihood ANOVA.

Currently the script only contains one feature that allows the user to compare
means of multiple groups.

General References
------------------

Owen, A. B. (2001). Empirical Likelihood. Chapman and Hall.
é    Né   )Ú
_OptFuncts)Úoptimize)Úchi2c                   @   s   e Zd ZdZdd„ ZdS )Ú	_ANOVAOptzX

    Class containing functions that are optimized over when
    conducting ANOVA.
    c                 C   sÚ   | j }| j}| j}t ||f¡}d}tt|ƒƒD ]0}|t|| ƒ }|| | |||…|f< |}q0|}	t |	jd ¡d|	jd   }
|  	t |¡|	|
¡}dt 
||	j¡ }d| d | | _t t || j ¡¡}d| S )a7  
        Optimizes the likelihood under the null hypothesis that all groups have
        mean mu.

        Parameters
        ----------
        mu : float
            The common mean.

        Returns
        -------
        llr : float
            -2 times the llr ratio, which is the test statistic.
        r   g      ð?éþÿÿÿ)ÚnobsÚendogÚ
num_groupsÚnpÚzerosÚrangeÚlenZonesÚshapeZ_modif_newtonÚdotÚTÚnew_weightsÚsumÚlog)ÚselfÚmur   r	   r
   Zendog_asarrayZobs_numZarr_numZnew_obs_numZest_vectZwtsZeta_starZdenomÚllr© r   ú?/tmp/pip-unpacked-wheel-2v6byqio/statsmodels/emplike/elanova.pyÚ_opt_common_mu   s$    ÿz_ANOVAOpt._opt_common_muN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   r   r      s   r   c                   @   s"   e Zd ZdZdd„ Zddd„ZdS )	ÚANOVAzë
    A class for ANOVA and comparing means.

    Parameters
    ----------

    endog : list of arrays
        endog should be a list containing 1 dimensional arrays.  Each array
        is the data collected from a certain group.
    c                 C   s8   || _ t| j ƒ| _d| _| j D ]}| jt|ƒ | _qd S )Nr   )r	   r   r
   r   )r   r	   Úir   r   r   Ú__init__F   s
    
zANOVA.__init__Nr   c                 C   sª   |dk	rF|   |¡}dt || jd ¡ }|r:|||| jfS |||fS n`tj| j |ddd}|d }tt 	|d ¡ƒ}dt || jd ¡ }|rœ|||| jfS |||fS dS )a(  
        Returns -2 log likelihood, the pvalue and the maximum likelihood
        estimate for a common mean.

        Parameters
        ----------

        mu : float
            If a mu is specified, ANOVA is conducted with mu as the
            common mean.  Otherwise, the common mean is the maximum
            empirical likelihood estimate of the common mean.
            Default is None.

        mu_start : float
            Starting value for commean mean if specific mu is not specified.
            Default = 0.

        return_weights : bool
            if TRUE, returns the weights on observations that maximize the
            likelihood.  Default is FALSE.

        Returns
        -------

        res: tuple
            The log-likelihood, p-value and estimate for the common mean.
        Nr   F)Zfull_outputZdispr   )
r   r   Zcdfr
   r   r   Zfmin_powellÚfloatr   Zsqueeze)r   r   Zmu_startZreturn_weightsr   ZpvalÚresZ	mu_commonr   r   r   Úcompute_ANOVAM   s     

 ÿzANOVA.compute_ANOVA)Nr   r   )r   r   r   r   r!   r$   r   r   r   r   r   :   s   r   )r   Znumpyr   Zdescriptiver   Zscipyr   Zscipy.statsr   r   r   r   r   r   r   Ú<module>   s   (