
    M/Pho                         d Z ddlZddlZddlZddlmc mZ ddl	m
Z
 ddlZddlmZ ddlmZ ddlmZ ddlZ G d d          Z G d	 d
e          Zd Z G d dej                  Z G d dej                  ZdS )ah  
This module implements maximum likelihood-based estimation (MLE) of
Gaussian regression models for finite-dimensional observations made on
infinite-dimensional processes.

The ProcessMLE class supports regression analyses on grouped data,
where the observations within a group are dependent (they are made on
the same underlying process).  One use-case is repeated measures
regression for temporal (longitudinal) data, in which the repeated
measures occur at arbitrary real-valued time points.

The mean structure is specified as a linear model.  The covariance
parameters depend on covariates via a link function.
    N)OLS)minimize)summary2)approx_fprimec                       e Zd ZdZd Zd ZdS )ProcessCovariancea  
    A covariance model for a process indexed by a real parameter.

    An implementation of this class is based on a positive definite
    correlation function h that maps real numbers to the interval [0,
    1], such as the Gaussian (squared exponential) correlation
    function :math:`\exp(-x^2)`.  It also depends on a positive
    scaling function `s` and a positive smoothness function `u`.
    c                     t           )a  
        Returns the covariance matrix for given time values.

        Parameters
        ----------
        time : array_like
            The time points for the observations.  If len(time) = p,
            a pxp covariance matrix is returned.
        sc : array_like
            The scaling parameters for the observations.
        sm : array_like
            The smoothness parameters for the observation.  See class
            docstring for details.
        NotImplementedErrorselftimescsms       i/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/statsmodels/regression/process_regression.pyget_covzProcessCovariance.get_cov'   
     "!    c                     t           )a  
        The Jacobian of the covariance with respect to the parameters.

        See get_cov for parameters.

        Returns
        -------
        jsc : list-like
            jsc[i] is the derivative of the covariance matrix
            with respect to the i^th scaling parameter.
        jsm : list-like
            jsm[i] is the derivative of the covariance matrix
            with respect to the i^th smoothness parameter.
        r
   r   s       r   jaczProcessCovariance.jac8   r   r   N__name__
