Ë
    ¢�Dj¬  ã                   óˆ   — d Z ddlZddlmZ ddlmZ ddlmZm	Z	m
Z
mZmZmZ  G d„ d«      Z G d„ d	e«      Z G d
„ de«      Zy)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                   óN   — e Zd ZdZd„ Zed„ «       Zed„ «       Zd„ Z	d„ Z
d„ Zd„ Zy	)
ÚBernsteinDistributiona–  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                 ó(  — t        j                  |«      x| _        }|j                  | _        |j
                  | _        t        j                  | j                  D �cg c]  }|dz
  ‘Œ	 c}«      | _        t        | j                  «      | _
        y c c}w )Né   )ÚnpÚasarrayÚcdf_gridÚndimÚk_dimÚshapeÚk_gridÚprodÚk_grid_productr   Ú_grid)Úselfr   Úis      úgC:\Crop_Prediction\Backend\crop-ai-system\venv\Lib\site-packages\statsmodels/distributions/bernstein.pyÚ__init__zBernsteinDistribution.__init__&   sg   € Ü#%§:¡:¨hÓ#7Ð7ˆŒ˜Ø—]‘]ˆŒ
Ø—n‘nˆŒÜ Ÿg™g°D·K±KÖ&@¨q q¨£sÒ&@ÓAˆÔÜ˜4Ÿ;™;Ó'ˆ�
ùò 'As   ÁBc           	      óN  — t        j                  |«      }t        j                  |dk  «      st        j                  |dkD  «      rt        d«      ‚|j                  dk(  r	|dd…df   }|j
                  d   }t        j                  |«      dk(  r|g|z  }|D �cg c]  }t        j                  d|z  d|dz   «      ‘Œ! }}t        j                  ||d¬«      \  }}t        |D �cg c]
  }|d   dk(  ‘Œ c}«      sJ ‚|t        |«      z  }t        |«      }	 | |	«      S c c}w c c}w )	ao  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_datazBernsteinDistribution.from_data-   s  € ô( �z‰z˜$ÓˆÜ�6‰6�$˜‘(ÔœrŸv™v d¨Q¡hÔ/ÜÐ9Ó:Ð:à�9‰9˜Š>Øš˜4˜‘=ˆDà—
‘
˜1‘ˆÜ�7‰7�6‹?˜aÒØ�X Ñ%ˆFØ:@ÖA°B”—‘˜B ™G Q¨¨Q©Õ/ÐAˆÐAÜ�~‰~˜d¨°uÔ=‰ˆˆ1ô ¨Ö+ 2�B�q‘E˜Q“JÒ+Ô,Ñ,Ø	ŒS�‹Y‰ˆä  Ó#ˆÙ�8‹}Ðùò Bùò ,s   Â$DÃ$D"c                 ó0   — t        | j                  d ¬«      S )N)Úprepend)r   r   )r   s    r   Ú	prob_gridzBernsteinDistribution.prob_gridU   s   € ä˜TŸ]™]°DÔ9Ð9ó    c                 óª   — t        j                  |«      }|j                  dk(  r| j                  dk(  r	|dd…df   }t	        || j
                  «      }|S )aþ  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   ÚcdfzBernsteinDistribution.cdfY   sH   € ô. �J‰J�q‹MˆØ�6‰6�QŠ;˜4Ÿ:™:¨š?Ø’!�T�'‘
ˆAÜ! ! T§]¡]Ó3ˆØˆr4   c                 óÄ   — t        j                  |«      }|j                  dk(  r| j                  dk(  r	|dd…df   }| j                  t        || j                  «      z  }|S )aþ  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   r3   ©r   r7   Úpdf_s      r   ÚpdfzBernsteinDistribution.pdfv   sT   € ô. �J‰J�q‹MˆØ�6‰6�QŠ;˜4Ÿ:™:¨š?Ø’!�T�'‘
ˆAà×"Ñ"Ô%7¸¸4¿>¹>Ó%JÑJˆØˆr4   c                 óò   — | j                   dk(  r| S dg| j                   z  }t        j                  |«      dk(  r|g}|D ]  }t        ddd«      ||<   Œ | j                  t        |«         }t        |«      }|S )aF  Get 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_marginalz"BernsteinDistribution.get_marginal”   s~   € ð �:‰:˜Š?ØˆKàˆT�D—J‘JÑˆÜ�8‰8�C‹=˜BÒØ�%ˆCØò 	-ˆBÜ˜4  tÓ,ˆBˆrŠFð	-à—‘œe B›iÑ(ˆÜ,¨UÓ3ˆØÐr4   c           
      ó¢  — t         j                  j                  || j                  j	                  «       «      }| j
                  }g }t        t        |«      «      D ]Ü  }||   dk7  sŒt        j                  || j                  j                  «      }g }t        |«      D ]s  }| j                  |   }	| j                  j                  |   ||      }
