Ë
    £�DjÍg  ã                   ól   — d Z ddlZddlmZ ddlmZmZmZm	Z	m
Z
 g d¢Z G d„ de«      Z G d	„ d
e«      Zy)aZ  
Multivariate Conditional and Unconditional Kernel Density Estimation
with Mixed Data Types.

References
----------
[1] Racine, J., Li, Q. Nonparametric econometrics: theory and practice.
    Princeton University Press. (2007)
[2] Racine, Jeff. "Nonparametric Econometrics: A Primer," Foundation
    and Trends in Econometrics: Vol 3: No 1, pp1-88. (2008)
    http://dx.doi.org/10.1561/0800000009
[3] Racine, J., Li, Q. "Nonparametric Estimation of Distributions
    with Categorical and Continuous Data." Working Paper. (2000)
[4] Racine, J. Li, Q. "Kernel Estimation of Multivariate Conditional
    Distributions Annals of Economics and Finance 5, 211-235 (2004)
[5] Liu, R., Yang, L. "Kernel estimation of multivariate
    cumulative distribution function."
    Journal of Nonparametric Statistics (2008)
[6] Li, R., Ju, G. "Nonparametric Estimation of Multivariate CDF
    with Categorical and Continuous Data." Working Paper
[7] Li, Q., Racine, J. "Cross-validated local linear nonparametric
    regression" Statistica Sinica 14(2004), pp. 485-512
[8] Racine, J.: "Consistent Significance Testing for Nonparametric
        Regression" Journal of Business & Economics Statistics
[9] Racine, J., Hart, J., Li, Q., "Testing the Significance of
        Categorical Predictor Variables in Nonparametric Regression
        Models", 2006, Econometric Reviews 25, 523-544

é    Né   )Úkernels)Ú
GenericKDEÚEstimatorSettingsÚgpkeÚLeaveOneOutÚ_adjust_shape)ÚKDEMultivariateÚKDEMultivariateConditionalr   c                   óF   — e Zd ZdZdd„Zd„ Zd„ fd„Zdd„Zdd„Zd	„ Z	d
„ Z
y)r
   a¯  
    Multivariate kernel density estimator.

    This density estimator can handle univariate as well as multivariate data,
    including mixed continuous / ordered discrete / unordered discrete data.
    It also provides cross-validated bandwidth selection methods (least
    squares, maximum likelihood).

    Parameters
    ----------
    data : list of ndarrays or 2-D ndarray
        The training data for the Kernel Density Estimation, used to determine
        the bandwidth(s).  If a 2-D array, should be of shape
        (num_observations, num_variables).  If a list, each list element is a
        separate observation.
    var_type : str
        The type of the variables:

            - c : continuous
            - u : unordered (discrete)
            - o : ordered (discrete)

        The string should contain a type specifier for each variable, so for
        example ``var_type='ccuo'``.
    bw : array_like or str, optional
        If an array, it is a fixed user-specified bandwidth.  If a string,
        should be one of:

            - normal_reference: normal reference rule of thumb (default)
            - cv_ml: cross validation maximum likelihood
            - cv_ls: cross validation least squares

    defaults : EstimatorSettings instance, optional
        The default values for (efficient) bandwidth estimation.

    Attributes
    ----------
    bw : array_like
        The bandwidth parameters.

    See Also
    --------
    KDEMultivariateConditional

    Examples
    --------
    >>> import statsmodels.api as sm
    >>> nobs = 300
    >>> np.random.seed(1234)  # Seed random generator
    >>> c1 = np.random.normal(size=(nobs,1))
    >>> c2 = np.random.normal(2, 1, size=(nobs,1))

    Estimate a bivariate distribution and display the bandwidth found:

    >>> dens_u = sm.nonparametric.KDEMultivariate(data=[c1,c2],
    ...     var_type='cc', bw='normal_reference')
    >>> dens_u.bw
    array([ 0.39967419,  0.38423292])
    Nc                 óÚ  — || _         t        | j                   «      | _        t        || j                  «      | _        || _        t        j                  | j                  «      \  | _        | _        | j                  | j                  k  rt        d«      ‚|€
