
    M/Ph                     N   d Z ddlZddlmZ ddlmZ ddlmc m	Z ddl
mZ ddlmZ ddlZddlZddlZddgdd	gd
dgd
dgddgddgddgddgddgddgg
ZdZdZdZ G d dej                  Z G d d          Z G d d          Z G d d ee          Z G d! d"ee          ZdS )#a  
Bayesian inference for generalized linear mixed models.

Currently only families without additional scale or shape parameters
are supported (binomial and Poisson).

Two estimation approaches are supported: Laplace approximation
('maximum a posteriori'), and variational Bayes (mean field
approximation to the posterior distribution).

All realizations of random effects are modeled to be mutually
independent in this implementation.

The `exog_vc` matrix is the design matrix for the random effects.
Every column of `exog_vc` corresponds to an independent realization of
a random effect.  These random effects have mean zero and an unknown
standard deviation.  The standard deviation parameters are constrained
to be equal within subsets of the columns. When not using formulas,
these subsets are specified through the parameter `ident`.  `ident`
must have the same length as the number of columns of `exog_vc`, and
two columns whose `ident` values are equal have the same standard
deviation.  When formulas are used, the columns of `exog_vc` derived
from a common formula are constrained to have the same standard
deviation.

In many applications, `exog_vc` will be sparse.  A sparse matrix may
be passed when constructing a model class.  If a dense matrix is
passed, it will be converted internally to a sparse matrix.  There
currently is no way to avoid creating a temporary dense version of
`exog_vc` when using formulas.

Model and parameterization
--------------------------
The joint density of data and parameters factors as:

.. math::

    p(y | vc, fep) p(vc | vcp) p(vcp) p(fe)

The terms :math:`p(vcp)` and :math:`p(fe)` are prior distributions
that are taken to be Gaussian (the :math:`vcp` parameters are log
standard deviations so the standard deviations have log-normal
distributions).  The random effects distribution :math:`p(vc | vcp)`
is independent Gaussian (random effect realizations are independent
within and between values of the `ident` array).  The model
:math:`p(y | vc, fep)` depends on the specific GLM being fit.
    N)minimize)sparse)summary2)familiesgp?gyxPÿgyxP?gUz;?gj	ۿgj	?g0 ?g"g"?gx8!?g^g^?gb_?g*>*g*>*?ad  
    Generalized Linear Mixed Model with Bayesian estimation

    The class implements the Laplace approximation to the posterior
    distribution (`fit_map`) and a variational Bayes approximation to
    the posterior (`fit_vb`).  See the two fit method docstrings for
    more information about the fitting approaches.

    Parameters
    ----------
    endog : array_like
        Vector of response values.
    exog : array_like
        Array of covariates for the fixed effects part of the mean
        structure.
    exog_vc : array_like
        Array of covariates for the random part of the model.  A
        scipy.sparse array may be provided, or else the passed
        array will be converted to sparse internally.
    ident : array_like
        Array of integer labels showing which random terms (columns
        of `exog_vc`) have a common variance.
    vcp_p : float
        Prior standard deviation for variance component parameters
        (the prior standard deviation of log(s) is vcp_p, where s is
        the standard deviation of a random effect).
    fe_p : float
        Prior standard deviation for fixed effects parameters.
    family : statsmodels.genmod.families instance
        The GLM family.
    fep_names : list[str]
        The names of the fixed effects parameters (corresponding to
        columns of exog).  If None, default names are constructed.
    vcp_names : list[str]
        The names of the variance component parameters (corresponding
        to distinct labels in ident).  If None, default names are
        constructed.
    vc_names : list[str]
        The names of the random effect realizations.

    Returns
    -------
    MixedGLMResults object

    Notes
    -----
    There are three types of values in the posterior distribution:
    fixed effects parameters (fep), corresponding to the columns of
    `exog`, random effects realizations (vc), corresponding to the
    columns of `exog_vc`, and the standard deviations of the random
    effects realizations (vcp), corresponding to the unique integer
    labels in `ident`.

    All random effects are modeled as being independent Gaussian
    values (given the variance structure parameters).  Every column of
    `exog_vc` has a distinct realized random effect that is used to
    form the linear predictors.  The elements of `ident` determine the
    distinct variance structure parameters.  Two random effect
    realizations that have the same value in `ident` have the same
    variance.  When fitting with a formula, `ident` is constructed
    internally (each element of `vc_formulas` yields a distinct label
    in `ident`).

    The random effect standard deviation parameters (`vcp`) have
    log-normal prior distributions with mean 0 and standard deviation
    `vcp_p`.

    Note that for some families, e.g. Binomial, the posterior mode may
    be difficult to find numerically if `vcp_p` is set to too large of
    a value.  Setting `vcp_p` to 0.5 seems to work well.

    The prior for the fixed effects parameters is Gaussian with mean 0
    and standard deviation `fe_p`.  It is recommended that quantitative
    covariates be standardized.

    Examples
    --------{example}


    References
    ----------
    Introduction to generalized linear mixed models:
    https://stats.idre.ucla.edu/other/mult-pkg/introduction-to-generalized-linear-mixed-models

    SAS documentation:
    https://support.sas.com/documentation/cdl/en/statug/63033/HTML/default/viewer.htm#statug_intromix_a0000000215.htm

    An assessment of estimation methods for generalized linear mixed
    models with binary outcomes
    https://www.ncbi.nlm.nih.gov/pmc/articles/PMC3866838/
    a\  
    A binomial (logistic) random effects model with random intercepts
    for villages and random slopes for each year within each village:

    >>> random = {"a": '0 + C(Village)', "b": '0 + C(Village)*year_cen'}
    >>> model = BinomialBayesMixedGLM.from_formula(
                   'y ~ year_cen', random, data)
    >>> result = model.fit_vb()
