Ë
    ¢�Dj÷4  ã                   óP   — d Z ddlZ G d„ d«      Z G d„ d«      Zd„ Z	 d
d„Zdd	„Zy)zM
Created on Thu May 15 16:36:05 2014

Author: Josef Perktold
License: BSD-3

é    Nc                   ó8   — e Zd ZdZd„ Zd„ Zd„ Zd„ Zed„ «       Z	y)ÚLinearConstraintsa»  Class to hold linear constraints information

    Affine constraints are defined as ``R b = q` where `R` is the constraints
    matrix and `q` are the constraints values and `b` are the parameters.

    This is in analogy to patsy's LinearConstraints class but can be pickled.

    Parameters
    ----------
    constraint_matrix : ndarray
        R matrix, 2-dim with number of columns equal to the number of
        parameters. Each row defines one constraint.
    constraint_values : ndarray
        1-dim array of constant values
    variable_names : list of strings
        parameter names, used only for display
    kwds : keyword arguments
        keywords are attached to the instance.

    c                 óº   — || _         || _        || _        || _        || _        | j
                  j                  |«       | j                   | j                  f| _        y ©N)Úconstraint_matrixÚconstraint_valuesÚvariable_namesÚcoefsÚ	constantsÚ__dict__ÚupdateÚtuple)Úselfr   r   r	   Úkwdss        úaC:\Crop_Prediction\Backend\crop-ai-system\venv\Lib\site-packages\statsmodels/base/_constraints.pyÚ__init__zLinearConstraints.__init__"   sV   € ð "3ˆÔØ!2ˆÔØ,ˆÔð 'ˆŒ
Ø*ˆŒà�‰×Ñ˜TÔ"Ø×,Ñ,¨d×.DÑ.DÐEˆ�
ó    c              #   ó8   K  — | j                   E d {  –—†  y 7 Œ­wr   ©r   )r   s    r   Ú__iter__zLinearConstraints.__iter__0   s   è ø€ Ø—:‘:×Òús   ‚’“c                 ó    — | j                   |   S r   r   )r   Úidxs     r   Ú__getitem__zLinearConstraints.__getitem__3   s   € Ø�z‰z˜#‰Ðr   c           	      ó´  — d„ }g }t        | Ž D ]¸  \  }}g }t        || j                  «      D ]Y  \  }}|dk7  r|g k(  r| |||«      z  }Œ|dkD  r|d |||«      z   z  }Œ2|dk  sŒ8|d |t        j                  |«      |«      z   z  }Œ[ |dt	        |j                  «       «      z   z  }|j                  dj                  |«      «       Œº dj                  |«      S )Nc                 ód   — t        j                  | «      } | dk7  rt        | «      dz   |z   }|S |}|S )Né   z * )ÚnpÚabsÚstr)ÚvÚnameÚsss      r   Úprod_stringz.LinearConstraints.__str__.<locals>.prod_string7   s;   € Ü—‘�q“	ˆAØ�AŠvÜ˜“V˜e‘^ dÑ*�ð ˆIð �ØˆIr   r   z + z - z = Ú ú
)Úzipr	   r   r   r   ÚitemÚappendÚjoin)r   r#   Úconstraints_stringsÚrÚqr"   r    r!   s           r   Ú__str__zLinearConstraints.__str__6   sî   € ò	ð !ÐÜ˜�Jò 
	4‰DˆAˆqØˆBÜ˜q $×"5Ñ"5Ó6ò ?‘��4Ø˜’6˜b BšhØ™+ a¨Ó.Ñ.‘BØ˜’UØ˜%¡+¨a°Ó"6Ñ6Ñ6‘BØ˜“UØ˜%¡+¬b¯f©f°Q«i¸Ó">Ñ>Ñ>‘Bð?ð �%œ#˜aŸf™f›h›-Ñ'Ñ'ˆBØ×&Ñ& r§w¡w¨r£{Õ3ð
	4ð �y‰yÐ,Ó-Ð-r   c                 óR   —  | |j                   |j                  |j                  «      S )aL  class method to create instance from patsy instance

        Parameters
        ----------
        lc : instance
            instance of patsy LinearConstraint, or other instances that have
            attributes ``lc.coefs, lc.constants, lc.variable_names``

        Returns
        -------
        instance of this class

        )r
   r   r	   )ÚclsÚlcs     r   Ú
from_patsyzLinearConstraints.from_patsyN   s!   € ñ �2—8‘8˜RŸ\™\¨2×+<Ñ+<Ó=Ð=r   N)
Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r-   Úclassmethodr1   © r   r   r   r      s0   „ ñò*Fòòò.ð0 ñ>ó ñ>r   r   c                   ó$   — e Zd ZdZdd„Zd„ Zd„ Zy)ÚTransformRestrictiona�  Transformation for linear constraints `R params = q`

    Note, the transformation from the reduced to the full parameters is an
    affine and not a linear transformation if q is not zero.


    Parameters
    ----------
    R : array_like
        Linear restriction matrix
    q : arraylike or None
        values of the linear restrictions


    Notes
    -----
    The reduced parameters are not sorted with respect to constraints.

    TODO: error checking, eg. inconsistent constraints, how?

    Inconsistent constraints will raise an exception in the calculation of
    the constant or offset. However, homogeneous constraints, where q=0, will
    can have a solution where the relevant parameters are constraint to be
    zero, as in the following example::

        b1 + b2 = 0 and b1 + 2*b2 = 0, implies that b2 = 0.

    The transformation applied from full to reduced parameter space does not
    raise and exception if the constraint does not hold.
    TODO: maybe change this, what's the behavior in this case?


    The `reduce` transform is applied to the array of explanatory variables,
    `exog`, when transforming a linear model to impose the constraints.
    Nc                 ón  — t        j                  |«      x}| _        |�t        j                  |«      x}| _        |j
                  \  }}||c| _        | _        ||z
  | _        t        j                  |«      |j                  j                  t         j                  j                  |«      j                  «      z
  }t         j                  j                  |«      \  }}|| _        || _        |d d …d |…f   x}| _        |d d …|d …f   | _        |�m	 |j                  j                  t         j                  j'                  |j                  j                  |j                  «      |j                  «      «      | _        y d| _        y # t         j                  j*                  $ r}	t-        d|	›�«      ‚d }	~	ww xY w)Nz6possibly inconsistent constraints. error generated by
r   )r   Ú
atleast_2dÚRÚasarrayr,   ÚshapeÚk_constrÚk_varsÚ
k_unconstrÚeyeÚTÚdotÚlinalgÚpinvÚeighÚevalsÚevecsÚLÚ
transf_matÚsolveÚconstantÚLinAlgErrorÚ
ValueError)
r   r<   r,   r?   r@   ÚmrH   rI   rJ   Úes
             r   r   zTransformRestriction.__init__…   sZ  € ô —]‘] 1Ó%Ð%ˆˆDŒFØˆ=ÜŸ™ A›Ð&ˆA�”àŸ7™7Ñˆ�&Ø%-¨vÐ"ˆŒ�t”{Ø  8Ñ+ˆŒä�F‰F�6‹N˜QŸS™SŸW™W¤R§Y¡Y§^¡^°AÓ%6×%8Ñ%8Ó9Ñ9ˆÜ—y‘y—~‘~ aÓ(‰ˆˆuð ˆŒ
ØˆŒ
Øš1˜i˜x˜i˜<Ñ(Ð(ˆˆDŒFØ¢ 8¡9 Ñ-ˆŒàˆ=ð=Ø !§¡§¡¬¯	©	¯©¸¿¹¿¹ÀÇÁ»ÀaÇcÁcÓ(JÓ K�•ð
 ˆD�Møô	 —9‘9×(Ñ(ò =Ý Ù78ð"<ó =ð =ûð=ús   ÄA+F ÆF4Æ!F/Æ/F4c                 ó¤   — t        j                  |«      }| j                  j                  |j                  «      j                  | j
                  z   S )a·  transform from the reduced to the full parameter space

        Parameters
        ----------
        params_reduced : array_like
            parameters in the transformed space

        Returns
        -------
        params : array_like
            parameters in the original space

        Notes
        -----
        If the restriction is not homogeneous, i.e. q is not equal to zero,
        then this is an affine transform.
        )r   r=   rK   rD   rC   rM   )r   Úparams_reduceds     r   ÚexpandzTransformRestriction.expandª   s<   € ô$ Ÿ™ NÓ3ˆØ�‰×"Ñ" >×#3Ñ#3Ó4×6Ñ6¸¿¹ÑFÐFr   c                 ób   — t        j                  |«      }|j                  | j                  «      S )a¾  transform from the full to the reduced parameter space

        Parameters
        ----------
        params : array_like
            parameters or data in the original space

        Returns
        -------
        params_reduced : array_like
            parameters in the transformed space

        This transform can be applied to the original parameters as well
        as to the data. If params is 2-d, then each row is transformed.
        )r   r=   rD   rK   )r   Úparamss     r   ÚreducezTransformRestriction.reduce¿   s%   € ô  —‘˜FÓ#ˆØ�z‰z˜$Ÿ/™/Ó*Ð*r   r   )r2   r3   r4   r5   r   rT   rW   r7   r   r   r9   r9   `   s   „ ñ"óH#òJGó*+r   r9   c                 ó  — |j                  |«      j                  |j                  «      }|j                  |j                  «      j                  t        j                  j	                  ||j                  | «      |z
  «      «      }| |z
  S )a¢  find the parameters that statisfy linear constraint from unconstrained

    The linear constraint R params = q is imposed.

    Parameters
    ----------
    params : array_like
        unconstrained parameters
    Sinv : ndarray, 2d, symmetric
        covariance matrix of the parameter estimate
    R : ndarray, 2d
        constraint matrix
    q : ndarray, 1d
        values of the constraint

    Returns
    -------
    params_constraint : ndarray
        parameters of the same length as params satisfying the constraint

    Notes
    -----
    This is the exact formula for OLS and other linear models. It will be
    a local approximation for nonlinear models.

    TODO: Is Sinv always the covariance matrix?
    In the linear case it can be (X'X)^{-1} or sigmahat^2 (X'X)^{-1}.

    My guess is that this is the point in the subspace that satisfies
    the constraint that has minimum Mahalanobis distance. Proof ?
    )rD   rC   r   rE   rL   )rV   ÚSinvr<   r,   ÚrsrÚ	reductions         r   Útransform_params_constraintr\   Ó   sb   € ðB �%‰%�‹+�/‰/˜!Ÿ#™#Ó