t        «       n|}| j                  |«       | j                  s| j                  |«      | _        y | j                  |«      | _        y )NzGThe number of observations must be larger than the number of variables.)Úvar_typeÚlenÚk_varsr	   ÚdataÚ	data_typeÚnpÚshapeÚnobsÚ
ValueErrorr   Ú_set_defaultsÚ	efficientÚ_compute_bwÚbwÚ_compute_efficient)Úselfr   r   r   Údefaultss        úlC:\Crop_Prediction\Backend\crop-ai-system\venv\Lib\site-packages\statsmodels/nonparametric/kernel_density.pyÚ__init__zKDEMultivariate.__init__e   s¸   € Ø ˆŒÜ˜$Ÿ-™-Ó(ˆŒÜ! $¨¯©Ó4ˆŒ	Ø!ˆŒÜ!#§¡¨$¯)©)Ó!4ÑˆŒ	�4”;Ø�9‰9˜Ÿ™Ò#Üð =ó >ð >à*2Ð*:Ô$Ô&ÀˆØ×Ñ˜8Ô$Ø�~Š~Ø×&Ñ& rÓ*ˆD�Gà×-Ñ-¨bÓ1ˆD�Gó    c                 óÖ   — d}|dt        | j                  «      z   dz   z  }|dt        | j                  «      z   dz   z  }|d| j                  z   dz   z  }|d| j                  z   dz   z  }|S )ú Provide something sane to print.zKDE instance
zNumber of variables: k_vars = ú
zNumber of samples:   nobs = zVariable types:      úBW selection method: )Ústrr   r   r   Ú
_bw_method©r   Úrprs     r   Ú__repr__zKDEMultivariate.__repr__u   sy   € àˆØÐ/´#°d·k±kÓ2BÑBÀTÑIÑIˆØÐ-´°D·I±I³Ñ>ÀÑEÑEˆØÐ&¨¯©Ñ6¸Ñ=Ñ=ˆØÐ&¨¯©Ñ8¸4Ñ?Ñ?ˆØˆ
r    c                 ó   — | S ©N© ©Úxs    r   ú<lambda>zKDEMultivariate.<lambda>~   ó   € °€ r    c           	      óÊ   — t        | j                  «      }d}t        |«      D ]<  \  }}t        || | j                  |dd…f    | j                  ¬«      }| ||«      z  }Œ> | S )aX  
        Returns the leave-one-out likelihood function.

        The leave-one-out likelihood function for the unconditional KDE.

        Parameters
        ----------
        bw : array_like
            The value for the bandwidth parameter(s).
        func : callable, optional
            Function to transform the likelihood values (before summing); for
            the log likelihood, use ``func=np.log``.  Default is ``f(x) = x``.

        Notes
        -----
        The leave-one-out kernel estimator of :math:`f_{-i}` is:

        .. math:: f_{-i}(X_{i})=\frac{1}{(n-1)h}
                    \sum_{j=1,j\neq i}K_{h}(X_{i},X_{j})

        where :math:`K_{h}` represents the generalized product kernel
        estimator:

        .. math:: K_{h}(X_{i},X_{j}) =
            \prod_{s=1}^{q}h_{s}^{-1}k\left(\frac{X_{is}-X_{js}}{h_{s}}\right)
        r   N©r   Údata_predictr   )r   r   Ú	enumerater   r   )r   r   ÚfuncÚLOOÚLÚiÚX_not_iÚf_is           r   Úloo_likelihoodzKDEMultivariate.loo_likelihood~   sl   € ô6 ˜$Ÿ)™)Ó$ˆØˆÜ# C›.ò 	‰JˆAˆwÜ�r  ¸¿	¹	À!ÂQÀ$¹Ð7GØ $§¡ô/ˆCà‘�c“‰N‰Að	ð
 ˆrˆ	r    c                 ój  — |€| j                   }nt        || j                  «      }g }t        t	        j
                  |«      d   «      D ]R  }|j                  t        | j                  | j                   ||dd…f   | j                  ¬«      | j                  z  «       ŒT t	        j                  |«      }|S )aT  
        Evaluate the probability density function.

        Parameters
        ----------
        data_predict : array_like, optional
            Points to evaluate at.  If unspecified, the training data is used.

        Returns
        -------
        pdf_est : array_like
            Probability density function evaluated at `data_predict`.

        Notes
        -----
        The probability density is given by the generalized product kernel
        estimator:

        .. math:: K_{h}(X_{i},X_{j}) =
            \prod_{s=1}^{q}h_{s}^{-1}k\left(\frac{X_{is}-X_{js}}{h_{s}}\right)
        Nr   r2   ©r   r	   r   Úranger   r   Úappendr   r   r   r   Úsqueeze)r   r3   Úpdf_estr8   s       r   ÚpdfzKDEMultivariate.pdf¢   s¡   € ð, ÐØŸ9™9‰Lä(¨°t·{±{ÓCˆLàˆÜ”r—x‘x Ó-¨aÑ0Ó1ò 	EˆAØ�N‰Nœ4 §¡¨d¯i©iØ-9¸!ºQ¸$Ñ-?Ø)-¯©ô8à:>¿)¹)ñDõ Eð	Eô
 —*‘*˜WÓ%ˆØˆr    c                 óp  — |€| j                   }nt        || j                  «      }g }t        t	        j
                  |«      d   «      D ]U  }|j                  t        | j                  | j                   ||dd…f   | j                  ddd¬«      | j                  z  «       ŒW t	        j                  |«      }|S )a¤  
        Evaluate the cumulative distribution function.

        Parameters
        ----------
        data_predict : array_like, optional
            Points to evaluate at.  If unspecified, the training data is used.

        Returns
        -------
        cdf_est : array_like
            The estimate of the cdf.

        Notes
        -----
        See https://en.wikipedia.org/wiki/Cumulative_distribution_function
        For more details on the estimation see Ref. [5] in module docstring.

        The multivariate CDF for mixed data (continuous and ordered/unordered
        discrete) is estimated by:

        .. math::

            F(x^{c},x^{d})=n^{-1}\sum_{i=1}^{n}\left[G(\frac{x^{c}-X_{i}}{h})\sum_{u\leq x^{d}}L(X_{i}^{d},x_{i}^{d}, \lambda)\right]

        where G() is the product kernel CDF estimator for the continuous
        and L() for the discrete variables.

        Used bandwidth is ``self.bw``.
        Nr   Úgaussian_cdfÚaitchisonaitken_cdfÚwangryzin_cdf)r   r3   r   ÚckertypeÚukertypeÚokertyper=   )r   r3   Úcdf_estr8   s       r   ÚcdfzKDEMultivariate.cdfÆ   s­   € ð> ÐØŸ9™9‰Lä(¨°t·{±{ÓCˆLàˆÜ”r—x‘x Ó-¨aÑ0Ó1ò 	GˆAØ�N‰Nœ4 §¡¨d¯i©iØ-9¸!ºQ¸$Ñ-?Ø)-¯©Ø)7Ø)>Ø)8ô:ð
 =A¿I¹IñFõ Gð	Gô —*‘*˜WÓ%ˆØˆr    c           	      óz  — d}t        t        j                  t        j                  t        j                  ¬«      }| j
                  }| j                   }| j                  }t        j                  |D �cg c]  }|dk(  ‘Œ	 c}«      }||   j                  «       }	t        j                  |j                  «      }
