
    M/Ph                         d Z ddlZddlmZ ddlmZmZmZ ddl	m
Z
 ddlZddlmZ d Z G d d	          Z G d
 de          Z G d de          ZdS )a  
Empirical likelihood inference on descriptive statistics

This module conducts hypothesis tests and constructs confidence
intervals for the mean, variance, skewness, kurtosis and correlation.

If matplotlib is installed, this module can also generate multivariate
confidence region plots as well as mean-variance contour plots.

See _OptFuncts docstring for technical details and optimization variable
definitions.

General References:
------------------
Owen, A. (2001). "Empirical Likelihood." Chapman and Hall

    N)optimize)chi2skewkurtosis)_fit_newton)utilsc                     | j         dk    r#|                     t          |           d          } | j        d         dk    rt	          |           S | j        d         dk    rt          |           S dS )a  
    Returns an instance to conduct inference on descriptive statistics
    via empirical likelihood.  See DescStatUV and DescStatMV for more
    information.

    Parameters
    ----------
    endog : ndarray
         Array of data

    Returns : DescStat instance
        If k=1, the function returns a univariate instance, DescStatUV.
        If k>1, the function returns a multivariate instance, DescStatMV.
       N)ndimreshapelenshape
DescStatUV
DescStatMV)endogs    _/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/statsmodels/emplike/descriptive.pyDescStatr      sn     zQc%jj!,,{1~%   {1~%        c                   z    e Zd ZdZd Zd Zd Zd Zd Zd Z	d Z
d	 ZddZd Zd Zd Zd Zd Zd Zd Zd ZdS )
_OptFunctsa:  
    A class that holds functions that are optimized/solved.

    The general setup of the class is simple.  Any method that starts with
    _opt_ creates a vector of estimating equations named est_vect such that
    np.dot(p, (est_vect))=0 where p is the weight on each
    observation as a 1 x n array and est_vect is n x k.  Then _modif_Newton is
    called to determine the optimal p by solving for the Lagrange multiplier
    (eta) in the profile likelihood maximization problem.  In the presence
    of nuisance parameters, _opt_ functions are  optimized over to profile
    out the nuisance parameters.

    Any method starting with _ci_limits calculates the log likelihood
    ratio for a specific value of a parameter and then subtracts a
    pre-specified critical value.  This is solved so that llr - crit = 0.
    c                     d S N selfr   s     r   __init__z_OptFuncts.__init__C   s    r   c                 >   t          j        |          t          j        |          t          j        ||          z   z   }|d|z  k     }| }|||         z  }t          j        d|z            dz
  |d|dz  z
  z  z   ||<   t          j        ||                   ||<   |S )a  
        Transforms the log of observation probabilities in terms of the
        Lagrange multiplier to the log 'star' of the probabilities.

        Parameters
        ----------
        eta : float
            Lagrange multiplier

        est_vect : ndarray (n,k)
            Estimating equations vector

        wts : nx1 array
            Observation weights

        Returns
        ------
        data_star : ndarray
            The weighted logstar of the estimting equations

        Notes
        -----
        This function is only a placeholder for the _fit_Newton.
        The function value is not used in optimization and the optimal value
        is disregarded when computing the log likelihood ratio.
              ?      ?       @   )nplogsumdot)	r   etaest_vectweightsnobs	data_staridxnot_idxnxs	            r   	_log_starz_OptFuncts._log_starF   s    6 F7OOrvg')vh'<'<(= >	"t)#$IcN"T	**S02b1f3EE	#VIg$677	'r   c                     t          j        |          t          j        ||          z   }|d|z  k     }| }|dz   ||<   ||         dz   ||<   ||z  }t          j        |j        |dddf         |z            S )a  
        Calculates the hessian of a weighted empirical likelihood
        problem.

        Parameters
        ----------
        eta : ndarray, (1,m)
            Lagrange multiplier in the profile likelihood maximization

        est_vect : ndarray (n,k)
            Estimating equations vector

        weights : 1darray
            Observation weights

        Returns
        -------
        hess : m x m array
            Weighted hessian used in _wtd_modif_newton
        r   r!   N)r"   r$   r%   T)	r   r&   r'   r(   r)   data_star_doub_primer+   r,   wtd_dsdps	            r   _hessz_OptFuncts._hessj   s    ,  "vg#1F1FF"R$Y.$&*aiKS!+?+HR*O(OW%11vhj(111d7"3h">???r   c                     t          j        |          t          j        ||          z   }|d|z  k     }| }|d|||         z  z
  z  ||<   d||         z  ||<   t          j        ||z  |          S )a  
        Calculates the gradient of a weighted empirical likelihood
        problem

        Parameters
        ----------
        eta : ndarray, (1,m)
            Lagrange multiplier in the profile likelihood maximization

        est_vect : ndarray, (n,k)
            Estimating equations vector

        weights : 1darray
            Observation weights

        Returns
        -------
        gradient : ndarray (m,1)
            The gradient used in _wtd_modif_newton
        r   r!   )r"   r$   r%   )r   r&   r'   r(   r)   data_star_primer+   r,   s           r   _gradz_OptFuncts._grad   s    , &//BF8S,A,AAT	)$#q4/#2F+F'FG#%(@#@ vg/:::r   c           
          	 t                    		 fd}	 fd}	 fd}ddi}|                                }t          |||d||dd	          }|d         S )
