
    "g                         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      `/var/www/dash_apps/app1/venv/lib/python3.12/site-packages/statsmodels/distributions/bernstein.py__init__zBernsteinDistribution.__init__&   sg    #%::h#77]]
nn ggDKK&@qq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   ( zz$66$(rvvdQh/9::99>4=D

1776?aX%F:@ABBGQQ/AA~~du=1 +2BqEQJ+,,,	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    . JJqM66Q;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    . JJqM66Q;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TDJJ88C=B%C 	-B4t,BrF	-eBi(,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 )zGenerate 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'    ))''dnn.D.D.FGs7|$ 	4AqzQ&&q$..*>*>?v AAAA**//23q6:C KK

q3w{A3K!O4;AJ !/ !@ 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   