
    !g#                        d Z ddlZddlmZ ddlmZ ddlmZ ddl	m
Z
mZ  G d d	e      Z G d
 de      Zd Zd Zedk(  r1 ej"                  dd id        eddd      ZdZeegZ ej,                  g dg      Zej0                  d   ZeD ]  Zg Zg Zg Zg Z ee      D ]  Z  e       \  Z!Z" e       Z# ee!jH                  e!jJ                  e!jL                  e!jN                  e# e             Z(e(jS                         Z* ee!jH                  e!jJ                  e!jL                  e!jN                  e#e"      Z(e(jS                  e*j8                        Z*e*e*jV                  s	 e,d       de(j[                         z  Z.ej_                  ej0                  e"j<                  e.f          ej_                   ej`                  e*j8                               ej_                   ej`                  e*jb                                e       \  Z!Z" e       Z# ee!jH                  e!jJ                  e!jL                  e!jN                  e#e"eef      Z(e(jS                         Z*e*e*jV                  s
 e,d       e(jd                  Z3e3d   Z4ej_                  e4         ej,                   e5e       e6e      z        Z7ejq                  d e6e      efz         ejq                  d        e!js                  e7        ej,                  e      Zeju                  d      Z;ejy                  d      Z= ej,                  e      Zeju                  d      Zejq                  d!       ejq                  d"       ejq                   ej|                  e;      d#z          ejq                  d$       ejq                   ej|                  e!j8                        d#z          ejq                  d%       ejq                   ej|                  e;e!j8                  z
        d#z          ejq                  d&       ejq                   ej|                  e;e!j8                  z
  e!j8                  z        d#z          ejq                  d#       ejq                  d'       ejq                  d"       ejq                   ej|                  e=      d#z          ejq                  d$       ejq                   ej|                  e      d#z          ejq                  d%       ejq                   ej|                  e=ez
        d#z          ejq                  d&       ejq                   ej|                  e=ez
  ez        d#z          ejq                  d#       ej                          ejq                  d(       ejq                  d)       ejq                   ej|                  e      d#z          ejq                  d*       ejq                   ej|                  e      d#z          ejq                  d+        ej                  d,d-d,      D ]*  ZAejq                  d.e eBeA e6e      z           eAfz         , ejq                  d/        ej                          yy)0z
Assesment of Generalized Estimating Equations using simulation.

This script checks Poisson models.

See the generated file "gee_poisson_simulation_check.txt" for results.
    N)Poisson   )GEE_simulator)GEE)ExchangeableIndependencec                        e Zd ZdZdZd Zd Zy)Exchangeable_simulatora6  
    Simulate exchangeable Poisson data.

    The data within a cluster are simulated as y_i = z_c + z_i.  The
    z_c, and {z_i} are independent Poisson random variables with
    expected values e_c and {e_i}, respectively.  In order for the
    pairwise correlation to be equal to `f` for all pairs, we need

         e_c / sqrt((e_c + e_i) * (e_c + e_j)) = f for all i, j.

    By setting all e_i = e within a cluster, these equations can be
    satisfied.  We thus need

         e_c * (1 - f) = f * e,

    which can be solved (non-uniquely) for e and e_c.
          ?c                 (   t         j                  d|d   z         t         j                  d| j                  d   z         t         j                  d|d   z         t         j                  d| j                  z         t         j                  d       y )Nz/Estimated common pairwise correlation:   %8.4f
r   z/True common pairwise correlation:        %8.4f
/Estimated inverse scale parameter:       %8.4f
r   /True inverse scale parameter:            %8.4f

)OUTwritedparams	scale_invselfdparams_ests     r/var/www/dash_apps/app1/venv/lib/python3.12/site-packages/statsmodels/genmod/tests/gee_poisson_simulation_check.pyprint_dparamsz$Exchangeable_simulator.print_dparams%   s}    		Da.! 	"		D,,q/" 	#		Da.! 	"		D..! 	"		$    c                    g g g g f\  }}}}t        j                  | j                  dz        }t         j                  j	                  t        j
                  t        | j                              t        j                  | j                  | j                        |z  z
        \  }}}|d d t        j                  |dkD        f   }	t        | j                        D ]  }
t         j                  j                  | j                  d   | j                  d         }|j                  |
g|z         t         j                  j                  |df      }|j                  |       t         j                  j!                  dd      }|d| j"                  d   z
  z  | j"                  d   z  }t         j                  j%                  |      }t         j                  j%                  ||      }||z   }|j                  |       t        j&                  ||z         t        j(                  |      z  }t        j                  || j                        t        j                  | j                  dz        z  }t         j                  j                  t        |      |	j*                  d   f      }|t        j,                  ||	j.                        z  }|j                  |         t        j0                  |d      | _        t        j0                  |      | _        t        j0                  |d      | _        t        j0                  |      | _        y )	N   ư>r   r   size
   )lowhighaxis)npsumparamslinalgsvdeyelenouterflatnonzerorangengroupsrandomrandintgroup_size_rangeappendnormaluniformr   poissonlogonesshapedotTconcatenateexogendogtimegroup)r   r=   r<   r?   r>   fusvtparams0igsizetime1e_cecommonuniqueendog1lprexog1emats                        r   simulatezExchangeable_simulator.simulate1   sw   #%r2r> tUD FF4;;>"rvvc$++&67!xxT[[AAE F G!BAbnnQX../t||$ 	AII%%d&;&;A&>&*&;&;A&>@E LL!&II$$5!*$5EKK))###3Cq4<<?*+dll1o=AYY&&s+FYY&&q%0Ff_FLL &&q/BGGEN2C HHS$++.Q1GGE99###c(GMM!4D)E#FDRVVD')),,EKK3	6 NN4a0	^^E*