aP  
    A Poisson random effects model with random intercepts for villages
    and random slopes for each year within each village:

    >>> random = {"a": '0 + C(Village)', "b": '0 + C(Village)*year_cen'}
    >>> model = PoissonBayesMixedGLM.from_formula(
                    'y ~ year_cen', random, data)
    >>> result = model.fit_vb()
c                        e Zd Z	 	 	 	 	 	 	 	 d fd	Zd Zd Zd Zd Ze	 	 	 d fd		            Z	ddZ
ddZddZ xZS )_BayesMixedGLMN      c                    |j         dk    r;t          |t          j                  r|d d d f         }nt	          j        |          }|j         dk    rd}t          |          |j         dk    r;t          |t          j                  r|d d d f         }nt	          j        |          }|j         dk    rd}t          |          t          j        |          }|j         dk    rd}t          |          t          |          |j	        d         k    rd}t          |          t          j
        |j        t          j                  sd}t          |          |Nt          |d          r|j                                        }n$d	 t!          |j	        d                   D             }|	7d
 t!          t#          t%          |                    dz             D             }	n>t          |	          t          t'          |                    k    rd}t          |          t)          j        |          st)          j        |          }|                    t"                    }t1          |          }t1          |          }|d}n|j	        d         }|d}d}n|j	        d         }t%          |          dz   }|                    |          } t5                      j        ||fi | || _        || _        || _        || _        || _         || _!        || _"        || _#        |	| _$        |
| _%        || _&        || _'        ||	z   | _(        |
| xj(        |
z  c_(        d S d S )Nr	   r
   z#'exog' must have one or two columnsz&'exog_vc' must have one or two columnsz%ident must be a one-dimensional arrayz8len(ident) should match the number of columns of exog_vcz ident must have an integer dtypecolumnsc                     g | ]
}d |dz   z  S )zFE_%dr	    .0ks     b/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/statsmodels/genmod/bayes_mixed_glm.py
<listcomp>z+_BayesMixedGLM.__init__.<locals>.<listcomp>   s!    MMM1WA.MMM    c                     g | ]
}d |dz   z  S )zVC_%dr	   r   r   s     r   r   z+_BayesMixedGLM.__init__.<locals>.<listcomp>   s!    OOOqAE*OOOr   z5The lengths of vcp_names and ident should be the samer   ))ndim
isinstancenpndarraypd	DataFrame
ValueErrorasarraylenshape
issubdtypedtypeintegerhasattrr   tolistrangeintmaxsetr   issparse
csr_matrixastypefloatmultiplysuper__init__exog_vcexog_vc2identfamilyk_fepk_vck_vcp	fep_names	vcp_namesvc_namesfe_pvcp_pnames)selfendogexogr0   r2   r3   r;   r:   r7   r8   r9   kwargsmsgr4   r5   r6   r1   	__class__s                    r   r/   z_BayesMixedGLM.__init__   s>    9>>$
++ *AAAtG}|D))9>>7CS//!<1'2:.. 0!!!!T'*,w//<1:CS//!
5!!:??9CS//!u::q)))LCS//!}U["*55 	"4CS//! tY'' N L//11		MMdjm8L8LMMM	 OOE#c%jj//A:M4N4NOOOII9~~SZZ00M oo%w'' 	1'00GS!!eT{{ <EEJqME ?DEE=#DJJNE ##G,,///// 

	
"" 	
*
JJ("JJJJ  r   c                     d}|d || j         z            }|| j         z  }|||| j        z            }|| j        z  }||d          }|||fS )Nr   )r4   r6   )r=   veciifepvcpvcs         r   _unpackz_BayesMixedGLM._unpack'  si     "2
?"#
dj
 "R$*_$%
dj XC|r   c                    |                      |          \  }}}d}| j        dk    r|t          j        | j        |          z  }| j        dk    r|| j                            |          z  }| j        j        	                    |          }| j        
                    | j        |          }| j        dk    r|| j                 }t          j        |          }	|dt          j        |dz  |	dz  z            z  t          j        |          z   z  }|dt          j        |dz  | j        dz  z            z  z  }| j        dk    r(|dt          j        |dz  | j        dz  z            z  z  }|S )z
        The overall log-density: log p(y, fe, vc, vcp).

        This differs by an additive constant from the log posterior
        log p(fe, vc, vcp | y).
        r         ?r
   )rI   r4   r   dotr?   r5   r0   r3   linkinverselogliker>   r2   expsumr;   r:   )