__module____qualname____doc__r   r    r   r   r   r      s<         " " """ " " " "r   r   c                       e Zd ZdZd Zd ZdS )GaussianCovarianceu]  
    An implementation of ProcessCovariance using the Gaussian kernel.

    This class represents a parametric covariance model for a Gaussian
    process as described in the work of Paciorek et al. cited below.

    Following Paciorek et al [1]_, the covariance between observations with
    index `i` and `j` is given by:

    .. math::

      s[i] \cdot s[j] \cdot h(|time[i] - time[j]| / \sqrt{(u[i] + u[j]) /
      2}) \cdot \frac{u[i]^{1/4}u[j]^{1/4}}{\sqrt{(u[i] + u[j])/2}}

    The ProcessMLE class allows linear models with this covariance
    structure to be fit using maximum likelihood (ML). The mean and
    covariance parameters of the model are fit jointly.

    The mean, scaling, and smoothing parameters can be linked to
    covariates.  The mean parameters are linked linearly, and the
    scaling and smoothing parameters use an log link function to
    preserve positivity.

    The reference of Paciorek et al. below provides more details.
    Note that here we only implement the 1-dimensional version of
    their approach.

    References
    ----------
    .. [1] Paciorek, C. J. and Schervish, M. J. (2006). Spatial modeling using
        a new class of nonstationary covariance functions. Environmetrics,
        17:483–506.
        https://papers.nips.cc/paper/2350-nonstationary-covariance-functions-for-gaussian-process-regression.pdf
    c                 \   t           j                            ||          }t           j                            ||          dz  }||z  |z  }t          j        | dz            t          j        |          z  }|t          j        ||          dz  z  }|t          j        ||          z  }|S )N         ?)npsubtractouteraddexpsqrt)r   r   r   r   dadsqmatcms           r   r   zGaussianCovariance.get_covn   s    [tT**V\\"b!!A%Bw|VTEAI,
bhr2$$
bhr2	r   c                    t           j                            ||          }t           j                            ||          dz  }t          j        |          }||z  }||z  }t          |          }	t          j        | dz            }
t          j        ||          dz  }|
|z  |z  }| |dz  z  }t          j        |	|	f          }t          j        |	|	f          }t          j        ||          }g }t          |          D ]\  }}|dz  }||d d fxx         dz  cc<   |d d |fxx         dz  cc<   d|z  |z  }d|
z  |z  |z  }||z  |
|z  |z  z
  }|
|z  }d|dz  z  ||         dz  z  }|dz  }|||d d f<   ||d d |f<   d||         dz  z  |||f<   ||z  ||z  z   }||z  }|	                    |           g }t          dt          |                    D ]d}t          j        |	|	f          }||d d f         |z  ||d d f<   |d d |fxx         |d d |f         |z  z  cc<   |	                    |           e||fS )Nr    r!   r         ?g      g      ?)r"   r#   r$   r%   r'   lenr&   zeros	enumerateappendrange)r   r   r   r   r(   r)   sdsdaar*   peqmsm4cmxdq0difisccjsmi_dbottomdtopbcvjscs                             r   r   zGaussianCovariance.jacz   s   [tT**V\\"b!!A%gbkk2gRxIIfdUQYhr2$Ci#odRUlXq!fXq!fhr2 bMM 	 	DAq!GBq!!!tHHHOHHHqqq!tHHHOHHHBhnG#:#b(Ds
S7]R//Ac	Ar4x"Q%+-A!GBBq!!!tHBqqq!tHRUCZ'Bq!tHBS AHAJJqMMMM q#b''"" 	 	A!Q  A!QQQ$i"nAadGaaadGGGs111a4y2~%GGGJJqMMMMCxr   Nr   r   r   r   r   r   J   s=        ! !F
 
 
+ + + + +r   r   c                 L   t          |           |j        d         |j        d         |j        d         t          |          t          |          g}| |                    |j        d                    t          |          t	          |          k    rd}t          |          d S )Nr   z<The leading dimensions of all array arguments must be equal.)r.   shaper1   minmax
ValueError)	endogexog
exog_scaleexog_smooth
exog_noiser   groupsrD   msgs	            r   _check_argsrR      s     	E


1!D		F	A 	!!$%%%
1vvQ oo r   c                        e Zd ZdZ	 d fd	Zd Ze	 	 d fd	            Zd Zd Z	d Z
d	 Zd
 ZddZd ZddZ xZS )
ProcessMLEa  
    Fit a Gaussian mean/variance regression model.

    This class fits a one-dimensional Gaussian process model with
    parametrized mean and covariance structures to grouped data.  For
    each group, there is an independent realization of a latent
    Gaussian process indexed by an observed real-valued time
    variable..  The data consist of the Gaussian process observed at a
    finite number of `time` values.

    The process mean and variance can be lined to covariates.  The
    mean structure is linear in the covariates.  The covariance
    structure is non-stationary, and is defined parametrically through
    'scaling', and 'smoothing' parameters.  The covariance of the
    process between two observations in the same group is a function
    of the distance between the time values of the two observations.
    The scaling and smoothing parameters can be linked to covariates.

    The observed data are modeled as the sum of the Gaussian process
    realization and (optionally) independent white noise.  The standard
    deviation of the white noise can be linked to covariates.

    The data should be provided in 'long form', with a group label to
    indicate which observations belong to the same group.
    Observations in different groups are always independent.

    Parameters
    ----------
    endog : array_like
        The dependent variable.
    exog : array_like
        The design matrix for the mean structure
    exog_scale : array_like
        The design matrix for the scaling structure
    exog_smooth : array_like
        The design matrix for the smoothness structure
    exog_noise : array_like
        The design matrix for the additive white noise. The
        linear predictor is the log of the white noise standard
        deviation.  If None, there is no additive noise (the
        process is observed directly).
    time : array_like (1-dimensional)
        The univariate index values, used to calculate distances
        between observations in the same group, which determines
        their correlations.
    groups : array_like (1-dimensional)
        The group values.
    cov : a ProcessCovariance instance
        Defaults to GaussianCovariance.
    Nc	           	          t                      j        ||f|||||d|	 |d u| _        g }
t          |d          rt	          |j                  }
n$d t          |j        d                   D             }
t          |d          r|
t	          |j                  z  }
n'|
d t          |j        d                   D             z  }
t          |d          r|
t	          |j                  z  }
n'|
d t          |j        d                   D             z  }
| j        rOt          |d          r|
t	          |j                  z  }
n'|
d t          |j        d                   D             z  }
|
| j        _	        |t                      }|| _        t          |||||||           t          j        t                    }t          |          D ] \  }}||                             |           !|| _        d| _        | j        j        d         | _        | j        j        d         | _        | j        j        d         | _        | j        r| j        j        d         | _        d S d S )	N)rM   rN   rO   r   rP   columnsc                     g | ]}d |z  S )zMean%dr   .0js     r   
<listcomp>z'ProcessMLE.__init__.<locals>.<listcomp>  s    AAAqhlAAAr      c                     g | ]}d |z  S )zScale%dr   rX   s     r   r[   z'ProcessMLE.__init__.<locals>.<listcomp>  s    IIIy1}IIIr   c                     g | ]}d |z  S )zSmooth%dr   rX   s     r   r[   z'ProcessMLE.__init__.<locals>.<listcomp>  s    KKK!zA~KKKr   c                     g | ]}d |z  S )zNoise%dr   rX   s     r   r[   z'ProcessMLE.__init__.<locals>.<listcomp>  s    MMMQ9q=MMMr   F)super__init__
_has_noisehasattrlistrV   r2   rG   dataparam_namesr   covrR   collectionsdefaultdictr0   r1   
_groups_ixverboserL   k_exogrM   k_scalerN   k_smoothrO   k_noise)r   rK   rL   rM   rN   rO   r   rP   rg   kwargsxnames	groups_ixr>   g	__class__s                 r   ra   zProcessMLE.__init__   s    		 "#!	 	 	 	 	 %D0 4## 	B$,''FFAAE$*Q-,@,@AAAF:y)) 	Jd:-...FFIIeJ4DQ4G.H.HIIIIF;	** 	Ld;.///FFKKu[5Fq5I/J/JKKKKF? 	Nz9-- N$z1222 MM%
8H8K2L2LMMMM &	;$&&CE4[*&	" 	" 	"  +D11	f%% 	# 	#DAqaL""""# ioa(,Q/(.q1? 	4?03DLLL	4 	4r   c                 
   | j         j        }d}|||| j        z            }|| j        z  }|||| j        z            }|| j        z  }|||| j        z            }| j        r|| j        z  }|||| j        z            }ng }||||fS )Nr   )re   rf   rl   rm   rn   rb   ro   )r   rq   q
