
    "gZ                     `    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
y)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y)	_ANOVAOptzX

    Class containing functions that are optimized over when
    conducting ANOVA.
    c                    | j                   }| j                  }| j                  }t        j                  ||f      }d}t        t        |            D ]$  }|t        ||         z   }||   |z
  ||||f<   |}& |}	t        j                  |	j                  d         d|	j                  d   z  z  }
| j                  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                 X/var/www/dash_apps/app1/venv/lib/python3.12/site-packages/statsmodels/emplike/elanova.py_opt_common_muz_ANOVAOpt._opt_common_mu   s.    yy

__
$
!34SZ( 	"G!Cg$77K;@><M';.78!G		"
 !gghnnQ'(B(..2C,DE%%bhhz&:HcJRVVHhjj119r>E1ffRVVD4#3#334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y)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   | _        ! y )Nr   )r   r   r   r   )r   r   is      r%   __init__zANOVA.__init__F   sF    
djj/	 	+A		CF*DI	+r'   Nc                    |N| j                  |      }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.
        r   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txxT__q%899DD"d&6&666D"}$&&t':':H345BCa&CbjjQ01ItxxT__q%899DD)T-=-===D)++r'   )Nr   r   )r(   r)   r*   r+   r1   r>   r,   r'   r%   r.   r.   :   s    	+,,r'   r.   )r+   numpyr   descriptiver   scipyr   scipy.statsr   r   r.   r,   r'   r%   <module>rC      s1   
  #  %
 %P?,I ?,r'   