ó
áp7]c           @   s’   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 d	 e f d
 „  ƒ  YZ d e f d „  ƒ  YZ d S(   sÿ   
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.
iÿÿÿÿ(   t   division(   t   rangeNi   (   t
   _OptFuncts(   t   optimize(   t   chi2t	   _ANOVAOptc           B   s   e  Z d  Z d „  Z RS(   sX   

    Class containing functions that are optimized over when
    conducting ANOVA

    c         C   s)  |  j  } |  j } |  j } t j | | f ƒ } d } xR t t | ƒ ƒ D]> } | t | | ƒ } | | | | | | … | f <| } qI W| }	 t j |	 j d ƒ d |	 j d }
 |  j	 t j | ƒ |	 |
 ƒ } d t j
 | |	 j ƒ } d | d | |  _ t j t j | |  j ƒ ƒ } d | S(   s4  
        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
        i    g      ð?iþÿÿÿ(   t   nobst   endogt
   num_groupst   npt   zerosR   t   lent   onest   shapet   _modif_newtont   dott   Tt   new_weightst   sumt   log(   t   selft   muR   R   R   t   endog_asarrayt   obs_numt   arr_numt   new_obs_numt   est_vectt   wtst   eta_start   denomt   llr(    (    s:   lib/python2.7/site-packages/statsmodels/emplike/elanova.pyt   _opt_common_mu   s"    			
%(   t   __name__t
   __module__t   __doc__R   (    (    (    s:   lib/python2.7/site-packages/statsmodels/emplike/elanova.pyR      s   t   ANOVAc           B   s)   e  Z d  Z d „  Z d d d d „ Z RS(   së   
    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   sR   | |  _  t |  j  ƒ |  _ d |  _ x' |  j  D] } |  j t | ƒ |  _ q. Wd  S(   Ni    (   R   R   R   R   (   R   R   t   i(    (    s:   lib/python2.7/site-packages/statsmodels/emplike/elanova.pyt   __init__J   s
    		i    c         C   sã   | d k	 ra |  j | ƒ } d t j | |  j d ƒ } | rQ | | | |  j f S| | | f Sn~ t j |  j | d d d t ƒ} | d } t	 | d ƒ } d t j | |  j d ƒ } | rÒ | | | |  j f S| | | f Sd S(   s&  
        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.
        i   t   full_outputt   dispi    N(
   t   NoneR   R   t   cdfR   R   R   t   fmin_powellt   Falset   float(   R   R   t   mu_startt   return_weightsR   t   pvalt   rest	   mu_common(    (    s:   lib/python2.7/site-packages/statsmodels/emplike/elanova.pyt   compute_ANOVAQ   s    
N(   R    R!   R"   R%   R(   R2   (    (    (    s:   lib/python2.7/site-packages/statsmodels/emplike/elanova.pyR#   >   s   
	(   R"   t
   __future__R    t   statsmodels.compat.pythonR   t   numpyR	   t   descriptiveR   t   scipyR   t   scipy.statsR   R   R#   (    (    (    s:   lib/python2.7/site-packages/statsmodels/emplike/elanova.pyt   <module>   s   )