€Cà—‘˜Ÿ™“×!Ñ!¤"§)¡)§/¡/°#°q·u±u¸V³}ÀqÑ7HÓ"IÓJ€IØ�IÑÐr   c                 óÊ  — | }|€i }||}}|j                   |j                  }	}t        ||«      }
|
j                  |	«      }|	j	                  |
j
                  j                  «       «      }t        |d«      r||j                  z  }|�|
j                  |«      }ddl	}|j                  |j                  «       «      }d|v r|d=  |j                  ||fd|i|¤Ž} |j                  dd|i|¤Ž}|
j                  |j                  «      j                  «       }|
j                  j	                  |j!                  «       «      j	                  |
j                  j"                  «      }|||fS )a4  fit model subject to linear equality constraints

    The constraints are of the form   `R params = q`
    where R is the constraint_matrix and q is the vector of constraint_values.

    The estimation creates a new model with transformed design matrix,
    exog, and converts the results back to the original parameterization.


    Parameters
    ----------
    model: model instance
        An instance of a model, see limitations in Notes section
    constraint_matrix : array_like, 2D
        This is R in the linear equality constraint `R params = q`.
        The number of columns needs to be the same as the number of columns
        in exog.
    constraint_values :
        This is `q` in the linear equality constraint `R params = q`
        If it is a tuple, then the constraint needs to be given by two
        arrays (constraint_matrix, constraint_value), i.e. (R, q).
        Otherwise, the constraints can be given as strings or list of
        strings.
        see t_test for details
    start_params : None or array_like
        starting values for the optimization. `start_params` needs to be
        given in the original parameter space and are internally
        transformed.
    **fit_kwds : keyword arguments
        fit_kwds are used in the optimization of the transformed model.

    Returns
    -------
    params : ndarray ?
        estimated parameters (in the original parameterization
    cov_params : ndarray
        covariance matrix of the parameter estimates. This is a reverse
        transformation of the covariance matrix of the transformed model given
        by `cov_params()`
        Note: `fit_kwds` can affect the choice of covariance, e.g. by
        specifying `cov_type`, which will be reflected in the returned
        covariance.
    res_constr : results instance
        This is the results instance for the created transformed model.


    Notes
    -----
    Limitations:

    Models where the number of parameters is different from the number of
    columns of exog are not yet supported.

    Requires a model that implement an offset option.
    NÚoffsetr   Ústart_paramsr7   )ÚendogÚexogr9   rW   rD   rM   ÚsqueezeÚhasattrr^   ÚcopyÚ_get_init_kwdsÚ	__class__ÚfitrT   rV   rK   Ú
