
    M/Ph:                         d Z ddlZddlZddlmZmZ  G d d          Zd ZddZ	dd	Z
ddZd ZddZddZddZd Zd ZddZdS ) zM
Created on Thu Feb 11 09:19:30 2021

Author: Josef Perktold
License: BSD-3

    N)interpolatestatsc                       e Zd ZdZddZdS )_Gridaz  Create Grid values and indices, grid in [0, 1]^d

    This class creates a regular grid in a d dimensional hyper cube.

    Intended for internal use, implementation might change without warning.


    Parameters
    ----------
    k_grid : tuple or array_like
        number of elements for axes, this defines k_grid - 1 equal sized
        intervals of [0, 1] for each axis.
    eps : float
        If eps is not zero, then x values will be clipped to [eps, 1 - eps],
        i.e. to the interior of the unit interval or hyper cube.


    Attributes
    ----------
    k_grid : list of number of grid points
    x_marginal: list of 1-dimensional marginal values
    idx_flat: integer array with indices
    x_flat: flattened grid values,
        rows are grid points, columns represent variables or axis.
        ``x_flat`` is currently also 2-dim in the univariate 1-dim grid case.

    r   c           	         || _         d |D             }t          j        t          j        t          j        t          j        |                    |                                        t                    }||                    d          z  }dk    r'fd|D             }t          j	        |dz
            }|| _
        || _        || _        d S )Nc                 B    g | ]}t          j        |          |d z
  z  S    )nparange).0kis     _/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/statsmodels/distributions/tools.py
<listcomp>z"_Grid.__init__.<locals>.<listcomp>/   s)    @@@2bimmrAv.@@@    r   c                 B    g | ]}t          j        |d z
            S r	   )r   clip)r   xiepss     r   r   z"_Grid.__init__.<locals>.<listcomp>6   s+    III"'"c1s733IIIr   r
   )k_gridr   column_stackunravel_indexr   prodastypefloatmaxr   