a-  
        Modified Newton's method for maximizing the log 'star' equation.  This
        function calls _fit_newton to find the optimal values of eta.

        Parameters
        ----------
        eta : ndarray, (1,m)
            Lagrange multiplier in the profile likelihood maximization

        est_vect : ndarray, (n,k)
            Estimating equations vector

        weights : 1darray
            Observation weights

        Returns
        -------
        params : 1xm array
            Lagrange multiplier that maximizes the log-likelihood
        c                 Z    t          j                            |                      S r   )r"   r$   r.   x0r'   r)   r   r(   s    r   <lambda>z*_OptFuncts._modif_newton.<locals>.<lambda>   s&    r8Wd K KLLL r   c                 6                         |            S r   )r7   r:   s    r   r<   z*_OptFuncts._modif_newton.<locals>.<lambda>       DJJr8WdCCC r   c                 6                         |            S r   )r4   r:   s    r   r<   z*_OptFuncts._modif_newton.<locals>.<lambda>   r>   r   tol:0yE>r   2   r   )hessmaxiterdisp)r   squeezer   )
r   r&   r'   r(   fgradrC   kwdsresr)   s
   ` ``     @r   _modif_newtonz_OptFuncts._modif_newton   s    * 8}}LLLLLLLCCCCCCCCCCCCCCt}kkmm!T3DtR#$& & &1vr   c                 p    t          j        | j        | j        z
  d|| j        | j        z
  z  z   z            S )a}  
        Finding the root of sum(xi-h0)/(1+eta(xi-mu)) solves for
        eta when computing ELR for univariate mean.

        Parameters
        ----------
        eta : float
            Lagrange multiplier in the empirical likelihood maximization

        Returns
        -------
        llr : float
            n times the log likelihood value for a given value of eta
        r   )r"   r$   r   mu0)r   r&   s     r   	_find_etaz_OptFuncts._find_eta   s>     vtzDH,C4:0113 4 4 	4r   c                 H    |                      |          d         | j        z
  S )az  
        Calculates the difference between the log likelihood of mu_test and a
        specified critical value.

        Parameters
        ----------
        mu : float
           Hypothesized value of the mean.

        Returns
        -------
        diff : float
            The difference between the log likelihood value of mu0 and
            a specified value.
        r   )	test_meanr0)r   mus     r   _ci_limits_muz_OptFuncts._ci_limits_mu   s"      ~~b!!!$tw..r   c                     t          j        | j        |z
  dz            }| j        |z
  dz  |z  }dt          j        t          j        | j        |z                      z  | j        z
  S )a  
        Finds gamma that satisfies
        sum(log(n * w(gamma))) - log(r0) = 0

        Used for confidence intervals for the mean

        Parameters
        ----------
        gamma : float
            Lagrange multiplier when computing confidence interval

        Returns
        -------
        diff : float
            The difference between the log-liklihood when the Lagrange
            multiplier is gamma and a pre-specified value
        r0   )r"   r$   r   r#   r)   rQ   )r   gammadenomnew_weightss       r   _find_gammaz_OptFuncts._find_gamma   sg    $ 
U*r122zE)b058BF26$)k"9::;;;G 	r   Fc                    | j         }| j        }||z
  dz  | j        z
  }||z
  }t          j        ||f          }|                     t          j        d|z  d|z  g          |t          j        |          d|z  z            }dt          j        ||j	                  z   }	d|z  dz  |	z  | _
        t          j        t          j        || j
        z                      }
|rt          j        d|
z  d          S d|
z  S )a  
        This is the function to be optimized over a nuisance mean parameter
        to determine the likelihood ratio for the variance

        Parameters
        ----------
        nuisance_mu : float
            Value of a nuisance mean parameter

        Returns
        -------
        llr : float
            Log likelihood of a pre-specified variance holding the nuisance
            parameter constant
        r!   r   r
   r0   )r   r)   sig2_0r"   column_stackrK   arrayonesr%   r1   rX   r$   r#   r   sf)r   nuisance_mupvalr   r)   sig_datamu_datar'   eta_starrW   llrs              r   _opt_varz_OptFuncts._opt_var   s     
y[(Q.k";&?GX#677%%bhT	/1Dy0: '; ';<D02d0KM M BF8XZ0009r>E1fRVD4#334455 	(728Q'''Cxr   c                 H    |                      |          d         | j        z
  S )a  
        Used to determine the confidence intervals for the variance.
        It calls test_var and when called by an optimizer,
        finds the value of sig2_0 that is chi2.ppf(significance-level)

        Parameters
        ----------
        var_test : float
            Hypothesized value of the variance

        Returns
        -------
        diff : float
            The difference between the log likelihood ratio at var_test and a
            pre-specified value.
        r   )test_varrQ   )r   vars     r   _ci_limits_varz_OptFuncts._ci_limits_var   s"    " }}S!!!$tw..r   c                 .   | j         }| j        }||d         z
  }||d         z
  dz  |d         z
  }||d         z
  dz  |d         dz  z  | j        z
  }t          j        |||f          }|                     t          j        d|z  d|z  d|z  g          |t          j        |          d|z  z            }dt          j        ||j	                  z   }	d|z  dz  |	z  | _
        t          j        t          j        || j
        z                      }
d|
z  S )a  
        Called by test_skew.  This function is optimized over
        nuisance parameters mu and sigma

        Parameters
        ----------
        nuis_params : 1darray
            An array with a  nuisance mean and variance parameter

        Returns
        -------
        llr : float
            The log likelihood ratio of a pre-specified skewness holding
            the nuisance parameters constant.
        r   r!   r
      r   r   r0   )r   r)   skew0r"   r\   rK   r]   r^   r%   r1   rX   r$   r#   )r   nuis_paramsr   r)   rc   rb   	skew_datar'   rd   rW   re   s              r   	_opt_skewz_OptFuncts._opt_skew3  s.     
y+a.([^+1[^C{1~-!3 ^s*,/3z:	?GXy#ABB%%bhT	/1Dy/1Dy0: '; ';<D/1wt}}T	/JL L RVHhj1119r>E1fRVD4#334455Cxr   c                 4   | j         }| j        }||d         z
  }||d         z
  dz  |d         z
  }||d         z
  dz  |d         dz  z  dz
  | j        z
  }t          j        |||f          }|                     t          j        d|z  d|z  d|z  g          |t          j        |          d|z  z            }dt          j        ||j	                  z   }	d|z  dz  |	z  | _
        t          j        t          j        || j
        z                      }
d|
z  S )a  
        Called by test_kurt.  This function is optimized over
        nuisance parameters mu and sigma

        Parameters
        ----------
        nuis_params : 1darray
            An array with a nuisance mean and variance parameter

        Returns
        -------
        llr : float
            The log likelihood ratio of a pre-speified kurtosis holding the
            nuisance parameters constant
        r   r!   r
      rl   r   r0   )r   r)   kurt0r"   r\   rK   r]   r^   r%   r1   rX   r$   r#   )r   rn   r   r)   rc   rb   	kurt_datar'   rd   rW   re   s              r   	_opt_kurtz_OptFuncts._opt_kurtS  s6     
y+a.([^+1[^CA.14 ^q(*-./26*=	?GXy#ABB%%bhT	/1Dy/1Dy0: '; ';<D/1wt}}T	/JL L BF8XZ0009r>E1fRVD4#334455Cxr   c           	         | j         }| j        }||d         z
  }||d         z
  dz  |d         z
  }||d         z
  dz  |d         dz  z  | j        z
  }||d         z
  dz  |d         dz  z  dz
  | j        z
  }t	          j        ||||f          }|                     t	          j        d|z  d|z  d|z  d|z  g          |t	          j        |          d|z  z            }	dt	          j	        |	|j
                  z   }
d|z  dz  |
z  | _        t	          j        t	          j        || j        z                      }d|z  S )	a  
        Called by test_joint_skew_kurt.  This function is optimized over
        nuisance parameters mu and sigma

        Parameters
        ----------
        nuis_params : 1darray
            An array with a nuisance mean and variance parameter

        Returns
        ------
        llr : float
            The log likelihood ratio of a pre-speified skewness and
            kurtosis holding the nuisance parameters constant.
        r   r!   r
   rl   r   rr   r   r0   )r   r)   rm   rs   r"   r\   rK   r]   r^   r%   r1   rX   r$   r#   )r   rn   r   r)   rc   rb   ro   rt   r'   rd   rW   re   s               r   _opt_skew_kurtz_OptFuncts._opt_skew_kurts  sp     
y+a.([^+1[^C{1~-!3 ^s*,/3z:	A.14 ^q(*-./26*=	?GXy)#LMM%%bhT	/1Dy/1Dy/1Dy0: '; '; =E/1wt}}T	/J	L L
 RVHhj1119r>E1fRVD4#334455Cxr   c                 H    |                      |          d         | j        z
  S )a  
        Parameters
        ----------
        skew0 : float
            Hypothesized value of skewness

        Returns
        -------
        diff : float
            The difference between the log likelihood ratio at skew and a
            pre-specified value.
        r   )	test_skewrQ   )r   r   s     r   _ci_limits_skewz_OptFuncts._ci_limits_skew  "     ~~d##A&00r   c                 H    |                      |          d         | j        z
  S )a  
        Parameters
        ----------
        skew0 : float
            Hypothesized value of kurtosis

        Returns
        -------
        diff : float
            The difference between the log likelihood ratio at kurt and a
            pre-specified value.
        r   )	test_kurtrQ   )r   kurts     r   _ci_limits_kurtz_OptFuncts._ci_limits_kurt  r{   r   c                    ||ddd         z
  j         \  }}|dz  |d         z
  }	|dz  |d         z
  }
||z  ||d         |d         z  dz  z  z
  }t          j        ||	||
|f          }|                     |||          }dt          j        ||          z   }d|z  dz  |z  | _        t          j        t          j        || j        z                      }d|z  S )a<  
        Parameters
        ----------
        nuis_params : 1darray
            Array containing two nuisance means and two nuisance variances

        Returns
        -------
        llr : float
            The log-likelihood of the correlation coefficient holding nuisance
            parameters constant
        Nr!   r
   rl         ?r   r0   )r1   r"   r\   rK   r%   rX   r$   r#   )r   rn   corr0r   r)   r;   weights0mu1_datamu2_data	sig1_data	sig2_datacorrel_datar'   rd   rW   re   s                   r   _opt_correlz_OptFuncts._opt_correl  s     $k##A#&669(MKN2	MKN2	 8+u ^k!n4;0< <?Hi$,i$F G G%%b(H==RVHh///9r>E1fRVD4#334455Cxr   c                 H    |                      |          d         | j        z
  S Nr   )	test_corrrQ   )r   corrs     r   _ci_limits_corrz_OptFuncts._ci_limits_corr  s     ~~d##A&00r   NF)__name__
__module____qualname____doc__r   r.   r4   r7   rK   rN   rS   rY   rf   rj   rp   ru   rw   rz   r   r   r   r   r   r   r   r   1   s*        "  " " "H@ @ @<; ; ;:  >4 4 4$/ / /$  .   B/ / /&  @  @! ! !F1 1 11 1 1  41 1 1 1 1r   r   c                   r    e Zd ZdZd ZddZ	 	 dd
ZddZddZg dfdZ	ddZ
ddZddZddZddZdS )r   aj  
    A class to compute confidence intervals and hypothesis tests involving
    mean, variance, kurtosis and skewness of a univariate random variable.

    Parameters
    ----------
    endog : 1darray
        Data to be analyzed

    Attributes
    ----------
    endog : 1darray
        Data to be analyzed

    nobs : float
        Number of observations
    c                 \    t          j        |          | _        |j        d         | _        d S r   )r"   rF   r   r   r)   r   s     r   r   zDescStatUV.__init__  s$    Z&&
KN			r   Fc                    || _         | j        }| j        }dd|z  z
  | j         t          |          z
  z  }dd|z  z
  | j         t	          |          z
  z  }t          j        | j        ||          }d|z  dz  d||| j         z
  z  z   z  }dt          j	        t          j
        ||z                      z  }	|r|	t          j        |	d          |fS |	t          j        |	d          fS )a  
        Returns - 2 x log-likelihood ratio, p-value and weights
        for a hypothesis test of the mean.

        Parameters
        ----------
        mu0 : float
            Mean value to be tested

        return_weights : bool
            If return_weights is True the function returns
            the weights of the observations under the null hypothesis.
            Default is False

        Returns
        -------
        test_results : tuple
            The log-likelihood ratio and p-value of mu0
        r   r0   r
   )rM   r   r)   maxminr   brentqrN   r"   r$   r#   r   r_   )
r   rM   return_weightsr   r)   eta_mineta_maxrd   rX   re   s
             r   rP   zDescStatUV.test_mean  s    ( 
yd#3u::(=>d#3u::(=>?4>7GDDDyB&"x548;K/L*LM26"&!344555 	(Q44Q''r   皙?rV   rA    d(	     d(	 c                    | j         }d|z
  }|dk    rt          j        |d          | _        t	          j        |          }t          |          t	          j        |          z
  |z  }t	          j        |          t          |          z
  |z  }	t          j	        | j
        |t          |          |z
            }
t          j	        | j
        |t          |          |	z             }||
fS |dk    rt          j        |d          | _        t          j	        | j        |t          |          |z
            }t          j	        | j        t          |          |z   |          }||z
  dz  t	          j        ||z
  dz            z  }||z
  dz  t	          j        ||z
  dz            z  }t	          j        ||z            }t	          j        ||z            }||fS dS )av  
        Returns the confidence interval for the mean.

        Parameters
        ----------
        sig : float
            significance level. Default is .05

        method : str
            Root finding method,  Can be 'nested-brent' or
            'gamma'.  Default is 'gamma'

            'gamma' Tries to solve for the gamma parameter in the
            Lagrange (see Owen pg 22) and then determine the weights.

            'nested brent' uses brents method to find the confidence
            intervals but must maximize the likelihood ratio on every
            iteration.

            gamma is generally much faster.  If the optimizations does not
            converge, try expanding the gamma_high and gamma_low
            variable.

        gamma_low : float
            Lower bound for gamma when finding lower limit.
            If function returns f(a) and f(b) must have different signs,
            consider lowering gamma_low.

        gamma_high : float
            Upper bound for gamma when finding upper limit.
            If function returns f(a) and f(b) must have different signs,
            consider raising gamma_high.

        epsilon : float
            When using 'nested-brent', amount to decrease (increase)
            from the maximum (minimum) of the data when
            starting the search.  This is to protect against the
            likelihood ratio being zero at the maximum (minimum)
            value of the data.  If data is very small in absolute value
            (<10 ``**`` -6) consider shrinking epsilon

            When using 'gamma', amount to decrease (increase) the
            minimum (maximum) by to start the search for gamma.
            If function returns f(a) and f(b) must have different signs,
            consider lowering epsilon.

        Returns
        -------
        Interval : tuple
            Confidence interval for the mean
        r
   znested-brentrV   rU   N)r   r   ppfrQ   r"   meanr   r   r   r   rS   rY   r$   )r   sigmethodepsilon	gamma_low
gamma_highr   middle	epsilon_u	epsilon_lulimllimgamma_star_lgamma_star_uweights_lowweights_highmu_lowmu_highs                     r   ci_meanzDescStatUV.ci_mean
  s   j 
#g^##hsA&&DGWU^^FUbgenn4?I#e**4?I?4#5vE

Y&( (D?4#5vE

Y&( (D:WhsA&&DG#?4+;YE

W$& &L#?4+;Ug-z; ;L!L0R7,3445K"\1b8,3445LVK%/00Ff\E122GG## r   c                     || _         t          | j                  }t          | j                  }t	          j        | j        ||d          d         }t          j        |d          }|r||| j	        j
        fS ||fS )a	  
        Returns  -2 x log-likelihood ratio and the p-value for the
        hypothesized variance

        Parameters
        ----------
        sig2_0 : float
            Hypothesized variance to be tested

        return_weights : bool
            If True, returns the weights that maximize the
            likelihood of observing sig2_0. Default is False

        Returns
        -------
        test_results : tuple
            The  log-likelihood ratio and the p_value  of sig2_0

        Examples
        --------
        >>> import numpy as np
        >>> import statsmodels.api as sm
        >>> random_numbers = np.random.standard_normal(1000)*100
        >>> el_analysis = sm.emplike.DescStat(random_numbers)
        >>> hyp_test = el_analysis.test_var(9500)
        r
   )full_output)r[   r   r   r   r   	fminboundrf   r   r_   rX   r1   )r   r[   r   mu_maxmu_minre   p_vals          r   rh   zDescStatUV.test_varZ  s    6 TZTZ -.0 0 0013Q 	t/111:r   Nc                    | j         }|=| j        dz
  |                                z  t          j        d| j        dz
            z  }|=| j        dz
  |                                z  t          j        d| j        dz
            z  }t          j        d|z
  d          | _        t          j        | j        ||                                          }t          j        | j        |                                |          }||fS )ay  
        Returns the confidence interval for the variance.

        Parameters
        ----------
        lower_bound : float
            The minimum value the lower confidence interval can
            take. The p-value from test_var(lower_bound) must be lower
            than 1 - significance level. Default is .99 confidence
            limit assuming normality

        upper_bound : float
            The maximum value the upper confidence interval
            can take. The p-value from test_var(upper_bound) must be lower
            than 1 - significance level.  Default is .99 confidence
            limit assuming normality

        sig : float
            The significance level. Default is .05

        Returns
        -------
        Interval : tuple
            Confidence interval for the variance

        Examples
        --------
        >>> import numpy as np
        >>> import statsmodels.api as sm
        >>> random_numbers = np.random.standard_normal(100)
        >>> el_analysis = sm.emplike.DescStat(random_numbers)
        >>> el_analysis.ci_var()
        (0.7539322567470305, 1.229998852496268)
        >>> el_analysis.ci_var(.5, 2)
        (0.7539322567469926, 1.2299988524962664)

        Notes
        -----
        If the function returns the error f(a) and f(b) must have
        different signs, consider lowering lower_bound and raising
        upper_bound.
        Nr
   g-C6?gH.?)	r   r)   ri   r   r   rQ   r   r   rj   )r   lower_boundupper_boundr   r   r   r   s          r   ci_varzDescStatUV.ci_var  s    V 
 IMUYY[[8xty1}--/K IMUYY[[8xty1}--/K(1s7A&&t2KMMt2EIIKKMMTzr   )皙?皙?r   {Gz?MbP?c                 B   t          j                    \  }}	|	                    d           |	                    d           t	          t          j        |||                    }
t	          t          j        |||                    }g }|D ]8}|| _        |
D ],}|                    | 	                    |d                     -9t          j
        |                              t          |          t          |
                    }|	                    |
|||           |S )a  
        Returns a plot of the confidence region for a univariate
        mean and variance.

        Parameters
        ----------
        mu_low : float
            Lowest value of the mean to plot

        mu_high : float
            Highest value of the mean to plot

        var_low : float
            Lowest value of the variance to plot

        var_high : float
            Highest value of the variance to plot

        mu_step : float
            Increments to evaluate the mean

        var_step : float
            Increments to evaluate the mean

        levs : list
            Which values of significance the contour lines will be drawn.
            Default is [.2, .1, .05, .01, .001]

        Returns
        -------
        Figure
            The contour plot
        VarianceMeanT)ra   levels)r   create_mpl_ax
set_ylabel
set_xlabellistr"   aranger[   appendrf   asarrayr   r   contour)r   r   r   var_lowvar_highmu_stepvar_steplevsfigaxmu_vectvar_vectzsig0rM   s                  r   plot_contourzDescStatUV.plot_contour  s   H %''R
j!!!
fry'::;;	'8X>>?? 	8 	8DDK 8 8s6677778JqMM!!#h--W>>


7Ha
555
r   c                 2   || _         t          j        | j                                        | j                                        g          }t          j        | j        |dd          d         }t          j
        |d          }|r||| j        j        fS ||fS )a  
        Returns  -2 x log-likelihood and p-value for the hypothesized
        skewness.

        Parameters
        ----------
        skew0 : float
            Skewness value to be tested

        return_weights : bool
            If True, function also returns the weights that
            maximize the likelihood ratio. Default is False.

        Returns
        -------
        test_results : tuple
            The log-likelihood ratio and p_value of skew0
        r
   r   r   rE   )rm   r"   r]   r   r   ri   r   fmin_powellrp   r   r_   rX   r1   )r   rm   r   start_nuisancere   r   s         r   ry   zDescStatUV.test_skew  s    & 
4:??#4#4'+z~~'7'7#9 : : "4>>12< < <<=?Q 	3 0 222Ezr   c                 2   || _         t          j        | j                                        | j                                        g          }t          j        | j        |dd          d         }t          j
        |d          }|r||| j        j        fS ||fS )a  
        Returns -2 x log-likelihood and the p-value for the hypothesized
        kurtosis.

        Parameters
        ----------
        kurt0 : float
            Kurtosis value to be tested

        return_weights : bool
            If True, function also returns the weights that
            maximize the likelihood ratio. Default is False.

        Returns
        -------
        test_results : tuple
            The log-likelihood ratio and p-value of kurt0
        r
   r   r   )rs   r"   r]   r   r   ri   r   r   ru   r   r_   rX   r1   )r   rs   r   r   re   r   s         r   r}   zDescStatUV.test_kurt  s    & 
4:??#4#4'+z~~'7'7#9 : : "4>>12< < <<=?Q 	2t/111Ezr   c                 @   || _         || _        t          j        | j                                        | j                                        g          }t          j        | j	        |dd          d         }t          j        |d          }|r||| j        j        fS ||fS )aG  
        Returns - 2 x log-likelihood and the p-value for the joint
        hypothesis test for skewness and kurtosis

        Parameters
        ----------
        skew0 : float
            Skewness value to be tested
        kurt0 : float
            Kurtosis value to be tested

        return_weights : bool
            If True, function also returns the weights that
            maximize the likelihood ratio. Default is False.

        Returns
        -------
        test_results : tuple
            The log-likelihood ratio and p-value  of the joint hypothesis test.
        r
   r   r   r!   )rm   rs   r"   r]   r   r   ri   r   r   rw   r   r_   rX   r1   )r   rm   rs   r   r   re   r   s          r   test_joint_skew_kurtzDescStatUV.test_joint_skew_kurt%  s    * 

4:??#4#4'+z~~'7'7#9 : : "4#612< < <<=?Q 	2t/111Ezr   c                    | j         }| j        }|3t          |          dd|z  |dz
  z  |dz
  |dz   z  |dz   z  z  dz  z  z   }|3t          |          dd|z  |dz
  z  |dz
  |dz   z  |dz   z  z  dz  z  z
  }t          j        d|z
  d          | _        t          j        | j        |t          |                    }t          j        | j        t          |          |          }||fS )	a  
        Returns the confidence interval for skewness.

        Parameters
        ----------
        sig : float
            The significance level.  Default is .05

        upper_bound : float
            Maximum value of skewness the upper limit can be.
            Default is .99 confidence limit assuming normality.

        lower_bound : float
            Minimum value of skewness the lower limit can be.
            Default is .99 confidence level assuming normality.

        Returns
        -------
        Interval : tuple
            Confidence interval for the skewness

        Notes
        -----
        If function returns f(a) and f(b) must have different signs, consider
        expanding lower and upper bounds
        N      @      @r   r          @r   r
   )	r)   r   r   r   r   rQ   r   r   rz   )r   r   r   r   r)   r   r   r   s           r   ci_skewzDescStatUV.ci_skewF  s   6 y
u++BI+r	dRi(r	 "# ##K u++BI+r	dRi(r	 "# ##K (1s7A&&t3[$u++NNt3T%[[+NNTzr   c                 @   | j         }| j        }|Nt          |          ddd|z  |dz
  z  |dz
  |dz   z  |dz   z  z  dz  z  z  |dz  dz
  |dz
  |dz   z  z  dz  z  z   }|Nt          |          ddd|z  |dz
  z  |dz
  |dz   z  |dz   z  z  dz  z  z  |dz  dz
  |dz
  |dz   z  z  dz  z  z
  }t          j        d	|z
  d	          | _        t          j        | j        |t          |                    }t          j        | j        t          |          |          }||fS )
a  
        Returns the confidence interval for kurtosis.

        Parameters
        ----------

        sig : float
            The significance level.  Default is .05

        upper_bound : float
            Maximum value of kurtosis the upper limit can be.
            Default is .99 confidence limit assuming normality.

        lower_bound : float
            Minimum value of kurtosis the lower limit can be.
            Default is .99 confidence limit assuming normality.

        Returns
        -------
        Interval : tuple
            Lower and upper confidence limit

        Notes
        -----
        For small n, upper_bound and lower_bound may have to be
        provided by the user.  Consider using test_kurt to find
        values close to the desired significance level.

        If function returns f(a) and f(b) must have different signs, consider
        expanding the bounds.
        Nr   r    r   r   r   r   g      @r
   )	r   r)   r   r   r   rQ   r   r   r   )r   r   r   r   r   r)   r   r   s           r   ci_kurtzDescStatUV.ci_kurtr  s   @ 
y"5//B29r	2r	dRi(r	 "# # $ "*"r	)( "$%%&K "5//B29r	2r	dRi(r	 "# # $ "*"r	)( "$%%&K (1s7A&&t3[%e__. .t3Xe__(* *Tzr   r   )r   rV   rA   r   r   )NNr   r   NN)r   r   r   r   r   rP   r   rh   r   r   ry   r}   r   r   r   r   r   r   r   r     s        $# # #( ( ( (B 8@19N$ N$ N$ N$`$ $ $ $L5 5 5 5r 6550 0 0 0d   <   <   B* * * *X5 5 5 5 5 5r   r   c                   <    e Zd ZdZd ZddZ	 	 ddZdd	ZddZdS )r   a   
    A class for conducting inference on multivariate means and correlation.

    Parameters
    ----------
    endog : ndarray
        Data to be analyzed

    Attributes
    ----------
    endog : ndarray
        Data to be analyzed

    nobs : float
        Number of observations
    c                 8    || _         |j        d         | _        d S r   )r   r   r)   r   s     r   r   zDescStatMV.__init__  s    
