
    M/Ph                         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dS )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                   \    e Zd ZdZd Zed             Zed             Zd Z	d Z
d Zd Zd	S )
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        d | j        D                       | _        t          | j                  | _
        d S )Nc                     g | ]}|d z
  S )    ).0is     c/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/statsmodels/distributions/bernstein.py
<listcomp>z2BernsteinDistribution.__init__.<locals>.<listcomp>*   s    &@&@&@qqs&@&@&@    )npasarraycdf_gridndimk_dimshapek_gridprodk_grid_productr   _grid)selfr   s     r   __init__zBernsteinDistribution.__init__&   sb    #%:h#7#77]
n g&@&@DK&@&@&@AA4;''


r   c                    t          j        |          }t          j        |dk               st          j        |dk              rt          d          |j        dk    r|dddf         }|j        d         }t          j        |          dk    r|g|z  }d |D             }t          j        ||d          \  }}t          d |D                       sJ |t          |          z  }t          |          } | |          S )	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]Nc                 F    g | ]}t          j        d |z  d|dz             S )r      )r   linspace)r   nis     r   r   z3BernsteinDistribution.from_data.<locals>.<listcomp>K   s.    AAABBGQQ//AAAr   F)binsdensityc                 &    g | ]}|d          dk    S )r   r   r   )r   eis     r   r   z3BernsteinDistribution.from_data.<locals>.<listcomp>O   s     +++2BqEQJ+++r   )r   r   any
ValueErrorr   r   sizehistogramddalllenr   )clsdatak_binsr   r(   cer   s           r   	from_datazBernsteinDistribution.from_data-   s   ( z$6$( 	;rvdQh// 	;9:::9>>4=D
176??aX%FAA&AAA~du===1 +++++,,,,,	SYY ##s8}}r   c                 .    t          | j        d           S )N)prepend)r   r   )r    s    r   	prob_gridzBernsteinDistribution.prob_gridU   s    T]D9999r   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   sM    . JqMM6Q;;4:??!!!T'
A!!T]33r   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   r:   r    r=   pdf_s      r   pdfzBernsteinDistribution.pdfv   sV    . JqMM6Q;;4:??!!!T'
A"%74>%J%JJr   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$   r   N)r   r   r   slicer   tupler   )r    idxsliicdf_mbpd_marginals         r   get_marginalz"BernsteinDistribution.get_marginal   s     :??KTDJ8C==B%C 	- 	-B4t,,BrFFeBii(,U33r   c           
         t           j                            || j                                                  }| j        }g }t          t          |                    D ]}||         dk    rt          j        || j        j	                  }g }t          |          D ]w}| j
        |         }	| j        j        |         ||                  }
|                    t          j                            |	|
z  dz   |	d|
z
  z  dz   ||                              x|                    t          j        |                     t          j        |          }|S )zGenerate random numbers from distribution.

        Parameters
        ----------
        nobs : int
            Number of random observations to generate.
        r   r   )r.   )r   randommultinomialr:   flattenr   ranger1   unravel_indexr   r   r   
x_marginalappendr   betarvscolumn_stackconcatenate)r    nobsrvs_mnlk_comprvs_mr   rG   rvsijnxgirvsms               r   rV   zBernsteinDistribution.rvs   s?    )''dn.D.D.F.FGGs7||$$ 	4 	4AqzQ&q$.*>??v A AAAA*/23q6:C KK
q3w{A3K!O4;AJ !/ !@ !@ A A A AR_T22333~e$$r   N)__name__
__module____qualname____doc__r!   classmethodr7   r   r:   r?   rC   rL   rV   r   r   r   r   r      s         &( ( ( % % [%N : : ^:  :  <  6    r   r   c                       e Zd Zd Zd ZdS )BernsteinDistributionBVc                 0    t          || j                  }|S N)r	   r   r<   s      r   r?   zBernsteinDistributionBV.cdf   s    !!T]33r   c                 @    | j         t          || j                  z  }|S rj   )r   r	   r:   rA   s      r   rC   zBernsteinDistributionBV.pdf   s!    "%74>%J%JJr   Nrb   rc   rd   r?   rC   r   r   r   rh   rh      s2              r   rh   c                       e Zd ZddZddZdS )BernsteinDistributionUVbinomc                 4    t          || j        |          }|S N)method)r
   r   )r    r=   rr   r>   s       r   r?   zBernsteinDistributionUV.cdf   s    !!T]6BBBr   c                 D    | j         t          || j        |          z  }|S rq   )r   r
   r:   )r    r=   rr   rB   s       r   rC   zBernsteinDistributionUV.pdf   s3    "%74>?E&G &G &G Gr   N)ro   rl   r   r   r   rn   rn      s<           
     r   rn   )re   numpyr   scipyr   statsmodels.tools.decoratorsr   statsmodels.distributions.toolsr   r   r   r   r	   r
   r   rh   rn   r   r   r   <module>rx      s2              7 7 7 7 7 7D D D D D D D D D D D D D D D D
v v v v v v v vr	 	 	 	 	3 	 	 	    3     r   