t        |«      D ]d  }t        |«      D ](  \  }} ||   ||   |dd…|f   |||f   «      |
dd…|f<   Œ* |
j                  d¬«      |	z  }|j                  d¬«      }||z  }Œf t        t        j                   t        j"                  t        j$                  ¬«      }t'        | j                  «      }d}t        j                  |j                  d   dz
  |j                  d   f«      }
t        |«      D ]f  \  }}t        |«      D ])  \  }} ||   ||   |dd…|f    |||f   «      |
dd…|f<   Œ+ |
j                  d¬«      |	z  }||j                  d¬«      z  }Œh ||dz  z  d|z  ||dz
  z  z  z
  S c c}w )aŠ  
        Returns the Integrated Mean Square Error for the unconditional KDE.

        Parameters
        ----------
        bw : array_like
            The bandwidth parameter(s).

        Returns
        -------
        CV : float
            The cross-validation objective function.

        Notes
        -----
        See p. 27 in [1]_ for details on how to handle the multivariate
        estimation with mixed data types see p.6 in [2]_.

        The formula for the cross-validation objective function is:

        .. math:: CV=\frac{1}{n^{2}}\sum_{i=1}^{n}\sum_{j=1}^{N}
            \bar{K}_{h}(X_{i},X_{j})-\frac{2}{n(n-1)}\sum_{i=1}^{n}
            \sum_{j=1,j\neq i}^{N}K_{h}(X_{i},X_{j})

        Where :math:`\bar{K}_{h}` is the multivariate product convolution
        kernel (consult [2]_ for mixed data types).

        References
        ----------
        .. [1] Racine, J., Li, Q. Nonparametric econometrics: theory and
                practice. Princeton University Press. (2007)
        .. [2] Racine, J., Li, Q. "Nonparametric Estimation of Distributions
                with Categorical and Continuous Data." Working Paper. (2000)
        r   )ÚcÚoÚurM   Nr   ©Úaxisé   )Údictr   Úgaussian_convolutionÚwang_ryzin_convolutionÚaitchison_aitken_convolutionr   r   r   r   ÚarrayÚprodÚemptyr   r>   r4   ÚsumÚgaussianÚ
