
    M/PhZ                     n    d Z ddlZddlmZ ddlmZ ddlmZ  G d de          Z	 G 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                       e Zd ZdZd ZdS )	_ANOVAOptzX

    Class containing functions that are optimized over when
    conducting ANOVA.
    c                 h   | j         }| j        }| j        }t          j        ||f          }d}t          t          |                    D ].}|t          ||                   z   }||         |z
  ||||f<   |}/|}	t          j        |	j        d                   d|	j        d         z  z  }
| 	                    t          j        |          |	|
          }dt          j
        ||	j                  z   }d|z  dz  |z  | _        t          j        t          j        || j        z                      }d|z  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lenonesshape_modif_newtondotTnew_weightssumlog)selfmur   r   r   endog_asarrayobs_numarr_numnew_obs_numest_vectwtseta_stardenomllrs                 [/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/statsmodels/emplike/elanova.py_opt_common_muz_ANOVAOpt._opt_common_mu   s0    y
_
$
!344SZZ(( 	" 	"G!Cg$7$77K;@><M';.78!GG ghnQ'((B(.2C,DE%%bhz&:&:HcJJRVHhj1119r>E1fRVD4#334455Cx    N)__name__
__module____qualname____doc__r&    r'   r%   r   r      s-         
    r'   r   c                        e Zd ZdZd Z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                     || _         t          | j                   | _        d| _        | j         D ]}| j        t          |          z   | _        d S )Nr   )r   r   r   r   )r   r   is      r%   __init__zANOVA.__init__F   sP    
dj//	 	+ 	+A	CFF*DII	+ 	+r'   Nr   c                    |G|                      |          }dt          j        || j        dz
            z
  }|r|||| j        fS |||fS t          j        | j         |dd          }|d         }t          t          j	        |d                             }dt          j        || j        dz
            z
  }|r|||| j        fS |||f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)full_outputdispr   )
r&   r   cdfr   r   r   fmin_powellfloatr   squeeze)r   r   mu_startreturn_weightsr$   pvalres	mu_commons           r%   compute_ANOVAzANOVA.compute_ANOVAM   s    8 >%%b))CtxT_q%8999D %D"d&666D"}$&t':H345B B BCa&CbjQ0011ItxT_q%8999D ,D)T-===D)++r'   )Nr   r   )r(   r)   r*   r+   r1   r>   r,   r'   r%   r.   r.   :   sA        	 	+ + +,, ,, ,, ,, ,, ,,r'   r.   )r+   numpyr   descriptiver   scipyr   scipy.statsr   r   r.   r,   r'   r%   <module>rC      s   
 
     # # # # # #            % % % % %
 % % %P?, ?, ?, ?, ?,I ?, ?, ?, ?, ?,r'   