Ë
    £�Dj_>  ã                   ó¾   — d Z ddlZddlZddlZddlmZ ddl	m
Z
 d„ Zdd„Zdd„Zdd„Zdd	„Zd
„ Zdd„Zd„ Zd„ Zd„ Zdd„Zd„ Zd„ Zd„ Z G d„ de«      Zdd„Zdd„Zy)z
Utility functions models code
é    N)Ú_is_using_pandas)Ú
array_likec                 ó~   — t        | t        «      r| S t        | t        «      r| j                  d«      S t        | «      S )NÚlatin1)Ú
isinstanceÚstrÚbytesÚdecode)Úss    ú[C:\Crop_Prediction\Backend\crop-ai-system\venv\Lib\site-packages\statsmodels\tools\tools.pyÚasstr2r      s3   € Ü�!”SÔØˆÜ	�A”uÔ	Ø�x‰x˜Ó!Ð!ä�1‹vˆó    c                 ó@   — i }t        | «      D ]  \  }}||||z   <   Œ |S )zd
    Helper function to create a dictionary mapping a column number
    to the name in tmp_arr.
    )Ú	enumerate)Útmp_arrÚoffsetÚcol_mapÚiÚcol_names        r   Ú_make_dictnamesr      s4   € ð
 €GÜ  Ó)ò '‰ˆˆ8Ø&ˆ��F‘