mean_namesscale_namessmooth_namesnoise_namess          r   _split_param_nameszProcessMLE._split_param_names8  s    &AamO,
	T[Qq~-.	T\a$-/0? 	A 1T\>!12KKK;kAAr   c           
      \   d|v r	|d         }nt          d          d|v r	|d         }nt          d          d|v r	|d         }	nd }	d|v r	|d         }
nt          d          d|v r	|d         }nt          d	          |t          j        d
           |t          j        d           t          |
t                    rt          j        ||
                   }
t          |t                    rt          j        ||                   }t          j        ||          }|j	        }|j
        }t          j        |          }t          j        ||          }|j	        }|j
        }t          j        |          }|	8t          j        |	|          }|j	        }|j
        }t          j        |          }nd d g d f\  }}}}t                                          ||d ||||
|          }||j        _        ||j        _        |j        r||j        _        |j        |z   |z   |z   |j        _        |S )Nscale_formulaz$scale_formula is a required argumentsmooth_formulaz%smooth_formula is a required argumentnoise_formular   ztime is a required argumentrP   zgroups is a required argumentz'subset' is ignoredz'drop_cols' is ignored)re   subsetrM   rN   rO   r   rP   )rJ   warningswarn
isinstancestrr"   asarraypatsydmatrixdesign_infocolumn_namesr`   from_formulare   scale_design_infosmooth_design_inforb   noise_design_info
exog_namesrf   )clsformulare   r   	drop_colsargsrp   r}   r~   r   r   rP   rM   r   rx   rN   r   ry   rO   r   rz   modrt   s                         r   r   zProcessMLE.from_formulaI  ss    f$$"?3MMCDDDv%%#$45NNDEEEf$$"?3MM MV&>DD:;;;vH%FF<===M/000 M2333dC   	*:d4j))Dfc"" 	.ZV--F]=$77
&2'4Z
++
mND99(4)6j--$}d;;J * 6+8KJ//JJ dB$ CJ);
 gg""!#! #   &7"&8#> 	;):CH& # < ,!-/:!; 