|j                  t        j                  j!                  |	|
z  dz   |	d|
z
  z  dz   ||   ¬«      «       Œu |j                  t        j"                  |«      «       ŒÞ t        j$                  |«      }|S )z¤Generate random numbers from distribution.

        Parameters
        ----------
        nobs : int
            Number of random observations to generate.
        r   r   )r$   )r   ÚrandomÚmultinomialr3   Úflattenr   Úranger(   Úunravel_indexr   r   r   Ú
x_marginalÚappendr   ÚbetaÚrvsÚcolumn_stackÚconcatenate)r   ÚnobsÚrvs_mnlÚk_compÚrvs_mr   rB   ÚrvsiÚjÚnÚxgiÚrvsms               r   rQ   zBernsteinDistribution.rvs¯   s'  € ô —)‘)×'Ñ'¨¨d¯n©n×.DÑ.DÓ.FÓGˆØ—‘ˆØˆÜ”s˜7“|Ó$ò 	4ˆAØ�q‰z˜Q‹Ü×&Ñ& q¨$¯.©.×*>Ñ*>Ó?�Ø�Ü˜v›ò A�AØŸ™ A™�AØŸ*™*×/Ñ/°Ñ2°3°q±6Ñ:�Cð —K‘K¤§
¡
§¡¨q°3©w¸©{¸AÀÀ3Á¹KÈ!¹OØ4;¸A±Jð !/ó !@õ AðAð —‘œRŸ_™_¨TÓ2Õ3ð	4ô �~‰~˜eÓ$ˆØˆr4   N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Úclassmethodr0   r   r3   r9   r=   rG   rQ   r?   r4   r   r   r      sI   „ ñò&(ð ñ%ó ð%ðN ñ:ó ð:òò:ò<ó6r4   r   c                   ó   — e Zd Zd„ Zd„ Zy)ÚBernsteinDistributionBVc                 ó2   — t        || j                  «      }|S ©N)r	   r   r6   s      r   r9   zBernsteinDistributionBV.cdfÍ   s   € Ü! ! T§]¡]Ó3ˆØˆr4   c                 óL   — | j                   t        || j                  «      z  }|S re   )r   r	   r3   r;   s      r   r=   zBernsteinDistributionBV.pdfÑ   s#   € à×"Ñ"Ô%7¸¸4¿>¹>Ó%JÑJˆØˆr4   N©r]   r^   r_   r9   r=   r?   r4   r   rc   rc   Ë   s   „ òór4   rc   c                   ó   — e Zd Zdd„Zdd„Zy)ÚBernsteinDistributionUVc                 ó6   — t        || j                  |¬«      }|S ©N)Úmethod)r
   r   )r   r7   rl   r8   s       r   r9   zBernsteinDistributionUV.cdfÙ   s   € ä! ! T§]¡]¸6ÔBˆØˆr4   c                 óP   — | j                   t        || j                  |¬«      z  }|S rk   )r   r
   r3   )r   r7   rl   r<   s       r   r=   zBernsteinDistributionUV.pdfÞ   s,   € à×"Ñ"Ô%7¸¸4¿>¹>Ø?Eô&Gñ Gˆàˆr4   N)Úbinomrg   r?   r4   r   ri   ri   ×   s   „ óô
r4   ri   )r`   Únumpyr   Úscipyr   Ústatsmodels.tools.decoratorsr   Ústatsmodels.distributions.toolsr   r   r   r   r	   r
   r   rc   ri   r?   r4   r   ú<module>rs      sH   ðñó Ý å 7÷D÷ D÷
vñ vôr	Ð3ô 	ôÐ3õ r4   