wang_ryzinÚaitchison_aitkenr   )r   r   ÚFÚkertypesr   r   r   rM   Úix_contÚ_bw_cont_productÚKvalr8   ÚiiÚvtypeÚdensÚ	k_bar_sumr6   r7   r9   s                      r   ÚimsezKDEMultivariate.imseö   s@  € ðf ˆÜœ'×6Ñ6Ü!×8Ñ8Ü!×>Ñ>ô@ˆð �y‰yˆØ—	‘	ˆzˆØ—=‘=ˆÜ—(‘(¨hÖ7¨˜A ›HÒ7Ó8ˆØ˜g™;×+Ñ+Ó-ÐÜ�x‰x˜Ÿ
™
Ó#ˆÜ�t“ò 	ˆAÜ& xÓ0ò ;‘	��EØ-˜h u™o¨b°©fØ.2²1°b°5©kØ.2°1°b°5©kó;�’Q˜�U’ð;ð
 —9‘9 !�9Ó$Ð'7Ñ7ˆDØŸ™ a˜Ó(ˆIØ�‰N‰Að	ô œ'×*Ñ*Ü!×,Ñ,Ü!×2Ñ2ô4ˆô ˜$Ÿ)™)Ó$ˆØˆÜ�x‰x˜Ÿ™ A™ q™¨$¯*©*°Q©-Ð8Ó9ˆÜ# C›.ò 	"‰JˆAˆwÜ& xÓ0ò ;‘	��EØ-˜h u™o¨b°©fØ/6²q¸"°u©~¨oØ.2°1°b°5©kó;�’Q˜�U’ð;ð —9‘9 !�9Ó$Ð'7Ñ7ˆDØ�—‘˜q�Ó!Ñ!‰Að	"ð �D˜!‘G‘˜a !™e t¨t°a©xÑ'8Ñ9Ñ9Ð:ùò9 8s   Á3H8c                 ó(   — d}| j                   f}||fS )ú@Helper method to be able to pass needed vars to _compute_subset.r
   )r   ©r   Ú
class_typeÚ
class_varss      r   Ú_get_class_vars_typez$KDEMultivariate._get_class_vars_typeN  s   € à&ˆ
Ø—m‘mÐ&ˆ
Ø˜:Ð%Ð%r    ©NNr+   ©Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r)   r;   rB   rK   rg   rm   r,   r    r   r
   r
   )   s5   „ ñ:óv2ò ñ '2ó "óH"óH.ò`V;óp&r    r
   c                   óH   — e Zd ZdZ	 dd„Zd„ Zd„ fd„Zdd„Zdd„Zd	„ Z	d