r=   paramsrF   rG   rH   lpmullvcp0ss
             r   logposteriorz_BayesMixedGLM.logposterior:  sV    ||F++S" :>>"&C(((B9q==$,""2&&&B[%%b))[  R009q== tz?DtA#r1uq!t|,,,rvd||;;B #sAv
A56666B :>>#sAv	145555B	r   c                    |                      |          \  }}}d}| j        dk    r|t          j        | j        |          z  }| j        dk    r|| j                            |          z  }| j        j        	                    |          }| j
        |z
  | j        j                            |          z  }|| j                            |          z  }g d}| j        dk    rt          j        || j                  |d<   | j        dk    r/| j                                                            |          |d<   | j        dk    r~|| j                 }	t          j        |	          }
|dz  |
dz  z  dz
  }t          j        | j        |          |d<   |dxx         ||
dz  z  z  cc<   |dxx         || j        dz  z  z  cc<   | j        dk    r|dxx         || j        dz  z  z  cc<   d |D             }t          j        |          S )z4
        The gradient of the log posterior.
        r   )NNNr
   r	   weightsc                     g | ]}||S Nr   r   xs     r   r   z4_BayesMixedGLM.logposterior_grad.<locals>.<listcomp>  s    ---Aq}a}}}r   )rI   r4   r   rL   r?   r5   r0   r3   rM   rN   r>   derivvariance	transposer2   rP   bincountr;   r:   concatenate)r=   rR   rF   rG   rH   rS   rT   score_factorterV   rW   us               r   logposterior_gradz _BayesMixedGLM.logposterior_grad^  s   
 ||F++S":>>"&C(((B9q==$,""2&&&B[%%b))
R4;+;+A+A"+E+EE,,R000 :>>F<33BqE9q==L**,,00>>BqE9q==
 tz?DtAA1q AK
A666BqEqEEER!Q$YEEE qEEES4:q=((EEE :>>qEEES49a<''EEE-----~b!!!r   c                     t          j        | j                  }t          j        | j                  }t           j                            | j                  }t          j        |||f          }|S )Nsize)	r   zerosr4   onesr6   randomnormalr5   rd   )r=   	start_fep	start_vcpstart_vcstarts        r   
_get_startz_BayesMixedGLM._get_start  sY    HTZ((	GDJ''	9###33	9h?@@r   c                 6   g }g }g }	d}
|                                 D ]\  }}t          j        ||d          }|                    |           |	                    |           |                    |
t	          j        |j        d         t          j                  z             |
dz  }
t          j	        |d          }|j
                                        }t	          j        |          }t                                          |||d||||	||
  
        }|S )	a  
        Fit a BayesMixedGLM using a formula.

        Parameters
        ----------
        formula : str
            Formula for the endog and fixed effects terms (use ~ to
            separate dependent and independent expressions).
        vc_formulas : dictionary
            vc_formulas[name] is a one-sided formula that creates one
            collection of random effects with a common variance
            parameter.  If using categorical (factor) variables to
            produce variance components, note that generally `0 + ...`
            should be used so that an intercept is not included.
        data : data frame
            The data to which the formulas are applied.
        family : genmod.families instance
            A GLM family.
        vcp_p : float
            The prior standard deviation for the logarithms of the standard
            deviations of the random effects.
        fe_p : float
            The prior standard deviation for the fixed effects parameters.
        r   	dataframe)return_typer	   )r!   )axisN)	datar3   subsetr0   r2   r9   r8   r:   r;   )itemspatsydmatrixappendr   rm   r   int_r   concatr   r$   rd   r.   from_formula)clsformulavc_formulasry   r3   r;   r:   r2   r0   r8   jnafmlmatr9   modelrB   s                   r   r   z_BayesMixedGLM.from_formula  s'   B 	"((** 	 	GB-T{CCCCNN3R   LLRWSYq\AAAABBBFAA)G!,,,?))++u%%$$ % 
 
 r   BFGSc                 2    |                      ||           dS )z
        fit is equivalent to fit_map.

        See fit_map for parameter information.

        Use `fit_vb` to fit the model using variational Bayes.
        N)fit_map)r=   method
minim_optss      r   fitz_BayesMixedGLM.fit  s     	VZ(((((r   Fc                     |r j                             d          } j                             d          } j          _         j                                          _         t          j        |dk              } j         dd|fxx         ||         z  cc<    j         dd|fxx         ||         z  cc<    fd} fd}                                 }	t          ||	|||          }
|
j	        sEdt          j
        t          j        |
j        dz                      z  }t          j        |           dd	lm}  ||
j        |          }t
          j                            |          }|