KN			r   Fc                    | j         }| j        }t          |          |j        d         k    rt	          d          |                    d|j        d                   }t          j        |j        d         |j        d         f          }||z  }||z
  }d|z  t          j        |j        d                   z  }|                     ||t          j        |          d|z  z            }dt          j	        ||j
                  z   }	d|z  dz  |	z  | _        dt          j        t          j        || j        z                      z  }
t          j        |
|j        d                   }|r|
|| j        j
        fS |
|fS )a5  
        Returns -2 x log likelihood and the p-value
        for a multivariate hypothesis test of the mean

        Parameters
        ----------
        mu_array  : 1d array
            Hypothesized values for the mean.  Must have same number of
            elements as columns in endog

        return_weights : bool
            If True, returns the weights that maximize the
            likelihood of mu_array. Default is False.

        Returns
        -------
        test_results : tuple
            The log-likelihood ratio and p-value for mu_array
        r
   zJmu_array must have the same number of elements as the columns of the data.r   r   r0   )r   r)   r   r   
ValueErrorr   r"   r^   rK   r%   r1   rX   r$   r#   r   r_   )r   mu_arrayr   r   r)   meansr'   
start_valsrd   rW   re   r   s               r   mv_test_meanzDescStatMV.mv_test_mean  so   ( 
yx==EKN** D E E E##Au{1~66QQ8995 5=$YQ!8!88
%%j(&(gdmmrDy&AC CBF8XZ000t8a<%/26"&(8!899:::X^A.// 	 0 222:r   r   r   r   r   r   Nc                    | j         j        d         dk    rt          d          t          j                    \  }}|	|                    d           n|                    |	           ||                    d           n|                    |           t          j        |||          }t          j        |||          }t          j
        ||          }g }|D ]B}|                    |                     t          j        |                    d                    Ct          j        ||          \  }}t          j        |          }|                    |j        d         |j        d                   }|                    |||j        |           |