NN4a0	^^E*
r   N)__name__
__module____qualname____doc__r   r   rP    r   r   r
   r
      s    $ I	(+r   r
   c                       e Zd ZdZd Zd Zy)Overdispersed_simulatora  
    Use the negative binomial distribution to check GEE estimation
    using the overdispered Poisson model with independent dependence.

    Simulating
        X = np.random.negative_binomial(n, p, size)
    then EX = (1 - p) * n / p
         Var(X) = (1 - p) * n / p**2

    These equations can be inverted as follows:

        p = E / V
        n = E * p / (1 - p)

    dparams[0] is the common correlation coefficient
    c                     t         j                  d|d   z         t         j                  d| j                  z         t         j                  d       y )Nr   r   r   r   )r   r   r   r   s     r   r   z%Overdispersed_simulator.print_dparamso   sC    		Da.! 	"		D..! 	"		$r   c                 2   g g g g f\  }}}}t        j                  | j                  dz        }t         j                  j	                  t        j
                  t        | j                              t        j                  | j                  | j                        |z  z
        \  }}}|d d t        j                  |dkD        f   }	t        | j                        D ]O  }
t         j                  j                  | j                  d   | j                  d         }|j                  |
g|z         t         j                  j                  |df      }|j                  |       t         j                  j                  |t        | j                        f      }|j                  |       t        j                   t        j"                  || j                              }|| j$                  z  }||z  }||z  d|z
  z  }t         j                  j'                  |||      }|j                  |       R t        j(                  |d      | _        t        j(                  |      | _        t        j(                  |d      | _        t        j(                  |      | _        y )Nr   r   r   r   r   r"   )r$   r%   r&   r'   r(   r)   r*   r+   r,   r-   r.   r/   r0   r1   r2   r3   expr9   r   negative_binomialr;   r<   r=   r>   r?   )r   r=   r<   r?   r>   r@   rA   rB   rC   rD   rE   rF   rG   rN   EVpnrL   s                      r   rP   z Overdispersed_simulator.simulatew   s   #%r2r> tUD FF4;;>"rvvc$++&67!xxT[[AAE F G!BAbnnQX../t||$ 	!AII%%d&;&;A&>&*&;&;A&>@E LL!&II$$5!*$5EKKII$$5#dkk2B*C$DEKKrvveT[[12ADNN"AAAAQAYY00Au=FLL )	!, NN4a0	^^E*
NN4a0	^^E*
r   N)rQ   rR   rS   rT   r   rP   rU   r   r   rW   rW   \   s    $#+r   rW   c                      t               } t        j                  d   | _        d| _        dg| _        | j                          | t               fS )N       @皙?rc   ggɿ   g333333?)r
   r$   r_r&   r.   r   rP   r   exss    r   gendat_exchangeablerh      sA    
 
"C/0CJCK&CKLLNr   c                      t               } t        j                  d   | _        d| _        d| _        g | _        | j                          | t               fS )Nra   rd   rb   )	rW   r$   re   r&   r.   r   r   rP   r   rf   s    r   gendat_overdispersedrj      sF    
!
#C/0CJCKCMCKLLNr   __main__allc                     d| z  S )Nz%8.3frU   )xs    r   <lambda>ro      s
    GaK r   T)	formattersuppressz gee_poisson_simulation_check.txtwzutf-8)encodingd   )        r   r   r   )ru   )start_paramszFailed to converger   )
constraintzp-valuez:Results based on %d successful fits out of %d data sets.

z Checking dependence parameters:
zChecking parameter values:
zObserved:            r   zExpected:            zAbsolute difference: zRelative difference: zChecking standard errors
z!Checking constrained estimation:
zLeft hand side:
zRight hand side:
z+Observed p-values   Expected Null p-values
g?gQ?z%20.3f %20.3f
zR================================================================================

)DrT   numpyr$   statsmodels.genmod.familiesr   gee_gaussian_simulation_checkr   3statsmodels.genmod.generalized_estimating_equationsr   statsmodels.genmod.cov_structr   r   r
   rW   rh   rj   rQ   set_printoptionsopenr   nrepgendatsarraylhsre   rhsgendatpvaluesr&   
std_errorsr   r-   jdavagar=   r<   r?   r>   mdfitmdf	convergedprintestimate_scaler   r2   asarraystandard_errorsscore_test_resultsscorepvaluer%   r*   dparams_meanr   r   meaneparamsstdsdparams	array_strsortarangeqintcloserU   r   r   <module>r      s    / 8 C CI+] I+X>+m >+D zB5*?"@!%' 13
ICD"$89G
"((%'
(C
%%+C  Z%
t "	#AXFBB RXXrww"''2!^%B&&(CRXXrww"''2rBB&&

&3C{3==*+R..00INN255Y!678MM*"**SZZ01jbjj)<)<=>HEBrBRXXrww"''2r!$c
,B&&(C{3==*+))E9%FNN6"E"	#H  rxxGs7| ;<		P\4() 	*		56
&&!++a.::a=RXXj)
__Q'
		01		)*		,",,w'$./		)*		,",,ryy)D01		)*		,",,w23d:;		)*		,",,")) 3ryy@A 			$		./		)*		,",,x(4/0		)*		,",,z*T12		)*		,",,x*45<=		)*		,",,: 5CD 			$		67		%&		,",,s#d*+		&'		,",,s#d*+		@A3c* 	9AII's1S\>23Q78 9	9 			#$uZ%x IIKW r   