cov_paramsrC   )Úmodelr   r   r_   Úfit_kwdsr   r<   r,   r`   ra   ÚtransfÚexogp_str^   rd   Ú	init_kwdsÚ
mod_constrÚ
res_constrÚparams_origrh   s                      r   Úfit_constrainedrq   ú   sU  € ðt €DØÐØˆàÐ/€q€AØ—*‘*˜dŸi™iˆ4€Eä! ! QÓ'€Fà�}‰}˜TÓ"€Hà�X‰X�f—o‘o×-Ñ-Ó/Ó0€FÜˆt�XÔØ�$—+‘+ÑˆàÐØŸ™ lÓ3ˆó Ø—	‘	˜$×-Ñ-Ó/Ó0€Ið �9ÑØ�hÐð  �—‘  xÑL¸ÐLÀ)ÑL€JØ�—‘ÑF¨\ÐF¸XÑF€JØ—-‘- 
× 1Ñ 1Ó2×:Ñ:Ó<€KØ×"Ñ"×&Ñ& z×'<Ñ'<Ó'>Ó?×CÑCÀF×DUÑDU×DWÑDWÓX€Jà˜
 JÐ.Ð.r   c                 óÈ  — | }ddl m}  ||j                  «      j                  |«      }|j                  |j
                  }}t        |||||¬«      \  }	}
}|j                  |	dd¬«      }|	|j                  _	        |
|j                  _
        |j                  dd«      }|dk(  r|
|j                  z  |j                  _        nd|j                  _        t        |«      }|j                  xj                  |z  c_        |j                  xj                   |z  c_        t"        j%                  |«      |j                  _        ||j                  _        ||j                  _        |S )	aÃ  fit_constraint that returns a results instance

    This is a development version for fit_constrained methods or
    fit_constrained as standalone function.

    It will not work correctly for all models because creating a new
    results instance is not standardized for use outside the `fit` methods,
    and might need adjustements for this.

    This is the prototype for the fit_constrained method that has been added
    to Poisson and GLM.
    r   )Ú
DesignInfo)r_   rj   F)r_   ÚmaxiterÚwarn_convergenceÚcov_typeÚ	nonrobustN)Úpatsyrs   Ú
exog_namesÚlinear_constraintr
   r   rq   rg   Ú_resultsrV   Úcov_params_defaultÚgetÚscaleÚnormalized_cov_paramsÚlenÚdf_residÚdf_modelr   r1   Úconstraintsr?   Úresults_constrained)ri   rƒ   r_   rj   r   rs   r0   r<   r,   rV   Úcovro   Úresrv   r?   s                  r   Úfit_constrained_wrapr‡   W  s0  € ð €Dõ !ñ
 
�D—O‘OÓ	$×	6Ñ	6°{Ó	C€BØ�8‰8�R—\‘\€q€Aô .¨d°A°qØ;GØ7?ôAÑ€FˆC�ð �(‰( °Ø$)ð ó +€Cà €C‡L�LÔØ&)€C‡L�LÔ#Ø�|‰|˜J¨Ó4€HØ�;ÒØ-0°:×3CÑ3CÑ-Cˆ�‰Õ*à-1ˆ�‰Ô*ä�1‹v€HØ‡L�L×Ò˜XÑ%ÕØ‡L�L×Ò˜XÑ%ÕÜ0×;Ñ;¸BÓ?€C‡L�LÔØ$€C‡L�LÔØ'1€C‡L�LÔ$Ø€Jr   )NNr   )r5   Únumpyr   r   r9   r\   rq   r‡   r7   r   r   ú<module>r‰      s@   ðñó ÷Q>ñ Q>÷hp+ñ p+òf$ðP 15óZ/ôz2r   