r   c                     | j         j        d         }|d|         }| j        j        d         }||||z            }| j        j        d         }|||z   ||z   |z            }|||z   |z   d         }||||fS )z@
        Split the packed parameter vector into blocks.
        r\   r   N)rL   rG   rM   rN   )	r   zpmmnparpvscparpssmparnopars	            r   unpackzProcessMLE.unpack  s     Y_Q!B$ _"1%"R"W* #A&"r'"r'B,&' "r'B,-- eUE))r   c                 :   t          | j        | j                  }|                                }| j        j        d         | j        j        d         z   }| j        r|| j        j        d         z  }t          j
        |j        t          j        |          f          S )Nr\   )r   rK   rL   fitrM   rG   rN   rb   rO   r"   concatenateparamsr/   )r   modelresultms       r   
_get_startzProcessMLE._get_start  s     DJ	**O!!$t'7'=a'@@? 	*&q))A~v}bhqkk:;;;r   c           
         |                      |          \  }}}}| j        t          j        | j        |          z
  }t          j        t          j        | j        |                    }t          j        t          j        | j        |                    }| j        r,t          j        t          j        | j	        |                    }	d}
| j
                                        D ]\  }}| j                            | j        |         ||         ||                   }| j        r/|j        dd|j        d         dz   xx         |	|         dz  z  cc<   ||         }|
dt          j                            |          d         z  z  }
|
dt          j        |t          j                            ||                    z  z  }
| j        rt+          d|
           |
S )a  
        Calculate the log-likelihood function for the model.

        Parameters
        ----------
        params : array_like
            The packed parameters for the model.

        Returns
        -------
        The log-likelihood value at the given parameter point.

        Notes
        -----
        The mean, scaling, and smoothing parameters are packed into
        a vector.  Use `unpack` to access the component vectors.
        g        Nr   r\   r    r-   zL=)r   rK   r"   dotrL   r&   rM   rN   rb   rO   rj   itemsrg   r   r   flatrG   linalgslogdetsolverk   print)r   r   r   r   r   r   residr   r   nollr?   ixr+   res                  r   loglikezProcessMLE.loglike  s   & &*[[%8%8"ueU 
RVDIu555 VBF4?E2233 VBF4+U3344 ? 	8t6677B _**,, 	< 	<EAr !!$)B-BB@@B  8))"(1+/)***bfai7***rB#	))"--a000B#r29??2r#:#:;;;;BB< 	$OOO	r   c                 
   |                      |          \  }}}}t          |          t          |          t          |          }}}| j        t          j        | j        |          z
  }	t          j        t          j        | j        |                    }
t          j        t          j        | j        |                    }| j	        r,t          j        t          j        | j
        |                    }t          j        t          |          t          |          z   t          |          z   t          |          z             }| j                                        D ]\  }}|
