
    >h                         S r SSKrSSKJr  SSKJr  SSKJrJ	r	J
r
JrJrJr   " S S5      r " S S	\5      r " S
 S\5      rg)zM
Created on Wed Feb 17 15:35:23 2021

Author: Josef Perktold
License: BSD-3

    N)stats)cache_readonly)_Gridcdf2prob_gridprob2cdf_grid_eval_bernstein_dd_eval_bernstein_2d_eval_bernstein_1dc                   V    \ rS rSrSrS r\S 5       r\S 5       r	S r
S rS rS	 rS
rg)BernsteinDistribution   aZ  Distribution based on Bernstein Polynomials on unit hypercube.

Parameters
----------
cdf_grid : array_like
    cdf values on a equal spaced grid of the unit hypercube [0, 1]^d.
    The dimension of the arrays define how many random variables are
    included in the multivariate distribution.

Attributes
----------
cdf_grid : grid of cdf values
prob_grid : grid of cell or bin probabilities
k_dim : (int) number of components, dimension of random variable
k_grid : (tuple) shape of cdf_grid
k_grid_product : (int) total number of bins in grid
_grid : Grid instance with helper methods and attributes
c                 0   [         R                  " U5      =U l        nUR                  U l        UR
                  U l        [         R                  " U R                   Vs/ s H  o"S-
  PM	     sn5      U l        [        U R                  5      U l
        g s  snf )N   )npasarraycdf_gridndimk_dimshapek_gridprodk_grid_productr   _grid)selfr   is      vC:\Users\julio\OneDrive\Documentos\Trabajo\Ideas Frescas\venv\Lib\site-packages\statsmodels/distributions/bernstein.py__init__BernsteinDistribution.__init__&   si    #%::h#77]]