„ Z
y)r   a  
    Conditional multivariate kernel density estimator.

    Calculates ``P(Y_1,Y_2,...Y_n | X_1,X_2...X_m) =
    P(X_1, X_2,...X_n, Y_1, Y_2,..., Y_m)/P(X_1, X_2,..., X_m)``.
    The conditional density is by definition the ratio of the two densities,
    see [1]_.

    Parameters
    ----------
    endog : list of ndarrays or 2-D ndarray
        The training data for the dependent variables, used to determine
        the bandwidth(s).  If a 2-D array, should be of shape
        (num_observations, num_variables).  If a list, each list element is a
        separate observation.
    exog : list of ndarrays or 2-D ndarray
        The training data for the independent variable; same shape as `endog`.
    dep_type : str
        The type of the dependent variables:

            c : Continuous
            u : Unordered (Discrete)
            o : Ordered (Discrete)

        The string should contain a type specifier for each variable, so for
        example ``dep_type='ccuo'``.
    indep_type : str
        The type of the independent variables; specified like `dep_type`.
    bw : array_like or str, optional
        If an array, it is a fixed user-specified bandwidth.  If a string,
        should be one of:

            - normal_reference: normal reference rule of thumb (default)
            - cv_ml: cross validation maximum likelihood
            - cv_ls: cross validation least squares

    defaults : Instance of class EstimatorSettings
        The default values for the efficient bandwidth estimation

    Attributes
    ----------
    bw : array_like
        The bandwidth parameters

    See Also
    --------
    KDEMultivariate

    References
    ----------
    .. [1] https://en.wikipedia.org/wiki/Conditional_probability_distribution

    Examples
    --------
    >>> import statsmodels.api as sm
    >>> nobs = 300
    >>> c1 = np.random.normal(size=(nobs,1))
    >>> c2 = np.random.normal(2,1,size=(nobs,1))

    >>> dens_c = sm.nonparametric.KDEMultivariateConditional(endog=[c1],
    ...     exog=[c2], dep_type='c', indep_type='c', bw='normal_reference')
    >>> dens_c.bw   # show computed bandwidth
    array([ 0.41223484,  0.40976931])
    Nc                 ó¾  — || _         || _        ||z   | _        t        | j                   «      | _        t        | j                  «      | _        t        || j                  «      | _        t        || j
                  «      | _        t        j                  | j                  «      \  | _        | _        t        j                  | j                  | j                  f«      | _        t        j                  | j                  «      d   | _        |€
t        «       n|}| j!                  |«       | j"                  s| j%                  |«      | _        y | j)                  |«      | _        y )Nr   )Údep_typeÚ
indep_typer   r   Úk_depÚk_indepr	   ÚendogÚexogr   r   r   Úcolumn_stackr   r   r   r   r   r   r   r   )r   rz   r{   rv   rw   r   r   s          r   r   z#KDEMultivariateConditional.__init__—  sú   € à ˆŒØ$ˆŒØ! JÑ.ˆŒÜ˜Ÿ™Ó'ˆŒ
Ü˜4Ÿ?™?Ó+ˆŒÜ" 5¨$¯*©*Ó5ˆŒ
Ü! $¨¯©Ó5ˆŒ	Ü "§¡¨¯©Ó 4ÑˆŒ	�4”:Ü—O‘O T§Z¡Z°·±Ð$;Ó<ˆŒ	Ü—h‘h˜tŸy™yÓ)¨!Ñ,ˆŒØ*2Ð*:Ô$Ô&ÀˆØ×Ñ˜8Ô$Ø�~Š~Ø×&Ñ& rÓ*ˆD�Gà×-Ñ-¨bÓ1ˆD�Gr    c                 ó<  — d}|dt        | j                  «      z   dz   z  }|dt        | j                  «      z   dz   z  }|dt        | j                  «      z   dz   z  }|d| j                  z   dz   z  }|d| j
                  z   dz   z  }|d| j                  z   dz   z  }|S )	r"   z$KDEMultivariateConditional instance