|         }||         }|	|         }| j        |         }| j        |ddf         }| j        |ddf         }| j        |ddf         }| j                            |||          }| j	        rH||         }| j
        |ddf         }|j        dd|j        d         dz   xx         ||         dz  z  cc<   t          j                            |          }| j                            |||          \  }}t          j                            ||          }|d|xx         t          j        |j        |          z  cc<   t          j        ||          }t          j                            ||          }dt          j                            ||j                  z  }|dddf         |z  } t3          |          D ]\  }!}t          j        ||!         |z            }"||||z   xx         |"| |!ddf         z  z  cc<   ||||z   xx         dt          j        ||!         |z            z  | |!ddf         z  z  cc<   |dddf         |z  }#t3          |          D ]\  }!}t          j        ||!         |z            }"|||z   ||z   |z   xx         |"|#|!ddf         z  z  cc<   |||z   ||z   |z   xx         dt          j        ||!         |z            z  |#|!ddf         z  z  cc<   | j	        r|dddf         dz  |z  }$|||z   |z   dxx         t          j        |j        dd|j        d         dz            |$          z  cc<   t          j        |t          j        ||                    }%|||z   |z   dxx         t          j        |%j        dd|%j        d         dz            |$          z  cc<   | j        r7t9          dt          j        t          j        ||z                                 |S )a  
        Calculate the score function for the model.

        Parameters
        ----------
        params : array_like
            The packed parameters for the model.

        Returns
        -------
        The score vector at the given parameter point.

        Notes
        -----
        The mean, scaling, and smoothing parameters are packed into
        a vector.  Use `unpack` to access the component vectors.
        Nr   r\   r    r-   z|G|=)r   r.   rK   r"   r   rL   r&   rM   rN   rb   rO   r/   rj   r   r   rg   r   r   rG   r   invr   r   Tr$   r0   sumrk   r   r'   )&r   r   r   r   r   r   r   r   r   r   r   r   r   scorer?   r   sc_ism_iresid_itime_iexog_iexog_scale_iexog_smooth_ir+   no_iexog_noise_icmijacvjacsdcrrxqmscxr>   jqsmxsnobms&                                         r   r   zProcessMLE.score  sw   & &*[[%8%8"ueUZZUSZZB 
RVDIu555 VBF4?E2233 VBF4+U3344 ? 	8t6677B Uc%jj03u::=E

JKK_**,, 2	P 2	PEArb6Db6DBiGYr]FYr111u%F?2qqq51L ,RU3M !!&$55B 8"v#r111u5))"(1+/)***bfai7***)--##CfdD99JD$ )//"g..C!B$KKK26&(C000KKK '7++BR((Bryr24000Bqqq$w-,.C!" M M1VDGbL))bbj!!!R#ad)^3!!!bbj!!!S26$q'C-+@+@%@3q!!!t9%LL!!!! qqq$w--/C!" B B1VDGbL))b2gb2gl*+++rC111I~=+++b2gb2gl*+++rvd1gm444s1aaa4y@B++++  P111d7mQ&5b2glmm$$$sx8I8I"(1+/8I/J/2)4 )4 4$$$VCC11b2glmm$$$rw7H7H!q7H/I3(O(OO$$$< 	:&"'"&"7"788999r   c                 0    t          || j                  }|S N)r   r   )r   r   hesss      r   hessianzProcessMLE.hessianW  s    VTZ00r   c           	          d|v r|d          _         i }d|v r|d         }|                                 }t          |t                    r|g}n|ddg}t	          |          D ]\  }}|dvr fd}nd}|5t          j        |          r||d<   n||t          |          z           |d<   t           fd	||||
          }	|	j	        ssd}
|4|
dt          j
        t          j        |	j        dz                      z  z  }
|t          |          dz
  k     r|
d||dz            z  z  }
t          j        |
           t          j        |	j                                                  r|	j        }                     |	j                  }	 t
          j                            |           }n# t*          $ r d}Y nw xY w G d d          } |            }|	j        |_        ||_        |	|_        d|_        t5           |          }|S )a  
        Fit a grouped Gaussian process regression using MLE.

        Parameters
        ----------
        start_params : array_like
            Optional starting values.
        method : str or array of str
            Method or sequence of methods for scipy optimize.
        maxiter : int
            The maximum number of iterations in the optimization.

        Returns
        -------
        An instance of ProcessMLEResults.
        rk   