r5|                    | j         dddf         | j         dddf         d	           |S )
a  
        Creates a confidence region plot for the mean of bivariate data

        Parameters
        ----------
        m1_low : float
            Minimum value of the mean for variable 1
        m1_upp : float
            Maximum value of the mean for variable 1
        mu2_low : float
            Minimum value of the mean for variable 2
        mu2_upp : float
            Maximum value of the mean for variable 2
        step1 : float
            Increment of evaluations for variable 1
        step2 : float
            Increment of evaluations for variable 2
        levs : list
            Levels to be drawn on the contour plot.
            Default =  (.001, .01, .05, .1, .2)
        plot_dta : bool
            If True, makes a scatter plot of the data on
            top of the contour plot. Defaultis False.
        var1_name : str
            Name of variable 1 to be plotted on the x-axis
        var2_name : str
            Name of variable 2 to be plotted on the y-axis

        Notes
        -----
        The smaller the step size, the more accurate the intervals
        will be

        If the function returns optimization failed, consider narrowing
        the boundaries of the plot

        Examples
        --------
        >>> import statsmodels.api as sm
        >>> two_rvs = np.random.standard_normal((20,2))
        >>> el_analysis = sm.emplike.DescStat(two_rvs)
        >>> contourp = el_analysis.mv_mean_contour(-2, 2, -2, 2, .1, .1)
        >>> contourp.show()
        r
   r!   z'Data must contain exactly two variablesNz
