
    >h                         S r SSKrSSKJr  SSKJrJrJr  SSK	J
r
  SSKrSSKJr  S r " S S	5      r " S
 S\5      r " S S\5      rg)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                     U R                   S:X  a  U R                  [        U 5      S5      n U R                  S   S:X  a  [	        U 5      $ U R                  S   S:  a  [        U 5      $ g)at  
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    rC:\Users\julio\OneDrive\Documentos\Trabajo\Ideas Frescas\venv\Lib\site-packages\statsmodels/emplike/descriptive.pyDescStatr      s^     zzQc%j!,{{1~%  {{1~%       c                       \ rS rSrSrS rS rS rS rS r	S r
S	 rS
 rSS jrS rS rS rS rS rS rS rS rSrg)
_OptFuncts1   a  
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                     g N selfr   s     r   __init___OptFuncts.__init__C   s    r   c                 :   [         R                  " U5      [         R                  " U5      [         R                  " X!5      -   -   nUSU-  :  nU) nXEU   -  n[         R                  " SU-  5      S-
  USUS-  -
  -  -   XV'   [         R                  " XW   5      XW'   U$ )a6  
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_star_OptFuncts._log_starF   s    6 FF7Orvvg')vvh'<(= >	"t)#$cN"T	*S02b1f3EE	VVI$67	r   c                     [         R                  " U5      [         R                  " X!5      -   nUSU-  :  nU) nUS-  * XV'   XW   S-  * XW'   X5-  n[         R                  " UR                  USS2S4   U-  5      $ )ag  
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   _hess_OptFuncts._hessj   s    ,  "vvg1FF"R$Y.$&*aiK!+?+HR*O(O%1vvhjj(1d7"3h">??r   c                     [         R                  " U5      [         R                  " X!5      -   nUSU-  :  nU) nUSXEU   -  -
  -  XV'   SXW   -  XW'   [         R                  " X5-  U5      $ )aj  
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   _grad_OptFuncts._grad   sr    , &&/BFF8,AAT	)$#q4#2F+F'FG#%(@#@ vvg/::r   c                    ^ ^^^	 [        T5      m	UU	U U4S jnUU	U U4S jnUU	U U4S jnSS0nUR                  5       n[        XEUSXvSSS	9nU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                 V   > [         R                  " TR                  U TTT5      5      * $ r   )r$   r&   r0   x0r)   r+   r   r*   s    r   <lambda>*_OptFuncts._modif_newton.<locals>.<lambda>   s     r8Wd KLLr   c                 .   > TR                  U TTT5      * $ r   )r;   r?   s    r   rA   rB          DJJr8WdCCr   c                 .   > TR                  U TTT5      * $ r   )r7   r?   s    r   rA   rB      rD   r   tol:0yE>r   2   r   )hessmaxiterdisp)r   squeezer   )
r   r(   r)   r*   fgradrI   kwdsresr+   s
   ` ``     @r   _modif_newton_OptFuncts._modif_newton   sQ    * 8}LCCt}kkm!3DR#$&1vr   c                     [         R                  " U R                  U R                  -
  SXR                  U R                  -
  -  -   -  5      $ )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_eta_OptFuncts._find_eta   sA     vvtzzDHH,C::0113 4 	4r   c                 D    U R                  U5      S   U R                  -
  $ )a  
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_mu_OptFuncts._ci_limits_mu   s!      ~~b!!$tww..r   c                    [         R                  " U R                  U-
  S-  5      nU R                  U-
  S-  U-  nS[         R                  " [         R                  " U R                  U-  5      5      -  U R
                  -
  $ )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
r3   )r$   r&   r   r%   r+   rY   )r   gammadenomnew_weightss       r   _find_gamma_OptFuncts._find_gamma   sl    $ 

U*r12zzE)b058BFF266$))k"9:;;GG 	r   c                 @   U R                   nU R                  nX1-
  S-  U R                  -
  nX1-
  n[        R                  " Xe45      nU R                  [        R                  " SU-  SU-  /5      U[        R                  " U5      SU-  -  5      nS[        R                  " XR                  5      -   n	SU-  S-  U	-  U l
        [        R                  " [        R                  " X@R                  -  5      5      n
U(       a  [        R                  " SU
-  S5      $ SU
-  $ )aB  
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
   r3   )r   r+   sig2_0r$   column_stackrQ   arrayonesr'   r4   ra   r&   r%   r   sf)r   nuisance_mupvalr   r+   sig_datamu_datar)   eta_starr`   llrs              r   _opt_var_OptFuncts._opt_var   s      

yy(Q.kk"&??G#67%%bhhT	/1Dy0: ';<D02d0KM BFF8ZZ009r>E1ffRVVD#3#33457728Q''Cxr   c                 D    U R                  U5      S   U R                  -
  $ )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_varrY   )r   vars     r   _ci_limits_var_OptFuncts._ci_limits_var   s!    " }}S!!$tww..r   c                 F   U R                   nU R                  nX!S   -
  nX!S   -
  S-  US   -
  nX!S   -
  S-  US   S-  -  U R                  -
  n[        R                  " XEU45      nU R                  [        R                  " SU-  SU-  SU-  /5      U[        R                  " U5      SU-  -  5      nS[        R                  " XR                  5      -   n	SU-  S-  U	-  U l
        [        R                  " [        R                  " X0R                  -  5      5      n
SU
-  $ )aC  
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    r3   )r   r+   skew0r$   rf   rQ   rg   rh   r'   r4   ra   r&   r%   )r   nuis_paramsr   r+   rm   rl   	skew_datar)   rn   r`   ro   s              r   	_opt_skew_OptFuncts._opt_skew3  s      

yya.(^+1[^C1~-!3 ^s*,/3zz:	??Gy#AB%%bhhT	/1Dy/1Dy0: ';<D/1wwt}T	/JL RVVHjj119r>E1ffRVVD#3#3345Cxr   c                 L   U R                   nU R                  nX!S   -
  nX!S   -
  S-  US   -
  nX!S   -
  S-  US   S-  -  S-
  U R                  -
  n[        R                  " XEU45      nU R                  [        R                  " SU-  SU-  SU-  /5      U[        R                  " U5      SU-  -  5      nS[        R                  " XR                  5      -   n	SU-  S-  U	-  U l
        [        R                  " [        R                  " X0R                  -  5      5      n
SU
-  $ )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
      rx   r    r3   )r   r+   kurt0r$   rf   rQ   rg   rh   r'   r4   ra   r&   r%   )r   rz   r   r+   rm   rl   	kurt_datar)   rn   r`   ro   s              r   	_opt_kurt_OptFuncts._opt_kurtS  s(     

yya.(^+1[^CA.14 ^q(*-./26**=	??Gy#AB%%bhhT	/1Dy/1Dy0: ';<D/1wwt}T	/JL BFF8ZZ009r>E1ffRVVD#3#3345Cxr   c           	         U R                   nU R                  nX!S   -
  nX!S   -
  S-  US   -
  nX!S   -
  S-  US   S-  -  U R                  -
  nX!S   -
  S-  US   S-  -  S-
  U R                  -
  n[        R
                  " XEXg45      nU R                  [        R                  " SU-  SU-  SU-  SU-  /5      U[        R                  " U5      SU-  -  5      n	S[        R                  " XR                  5      -   n
SU-  S-  U
-  U l        [        R                  " [        R                  " X0R                  -  5      5      nSU-  $ )	aX  
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
   rx   r!   r   r    r3   )r   r+   ry   r   r$   rf   rQ   rg   rh   r'   r4   ra   r&   r%   )r   rz   r   r+   rm   rl   r{   r   r)   rn   r`   ro   s               r   _opt_skew_kurt_OptFuncts._opt_skew_kurts  s`     

yya.(^+1[^C1~-!3 ^s*,/3zz:	A.14 ^q(*-./26**=	??Gy#LM%%bhhT	/1Dy/1Dy/1Dy0: '; =E/1wwt}T	/J	L
 RVVHjj119r>E1ffRVVD#3#3345Cxr   c                 D    U R                  U5      S   U R                  -
  $ )z
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_skewrY   )r   r   s     r   _ci_limits_skew_OptFuncts._ci_limits_skew  !     ~~d#A&00r   c                 D    U R                  U5      S   U R                  -
  $ )z
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_kurtrY   )r   kurts     r   _ci_limits_kurt_OptFuncts._ci_limits_kurt  r   r   c                    X1SSS2   -
  R                   u  pxUS-  US   -
  n	US-  US   -
  n
Xx-  UUS   US   -  S-  -  -
  n[        R                  " XyXU45      nU R                  X\U5      nS[        R                  " X5      -   nSU-  S-  U-  U l        [        R                  " [        R                  " X@R
                  -  5      5      nSU-  $ )z
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
   rx         ?r    r3   )r4   r$   rf   rQ   r'   ra   r&   r%   )r   rz   corr0r   r+   r@   weights0mu1_datamu2_data	sig1_data	sig2_datacorrel_datar)   rn   r`   ro   s                   r   _opt_correl_OptFuncts._opt_correl  s     $#A#&6699MKN2	MKN2	 +u ^k!n4;0< <??H$,$F G%%bH=RVVH//9r>E1ffRVVD#3#3345Cxr   c                 D    U R                  U5      S   U R                  -
  $ Nr   )	test_corrrY   )r   corrs     r   _ci_limits_corr_OptFuncts._ci_limits_corr  s    ~~d#A&00r   )ra   NF)__name__
__module____qualname____firstlineno____doc__r   r0   r7   r;   rQ   rU   r[   rb   rp   ru   r|   r   r   r   r   r   r   __static_attributes__r   r   r   r   r   1   sb    ""H@<;:>4$/$.B/&@@!F1141r   r   c                       \ rS rSrSrS rSS jr  SS jrSS jrSS jr	/ S	Q4S
 jr
SS jrSS jrSS jrSS jrSS jrSrg)r   i  a6  
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                 b    [         R                  " U5      U l        UR                  S   U l        g r   )r$   rL   r   r   r+   r   s     r   r   DescStatUV.__init__  s!    ZZ&
KKN	r   c                    Xl         U R                  nU R                  nSSU-  -
  U R                   [        U5      -
  -  nSSU-  -
  U R                   [	        U5      -
  -  n[
        R                  " U R                  XV5      nSU-  S-  SXsU R                   -
  -  -   -  nS[        R                  " [        R                  " XH-  5      5      -  n	U(       a  U	[        R                  " U	S5      U4$ U	[        R                  " U	S5      4$ )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    r3   r
   )rT   r   r+   maxminr   brentqrU   r$   r&   r%   r   ri   )
r   rT   return_weightsr   r+   eta_mineta_maxrn   ra   ro   s
             r   rX   DescStatUV.test_mean  s    ( 

yyd#3u:(=>d#3u:(=>??4>>7DDyB&"x488;K/L*LM266"&&!3455Q44Q''r   c                    U R                   nSU-
  nUS:X  a  [        R                  " US5      U l        [        R
                  " U5      n[        U5      [        R
                  " U5      -
  U-  n[        R
                  " U5      [        U5      -
  U-  n	[        R                  " U R                  U[        U5      U-
  5      n
[        R                  " U R                  U[        U5      U	-   5      nX4$ US:X  a  [        R                  " US5      U l        [        R                  " U R                  U[        U5      U-
  5      n[        R                  " U R                  [        U5      U-   U5      nXl-
  S-  [        R                  " Xl-
  S-  5      -  nXm-
  S-  [        R                  " Xm-
  S-  5      -  n[        R                  " X-  5      n[        R                  " X-  5      nUU4$ g)a6  
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-brentr_   r^   N)r   r   ppfrY   r$   meanr   r   r   r   r[   rb   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_meanDescStatUV.ci_mean
  s   j 

#g^#hhsA&DGWWU^FUbggen4?I#e*4?I??4#5#5vE
Y&(D??4#5#5vE
Y&(D:WhhsA&DG#??4+;+;YE
W$&L#??4+;+;Ug-z;L!0R7,345K"1b8,345LVVK/0Fff\12GG## r   c                    Xl         [        U R                  5      n[        U R                  5      n[        R
                  " U R                  XCSS9S   n[        R                  " US5      nU(       a  XVU R                  R                  4$ XV4$ )aa  
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)re   r   r   r   r   	fminboundrp   r   ri   ra   r4   )r   re   r   mu_maxmu_minro   p_vals          r   rs   DescStatUV.test_varZ  sw    6 TZZTZZ  -.0013Qt//1111:r   Nc                 <   U R                   nUcE  U R                  S-
  UR                  5       -  [        R                  " SU R                  S-
  5      -  nUcE  U R                  S-
  UR                  5       -  [        R                  " SU R                  S-
  5      -  n[        R                  " SU-
  S5      U l        [        R                  " U R                  XR                  5       5      n[        R                  " U R                  UR                  5       U5      nXV4$ )aa  
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.
r
   g-C6?gH.?)	r   r+   rt   r   r   rY   r   r   ru   )r   lower_boundupper_boundr   r   r   r   s          r   ci_varDescStatUV.ci_var  s    V 

 IIMUYY[8xxtyy1}-/K IIMUYY[8xxtyy1}-/K((1s7A&t22KMt22EIIKMzr   )皙?皙?皙?{Gz?MbP?c           	         [         R                  " 5       u  pU	R                  S5        U	R                  S5        [	        [
        R                  " XU5      5      n
[	        [
        R                  " X4U5      5      n/ nU H1  nXl        U
 H"  nUR                  U R                  USS95        M$     M3     [
        R                  " U5      R                  [        U5      [        U
5      5      nU	R                  XXS9  U$ )aT  
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)rk   levels)r   create_mpl_ax
set_ylabel
set_xlabellistr$   arangere   appendrp   asarrayr   r   contour)r   r   r   var_lowvar_highmu_stepvar_steplevsfigaxmu_vectvar_vectzsig0rT   s                  r   plot_contourDescStatUV.plot_contour  s    H %%'
j!
fryy':;		'X>?DKs67   JJqM!!#h-W>


7a
5
r   c                 X   Xl         [        R                  " U R                  R	                  5       U R                  R                  5       /5      n[        R                  " U R                  USSS9S   n[        R                  " US5      nU(       a  XEU R                  R                  4$ XE4$ )af  
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   rK   )ry   r$   rg   r   r   rt   r   fmin_powellr|   r   ri   ra   r4   )r   ry   r   start_nuisancero   r   s         r   r   DescStatUV.test_skew  s    & 
4::??#4'+zz~~'7#9 : ""4>>>12<<=?Q 0 0 2 222zr   c                 X   Xl         [        R                  " U R                  R	                  5       U R                  R                  5       /5      n[        R                  " U R                  USSS9S   n[        R                  " US5      nU(       a  XEU R                  R                  4$ XE4$ )ai  
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   )r   r$   rg   r   r   rt   r   r   r   r   ri   ra   r4   )r   r   r   r   ro   r   s         r   r   DescStatUV.test_kurt  s    & 
4::??#4'+zz~~'7#9 : ""4>>>12<<=?Qt//1111zr   c                 d   Xl         X l        [        R                  " U R                  R                  5       U R                  R                  5       /5      n[        R                  " U R                  USSS9S   n[        R                  " US5      nU(       a  XVU R                  R                  4$ XV4$ )a  
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#   )ry   r   r$   rg   r   r   rt   r   r   r   r   ri   ra   r4   )r   ry   r   r   r   ro   r   s          r   test_joint_skew_kurtDescStatUV.test_joint_skew_kurt%  s    * 

4::??#4'+zz~~'7#9 : ""4#6#612<<=?Qt//1111zr   c                    U R                   nU R                  nUc/  [        U5      SSU-  US-
  -  US-
  US-   -  US-   -  -  S-  -  -   nUc/  [        U5      SSU-  US-
  -  US-
  US-   -  US-   -  -  S-  -  -
  n[        R                  " SU-
  S5      U l        [        R                  " U R                  U[        U5      5      n[        R                  " U R                  [        U5      U5      nXg4$ )aC  
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
      @      @r    r"         @r   r
   )	r+   r   r   r   r   rY   r   r   r   )r   r   r   r   r+   r   r   r   s           r   ci_skewDescStatUV.ci_skewF  s   6 yy

u+BI+r	dRi(r	 "# ##K u+BI+r	dRi(r	 "# ##K ((1s7A&t33[$u+Nt33T%[+Nzr   c                 V   U R                   nU R                  nUcJ  [        U5      SSSU-  US-
  -  US-
  US-   -  US-   -  -  S-  -  -  US-  S-
  US-
  US-   -  -  S-  -  -   nUcJ  [        U5      SSSU-  US-
  -  US-
  US-   -  US-   -  -  S-  -  -  US-  S-
  US-
  US-   -  -  S-  -  -
  n[        R                  " SU-
  S5      U l        [        R                  " U R                  U[        U5      5      n[        R                  " U R                  [        U5      U5      nXg4$ )	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.
r   r"   r   r    r  r   g      @r
   )	r   r+   r   r   r   rY   r   r   r   )r   r   r   r   r   r+   r   r   s           r   ci_kurtDescStatUV.ci_kurtr  s   @ 

yy"5/B29r	2r	dRi(r	 "# # $ "*"r	)( "$%%&K "5/B29r	2r	dRi(r	 "# # $ "*"r	)( "$%%&K ((1s7A&t33[%e_.t33Xe_(*zr   )r   r   rT   r+   rY   re   ry   r   )r   r_   rG   l d(	 l    d(	 )NNr   r   NN)r   r   r   r   r   r   rX   r   rs   r   r   r   r   r   r  r  r   r   r   r   r   r     sT    $#(B 8@19N$`$L5r 60d<<B*X5r   r   c                   J    \ rS rSrSrS rS
S jr  SS jrSS jrSS jr	S	r
g)r   i  z
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    Xl         UR                  S   U l        g r   )r   r   r+   r   s     r   r   DescStatMV.__init__  s    
KKN	r   c                 >   U R                   nU R                  n[        U5      UR                  S   :w  a  [	        S5      eUR                  SUR                  S   5      n[        R                  " UR                  S   UR                  S   45      nX-  nX5-
  nSU-  [        R                  " UR                  S   5      -  nU R                  Xv[        R                  " U5      SU-  -  5      nS[        R                  " XR                  5      -   n	SU-  S-  U	-  U l        S[        R                  " [        R                  " X@R                  -  5      5      -  n
[        R                  " XR                  S   5      nU(       a  XU R                  R                  4$ X4$ )a  
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    r3   )r   r+   r   r   
ValueErrorr   r$   rh   rQ   r'   r4   ra   r&   r%   r   ri   )r   mu_arrayr   r   r+   meansr)   
start_valsrn   r`   ro   r   s               r   mv_test_meanDescStatMV.mv_test_mean  s[   ( 

yyx=EKKN* D E E##Au{{1~6QQ89 =$YQ!88
%%j&(ggdmrDy&ACBFF8ZZ00t8a<%/266"&&(8(8!89::^^A./ 0 0 2 222:r   Nc                    U R                   R                  S   S:w  a  [        S5      e[        R                  " 5       u  pU	c  UR                  S5        OUR                  U	5        Uc  UR                  S5        OUR                  U5        [        R                  " XU5      n[        R                  " X4U5      n[        R                  " X5      n/ nU H:  nUR                  U R                  [        R                  " U5      5      S   5        M<     [        R                  " X5      u  nn[        R                  " U5      nUR                  UR                  S   UR                  S   5      nUR!                  XUR"                  US9  U
(       a5  UR%                  U R                   SS2S4   U R                   SS2S4   S	5        U$ )
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   r4   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_contourDescStatMV.mv_mean_contour  s^   ^ ::A!#FGG%%'MM,'MM)$MM,'MM)$IIg.IIg.!!!'AHHT&&rzz!}5a89 {{1 1JJqMIIaggaj!''!*-


1T
*GGDJJq!t$djjA&6=
r   c                    U R                   nU R                  nUR                  S   S:w  a  [        S5      e[        R
                  " USS2S4   R                  5       USS2S4   R                  5       USS2S4   R                  5       USS2S4   R                  5       /5      n[        R                  " S5      n[        R
                  " SU-  /[        U5      -  5      nXX6U4n[        R                  " U R                  XXSSS9S   n	[        R                  " U	S5      n
U(       a  XU R                  R                   4$ X4$ )	a$  
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   rK   )r+   r   r   NotImplementedErrorr$   rg   r   rt   zerosintr   fminr   r   ri   ra   r4   )r   r   r   r+   r   nuis0r@   r   r+  ro   r   s              r   r   DescStatMV.test_corr1  s    yy

;;q>Q%&NOO%1+**,#AqDkoo/#AqDk..0#AqDkoo/1 2
 XXa[88R$YK#d)34d1mmD,,e12<<=?Qt//1111zr   c           
         U R                   nU R                  n[        R                  " SU-
  S5      U l        [
        R                  " USS2S4   USS2S4   5      S   nUc!  [        SUSSUS-  -
  US-
  -  S	-  -  -   5      nUc2  [        S
US[
        R                  " SUS-  -
  US-
  -  5      -  -
  5      n[        R                  " U R                  X65      n[        R                  " U R                  Xb5      nXx4$ )a  
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   rY   r$   corrcoefr   r   sqrtr   brenthr   )	r   r   r   r   r   r+   	point_estr   r   s	            r   ci_corrDescStatMV.ci_corrR  s   , 

yy((1s7A&KKadU1a4[9$?	dI"yB"6"9"&*,!- -%- .K fi"yB*>"9*& "' ('( )K t33[Lt33YLzr   )r   ra   r+   rY   r   ))r   r   r   r   r   NNF)r   r  )r   r   r   r   r   r   r  r'  r   r7  r   r   r   r   r   r     s.    "#'T BF16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>     sP   "   , , 2  &!.^1 ^1BU UpN Nr   