U
    Ãmœdâ  ã                   @   sv   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„ dƒZG dd	„ d	eƒZG d
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                   @   sP   e Zd ZdZdd„ Zedd„ ƒZedd„ ƒZdd	„ Z	d
d„ Z
dd„ Zd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                 C   sH   t  |¡ | _}|j| _|j| _t  dd„ | jD ƒ¡| _t	| jƒ| _
d S )Nc                 S   s   g | ]}|d  ‘qS )é   © )Ú.0Úir   r   ú\/home/sam/Atlas/atlas_env/lib/python3.8/site-packages/statsmodels/distributions/bernstein.pyÚ
<listcomp>+   s     z2BernsteinDistribution.__init__.<locals>.<listcomp>)ÚnpÚasarrayÚcdf_gridÚndimÚk_dimÚshapeÚk_gridÚprodÚk_grid_productr   Ú_grid)Úselfr   r   r   r   Ú__init__'   s
    zBernsteinDistribution.__init__c                 C   s¾   t  |¡}t  |dk ¡s&t  |dk¡r.tdƒ‚|jdkrH|dd…df }|jd }t  |¡dkrj|g| }dd„ |D ƒ}t j||dd�\}}td	d„ |D ƒƒs¢t	‚|t
|ƒ }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                 S   s"   g | ]}t  d | d|d ¡‘qS )éÿÿÿÿr   é   )r   Zlinspace)r   Únir   r   r   r   L   s     z3BernsteinDistribution.from_data.<locals>.<listcomp>F)ÚbinsZdensityc                 S   s   g | ]}|d  dk‘qS )r   r   r   )r   Úeir   r   r   r   P   s     )r   r   ÚanyÚ
ValueErrorr   r   ÚsizeZhistogramddÚallÚAssertionErrorÚlenr   )ÚclsÚdataZk_binsr   r    ÚcÚer   r   r   r   Ú	from_data.   s    



zBernsteinDistribution.from_datac                 C   s   t | jd d�S )N)Úprepend)r   r   )r   r   r   r   Ú	prob_gridV   s    zBernsteinDistribution.prob_gridc                 C   s>   t  |¡}|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_r   r   r   ÚcdfZ   s
    
zBernsteinDistribution.cdfc                 C   sD   t  |¡}|jdkr.| jdkr.|dd…df }| jt|| jƒ }|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   r0   Úpdf_r   r   r   Úpdfw   s
    
zBernsteinDistribution.pdfc                 C   sb   | j dkr| S dg| j  }t |¡dkr.|g}|D ]}tdddƒ||< q2| 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ÚiiZcdf_mZbpd_marginalr   r   r   Úget_marginal•   s    
z"BernsteinDistribution.get_marginalc              	   C   sÊ   t j || j ¡ ¡}| j}g }tt|ƒƒD ]�}|| dkr*t  || jj	¡}g }t|ƒD ]R}| j
| }	| jj| ||  }
| tjj|	|
 d |	d|
  d || d�¡ qV| t  |¡¡ q*t  |¡}|S )z¤Generate random numbers from distribution.

        Parameters
        ----------
        nobs : int
            Number of random observations to generate.
        r   r   )r$   )r   ÚrandomZmultinomialr.   Úflattenr   Úranger'   Zunravel_indexr   r   r   Z
x_marginalÚappendr   ÚbetaÚrvsZcolumn_stackZconcatenate)r   ZnobsZrvs_mnlZk_compZrvs_mr   r8   ZrvsiÚjÚnZxgiZrvsmr   r   r   rA   °   s     
"ÿ

zBernsteinDistribution.rvsN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   Úclassmethodr,   r   r.   r2   r5   r;   rA   r   r   r   r   r
      s   
'
r
   c                   @   s   e Zd Zdd„ Zdd„ ZdS )ÚBernsteinDistributionBVc                 C   s   t || jƒ}|S ©N)r   r   r/   r   r   r   r2   Î   s    zBernsteinDistributionBV.cdfc                 C   s   | j t|| jƒ }|S rJ   )r   r   r.   r3   r   r   r   r5   Ò   s    zBernsteinDistributionBV.pdfN©rD   rE   rF   r2   r5   r   r   r   r   rI   Ì   s   rI   c                   @   s    e Zd Zddd„Zddd„ZdS )	ÚBernsteinDistributionUVÚbinomc                 C   s   t || j|d�}|S ©N)Úmethod)r	   r   )r   r0   rO   r1   r   r   r   r2   Ú   s    zBernsteinDistributionUV.cdfc                 C   s   | j t|| j|d� }|S rN   )r   r	   r.   )r   r0   rO   r4   r   r   r   r5   ß   s    ÿzBernsteinDistributionUV.pdfN)rM   )rM   rK   r   r   r   r   rL   Ø   s   
rL   )rG   Únumpyr   Zscipyr   Zstatsmodels.tools.decoratorsr   Zstatsmodels.distributions.toolsr   r   r   r   r   r	   r
   rI   rL   r   r   r   r   Ú<module>   s     :