Òð'à€Nr   c                 óÚ  — t        j                  | «      } | j                  dk(  r	| dd…df   } |�“t        j                  |«      }|j                  dk(  r	|dd…df   }t        j                  t        j
                  | «      j                  |«       t        j
                  |«      j                  |«       «      }| |   ||   fS t        j
                  | «      j                  |«       }| |   S )a�  
    Returns views on the arrays Y and X where missing observations are dropped.

    Y : array_like
    X : array_like, optional
    axis : int
        Axis along which to look for missing observations.  Default is 1, ie.,
        observations in rows.

    Returns
    -------
    Y : ndarray
        All Y where the
    X : ndarray

    Notes
    -----
    If either Y or X is 1d, it is reshaped to be 2d.
    é   N)ÚnpÚasarrayÚndimÚarrayÚlogical_andÚisnanÚany)ÚYÚXÚaxisÚkeepidxs       r   Údrop_missingr$       sÄ   € ô( 	�
‰
�1‹€AØ‡v�v�‚{ØŠa�ˆg‰JˆØ€}Ü�H‰H�Q‹KˆØ�6‰6�QŠ;Ø’!�T�'‘
ˆAÜ—.‘.¤"§(¡(¨1£+§/¡/°$Ó"7Ð!7Ü"$§(¡(¨1£+§/¡/°$Ó"7Ð!7ó9ˆà�‰z˜1˜W™:Ð%Ð%ä—8‘8˜A“;—?‘? 4Ó(Ð(ˆØ�‰zÐr   c                 ó   — t        d«      ‚)ax  
    Construct a dummy matrix from categorical variables

    .. deprecated:: 0.12

       Use pandas.get_dummies instead.

    Parameters
    ----------
    data : array_like
        An array, Series or DataFrame.  This can be either a 1d vector of
        the categorical variable or a 2d array with the column specifying
        the categorical variable specified by the col argument.
    col : {str, int, None}
        If data is a DataFrame col must in a column of data. If data is a
        Series, col must be either the name of the Series or None. For arrays,
        `col` can be an int that is the (zero-based) column index
        number.  `col` can only be None for a 1d array.  The default is None.
    dictnames : bool, optional
        If True, a dictionary mapping the column number to the categorical
        name is returned.  Used to have information about plain arrays.
    drop : bool
        Whether or not keep the categorical variable in the returned matrix.

    Returns
    -------
    dummy_matrix : array_like
        A matrix of dummy (indicator/binary) float variables for the
        categorical data.
    dictnames :  dict[int, str], optional
        Mapping between column numbers and categorical names.

    Notes
    -----
    This returns a dummy variable for *each* distinct variable.  If a
    a DaataFrame is provided, the names for the new variable is the
    old variable name - underscore - category name.  So if the a variable
    'vote' had answers as 'yes' or 'no' then the returned array would have to
    new variables-- 'vote_yes' and 'vote_no'.  There is currently
    no name checking.

    Examples
    --------
    >>> import numpy as np
    >>> import statsmodels.api as sm

    Univariate examples

    >>> import string
    >>> string_var = [string.ascii_lowercase[0:5],
    ...               string.ascii_lowercase[5:10],
    ...               string.ascii_lowercase[10:15],
    ...               string.ascii_lowercase[15:20],
    ...               string.ascii_lowercase[20:25]]
    >>> string_var *= 5
    >>> string_var = np.asarray(sorted(string_var))
    >>> design = sm.tools.categorical(string_var, drop=True)

    Or for a numerical categorical variable

    >>> instr = np.floor(np.arange(10,60, step=2)/10)
    >>> design = sm.tools.categorical(instr, drop=True)

    With a structured array

    >>> num = np.random.randn(25,2)
    >>> struct_ar = np.zeros((25,1),
    ...                      dtype=[('var1', 'f4'),('var2', 'f4'),
    ...                             ('instrument','f4'),('str_instr','a5')])
    >>> struct_ar['var1'] = num[:,0][:,None]
    >>> struct_ar['var2'] = num[:,1][:,None]
    >>> struct_ar['instrument'] = instr[:,None]
    >>> struct_ar['str_instr'] = string_var[:,None]
    >>> design = sm.tools.categorical(struct_ar, col='instrument', drop=True)

    Or

    >>> design2 = sm.tools.categorical(struct_ar, col='str_instr', drop=True)
    zcategorical has been removed)ÚNotImplementedError)ÚdataÚcolÚ	dictnamesÚdrops       r   Úcategoricalr+   G   s   € ô` Ð<Ó
=Ð=r   c                 óä  — t        | d«      rddlm}  || d||¬«      S t        j                  | «      }|j
                  }|dk(  r
|dd…df   }n|j
                  dkD  rt        d«      ‚t        j                  |d¬	«      dk(  }|t        j                  |d
k7  d¬	«      z  }|j                  «       rx|dk(  r|S |dk(  rl|dk(  rt        d«      ‚t        j                  |j                  d   «      }dj                  ||   D �cg c]  }t        |«      ‘Œ c}«      }	t        d|	› d�«      ‚t        j                  |j                  d   «      |g}|r|n|ddd…   }t        j                  |«      S c c}w )aq  
    Add a column of ones to an array.

    Parameters
    ----------
    data : array_like
        A column-ordered design matrix.
    prepend : bool
        If true, the constant is in the first column.  Else the constant is
        appended (last column).
    has_constant : str {'raise', 'add', 'skip'}
        Behavior if ``data`` already has a constant. The default will return
        data without adding another constant. If 'raise', will raise an
        error if any column has a constant value. Using 'add' will add a
        column of 1s if a constant column is present.

    Returns
    -------
    array_like
        The original values with a constant (column of ones) as the first or
        last column. Returned value type depends on input type.

    Notes
    -----
    When the input is a pandas Series or DataFrame, the added column's name
    is 'const'.
    Nr   )Ú	add_trendÚc)ÚtrendÚprependÚhas_constantr   é   z)Only implemented for 2-dimensional arrays©r"   ç        ÚskipÚraisezdata is constant.ú,z
Column(s) z are constant.éÿÿÿÿ)r   Ústatsmodels.tsa.tsatoolsr-   r   r   r   Ú
ValueErrorÚptpÚallr   ÚarangeÚshapeÚjoinr   ÚonesÚcolumn_stack)
r'   r0   r1   r-   Úxr   Úis_nonzero_constÚcolumnsr.   Úcolss
             r   Úadd_constantrF   ›   sX  € ô8 ˜˜dÔ#Ý6Ù˜ S°'ÈÔUÐUô 	�
‰
�4Ó€AØ�6‰6€DØˆq‚yØŠa�ˆg‰J‰Ø	
�‰�!ŠÜÐDÓEÐEä—v‘v˜a aÔ(¨AÑ-ÐØœŸ™˜q C™x¨aÔ0Ñ0ÐØ×ÑÔØ˜6Ò!ØˆHØ˜WÒ$Ø�qŠyÜ Ð!4Ó5Ð5äŸ)™) A§G¡G¨A¡JÓ/�Ø—x‘x°Ð9IÑ1JÖ K¨A¤ Q¥Ò KÓL�Ü  :¨d¨V°>Ð!BÓCÐCä	�‰�—‘˜‘Ó	˜aÐ €AÙ‰˜!™D˜b˜D™'€AÜ�?‰?˜1ÓÐùò !Ls   Ä E-c                 ó’  — t        | dd¬«      } t        |dd¬«      }| j                  dk(  r	| ddd…f   n| } | j                  d   |j                  d   k7  rt        d|j                  d   z  «      ‚t	        j
                  | |g«      }t        j                  j                  |«      t        j                  j                  |«      k7  ry	y
)a6  
    True if (Q, P) contrast `c` is estimable for (N, P) design `d`.

    From an Q x P contrast matrix `C` and an N x P design matrix `D`, checks if
    the contrast `C` is estimable by looking at the rank of ``vstack([C,D])``
    and verifying it is the same as the rank of `D`.

    Parameters
    ----------
    c : array_like
        A contrast matrix with shape (Q, P). If 1 dimensional assume shape is
        (1, P).
    d : array_like
        The design matrix, (N, P).

    Returns
    -------
    bool
        True if the contrast `c` is estimable on design `d`.

    Examples
    --------
    >>> d = np.array([[1, 1, 1, 0, 0, 0],
    ...               [0, 0, 0, 1, 1, 1],
    ...               [1, 1, 1, 1, 1, 1]]).T
    >>> isestimable([1, 0, 0], d)
    False
    >>> isestimable([1, -1, 0], d)
    True
    r.   r2   )ÚmaxdimÚd©r   r   NzContrast should have %d columnsFT)r   r   r>   r:   r   ÚvstackÚlinalgÚmatrix_rank)r.   rI   Únews      r   ÚisestimablerO   Õ   s¨   € ô> 	�1�c !Ô$€AÜ�1�c Ô"€AØ—f‘f ’kˆˆ$’ˆ'Š
 q€AØ‡w�wˆq�z�Q—W‘W˜Q‘ZÒÜÐ:¸Q¿W¹WÀQ¹ZÑGÓHÐHÜ
�)‰)�Q˜�FÓ
€CÜ	‡y�y×Ñ˜SÓ!¤R§Y¡Y×%:Ñ%:¸1Ó%=Ò=ØØr   c           	      óv  — t        j                  | «      } | j                  «       } t         j                  j	                  | d«      \  }}}t        j
                  |«      }|j                  d   }|j                  d   }|t         j                  j                  |«      z  }t        t        ||«      «      D ]  }	||	   |kD  rd||	   z  ||	<   Œd||	<   Œ t        j                  t        j                  |«      t        j                  |dd…t         j                  f   t        j                  |«      «      «      }
|
|fS )z}
    Return the pinv of an array X as well as the singular values
    used in computation.

    Code adapted from numpy.
    Fr   r   ç      ð?r4   N)r   r   Ú	conjugaterL   ÚsvdÚcopyr>   ÚmaximumÚreduceÚrangeÚminÚdotÚ	transposeÚmultiplyÚnewaxis)rB   ÚrcondÚur   ÚvtÚs_origÚmÚnÚcutoffr   Úress              r   Úpinv_extendedre   ÿ   s  € ô 	�
‰
�1‹€AØ	�‰‹€AÜ�y‰y�}‰}˜Q Ó&�H€A€qˆ"Ü�W‰W�Q‹Z€FØ	�‰�‰
€AØ
�‰�‰€AØ”R—Z‘Z×&Ñ& qÓ)Ñ)€FÜ”3�q˜!“9Óò ˆØˆQ‰4�&Š=Ø�a˜‘d‘7ˆAˆaŠDàˆAˆaŠDð	ô
 �&‰&”—‘˜bÓ!¤2§;¡;¨q²´B·J±J°Ñ/?Ü/1¯|©|¸A«ó$@ó A€Cà�ˆ;Ðr   c                 ól  — t        j                  | «      } t        j                  | t         j                  ¬«      }t        j                  | j
                  «      }| }||   | j
                  |   dkD  z  ||<   d| j
                  |   z  |j
                  |<   t         j                  |j
                  |<   |S )zì
    Reciprocal of an array with entries less than or equal to 0 set to 0.

    Parameters
    ----------
    x : array_like
        The input array.

    Returns
    -------
    ndarray
        The array with 0-filled reciprocals.
    ©Údtyper   rQ   ©r   r   Ú
zeros_likeÚfloat64r   ÚflatÚnan)rB   ÚoutÚnansÚposs       r   Úreciprrq     sŒ   € ô 	�
‰
�1‹€AÜ
�-‰-˜¤§¡Ô
,€CÜ�8‰8�A—F‘FÓ€DØˆ%€CØ�3‰x˜1Ÿ6™6 #™;¨™?Ñ+€Cˆ�HØ˜!Ÿ&™& ™+Ñ%€C‡H�HˆS�MÜ—V‘V€C‡H�HˆT�NØ€Jr   c                 ól  — t        j                  | «      } t        j                  | t         j                  ¬«      }t        j                  | j
                  «      }| }||   | j
                  |   dk7  z  ||<   d| j
                  |   z  |j
                  |<   t         j                  |j
                  |<   |S )zà
    Reciprocal of an array with entries less than 0 set to 0.

    Parameters
    ----------
    x : array_like
        The input array.

    Returns
    -------
    ndarray
        The array with 0-filled reciprocals.
    rg   r   rQ   ri   )rB   rn   ro   Únon_zeros       r   Úrecipr0rt   /  s’   € ô 	�
‰
�1‹€AÜ
�-‰-˜¤§¡Ô
,€CÜ�8‰8�A—F‘FÓ€DØˆu€HØ! (Ñ+¨q¯v©v°hÑ/?À1Ñ/DÑE€HˆXÑØ˜qŸv™v hÑ/Ñ/€C‡H�HˆXÑÜ—V‘V€C‡H�HˆT�NØ€Jr   c                 óô   — t         j                  j                  | dz  d«      }t        j                  |«      D �cg c]  }| dd…|f   ‘Œ }}t        j                  t        j
                  |«      «      S c c}w )zÖ
    Erase columns of zeros: can save some time in pseudoinverse.

    Parameters
    ----------
    matrix : ndarray
        The array to clean.

    Returns
    -------
    ndarray
        The cleaned array.
    r2   r   N)r   ÚaddrV   Úflatnonzeror   rZ   )ÚmatrixÚcolsumr   Úvals       r   Úclean0r{   G  s]   € ô �V‰V�]‰]˜6 1™9 aÓ(€FÜ!#§¡°Ó!7Ö
8˜Aˆ6’!�Q�$‹<Ð
8€CÐ
8Ü�8‰8”B—L‘L Ó%Ó&Ð&ùò 9s   »A5c                 ó¬  — |€t         j                  j                  | «      }t         j                  j                  | d¬«      \  }}}t        j                  |«      }|ddd…   }g }t        |«      D ]  }|j                  |dd…||   f   «       Œ t        j                  t        j                  |«      «      j                  t         j                  «      S )aÖ  
    Return an array whose column span is the same as x.

    Parameters
    ----------
    x : ndarray
        The array to adjust, 2d.
    r : int, optional
        The rank of x. If not provided, determined by `np.linalg.matrix_rank`.

    Returns
    -------
    ndarray
        The array adjusted to have full rank.

    Notes
    -----
    If the rank of x is known it can be specified as r -- no check
    is made to ensure that this really is the rank of x.
    NF)Úfull_matricesr8   )r   rL   rM   rS   ÚargsortrW   Úappendr   rZ   Úastyperk   )rB   ÚrÚvrI   r^   ÚorderÚvaluer   s           r   Úfullrankr…   Z  sª   € ð* 	€yÜ�I‰I×!Ñ! !Ó$ˆä�i‰i�m‰m˜A¨UˆmÓ3�G€A€qˆ!Ü�J‰J�q‹M€EØ‘$�B�$‰K€EØ€EÜ�1‹Xò %ˆØ�‰�Q’q˜% ™(�{‘^Õ$ð%ä�:‰:”b—l‘l 5Ó)Ó*×1Ñ1´"·*±*Ó=Ð=r   c                 óD   — t        |«      }d||<   | j                  |«      S )aW  
    Unsqueeze a collapsed array.

    Parameters
    ----------
    data : ndarray
        The data to unsqueeze.
    axis : int
        The axis to unsqueeze.
    oldshape : tuple[int]
        The original shape before the squeeze or reduce operation.

    Returns
    -------
    ndarray
        The unsqueezed array.

    Examples
    --------
    >>> from numpy import mean
    >>> from numpy.random import standard_normal
    >>> x = standard_normal((3,4,5))
    >>> m = mean(x, axis=1)
    >>> m.shape
    (3, 5)
    >>> m = unsqueeze(m, 1, x.shape)
    >>> m.shape
    (3, 1, 5)
    >>>
    r   )ÚlistÚreshape)r'   r"   ÚoldshapeÚnewshapes       r   Ú	unsqueezer‹   {  s%   € ô> �H‹~€HØ€HˆT�NØ�<‰<˜Ó!Ð!r   c                 ó^  — t        j                  t        j                  | «      |dk7  «      }t        j                  | dk7  t        j                  |«      «      }||z   }t        j                  t        j                  | «      t        j                  |«      «      }t         j                  ||<   |S )z«
    Returns np.dot(left_matrix, right_matrix) with the convention that
    nan * 0 = 0 and nan * x = nan if x != 0.

    Parameters
    ----------
    A, B : ndarray
    r   )r   rY   r   Ú
nan_to_numrm   )ÚAÚBÚshould_be_nan_1Úshould_be_nan_2Úshould_be_nanÚCs         r   Únan_dotr”   Ÿ  s}   € ô —f‘fœRŸX™X a›[¨1°©6Ó3€OÜ—f‘f˜a 1™f¤r§x¡x°£{Ó3€OØ# oÑ5€Mô 	�‰Œr�}‰}˜QÓ¤§¡¨qÓ!1Ó2€Aä—v‘v€A€mÑà€Hr   c                 ó   — t        | d| «      S )zˆ
    Gets raw results back from wrapped results.

    Can be used in plotting functions or other post-estimation type
    routines.
    Ú_results)Úgetattr)Úresultss    r   Úmaybe_unwrap_resultsr™   ¶  s   € ô �7˜J¨Ó0Ð0r   c                   ó"   ‡ — e Zd ZdZˆ fd„Zˆ xZS )ÚBuncha  
    Returns a dict-like object with keys accessible via attribute lookup.

    Parameters
    ----------
    *args
        Arguments passed to dict constructor, tuples (key, value).
    **kwargs
        Keyword agument passed to dict constructor, key=value.
    c                 ó2   •— t        ‰| �  |i |¤Ž | | _        y ©N)ÚsuperÚ__init__Ú__dict__)ÚselfÚargsÚkwargsÚ	__class__s      €r   rŸ   zBunch.__init__Ë  s   ø€ Ü‰Ñ˜$Ð) &Ò)Øˆ�r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__rŸ   Ú__classcell__)r¤   s   @r   r›   r›   À  s   ø„ ñ	÷ð r   r›   c                 ó*  — | €| S t        | d«      }| j                  dk(  r|r| | j                  fS | dfS | j                  dkD  rt        d«      ‚|r| j                  nd}|rt        j                  | «      dd…df   |fS t        j                  | «      |fS )aå  

    Parameters
    ----------
    x : ndarray, Series, DataFrame or None
        Input to verify dimensions, and to transform as necesary
    ndarray : bool
        Flag indicating whether to always return a NumPy array. Setting False
        will return an pandas DataFrame when the input is a Series or a
        DataFrame.

    Returns
    -------
    out : ndarray, DataFrame or None
        array or DataFrame with 2 dimensiona.  One dimensional arrays are
        returned as nobs by 1. None is returned if x is None.
    names : list of str or None
        list containing variables names when the input is a pandas datatype.
        Returns None if the input is an ndarray.

    Notes
    -----
    Accepts None for simplicity
    Nr2   zx mst be 1 or 2-dimensional.)	r   r   rD   r:   Únamer   r   ÚpdÚ	DataFrame)rB   ÚndarrayÚ	is_pandasr«   s       r   Ú
_ensure_2dr°   Ð  s•   € ð2 	€yØˆÜ   DÓ)€IØ‡v�v�‚{ÙØ�a—i‘i�<Ðà�d�7ˆNØ	
�‰�!ŠÜÐ7Ó8Ð8áˆ1�6Š6 D€DÙÜ�z‰z˜!‹}šQ ˜WÑ% tÐ+Ð+ä�|‰|˜A‹ Ð$Ð$r   c                 ó°  — t        | dd¬«      } |dk(  r|| dd…t        j                  | dk7  d¬«      f   } | t        j                  | dz  j	                  d«      «      z  } | j
                  | z  } t        j                  j                  | |d¬	«      S |d
k(  r¢t        j                  j                  | d¬«      \  }t        j                  t        j                  |«      «      }|€9|d   | j                  d   z  t        j                  t        «      j                  z  }t!        ||kD  j	                  «       «      S t        j                  j                  | |¬«      S )a(  
    Matrix rank calculation using QR or SVD

    Parameters
    ----------
    m : array_like
        A 2-d array-like object to test
    tol : float, optional
        The tolerance to use when testing the matrix rank. If not provided
        an appropriate value is selected.
    method : {"ip", "qr", "svd"}
        The method used. "ip" uses the inner-product of a normalized version
        of m and then computes the rank using NumPy's matrix_rank.
        "qr" uses a QR decomposition and is the default. "svd" defers to
        NumPy's matrix_rank.

    Returns
    -------
    int
        The rank of m.

    Notes
    -----
    When using a QR factorization, the rank is determined by the number of
    elements on the leading diagonal of the R matrix that are above tol
    in absolute value.
    ra   r2   rJ   ÚipNr   r3   T)ÚtolÚ	hermitianÚqrr�   )Úmoder   )r³   )r   r   r   ÚsqrtÚsumÚTrL   rM   Úscipyrµ   ÚabsÚdiagr>   ÚfinfoÚfloatÚepsÚint)ra   r³   Úmethodr�   Úabs_diags        r   rM   rM   û  s  € ô8 	�1�c Ô"€AØ�‚~ØŠa”—‘˜˜Q™ QÔ'Ð'Ñ(ˆØ”—‘˜˜a™Ÿ™ Q›Ó(Ñ(ˆØ�C‰C�!‰GˆÜ�y‰y×$Ñ$ Q¨C¸4Ð$Ó@Ð@Ø	�4ŠÜ�\‰\�_‰_˜Q Sˆ_Ó)‰ˆÜ—6‘6œ"Ÿ'™' !›*Ó%ˆØˆ;Ø˜1‘+ §¡¨¡
Ñ*¬R¯X©X´e«_×-@Ñ-@Ñ@ˆCÜ�H˜s‘N×'Ñ'Ó)Ó*Ð*ä�y‰y×$Ñ$ Q¨CÐ$Ó0Ð0r   )r   )Nr   )NFF)Tr5   )gVçž¯Ò<r�   )F)Nrµ   )r¨   Únumpyr   Úpandasr¬   Úscipy.linalgrº   Ústatsmodels.tools.datar   Ústatsmodels.tools.validationr   r   r   r$   r+   rF   rO   re   rq   rt   r{   r…   r‹   r”   r™   Údictr›   r°   rM   © r   r   ú<module>rÊ      s   ðñó Û Û å 3Ý 3òóó óNP>óh7òt'óTò0ò0ò0'ó&>òB!"òHò.1ôˆDô ó (%ôV)1r   