x_marginalidx_flatx_flat)selfr   r   r   r   r   s     `   r   __init__z_Grid.__init__,   s    @@@@@
? 276??!;!;VDD &-- 	 HLLOO+!88IIIIjIIIJWVS!c'22F$ r   Nr   )__name__
__module____qualname____doc__r!    r   r   r   r      s2         8     r   r   c                     t          j        |                                           }|j        }t	          |          D ]}|                    |          }|S )a  Cumulative probabilities from cell provabilites on a grid

    Parameters
    ----------
    probs : array_like
        Rectangular grid of cell probabilities.

    Returns
    -------
    cdf : ndarray
        Grid of cumulative probabilities with same shape as probs.
    axis)r   asarraycopyndimrangecumsum)probscdfkis       r   prob2cdf_gridr4   >   sU     *U


 
 
"
"CA1XX ! !jjaj  Jr   c                     |t           j        }t          j        |                                           }|j        }t          |          D ]}t          j        |||          }|S )a  Cell probabilities from cumulative probabilities on a grid.

    Parameters
    ----------
    cdf : array_like
        Grid of cumulative probabilities with same shape as probs.

    Returns
    -------
    probs : ndarray
        Rectangular grid of cell probabilities.

    N)prependr*   )r   _NoValuer+   r,   r-   r.   diff)r1   r6   probr2   r3   s        r   cdf2prob_gridr:   S   sb     +:c??!!D	A1XX 6 6wtW1555Kr   slicingc                    | j         }|dk    r|                                 }t          |          D ]}t          ddd          g|z  }t          ddd          g|z  }t          ddd          ||<   t          ddd          ||<   t	          |          }t	          |          }||         ||         z   dz  }n@|dk    r:ddlm} |                    | d	|z  t          j	        dg|z            z  d
          }|Nt          j
        d          }	t          |          D ]%}|	d         t          j        ||                   z  }	&||	z  }|S )a-  Compute average for each cell in grid using endpoints

    Parameters
    ----------
    values : array_like
        Values on a grid that will average over corner points of each cell.
    coords : None or list of array_like
        Grid coordinates for each axis use to compute volumne of cell.
        If None, then averaged values are not rescaled.
    _method : {"slicing", "convolve"}
        Grid averaging is implemented using numpy "slicing" or using
        scipy.signal "convolve".

    Returns
    -------
    Grid with averaged cell values.
    r;   Nr
      convolver   )signalg      ?valid)mode.N)r-   r,   r.   slicetuplescipyr@   r?   r   onesarrayr8   )
valuescoords_methodk_dimpdsl1sl2r@   dxs
             r   average_gridrR   k   st   $ KE)KKMMu 		& 		&AtT**+e3CtT**+e3C4T**CF1dD))CF**C**C3!C&A%AA		& 
J		      OOFCJ!u1E1E$E!(  * * Xa[[u 	4 	4AI!3!33BBFHr   d   :0yE>c                    t          j        |           }d}t          |          D ])}|                                }t          |j                  D ]It          fdt          |j                  D                       }||                    |d          z  }J|}||                                z  }g }t          |j                  D ]mt          fdt          |j                  D                       }|                    |d          }	|                    t          j        |	                     nt          |          |k     rd} n+|sddl
m}
 t          j        d|
           |S )	a  nearest matrix with uniform margins

    Parameters
    ----------
    mat : array_like, 2-D
        Matrix that will be converted to have uniform margins.
        Currently, `mat` has to be two dimensional.
    maxiter : in
        Maximum number of iterations.
    tol : float
        Tolerance for convergence, defined for difference between largest and
        smallest margin in each dimension.

    Returns
    -------
    ndarray, nearest matrix with uniform margins.

    Notes
    -----
    This function is intended for internal use and will be generalized in
    future. API will change.

    changed in 0.14 to support k_dim > 2.


    Fc                      g | ]
}|k    |S r'   r'   r   r3   axs     r   r   z*nearest_matrix_margins.<locals>.<listcomp>       BBBq!r'''''r   T)r*   keepdimsc                      g | ]
}|k    |S r'   r'   rW   s     r   r   z*nearest_matrix_margins.<locals>.<listcomp>   rY   r   r   )ConvergenceWarningz,Iterations did not converge, maxiter reached)r   r+   r.   r,   r-   rE   sumappendptpr   statsmodels.tools.sm_exceptionsr\   warningswarn)matmaxitertolpc	converged_pc0axsmptpsmargr\   rX   s              @r   nearest_matrix_marginsrm      s   6 
CBI7^^  ggii.. 	3 	3BBBBBE"'NNBBBCCC266sT6222CC
bffhh .. 	' 	'BBBBBE"'NNBBBCCC66sU633DLL&&&&u::IE   *FFFFFFD(	* 	* 	*Ir   c                     | j         \  }}t          j        ||f          }t          j        | d          }t          j        d|dz             dddf         ||t          j        |          f<   |S )zrankdata without ties for 2-d array

    This is a simplified version for ranking data if there are no ties.
    Works vectorized across columns.

    See Also
    --------
    scipy.stats.rankdata

    r   r)   r
   N)shaper   rG   argsortr   )xnobsk_varsrankssidxs        r   _rankdata_no_tiesrv      sq     7LD&GT6N##E:aa   D%'Yq$(%;%;AAAtG%DE$	&!!
!"Lr   Tc                     t          j        |           } | j        d         }|dz   }t          |g|z  d          }|r t	          |           | j        d         dz   z  } t          j        | |j                  \  }}|S )a=  count of observations in bins (histogram)

    currently only for bivariate data

    Parameters
    ----------
    data : array_like
        Bivariate data with observations in rows and two columns. Binning is
        in unit rectangle [0, 1]^2. If use_rank is False, then data should be
        in unit interval.
    k_bins : int
        Number of bins along each dimension in the histogram
    use_ranks : bool
        If use_rank is True, then data will be converted to ranks without
        tie handling.

    Returns
    -------
    bin counts : ndarray
        Frequencies are the number of observations in a given bin.
        Bin counts are a 2-dim array with k_bins rows and k_bins columns.

    Notes
    -----
    This function is intended for internal use and will be generalized in
    future. API will change.
    r=   r
   r   r   )bins)r   r+   ro   r   rv   histogramddr   )datak_bins	use_ranksrL   r2   g2freqrrh   s           r   frequencies_fromdatar      s    8 :dDJrNE
A	sU{	"	"	"B = &&$*Q-!*;<
 ~d777HE1Lr   
   Fc                    | j         }|dz   }t          |g|z            }|rt          |g|z  d|z            } |                     |j                  j        | }|d||z  z  z  }t          |          }	|rt          |	dd          }
nw|	|	                                z  }
n_t          |g|z  d          } | 	                    |j                  j        | }t          |d	          }
|
|
                                z  }
|
S )
a  Histogram probabilities as approximation to a copula density.

    Parameters
    ----------
    copula : instance
        Instance of a copula class. Only the ``pdf`` method is used.
    k_bins : int
        Number of bins along each dimension in the approximating histogram.
    force_uniform : bool
        If true, then the pdf grid will be adjusted to have uniform margins
        using `nearest_matrix_margin`.
        If false, then no adjustment is done and the margins may not be exactly
        uniform.
    use_pdf : bool
        If false, then the grid cell probabilities will be computed from the
        copula cdf.
        If true, then the density, ``pdf``, is used and cell probabilities
        are approximated by averaging the pdf of the cell corners. This is
        only useful if the cdf is not available.

    Returns
    -------
    bin probabilites : ndarray
        Probability that random variable falls in given bin. This corresponds
        to a discrete distribution, and is not scaled to bin size to form a
        piecewise uniform, histogram density.
        Bin probabilities are a k-dim array with k_bins segments in each
        dimensionrows.

    Notes
    -----
    This function is intended for internal use and will be generalized in
    future. API will change.
    r
   g?rx   rS   rT   )rd   re   gư>N)r6   )rL   rE   r   pdfr   reshaperR   rm   r]   r1   r:   )copular|   force_uniformuse_pdfrL   r2   ksgpdfgagpdf_gridcdfgs               r   approx_copula_pdfr     s   F LE
A	sU{		B #1#+3<000+vzz!(##+R0AuH$ 	%-b#4HHHHHBFFHH}HH1#+4(((+vzz!(##+R0 t444 	HLLNN"Or   binomc                    |j         d         }t          j        |           }t          j        |                              t
                    }|dz
  }|                                dk    rt          j                    5  t          j	        dt                     t          j                            |||d                   }ddd           n# 1 swxY w Y   ||z                      d          }n |                                dk    r-t          j        |dddf         dd	g          }	 |	|           }n|                                d
k    rt          j                    5  t          j	        dt                     t          j                            |d         |d	z   ||z
  d	z             |d	z   z  }ddd           n# 1 swxY w Y   ||z                      d          }nt'          d          |S )a  Evaluate 1-dimensional bernstein polynomial given grid of values.

    experimental, comparing methods

    Parameters
    ----------
    x : array_like
        Values at which to evaluate the Bernstein polynomial.
    fvals : ndarray
        Grid values of coefficients for Bernstein polynomial basis in the
        weighted sum.
    method: "binom", "beta" or "bpoly"
        Method to construct Bernstein polynomial basis, used for comparison
        of parameterizations.

        - "binom" uses pmf of Binomial distribution
        - "beta" uses pdf of Beta distribution
        - "bpoly" uses one interval in scipy.interpolate.BPoly

    Returns
    -------
    Bernstein polynomial at evaluation points, weighted sum of Bernstein
    polynomial basis.
    r=   g      ?r   ignorerC   Nbpolyg        r
   betazmethod not recogized)ro   r   r+   r   r   r   lowerra   catch_warningssimplefilterRuntimeWarningr   r   pmfr]   r   BPolyr   r   
ValueError)
rq   fvalsmethodk_termsxxr2   n	poly_base	bp_valuesbpbs
             r   _eval_bernstein_1dr   M  sC   2 k"oG	AB
	'!!%((A"A||~~  $&& 	= 	=!(N;;;1bm<<I	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= 	= Y&++B//			7	"	"aaagQ88CFF			6	!	!$&& 	R 	R!(N;;;
r)}a!eQUQYGG1q5QI	R 	R 	R 	R 	R 	R 	R 	R 	R 	R 	R 	R 	R 	R 	R Y&++B//		/000s&   >ACCC!AF>>GGc           	         |j         }|j        }|dk    rt          d          t          j        |           }|j         d         dk    rt          d          |j        \  }}|d         dz
  |d         dz
  }}t          j        |d                                       t                    }	t          j        |d                                       t                    }
t          j
                            |	ddddf         ||ddddf                   t          j
                            |
ddddf         ||ddddf                   z  }||z                      d                              d          }|S )a  Evaluate 2-dimensional bernstein polynomial given grid of values

    experimental

    Parameters
    ----------
    x : array_like
        Values at which to evaluate the Bernstein polynomial.
    fvals : ndarray
        Grid values of coefficients for Bernstein polynomial basis in the
        weighted sum.

    Returns
    -------
    Bernstein polynomial at evaluation points, weighted sum of Bernstein
    polynomial basis.
    r>   z `fval` needs to be 2-dimensionalr
   z*x needs to be bivariate and have 2 columnsr   Nr=   )ro   r-   r   r   
atleast_2dTr   r   r   r   r   r   r]   )rq   r   r   rL   r   x1x2n1n2k1k2r   r   s                r   _eval_bernstein_2dr     s[   $ kGJEzz;<<<	q		B	x{aEFFFTFBQZ!^WQZ!^B	71:			%	%e	,	,B	71:			%	%e	,	,B D!!!TM!2B111dD=8IJJD$M!2B111dD=8IJJKI"''++//33Ir   c                 b   |j         }|j        }t          j        |           }t          j        | j         d                   }t          |          D ]}t          j        ||                                       t                    }t          |dz             D ]
}|d         }||         dz
  }	|dd|f         }
|d         t          j
                            ||	|
          z   }t          j        |          }|j        d         |z  }t          |          D ]}|                    d          }|S )a  Evaluate d-dimensional bernstein polynomial given grid of valuesv

    experimental

    Parameters
    ----------
    x : array_like
        Values at which to evaluate the Bernstein polynomial.
    fvals : ndarray
        Grid values of coefficients for Bernstein polynomial basis in the
        weighted sum.

    Returns
    -------
    Bernstein polynomial at evaluation points, weighted sum of Bernstein
    polynomial basis.
    r   r
   rC   N)N.)ro   r-   r   r   zerosr.   r   r   r   r   r   _logpmfexpr   r]   )rq   r   r   rL   r   r   r3   r   rh   nir   r   s               r   _eval_bernstein_ddr     s%   $ kGJE	q		B $$I5\\ K KYwqz""))%00qs 	 	AIBBQZ!^1Xi(5;+>+>r2r+J+JJ		y!!I	"Y.I5\\ % %MM!$$		r   seqc                   	 t          j        |           j        d         }|rt                    |z  |dk    r0fdt	          |          D             }t          j        |          }n|                    d          rtt          j        dddf                   }|         }|ddddf         		fdt	          |          D             }t          j        j        d                   }|||<   nt          d          |fS )	a  
    Multivariate empiricial distribution function, empirical copula


    Notes
    -----
    Method "seq" is faster than method "brute", but supports mainly bivariate
    case. Speed advantage of "seq" is increasing in number of observations
    and decreasing in number of variables.
    (see Segers ...)

    Warning: This does not handle ties. The ecdf is based on univariate ranks
    without ties. The assignment of ranks to ties depends on the sorting
    algorithm and the initial ordering of the data.

    When the original data is used instead of ranks, then method "brute"
    computes the correct ecdf counts even in the case of ties.

    r   brutec                 r    g | ]3}|         k                         d                                           4S r	   allr]   )r   r3   rq   s     r   r   z_ecdf_mv.<locals>.<listcomp>  s9    >>>1!9//!$$))++>>>r   r   Nr
   c                     g | ]>}d |         |         k                         d                                          dz   ?S )Nr
   r   )r   r3   r   s     r   r   z_ecdf_mv.<locals>.<listcomp>  sH    NNN"RaR&BqE/..q115577!;NNNr   zmethod not available)	r   r+   ro   rv   r.   
startswithrp   emptyr   )
r{   r   r}   r   count	sort_idx0x_s0count_smallerrq   r   s
           @@r   _ecdf_mvr     s   ( 	
4A	
A %a  1$>>>>U1XX>>>
5!!			5	!	! 1JqAw''	|!!!QRR%[NNNNU1XXNNN$$(i/000!8Or   r"   )Nr;   )rS   rT   )T)r   TF)r   )r   T)r&   ra   numpyr   rF   r   r   r   r4   r:   rR   rm   rv   r   r   r   r   r   r   r'   r   r   <module>r      s@         $ $ $ $ $ $ $ $, , , , , , , ,^  *   0- - - -`4 4 4 4n  $' ' ' 'T: : : :~0 0 0 0f$ $ $N) ) )X% % % % % %r   