z+Number of independent variables: k_indep = r#   z'Number of dependent variables: k_dep = zNumber of observations: nobs = z!Independent variable types:      zDependent variable types:      r$   )r%   ry   rx   r   rw   rv   r&   r'   s     r   r)   z#KDEMultivariateConditional.__repr__ª  s¿   € à5ˆØÐ<Ü�4—<‘<Ó ñ!Ø#'ñ(ñ 	(ˆàÐ8Ü�4—:‘:‹ñØ!%ñ&ñ 	&ˆàÐ0´3°t·y±y³>ÑAÀDÑHÑHˆØÐ2°T·_±_ÑDÀtÑKÑKˆØÐ0°4·=±=Ñ@À4ÑGÑGˆØÐ&¨¯©Ñ8¸4Ñ?Ñ?ˆØˆ
r    c                 ó   — | S r+   r,   r-   s    r   r/   z#KDEMultivariateConditional.<lambda>·  r0   r    c           	      ó¼  — t        | j                  «      }t        | j                  «      j                  «       }d}t	        |«      D ]’  \  }}t        |«      }t        || | j                  |dd…f    | j                  | j                  z   ¬«      }	t        || j                  d | | j                  |dd…f    | j                  ¬«      }
|	|
z  }| ||«      z  }Œ” | S )aò  
        Returns the leave-one-out conditional likelihood of the data.

        If `func` is not equal to the default, what's calculated is a function
        of the leave-one-out conditional likelihood.

        Parameters
        ----------
        bw : array_like
            The bandwidth parameter(s).
        func : callable, optional
            Function to transform the likelihood values (before summing); for
            the log likelihood, use ``func=np.log``.  Default is ``f(x) = x``.

        Returns
        -------
        L : float
            The value of the leave-one-out function for the data.

        Notes
        -----
        Similar to ``KDE.loo_likelihood`, but substitute ``f(y|x)=f(x,y)/f(x)``
        for ``f(x)``.
        r   Nr2   )
r   r   r{   Ú__iter__r4   Únextr   rv   rw   rx   )r   r   r5   ÚyLOOÚxLOOr7   r8   ÚY_jr9   Úf_yxÚf_xr:   s               r   r;   z)KDEMultivariateConditional.loo_likelihood·  sÔ   € ô2 ˜4Ÿ9™9Ó%ˆÜ˜4Ÿ9™9Ó%×.Ñ.Ó0ˆØˆÜ “oò 	‰FˆAˆsÜ˜4“jˆGÜ˜ # °T·Y±Y¸qÂ!¸t±_Ð4DØ"&§-¡-°$·/±/Ñ"AôDˆDä�r˜$Ÿ*™*˜+�¨g¨XØ%)§Y¡Y¨q²!¨t¡_Ð$4Ø $§¡ô1ˆCð ˜‘*ˆCØ‘�c“‰N‰Að	ð ˆrˆ	r    c           	      ój  — |€| j                   }nt        || j                  «      }|€| j                  }nt        || j                  «      }g }t        j                  ||f«      }t        t        j                  |«      d   «      D ]˜  }t        | j                  | j                  ||dd…f   | j                  | j                  z   ¬«      }t        | j                  | j                  d | j                  ||dd…f   | j                  ¬«      }|j                  ||z  «       Œš t        j                  |«      S )aQ  
        Evaluate the probability density function.

        Parameters
        ----------
        endog_predict : array_like, optional
            Evaluation data for the dependent variables.  If unspecified, the
            training data is used.
        exog_predict : array_like, optional
            Evaluation data for the independent variables.

        Returns
        -------
        pdf : array_like
            The value of the probability density at `endog_predict` and `exog_predict`.

        Notes
        -----
        The formula for the conditional probability density is:

        .. math:: f(y|x)=\frac{f(x,y)}{f(x)}

        with

        .. math:: f(x)=\prod_{s=1}^{q}h_{s}^{-1}k
                            \left(\frac{x_{is}-x_{js}}{h_{s}}\right)

        where :math:`k` is the appropriate kernel for each variable.
        Nr   r2   )rz   r	   rx   r{   ry   r   r|   r>   r   r   r   r   rv   rw   r?   r@   )r   Úendog_predictÚexog_predictrA   r3   r8   r…   r†   s           r   rB   zKDEMultivariateConditional.pdfß  s  € ð< Ð Ø ŸJ™J‰Mä)¨-¸¿¹ÓDˆMØÐØŸ9™9‰Lä(¨°t·|±|ÓDˆLàˆÜ—‘¨°|Ð'DÓEˆÜ”r—x‘x Ó-¨aÑ0Ó1ò 	'ˆAÜ˜Ÿ™ d§i¡iØ%1°!²Q°$Ñ%7Ø"&§-¡-°$·/±/Ñ"AôDˆDô �t—w‘w˜tŸz™z˜{Ð+°$·)±)Ø$0°²A°Ñ$6Ø $§¡ô1ˆCð �N‰N˜4 #™:Õ&ð	'ô �z‰z˜'Ó"Ð"r    c                 ó@  — |€| j                   }nt        || j                  «      }|€| j                  }nt        || j                  «      }t        j                  |«      d   }t        j                  |«      }t        |«      D �]  }t        | j                  | j                  d | j                  ||dd…f   | j                  ¬«      | j                  z  }t        j                  |«      }t        | j                  d| j                   | j                   ||dd…f   | j                  dddd¬«      }t        | j                  | j                  d | j                  ||dd…f   | j                  d¬	«      }||z  j                  d¬
«      }	|	| j                  |z  z  ||<   �Œ |S )aÞ  
        Cumulative distribution function for the conditional density.

        Parameters
        ----------
        endog_predict : array_like, optional
            The evaluation dependent variables at which the cdf is estimated.
            If not specified the training dependent variables are used.
        exog_predict : array_like, optional
            The evaluation independent variables at which the cdf is estimated.
            If not specified the training independent variables are used.

        Returns
        -------
        cdf_est : array_like
            The estimate of the cdf.

        Notes
        -----
        For more details on the estimation see [2]_, and p.181 in [1]_.

        The multivariate conditional CDF for mixed data (continuous and
        ordered/unordered discrete) is estimated by:

        .. math::

            F(y|x)=\frac{n^{-1}\sum_{i=1}^{n}G(\frac{y-Y_{i}}{h_{0}}) W_{h}(X_{i},x)}{\widehat{\mu}(x)}

        where G() is the product kernel CDF estimator for the dependent (y)
        variable(s) and W() is the product kernel CDF estimator for the
        independent variable(s).

        References
        ----------
        .. [1] Racine, J., Li, Q. Nonparametric econometrics: theory and
                practice. Princeton University Press. (2007)
        .. [2] Liu, R., Yang, L. "Kernel estimation of multivariate cumulative
                    distribution function." Journal of Nonparametric
                    Statistics (2008)
        Nr   r2   rD   rE   rF   F)r   r3   r   rG   rH   rI   Útosum©r   r3   r   r‹   rP   )rz   r	   rx   r{   ry   r   r   rY   r>   r   r   rw   r   r@   rv   rZ   )
r   rˆ   r‰   ÚN_data_predictrJ   r8   Úmu_xÚ	cdf_endogÚcdf_exogÚSs
             r   rK   zKDEMultivariateConditional.cdf  sy  € ðR Ð Ø ŸJ™J‰Mä)¨-¸¿¹ÓDˆMØÐØŸ9™9‰Lä(¨°t·|±|ÓDˆLäŸ™ ,Ó/°Ñ2ˆÜ—(‘(˜>Ó*ˆÜ�~Ó&ó 	0ˆAÜ˜Ÿ™ §
¡
 Ð,°4·9±9Ø%1°!²Q°$Ñ%7Ø!%§¡ô2à48·I±Iñ>ˆDô —:‘:˜dÓ#ˆDÜ˜TŸW™W Q t§z¡zÐ2¸¿¹Ø*7¸º1¸Ñ*=Ø&*§m¡mØ&4Ø&;Ø&5¸UôDˆIô ˜DŸG™G D§J¡J KÐ0°t·y±yØ)5°aº°dÑ);Ø%)§_¡_¸EôCˆHð ˜XÑ%×*Ñ*°Ð*Ó2ˆAØ˜dŸi™i¨$Ñ.Ñ/ˆG�A‹Jð!	0ð$ ˆr    c                 ó^  — t        | j                  «      }d}t        | j                  «      }t	        j
                  | j                  dz
  df«      }t        |«      D �]Ê  \  }}|dd…| j                  d…f   }|dd…d| j                  …f   }	t	        j                  |	|«      }
t	        j                  ||	«      }t	        j                  ||«      }t	        j                  ||«      }t        || j                  d || j                  |dd…f   | j                  d¬«      }t        || j                  d || j                  |dd…f   | j                  d¬«      }t        |d| j                   |
|| j                  dddd¬	«      }||z  |z  j                  «       |d
z  z  }t        || | j                  |dd…f    | j                  | j                  z   ¬«      |z  }t        || j                  d | | j                  |dd…f    | j                  ¬«      |z  }|||d
z  z  d
||z  z  z
  z  }�ŒÍ ||z  S )a‹  
        The integrated mean square error for the conditional KDE.

        Parameters
        ----------
        bw : array_like
            The bandwidth parameter(s).

        Returns
        -------
        CV : float
            The cross-validation objective function.

        Notes
        -----
        For more details see pp. 156-166 in [1]_. For details on how to
        handle the mixed variable types see [2]_.

        The formula for the cross-validation objective function for mixed
        variable types is:

        .. math:: CV(h,\lambda)=\frac{1}{n}\sum_{l=1}^{n}
            \frac{G_{-l}(X_{l})}{\left[\mu_{-l}(X_{l})\right]^{2}}-
            \frac{2}{n}\sum_{l=1}^{n}\frac{f_{-l}(X_{l},Y_{l})}{\mu_{-l}(X_{l})}

        where

        .. math:: G_{-l}(X_{l}) = n^{-2}\sum_{i\neq l}\sum_{j\neq l}
                        K_{X_{i},X_{l}} K_{X_{j},X_{l}}K_{Y_{i},Y_{j}}^{(2)}

        where :math:`K_{X_{i},X_{l}}` is the multivariate product kernel and
        :math:`\mu_{-l}(X_{l})` is the leave-one-out estimator of the pdf.

        :math:`K_{Y_{i},Y_{j}}^{(2)}` is the convolution kernel.

        The value of the function is minimized by the ``_cv_ls`` method of the
        `GenericKDE` class to return the bw estimates that minimize the
        distance between the estimated and "true" probability density.

        References
        ----------
        .. [1] Racine, J., Li, Q. Nonparametric econometrics: theory and
                practice. Princeton University Press. (2007)
        .. [2] Racine, J., Li, Q. "Nonparametric Estimation of Distributions
                with Categorical and Continuous Data." Working Paper. (2000)
        r   r   NFrŒ   Úgauss_convolutionÚwangryzin_convolutionÚaitchisonaitken_convolution)r   r3   r   rG   rI   rH   r‹   rR   r2   )r   r   Úfloatr   r   Úonesr4   rx   Úkronr   r{   rw   rv   rZ   )r   r   ÚzLOOÚCVr   Úexpanderrc   ÚZÚXÚYÚYe_LÚYe_RÚXe_LÚXe_RÚK_Xi_XlÚK_Xj_XlÚK2_Yi_YjÚGÚf_X_YÚm_xs                       r   rg   zKDEMultivariateConditional.imse[  s!  € ô^ ˜4Ÿ9™9Ó%ˆØˆÜ�T—Y‘YÓˆÜ—7‘7˜DŸI™I¨™M¨1Ð-Ó.ˆÜ˜t“_ó 	5‰EˆB�Ø’!�T—Z‘Z‘[�.Ñ!ˆAØ’!�[�d—j‘j�[�.Ñ!ˆAÜ—7‘7˜1˜hÓ'ˆDÜ—7‘7˜8 QÓ'ˆDÜ—7‘7˜1˜hÓ'ˆDÜ—7‘7˜8 QÓ'ˆDÜ˜2˜dŸj™j˜k˜?°Ø(,¯	©	°"²a°%Ñ(8Ø$(§O¡O¸5ôBˆGô ˜2˜dŸj™j˜k˜?°Ø(,¯	©	°"²a°%Ñ(8Ø$(§O¡O¸5ôBˆGô ˜B˜q §¡Ð,°4Ø)-¸¿¹Ø%8Ø%<Ø%BØ"'ô)ˆHð ˜7Ñ" XÑ-×2Ñ2Ó4°t¸Q±wÑ>ˆAÜ˜ 1 "°D·I±I¸bÂ!¸eÑ4DÐ3DØ#'§=¡=°4·?±?Ñ#BôEØGKñLˆEä�r˜$Ÿ*™*˜+�¨a¨RØ%)§Y¡Y¨r²1¨uÑ%5Ð$5Ø $§¡ô1à37ñ8ˆCð �1�s˜a‘x‘< 1¨°©Ñ#4Ñ4Ñ4ŠBð3	5ð6 �D‰yÐr    c                 óT   — d}| j                   | j                  | j                  f}||fS )ri   r   )rx   rv   rw   rj   s      r   rm   z/KDEMultivariateConditional._get_class_vars_type«  s*   € à1ˆ
Ø—j‘j $§-¡-°·±ÐAˆ
Ø˜:Ð%Ð%r    r+   rn   ro   r,   r    r   r   r   U  s;   „ ñ?ðD ó2ò&ñ '2ó &óP2#óhFòPNó`&r    r   )rs   Únumpyr   Ú r   Ú_kernel_baser   r   r   r   r	   Ú__all__r
   r   r,   r    r   ú<module>r®      sA   ðñó< å ÷õ ò Q€ôi&�jô i&ôX	Z& õ Z&r    