B
    ¤ŠãZ  ã               @   sl   d Z ddlmZ ddlm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 )zÿ
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.
é    )Údivision)ÚrangeNé   )Ú
_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}x@tt|ƒƒD ]0}|t|| ƒ }|| | |||…|f< |}q2W |}	t |	jd ¡d|	jd   }
|  	t |¡|	|
¡}dt 
||	j¡ }d| d | | _t t || j ¡¡}d| S )a4  
        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ÚnpZzerosr   Ú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   ú:lib/python3.7/site-packages/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   s<   || _ t| j ƒ| _d| _x| j D ]}| jt|ƒ | _q W d S )Nr   )r   r   r   r
   )r   r   Úir   r   r   Ú__init__J   s
    zANOVA.__init__Nr   c             C   s¤   |dk	rF|   |¡}dt || jd ¡ }|r:|||| jfS |||fS nZtj| j |ddd}|d }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Úfloat)r   r   Zmu_startZreturn_weightsr   ZpvalZresZ	mu_commonr   r   r   Úcompute_ANOVAQ   s    


zANOVA.compute_ANOVA)Nr   r   )r   r   r   r   r!   r#   r   r   r   r   r   >   s   
r   )r   Z
__future__r   Zstatsmodels.compat.pythonr   Znumpyr   Zdescriptiver   Zscipyr   Zscipy.statsr   r   r   r   r   r   r   Ú<module>   s   )