nn ggDKK&@KqsK&@A4;;'
 'As   Bc           	      |   [         R                  " U5      n[         R                  " US:  5      (       d  [         R                  " US:  5      (       a  [        S5      eUR                  S:X  a	  USS2S4   nUR
                  S   n[         R                  " U5      S:X  a  U/U-  nU Vs/ s H!  n[         R                  " SU-  SUS-   5      PM#     nn[         R                  " XSS9u  pg[        U Vs/ s H
  oS   S:H  PM     sn5      (       d   eU[        U5      -  n[        U5      n	U " U	5      $ s  snf s  snf )	a  Create distribution instance from data using histogram binning.

Classmethod to construct a distribution instance.

Parameters
----------
data : array_like
    Data with observation in rows and random variables in columns.
    Data can be 1-dimensional in the univariate case.
k_bins : int or list
    Number or edges of bins to be used in numpy histogramdd.
    If k_bins is a scalar int, then the number of bins of each
    component will be equal to it.

Returns
-------
Instance of a Bernstein distribution
r   r   zdata needs to be in [0, 1]N   F)binsdensity)r   r   any
ValueErrorr   r   sizelinspacehistogramddalllenr   )
clsdatak_binsr   nir"   ceeir   s
             r   	from_dataBernsteinDistribution.from_data-   s   ( zz$66$(rvvdQh//9::99>4=D

1776?aX%F:@A&BBGQQ/&A~~du= +2qEQJ+,,,,	SY #8} B ,s   *(D44D9c                 *    [        U R                  S S9$ )N)prepend)r   r   )r   s    r   	prob_gridBernsteinDistribution.prob_gridU   s    T]]D99    c                     [         R                  " U5      nUR                  S:X  a  U R                  S:X  a	  USS2S4   n[	        XR
                  5      nU$ )an  cdf values evaluated at x.

Parameters
----------
x : array_like
    Points of multivariate random variable at which cdf is evaluated.
    This can be a single point with length equal to the dimension of
    the random variable, or two dimensional with points (observations)
    in rows and random variables in columns.
    In the univariate case, a 1-dimensional x will be interpreted as
    different points for evaluation.

Returns
-------
pdf values

Notes
-----
Warning: 2-dim x with many points can be memory intensive because
currently the bernstein polynomials will be evaluated in a fully
vectorized computation.
r   N)r   r   r   r   r   r   r   xcdf_s      r   cdfBernsteinDistribution.cdfY   sF    . JJqM66Q;4::?!T'
A!!]]3r8   c                     [         R                  " U5      nUR                  S:X  a  U R                  S:X  a	  USS2S4   nU R                  [        XR                  5      -  nU$ )an  pdf values evaluated at x.

Parameters
----------
x : array_like
    Points of multivariate random variable at which pdf is evaluated.
    This can be a single point with length equal to the dimension of
    the random variable, or two dimensional with points (observations)
    in rows and random variables in columns.
    In the univariate case, a 1-dimensional x will be interpreted as
    different points for evaluation.

Returns
-------
cdf values

Notes
-----
Warning: 2-dim x with many points can be memory intensive because
currently the bernstein polynomials will be evaluated in a fully
vectorized computation.
r   N)r   r   r   r   r   r   r6   r   r;   pdf_s      r   pdfBernsteinDistribution.pdfv   sR    . JJqM66Q;4::?!T'
A""%7>>%JJr8   c                     U R                   S:X  a  U $ S/U R                   -  n[        R                  " U5      S:X  a  U/nU H  n[        SSS5      X#'   M     U R                  [        U5         n[        U5      nU$ )zGet marginal BernsteinDistribution.

Parameters
----------
idx : int or list of int
    Index or indices of the component for which the marginal
    distribution is returned.

Returns
-------
BernsteinDistribution instance for the marginal distribution.
r   r     N)r   r   r   slicer   tupler   )r   idxsliicdf_mbpd_marginals         r   get_marginal"BernsteinDistribution.get_marginal   sw     ::?KTDJJ88C=B%CB4t,BF eBi(,U3r8   c           
         [         R                  R                  XR                  R	                  5       5      nU R
                  n/ n[        [        U5      5       H  nX%   S:w  d  M  [         R                  " XPR                  R                  5      n/ n[        U5       Hn  nU R                  U   n	U R                  R                  U   Xh      n
UR                  [        R                  R!                  X-  S-   U	SU
-
  -  S-   X%   S95        Mp     UR                  [         R"                  " U5      5        M     [         R$                  " U5      nU$ )z|Generate random numbers from distribution.

Parameters
----------
nobs : int
    Number of random observations to generate.
r   r   )r&   )r   randommultinomialr6   flattenr   ranger*   unravel_indexr   r   r   
x_marginalappendr   betarvscolumn_stackconcatenate)r   nobsrvs_mnlk_comprvs_mr   rH   rvsijnxgirvsms               r   rX   BernsteinDistribution.rvs   s    ))''nn.D.D.FGs7|$AzQ&&q..*>*>?vAAA**//236:C KK

qw{A3K!O4;J !/ !@ A ' R__T23 % ~~e$r8   )r   r   r   r   r   N)__name__
__module____qualname____firstlineno____doc__r   classmethodr2   r   r6   r=   rB   rM   rX   __static_attributes__rE   r8   r   r   r      sI    &( % %N : ::<6r8   r   c                        \ rS rSrS rS rSrg)BernsteinDistributionBV   c                 0    [        XR                  5      nU$ N)r	   r   r:   s      r   r=   BernsteinDistributionBV.cdf   s    !!]]3r8   c                 J    U R                   [        XR                  5      -  nU$ rp   )r   r	   r6   r@   s      r   rB   BernsteinDistributionBV.pdf   s!    ""%7>>%JJr8   rE   Nre   rf   rg   rh   r=   rB   rk   rE   r8   r   rm   rm      s    r8   rm   c                   (    \ rS rSrSS jrSS jrSrg)BernsteinDistributionUV   c                 .    [        XR                  US9nU$ N)method)r
   r   )r   r;   rz   r<   s       r   r=   BernsteinDistributionUV.cdf   s    !!]]6Br8   c                 H    U R                   [        XR                  US9-  nU$ ry   )r   r
   r6   )r   r;   rz   rA   s       r   rB   BernsteinDistributionUV.pdf   s*    ""%7>>?E&G Gr8   rE   N)binomrt   rE   r8   r   rv   rv      s    
r8   rv   )ri   numpyr   scipyr   statsmodels.tools.decoratorsr   statsmodels.distributions.toolsr   r   r   r   r	   r
   r   rm   rv   rE   r8   r   <module>r      sH      7D D
v vr	3 	3 r8   