j        }|ra j         _          `||xx         ||         z  cc<   ||ddf         dd|fxx         t          j        ||         ||                   z  cc<   t+           |||

          S )a~  
        Construct the Laplace approximation to the posterior distribution.

        Parameters
        ----------
        method : str
            Optimization method for finding the posterior mode.
        minim_opts : dict
            Options passed to scipy.minimize.
        scale_fe : bool
            If True, the columns of the fixed effects design matrix
            are centered and scaled to unit variance before fitting
            the model.  The results are back-transformed so that the
            results are presented on the original scale.

        Returns
        -------
        BayesMixedGLMResults instance.
        r   :0yE>Nc                 0                         |            S r]   )rX   rR   r=   s    r   funz#_BayesMixedGLM.fit_map.<locals>.fun  s    %%f----r   c                 0                         |            S r]   )rh   r   s    r   gradz$_BayesMixedGLM.fit_map.<locals>.grad  s    **62222r   )r   jacoptionsz1Laplace fitting did not converge, |gradient|=%.6fr
   )approx_fprime)optim_retvals)r?   meanstd
_exog_savecopyr   flatnonzerort   r   successsqrtrQ   r   warningswarnstatsmodels.tools.numdiffr   r_   linalginvouterBayesMixedGLMResults)r=   r   r   scale_femnscixsr   r   rs   rrA   r   hesscovrR   s   `               r   r   z_BayesMixedGLM.fit_map  s   *  	)""Bq!!B"iDO	((DI.d++CIaaafC(IaaafC(	. 	. 	. 	. 	.	3 	3 	3 	3 	3 !!S%D*MMMy 	F726!%(++,,-CM#;;;;;;}QS$''immD!! 	>DI3KKK2c7"KKKQQQK328BsGRW#=#==#D&#QGGGGr   c                     || j         }|j        d         }t          j        ||d|                   }|s| j        j                            |          }|S )aX  
        Return the fitted mean structure.

        Parameters
        ----------
        params : array_like
            The parameter vector, may be the full parameter vector, or may
            be truncated to include only the mean parameters.
        exog : array_like
            The design matrix for the mean structure.  If omitted, use the
            model's design matrix.
        linear : bool
            If True, return the linear predictor without passing through the
            link function.

        Returns
        -------
        A 1-dimensional array of predicted values
        Nr	   r   )r?   r   r   rL   r3   rM   rN   )r=   rR   r?   linearqprs         r   predictz_BayesMixedGLM.predict  sY    * <9DJqMVD&1+&& 	.!))"--B	r   )NNNr	   r
   NNN)Nr	   r
   )r   N)r   NFNF)__name__
__module____qualname__r/   rI   rX   rh   rt   classmethodr   r   r   r   __classcell__rB   s   @r   r   r      s        f# f# f# f# f# f#P  &" " "H/" /" /"b   
 !; ; ; ; ; [;z) ) ) )8H 8H 8H 8Ht       r   r   c                   L    e Zd ZdZdZdZd Zd Zd Z	 	 	 	 	 	 dd	Z	d
 Z
d ZdS )_VariationalBayesMixedGLMzo
    A mixin providing generic (not family-specific) methods for
    variational Bayes mean field fitting.
       Fc                     t          j        | j        |          }t          j        | j        dz  |dz            }|| j                            |          z  }|| j                            |dz            z  }||fS Nr
   )r   rL   r?   r0   r1   )r=   fep_meanfep_sdvc_meanvc_sdtmtvs          r   	_lp_statsz#_VariationalBayesMixedGLM._lp_statsH  sq    VDIx((VDIqL&!),,