Variable 2z
Variable 1r   r   bo)r   r   r   r   r   r   r   r"   r   	itertoolsproductr   r   r   meshgridr   r   r1   plot)r   mu1_lowmu1_uppmu2_lowmu2_uppstep1step2r   	var1_name	var2_nameplot_dtar   r   xypairsr   iXYs                       r   mv_mean_contourzDescStatMV.mv_mean_contour  s   ^ :A!##FGGG%''RMM,''''MM)$$$MM,''''MM)$$$Igw..Igw..!!Q'' 	: 	:AHHT&&rz!}}55a89999{1a  1JqMMIIagaj!'!*--


1aT
*** 	>GGDJqqq!t$djA&6===
r   r   c                    | j         }| j        }|j        d         dk    rt          d          t	          j        |dddf                                         |dddf                                         |dddf                                         |dddf                                         g          }t	          j        d          }t	          j        d|z  gt          |          z            }|||||f}t          j        | j        ||dd          d         }	t          j        |	d          }
|r|	|
| j        j        fS |	|
fS )	at  
        Returns -2 x log-likelihood ratio and  p-value for the
        correlation coefficient between 2 variables

        Parameters
        ----------
        corr0 : float
            Hypothesized value to be tested

        return_weights : bool
            If true, returns the weights that maximize
            the log-likelihood at the hypothesized value
        r
   r!   z&Correlation matrix not yet implementedNr      r   )argsr   rE   )r)   r   r   NotImplementedErrorr"   r]   r   ri   zerosintr   fminr   r   r_   rX   r1   )r   r   r   r)   r   nuis0r;   r   r  re   r   s              r   r   zDescStatMV.test_corr1  sN    y