minim_optsNpowellbfgs)r   c                 0                         |            S r   )r   xr   s    r   r   zProcessMLE.fit.<locals>.jac  s     JJqMM>)r   maxiterc                 0                         |            S r   )r   r   s    r   <lambda>z ProcessMLE.fit.<locals>.<lambda>  s    4<<??* r   )methodx0r   optionszFitting did not convergez, |gradient|=%.6fr    r\   z, trying %s next...c                       e Zd ZdS )ProcessMLE.fit.<locals>.rsltN)r   r   r   r   r   r   rsltr     s        Dr   r   )rk   r   r   r   r0   r"   isscalarr.   r   successr'   r   r   r   r   isfiniter   allr   r   r   	Exceptionr   normalized_cov_paramsoptim_retvalsscaleProcessMLEResults)r   start_paramsr   r   rp   r   rZ   methr   frQ   r   
cov_paramsr   rs   `              r   r   zProcessMLE.fit\  s   & !),DL
6!!-J??,,Lfc"" 	(XFF^'F (( 	# 	#GAt;&&* * * * * * ";w'' F,3Jy)),3AG4D,EJy)****"$ $ $A 9 #0?.q9I9I1J1JJJCs6{{Q&&06!A#;>>Cc"""{13##%% # s||AC  	)-----JJ 	 	 	JJJ		 	 	 	 	 	 	 	 DFF3", q))s    F/ /F>=F>c                    t          | j        d          s+t          j        ||          }t          j        ||          }nt	          j        | j        j        |          }t	          j        | j        j        |          }	t          j        t          j        ||                    }t          j        t          j        |	|                    }| j	        
                    |||          S )a+  
        Returns a Gaussian process covariance matrix.

        Parameters
        ----------
        time : array_like
            The time points at which the fitted covariance matrix is
            calculated.
        scale_params : array_like
            The regression parameters for the scaling part
            of the covariance structure.
        smooth_params : array_like
            The regression parameters for the smoothing part
            of the covariance structure.
        scale_data : DataFrame
            The data used to determine the scale parameter,
            must have len(time) rows.
        smooth_data : DataFrame
            The data used to determine the smoothness parameter,
            must have len(time) rows.

        Returns
        -------
        A covariance matrix.

        Notes
        -----
        If the model was fit using formulas, `scale` and `smooth` should
        be Dataframes, containing all variables that were present in the
        respective scaling and smoothing formulas used to fit the model.
        Otherwise, `scale` and `smooth` should contain data arrays whose
        columns align with the fitted scaling and smoothing parameters.

        The covariance is only for the Gaussian process and does not include
        the white noise variance.
        r   )rc   re   r"   r   r   r   r   r   r&   rg   r   )
r   r   scale_paramssmooth_params
scale_datasmooth_datascasmor   r   s
             r   
covariancezProcessMLE.covariance  s    N ty"566 	4&\22C&m44CCty:JGGBty;[IIB&L1122C&M2233Cxc3///r   c                    || j         }n4t          | j        d          rt          j        | j        j        |          }t          |          |j        d         k    r|d|j        d                  }t          j	        ||          S )ao  
        Obtain predictions of the mean structure.

        Parameters
        ----------
        params : array_like
            The model parameters, may be truncated to include only mean
            parameters.
        exog : array_like
            The design matrix for the mean structure.  If not provided,
            the model's design matrix is used.
        Nr   r\   r   )
rL   rc   re   r   r   r   r.   rG   r"   r   )r   r   rL   r   rp   s        r   predictzProcessMLE.predict  sy     <9DDTY.. 	>=!6==Dv;;A&&AdjmO,FvdF###r   r   )NN)NNN)r   r   r   r   ra   r{   classmethodr   r   r   r   r   r   r   r   r   __classcell__rt   s   @r   rT   rT      s,       1 1v E4 E4 E4 E4 E4 E4NB B B"  !#	S S S S S [Sj* * *.< < <4 4 4l\ \ \|  
S S S Sj00 00 00d$ $ $ $ $ $ $ $r   rT   c                   >     e Zd ZdZ fdZd
dZd Zd Zdd	Z xZ	S )r   z?
    Results class for Gaussian process regression models.
    c                 J   t                                          ||           |                    |j                  }|d         | _        |d         | _        |d         | _        |d         | _        |j        j	        d         t          |j                  z
  | _        | j        j        j	        d         | _        | j        j        j	        d         | _        | j        j        j	        d         | _        |j        | _        |j        r| j        j        j	        d         | _        d S d S )Nr   r\   r       )r`   ra   r   r   mean_paramsr   r   	no_paramsrK   rG   r.   df_residr   rL   rl   rM   rm   rN   rn   rb   rO   ro   )r   r   mlefitpart   s       r   ra   zProcessMLEResults.__init__  s    6	 	 	 \\&-((a5qEUA)!,s6=/A/AAjo+A.z,215
.4Q7* 	::06q9DLLL	: 	:r   NTc                     |st          j        d           t          |          dk    st          |          dk    rt          j        d           | j                            | j        |          S )Nz''transform=False' is ignored in predictr   z$extra arguments ignored in 'predict')r   r   r.   r   r   r   )r   rL   	transformr   rp   s        r   r   zProcessMLEResults.predict  sd     	EMCDDDt99q==CKK!OOM@AAAz!!$+t444r   c                 R    | j                             || j        | j        ||          S )a  
        Returns a fitted covariance matrix.

        Parameters
        ----------
        time : array_like
            The time points at which the fitted covariance
            matrix is calculated.
        scale : array_like
            The data used to determine the scale parameter,
            must have len(time) rows.
        smooth : array_like
            The data used to determine the smoothness parameter,
            must have len(time) rows.

        Returns
        -------
        A covariance matrix.

        Notes
        -----
        If the model was fit using formulas, `scale` and `smooth` should
        be Dataframes, containing all variables that were present in the
        respective scaling and smoothing formulas used to fit the model.
        Otherwise, `scale` and `smooth` should be data arrays whose
        columns align with the fitted scaling and smoothing parameters.
        )r   r   r   r   )r   r   r   smooths       r   r   zProcessMLEResults.covariance"  s2    : z$$T4+<%)%7H H 	Hr   c                    | j         j        |         }t          |          dk    r!dt          |          z  }t	          |          | j         j        |d d f         }| j         j        |d d f         }| j                                         \  }}}}t          j	        ||          }t          j	        ||          }| j         j
        |         }	| j                             |	| j        | j        ||          S )Nr   zGroup '%s' does not exist)rV   )r   rj   r.   r   rJ   rM   rN   r{   pd	DataFramer   r   r   r   )