dlw'''
dmq)))2vr   c	           	      j   d}	t           D ]E}
| j        |
d         z  }|	|
d          ||          z  t          j        |dz   dz            z  z  }	F|	t          j        dt          j        z            z  }	|	| j        z  }	|	| j        |z  z  }	|	                                }	|	|                     ||||||          z  }	|	t          j        t          j	        |                    z   t          j        t          j	        |                    z   t          j        t          j	        |                    z   }|S )a  
        Returns the evidence lower bound (ELBO) for the model.

        This function calculates the family-specific ELBO function
        based on information provided from a subclass.

        Parameters
        ----------
        h : function mapping 1d vector to 1d vector
            The contribution of the model to the ELBO function can be
            expressed as y_i*lp_i + Eh_i(z), where y_i and lp_i are
            the response and linear predictor for observation i, and z
            is a standard normal random variable.  This formulation
            can be achieved for any GLM with a canonical link
            function.
        r   r	   r
   )
glwrngr   rP   r   pir>   rQ   _elbo_commonlog)r=   hr   r   vcp_meanr   r   vcp_sdr   ivwzr   s                r   vb_elbo_basez&_VariationalBayesMixedGLM.vb_elbo_baseQ  s%   (  	2 	2A1Q4A!A$1+1uqy 1 111BB
bga"%i   
dh
dj2oVVXX 	d&(FG %' ' 	' "&(((26"&..+A+AABFF5MME E  r   c
           
         d}
d}d}d}d}d}t           D ]v}| j        |d         z  } ||          t          j        |dz   dz            z  t          j        dt          j        z            z  }|t          j        |          z  }|
|d         t          j        || j                  z  z  }
||d         | j        	                                                    |          z  z  }||d         |z  t          j        || j        dz  |z            z  z  }| j
                            |	          	                                                    |          }t          j        t          j        |                    }||d         |z  |z  z  }x|
| j        z  }
|| j        z  }|| j        z  }|| j        z  }|
t          j        | j        | j                  z  }
|| j        	                                                    | j                  z  }|                     ||||||	          \  }}}}}}|
|z  }
||z  }||z  }||z  }||z  }||z  }|d|z  z  }|d|z  z  }|d|	z  z  }t          j        |
||f          }t          j        |||f          }| j        rQt%          dt          j        t          j        |dz            t          j        |dz            z             z             ||fS )ze
        Return the gradient of the ELBO function.

        See vb_elbo_base for parameters.
        g        r	   r
   r   z|G|=%f)r   r   r   rP   r   r   rL   r?   r0   rb   r1   r-   squeezer   r>   _elbo_grad_commonrd   verboseprintrQ   )r=   r   r   r   r   r   r   r   r   r   fep_mean_gradfep_sd_gradvcp_mean_gradvcp_sd_gradvc_mean_grad
vc_sd_gradr   r   rg   r   vfep_mean_grad_ifep_sd_grad_ivcp_mean_grad_ivcp_sd_grad_ivc_mean_grad_ivc_sd_grad_i	mean_gradsd_grads                                r   vb_elbo_grad_basez+_VariationalBayesMixedGLM.vb_elbo_grad_basew  s    
  		' 		'A1Q4A!rvq!teai(((271ru9+=+==ABGBKKAQqTBF1di$8$888MAaD4<#9#9#;#;#?#?#B#BBBL1Q4!8bfQ	1v0E&F&FFFK&&u--7799==a@@A
2:a==))A!A$(Q,&JJ! txdh

DI666..0044TZ@@@ *.)?)?vx%*A *A	'--	 	(}$(}$&l"
q6z!q6z!a%i
NM=$0$2 3 3	.+{J!GHH< 	O2726)Q,#7#7"&!:L:L#LMMMO O O '!!r   Nr   c                     | _         |r j                            d          } j                            d          } j         _         j                                         _        t          j        |dk              }	 j        dd|	fxx         ||	         z  cc<    j        dd|	fxx         ||	         z  cc<    j         j	        z    j
        z   }
 j         j	        z    j
        z   }|t          j        |
          }nHt          |          |k    r!t          dt          |          |fz            |                                }|'ddt          j                            |
          z  z   }nHt          |          |k    r!t          dt          |          |fz            t          j        |          } j         j         j	        z   }}t          j        |||         d	k     d	|||                   |||<   t          j        |d	k     d	|          } fd
} fd}t          j        ||f          }t'          |||||          }|j        st+          j        d           t          |j                  dz  }
|j        d|
         }t          j        d|j        |
d         z            }|r= j         _         `||	xx         ||	         z  cc<   ||	xx         ||	         dz  z  cc<   t3           |||          S )a  
        Fit a model using the variational Bayes mean field approximation.

        Parameters
        ----------
        mean : array_like
            Starting value for VB mean vector
        sd : array_like
            Starting value for VB standard deviation vector
        fit_method : str
            Algorithm for scipy.minimize
        minim_opts : dict
            Options passed to scipy.minimize
        scale_fe : bool
            If true, the columns of the fixed effects design matrix
            are centered and scaled to unit variance before fitting
            the model.  The results are back-transformed so that the
            results are presented on the original scale.
        verbose : bool
            If True, print the gradient norm to the screen each time
            it is calculated.

        Notes
        -----
        The goal is to find a factored Gaussian approximation
        q1*q2*...  to the posterior distribution, approximately
        minimizing the KL divergence from the factored approximation
        to the actual posterior.  The KL divergence, or ELBO function
        has the form

            E* log p(y, fe, vcp, vc) - E* log q

        where E* is expectation with respect to the product of qj.

        References
        ----------
        Blei, Kucukelbir, McAuliffe (2017).  Variational Inference: A
        review for Statisticians
        https://arxiv.org/pdf/1601.00670.pdf
        r   r   Nz#mean has incorrect length, %d != %dg      g?rj   z!sd has incorrect length, %d != %dc                     t          |           dz  }                    | d |         t          j        | |d                               S r   )r   vb_elbor   rP   )r_   nr=   s     r   elboz._VariationalBayesMixedGLM.fit_vb.<locals>.elbo  sA    A!ALL2A2qu6666r   c                 
   t          |           dz  }                    | d |         t          j        | |d                              \  }}|t          j        | |d                    z  }t          j        ||f           S r   )r   vb_elbo_gradr   rP   rd   )r_   r   gmgsr=   s       r   	elbo_gradz3_VariationalBayesMixedGLM.fit_vb.<locals>.elbo_grad  st    A!A&&q!ubfQqrrUmm<<FB"&122--BNB8,,,,r   )r   r   r   zVB fitting did not converger
   )r   r?   r   r   r   r   r   r   r4   r6   r5   rl   r   r   rn   ro   r   whererd   r   r   r   r   r_   rP   r   )r=   r   sd
fit_methodr   r   r   r   r   r   r   mlmrW   i1i2r   r   rs   mmrR   vas   `                     r   fit_vbz _VariationalBayesMixedGLM.fit_vb  s.   `  	)""Bq!!B"iDO	((DI.d++CIaaafC(IaaafC(J#di/Z$*$ty0<AA4yyB 9SYYOKM M M		A:sRY--1-5555AA2ww"}} 73r77B-GI I I r

A TZ$*4B8AbeHrM2qBx88"R% HQVR##	7 	7 	7 	7 	7	- 	- 	- 	- 	- 1v&&%Yz:O O Oz 	9M7888IINacVAQRRL!! 	"DI3KKK2c7"KKKsGGGr#wz!GGG#D&"b999r   c           	         d}|| j                  }|| j                  }	|t          j        |dz  |dz  z   t          j        d|	dz  |z
  z            z            dz  z  }|t          j        |          z  }|d|dz  |dz  z                                   z  | j        dz  z  z  }|d|dz  |dz  z                                   z  | j        dz  z  z  }|S )Nr   r
   rK   )r2   r   rQ   rP   r;   r:   )
r=   r   r   r   r   r   r   r   r   rW   s
             r   r   z&_VariationalBayesMixedGLM._elbo_common(  s     TZ 4:
bfgqj5!8+rva1a4!8n/E/EEFFJJ
bfQii 	cXq[619,11333dj!mCC 	cXq[619,11333dilBB	r   c                 H   || j                  }|| j                  }|dz  |dz  z   }	t          j        d|dz  |z
  z            }
|	|
z  dz
  }d|	z  |
z  |z  }t          j        | j         |          }t          j        | j         |          }|                                 |
z  }|                                 |
z  }||| j        dz  z  z  }||| j        dz  z  z  }|                                 | j        dz  z  }|                                 | j        dz  z  }||||||fS )Nr
   r	   rZ   )r2   r   rP   rc   r   r;   r:   )r=   r   r   r   r   r   r   r   rW   rg   vedmdsr   r   r   r   r   r   s                      r   r   z+_VariationalBayesMixedGLM._elbo_grad_common:  s6    TZ 4:QJ!VAAN##VaZ!Vb[1_DJ;;;k$*b999+jjll]R'
 	DJM11v
A-- "(49a<7{{}}nty!|3{M;j* 	*r   )NNr   NFF)r   r   r   __doc__r   r   r   r   r   r   r   r   r   r   r   r   r   9  s          CG  $ $ $L9" 9" 9"x  s: s: s: s:l  $* * * * *r   r   c                   6    e Zd ZdZd	dZd Zd Zd	dZd
dZdS )r   a  
    Class to hold results from a Bayesian estimation of a Mixed GLM model.

    Attributes
    ----------
    fe_mean : array_like
        Posterior mean of the fixed effects coefficients.
    fe_sd : array_like
        Posterior standard deviation of the fixed effects coefficients
    vcp_mean : array_like
        Posterior mean of the logged variance component standard
        deviations.
    vcp_sd : array_like
        Posterior standard deviation of the logged variance component
        standard deviations.
    vc_mean : array_like
        Posterior mean of the random coefficients
    vc_sd : array_like
        Posterior standard deviation of the random coefficients
    Nc                    || _         || _        || _        || _        |                    |          \  | _        | _        | _        |j        dk    rt          j
        |          }n|}|                    |          \  | _        | _        | _        t          j        | j                  | _        t          j        | j                  | _        t          j        | j                  | _        d S r   )r   rR   _cov_paramsr   rI   fe_meanr   r   r   r   diagfe_sdr   r   r   )r=   r   rR   
cov_paramsr   cps         r   r/   zBayesMixedGLMResults.__init__l  s    
%*5:]]65J5J1dmT\?a$$BBB.3mmB.?.?+
DKWTZ((
gdk**WTZ((


r   c                    t          | j        j        d          rm| j        j        | j        j        z   | j        j        z   }| j        j        dk    rt          j	        | j        ||          S t          j
        | j        |          S | j        S )Nframer
   )indexr   )r  )r#   r   ry   r7   r8   r9   r  r   r   r   Series)r=   r   s     r   r  zBayesMixedGLMResults.cov_params~  s    4:?G,, 	=*&)==*%&B$))|D$4BKKKKy!1<<<< r   c                 <   t          j                    }| j        j        | j        j        z   }d t          | j        j                  D             d t          | j        j                  D             z   |d<   | j        d|         |d<   | j        j        dk    r9t          j
        | j                  d|         }t          j        |          |d<   n$t          j        | j        d|                   |d<   t          j        |d                   |d<   t          j        |d         d|d         z  z
            |d	<   t          j        |d         d|d         z  z             |d
<   d |j        D             |d<   d |d	         D             |d	<   d |d
         D             |d
<   d|j        |j        | j        j        k     df<   d|j        |j        | j        j        k     d	f<   d|j        |j        | j        j        k     d
f<   | j        j        | j        j        z   |_        t%          j                    }|                    | j        j        j        j        dz              |                    |           |                    d           |                    d           |S )Nc                     g | ]}d S )Mr   r   s     r   r   z0BayesMixedGLMResults.summary.<locals>.<listcomp>      <<<qs<<<r   c                     g | ]}d S )Vr   r   s     r   r   z0BayesMixedGLMResults.summary.<locals>.<listcomp>  r  r   Typer   z
Post. Meanr
   zPost. SDSDzSD (LB)zSD (UB)c                     g | ]}d |z  S z%.3fr   r^   s     r   r   z0BayesMixedGLMResults.summary.<locals>.<listcomp>  s    ...1FQJ...r   c                     g | ]}d |z  S r  r   r^   s     r   r   z0BayesMixedGLMResults.summary.<locals>.<listcomp>      ;;;!;;;r   c                     g | ]}d |z  S r  r   r^   s     r   r   z0BayesMixedGLMResults.summary.<locals>.<listcomp>  r  r    z Mixed GLM ResultszAParameter types are mean structure (M) and variance structure (V)z:Variance parameters are modeled as log standard deviations)r   r   r   r4   r6   r%   rR   r  r   r   r
  r   rP   r  locr  r7   r8   r   Summary	add_titler3   rB   r   add_dfadd_text)r=   dfr   r   summs        r   summaryzBayesMixedGLMResults.summary  s   \^^Jtz//<<E$**:$;$;<<<<<E$**:$;$;<<<=6
  ;qs+< A%%())!A#.AWQZZBzNNWT%5ac%:;;BzN 6"\*++4r,/!bn2DDEE9r,/!bn2DDEE9.....4;;R	];;;9;;R	];;;946rx$***D019;rx$***I569;rx$***I56:'$**>>!!tz(2;+, 	- 	- 	-B / 	0 	0 	0 , 	- 	- 	- r   c                 >   | j         }| j        }| j        j        |_| j        j                            |          }t          j        | j        j        |k              }||         }||         }fd|D             t          j
        ||d          }|_        |S )a2  
        Posterior mean and standard deviation of random effects.

        Parameters
        ----------
        term : int or None
            If None, results for all random effects are returned.  If
            an integer, returns results for a given set of random
            effects.  The value of `term` refers to an element of the
            `ident` vector, or to a position in the `vc_formulas`
            list.

        Returns
        -------
        Data frame of posterior means and posterior standard
        deviations of random effects.
        Nc                      g | ]
}|         S r   r   )r   ir   s     r   r   z7BayesMixedGLMResults.random_effects.<locals>.<listcomp>  s    $$$A"Q%$$$r   )Meanr  )r   r   r   r9   r8   r  r   r   r2   r   r   )r=   termr   rW   termixrE   r_   r   s          @r   random_effectsz#BayesMixedGLMResults.random_effects  s    & LJZ Z)//55F
 0F :;;B"A"A$$$$$$$BL!1--..>AGr   Fc                 D    | j                             | j        ||          S )a  
        Return predicted values for the mean structure.

        Parameters
        ----------
        exog : array_like
            The design matrix for the mean structure.  If None,
            use the model's design matrix.
        linear : bool
            If True, returns the linear predictor, otherwise
            transform the linear predictor using the link function.

        Returns
        -------
        A one-dimensional array of fitted values.
        )r   r   rR   )r=   r?   r   s      r   r   zBayesMixedGLMResults.predict  s     $ z!!$+tV<<<r   r]   r   )	r   r   r   r  r/   r  r'  r.  r   r   r   r   r   r   V  sz         *) ) ) )$     & & &P# # # #J= = = = = =r   r   c                   z     e Zd Ze                    e          Z	 	 	 	 	 d	 fd	Zed
d            Z	d Z
d Z xZS )BinomialBayesMixedGLMexampler	   r
   Nc
                    t                                          ||||||t          j                    |||	
  
         t	          j        t	          j        |          t          j        d         k              sd}
t          |
          d S )N)r0   r2   r;   r:   r3   r7   r8   r9   )r   r	   z3endog values must be 0 and 1, and not all identical)	r.   r/   r   Binomialr   alluniquer_r   )r=   r>   r?   r0   r2   r;   r:   r7   r8   r9   rA   rB   s              r   r/   zBinomialBayesMixedGLM.__init__  s     	$&& 	 
	 
	 
	 vbi&&"%+566 	"GCS//!	" 	"r   c                    t          j                    }t                              ||||||          }t	          |j        |j        |j        |j        |j	        |j
        |j        |j        |j        	  	        }|j        |_        |S )Nr3   r;   r:   )r0   r2   r;   r:   r7   r8   r9   )r   r5  r   r   r1  r>   r?   r0   r2   r;   r:   r7   r8   r9   ry   )	r   r   r   ry   r;   r:   famr_   mods	            r   r   z"BinomialBayesMixedGLM.from_formula  s     !!''[$s%d ( L L $GFI''kkZ	! 	! 	! 6
r   c           
         
 |                      |          \  }}}|                      |          \  }}}|                     ||||          \  

fd}	|                     |	
||||||          S )H
        Returns the evidence lower bound (ELBO) for the model.
        c           	          t          j        dt          j        t          j                  | z  z             z              S )Nr	   )r   r   rP   r   r   r   r   s    r   r   z(BinomialBayesMixedGLM.vb_elbo.<locals>.h,  s6    F1rvb272;;?&:;;;<<<<r   rI   r   r   r=   vb_meanvb_sdr   r   r   r   r   r   r   r   r   s             @@r   r   zBinomialBayesMixedGLM.vb_elbo#  s    
 '+ll7&;&;#(G $U 3 3&'5AAB	= 	= 	= 	= 	= 	=   B(GV!'0 0 	0r   c                    
 |                      |          \  }}}|                      |          \  }}}|                     ||||          \  

fd}	|                     |	
||||||	  	        S )R
        Returns the gradient of the model's evidence lower bound (ELBO).
        c                    t          j                  | z  z   }t          j        |          }t          j        |dk              }||         }ddt          j        |           z   z  ||<   t          j        |dk              }||         }t          j        |          dt          j        |          z   z  ||<   | S )Nr   r	   )r   r   
zeros_liker   rP   )r   rg   r_   rE   uur   r   s        r   r   z-BinomialBayesMixedGLM.vb_elbo_grad.<locals>.h;  s    RWR[[1_$Aa  AA&&B2BRVRC[[)AbEQ''B2BF2JJ!bfRjj.1AbE2Ir   rI   r   r   rB  s             @@r   r   z"BinomialBayesMixedGLM.vb_elbo_grad2  s    
 '+ll7&;&;#(G $U 3 3&'5AAB		 		 		 		 		 		 %%aR8W&,fe= = 	=r   r	   r
   NNN)r	   r
   )r   r   r   	_init_docformat_logit_exampler  r/   r   r   r   r   r   r   s   @r   r1  r1    s        ~66G " " " " " "6    [*0 0 0= = = = = = =r   r1  c                        e Zd Ze                    e          Z	 	 	 	 	 d	 fd	Ze	 	 	 	 d
d            Z	d Z
d Z xZS )PoissonBayesMixedGLMr2  r	   r
   Nc
                     t                                          ||||||t          j                    |||	
  
         d S )N)
r>   r?   r0   r2   r;   r:   r3   r7   r8   r9   )r.   r/   r   Poisson)r=   r>   r?   r0   r2   r;   r:   r7   r8   r9   rB   s             r   r/   zPoissonBayesMixedGLM.__init__N  sW     	#%% 	 
	 
	 
	 
	 
	r   c                    t          j                    }t                              ||||||          }	t	          |	j        |	j        |	j        |	j        |	j	        |	j
        |	j        |	j        |	j        	  	        }
|	j        |
_        |
S )Nr:  )	r>   r?   r0   r2   r;   r:   r7   r8   r9   )r   rR  r   r   rP  r>   r?   r0   r2   r;   r:   r7   r8   r9   ry   )r   r   r   ry   r;   r:   r8   r9   r;  r_   r<  s              r   r   z!PoissonBayesMixedGLM.from_formulae  s       '' (   #'I''kkZ	! 	! 	! 6
r   c           
         
 |                      |          \  }}}|                      |          \  }}}|                     ||||          \  

fd}	|                     |	
||||||          S )r>  c                 ^    t          j        t          j                  | z  z              S r]   r   rP   r   r@  s    r   r   z'PoissonBayesMixedGLM.vb_elbo.<locals>.h  s'    F2a/0000r   rA  rB  s             @@r   r   zPoissonBayesMixedGLM.vb_elbo  s    
 '+ll7&;&;#(G $U 3 3&'5AAB	1 	1 	1 	1 	1 	1   B(GV!'0 0 	0r   c                    
 |                      |          \  }}}|                      |          \  }}}|                     ||||          \  

fd}	|                     |	
||||||	  	        S )rF  c                 b    t          j        t          j                  | z  z              }|S r]   rV  )r   yr   r   s     r   r   z,PoissonBayesMixedGLM.vb_elbo_grad.<locals>.h  s+    RWR[[1_,---AHr   rJ  rB  s             @@r   r   z!PoissonBayesMixedGLM.vb_elbo_grad  s    
 '+ll7&;&;#(G $U 3 3&'5AAB	 	 	 	 	 	 %%aR8W&,fe= = 	=r   rK  )r	   r
   NN)r   r   r   rL  rM  _poisson_exampler  r/   r   r   r   r   r   r   s   @r   rP  rP  J  s        '788G      . 
 #"   [B0 0 0= = = = = = =r   rP  )r  numpyr   scipy.optimizer   scipyr   statsmodels.base.modelbaser   statsmodels.iolibr   statsmodels.genmodr   pandasr   r   r|   r   rL  rN  rZ  Modelr   r   r   r1  rP  r   r   r   <module>rd     s(  . .`     # # # # # #       % % % % % % % % % & & & & & & ' ' ' ' ' '       ,-+,,-+,,-+,,-+,,-+,Z	| x x x x xTZ x x xvZ* Z* Z* Z* Z* Z* Z* Z*zU= U= U= U= U= U= U= U=pY= Y= Y= Y= Y=5~ Y= Y= Y=xZ= Z= Z= Z= Z=4n Z= Z= Z= Z= Z=r   