;q>Q%&NOOO%1+**,,#AAAqDkoo//#AAAqDk..00#AAAqDkoo//1 2 2
 Xa[[8R$YK#d))344udB1mD,e$12< < <<=?Q 	2t/111Ezr   r   c           
         | j         }| j        }t          j        d|z
  d          | _        t          j        |dddf         |dddf                   d         }|%t          d|dd|dz  z
  |dz
  z  d	z  z  z             }|4t          d
|dt          j	        d|dz  z
  |dz
  z            z  z
            }t          j        | j        ||          }t          j        | j        ||          }||fS )ad  
        Returns the confidence intervals for the correlation coefficient

        Parameters
        ----------
        sig : float
            The significance level.  Default is .05

        upper_bound : float
            Maximum value the upper confidence limit can be.
            Default is  99% confidence limit assuming normality.

        lower_bound : float
            Minimum value the lower confidence limit can be.
            Default is 99% confidence limit assuming normality.

        Returns
        -------
        interval : tuple
            Confidence interval for the correlation
        r
   Nr   )r   r
   g+?r   r   r    r   g+)r   r)   r   r   rQ   r"   corrcoefr   r   sqrtr   brenthr   )	r   r   r   r   r   r)   	point_estr   r   s	            r   ci_corrzDescStatMV.ci_corrR  s!   , 
y(1s7A&&KaaadU111a4[99$?	dI"yB"6"9"&*,!- -%- . .K fi"yB*>"9*& "' "' ('( ) )K t3[)LLt3YLLTzr   r   )r   NNF)r   r   )	r   r   r   r   r   r   r  r   r  r   r   r   r   r     s         "# # #' ' ' 'T BF16F F F FP   B& & & & & &r   r   )r   numpyr"   scipyr   scipy.statsr   r   r   statsmodels.base.optimizerr   r   statsmodels.graphicsr   r   r   r   r   r   r   r   <module>r     s.   "           , , , , , , , , , , 2 2 2 2 2 2     & & & & & &! ! !.^1 ^1 ^1 ^1 ^1 ^1 ^1 ^1BU U U U U U U UpN N N N N N N N N Nr   