r   groupr   rQ   r   r   r?   rx   ry   r   s
             r   covariance_groupz"ProcessMLEResults.covariance_groupB  s     Z"5)r77a<<-E

:CS//!Z*2qqq51
j,RU3*.**G*G*I*I';a\*kBBB
l;EEEzr"z$$T%)%6%)%7%/%0	2 2 	2r   皙?c                 b   t          j                    }dg| j        z  dg| j        z  z   dg| j        z  z   }| j        r|dg| j        z  z  }||d<   | j        |d<   	 t          j	        t          j
        |                                                     |d<   n# t          $ r t          j        |d<   Y nw xY wdd	lm} |j        |d         z  |d
<   d|                    t          j        |j                            z  |d<   |                    d|dz  z
            }|j        ||d         z  z
  |d|dz  z  <   |j        ||d         z  z   |dd|dz  z
  z  <   | j        j        j        |_        t3          j                    }	|d}|	                    |           |	                    |           |	S )NMeanScaleSmoothSDTypecoefzstd errr   )normtvaluesr    zP>|t|r\   z[%.3fz%.3f]z#Gaussian process regression results)r	  r
  rl   rm   rn   rb   ro   r   r"   r'   diagr   r   nanscipy.stats.distributionsr  r  sfabsr  ppfr   re   rf   indexr   Summary	add_titleadd_df)
r   ynamexnametitlealphadftypr  r   summs
             r   summaryzProcessMLEResults.summaryZ  s   \^^x$+%	DL(@@zDM)*? 	)D6DL((C6
[6
	#GBGDOO,=,=$>$>??ByMM 	# 	# 	#FByMMM	# 	322222"Y-/9$''"&"4"45557HHQ]##$&Ga"Y-.?$?7eai !(*!bm2C(C7a%!)m$%:?.!!=9EuBs   ;B B43B4)NT)NNNr  )
r   r   r   r   ra   r   r   r  r(  r   r   s   @r   r   r     s         : : : : :,5 5 5 5H H H@2 2 20! ! ! ! ! ! ! !r   r   )r   numpyr"   pandasr	  r   statsmodels.base.modelbaser   #statsmodels.regression.linear_modelr   rh   scipy.optimizer   statsmodels.iolibr   statsmodels.tools.numdiffr   r   r   r   rR   LikelihoodModelrT   GenericLikelihoodModelResultsr   r   r   r   <module>r3     s             % % % % % % % % % 3 3 3 3 3 3     # # # # # # & & & & & & 3 3 3 3 3 3 +" +" +" +" +" +" +" +"\[ [ [ [ [* [ [ [|  *}$ }$ }$ }$ }$% }$ }$ }$@~ ~ ~ ~ ~: ~ ~ ~ ~ ~r   