
    M/PhP                     *    d dl Zd dlmZ 	 ddZd ZdS )    N)PHRegTc           	         t          j        |           }| |         } ||         }||ddf         }t          |           }t          j        | d          \  }}	t          j        | |d          dz
  }
t          |                                          }|dk                        t           j                  }|||	                                z  }g }dg|z  }dg|z  }dg|z  }t          |          D ]}||dz   k                        t           j                  }|rt          | ||                                          }|                                j        }t          | |d|z
                                            }|                                j        }t          j        |dddf         |dddf         f          }||                    d          z  }||                    d          z  }n|}d}t          |          D ]}|dk    r|||ddf         z
  } ||          }|||<   |dk    rt          j        ||         ddd                   ddd         }||         |z  }||z  }d	}t          j        ||k     ||k     z            }d||<   d|z
  }t          j        |d
t           j                  }t          j        |          } t          j        |           |
         }!t          j        |!          }"t           j        d|"dd         f         ||<   ||
         ||<   t          j        |	|||         z  t          |                    }#t          j        ||         |#z  ||         z            }$t          |$          t          j        ||         ddd         d          z
  }%|%t          |$          k     r|$|%dz
           |$|%d<   |||$z  }|||         |$z  z  }|||z  }|                    |           ||fS )aI  
    Calculates cumulative incidence functions using kernels.

    Parameters
    ----------
    time : array_like
        The observed time values
    status : array_like
        The status values.  status == 0 indicates censoring,
        status == 1, 2, ... are the events.
    exog : array_like
        Covariates such that censoring becomes independent of
        outcome times conditioned on the covariate values.
    kfunc : function
        A kernel function
    freq_weights : array_like
        Optional frequency weights
    dimred : bool
        If True, proportional hazards regression models are used to
        reduce exog to two columns by predicting overall events and
        censoring in two separate models.  If False, exog is used
        directly for calculating kernel weights without dimension
        reduction.
    NT)return_inverserightside   r   V瞯<g|=)weights	minlength)npargsortlenuniquesearchsortedintmaxastypefloat64sumranger   fitpredictpredicted_valueshstackmeanstdcumsumflatnonzeroclipinflogexpr_bincountappend)&timestatusexogkfuncfreq_weightsdimrediinobsutimertimeiengrpstatusaipspn_riskkdkstatus0sfefitval_esfcfitval_cexog2dip0ikd1denomnumrattrratclratpratsfd0ip1jjs&                                         f/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/statsmodels/duration/_kernel_estimates.py_kernel_cumincidencerO      s   8 
D		B8DBZFAAA;Dt99D 9T$777LE5 
u7	3	3	3a	7Bvzz||D {""2:..G#l&6&6&8&88	B
$BVd]F
$B4[[ 9 9QU?**2:66  		dG,,0022C{{}}5HdAK004466C{{}}5HYD 18AAAtG3DEFFFfkk!nn$Ffjjmm#FFFt $	- $	-AAvvvad|+eCjj1 Avv	"Q%"+..ttt4egoEk^URZC"H$=>>B3wwtUBF33vd||yr*VD\\aCRCj)1!"Iq	 UGBqEM'*5zz3 3 3B
 )BqEBJ233CSBOF1IdddOQ???BCHH}}rAv;BCC#s
|A,,4KC
		#"9    c                    t          | ||                                          }|                                j        }t          | |d|z
                                            }|                                j        }t	          j        |dddf         |dddf         f          }	t          |           }
t	          j        |dk              }t	          j        | |                   }t	          j	        |           }| |         } ||         }|	|ddf         }	t	          j
        | |d          dz
  }|||                                z  }d}t          |
          D ]}|	|	|ddf         z
  } ||          }t	          j        |ddd                   ddd         }||z  }||z  }d}t	          j        ||k     ||k     z            }d||<   d|z
  }t	          j        |d	t          j                  }t	          j        |          }t	          j        |          |         }t	          j        |          }|||z  }||||         z  z  }|||
z  }||fS )
a  
    Estimate the marginal survival function under dependent censoring.

    Parameters
    ----------
    time : array_like
        The observed times for each subject
    status : array_like
        The status for each subject (1 indicates event, 0 indicates
        censoring)
    exog : array_like
        Covariates such that censoring is independent conditional on
        exog
    kfunc : function
        Kernel function
    freq_weights : array_like
        Optional frequency weights

    Returns
    -------
    probs : array_like
        The estimated survival probabilities
    times : array_like
        The times at which the survival probabilities are estimated

    References
    ----------
    Zeng, Donglin 2004. Estimating Marginal Survival Function by
    Adjusting for Dependent Censoring Using Many Covariates. The
    Annals of Statistics 32 (4): 1533 55.
    doi:10.1214/009053604000000508.
    https://arxiv.org/pdf/math/0409180.pdf
    r	   Nr   r   g        r
   r   r   g-q=)r   r   r   r   r   r   r   r    r   r   r   r   r   r   r!   r"   r#   r$   )r(   r)   r*   r+   r,   r;   r<   r=   r>   r?   nixdr0   r.   r2   sprobrA   r8   rC   rD   rE   rF   rG   rH   rI   s                            rN   _kernel_survfuncrU   w   sW   H dF
#
#
'
'
)
)C{{}}-H
dAJ
'
'
+
+
-
-C{{}}-HYD)8AAAtG+<=>>FD		A
.1
%
%C Id3i  E 
D		B8DBZFBE]F 
u7	3	3	3a	7B#l&6&6&8&88E1XX , ,fQTl"U2YY	"TTrT(##DDbD)6kEk^URZC"H566B3wwtUBF++vd||yr"vd||TMEETLO++EE
%<rP   )T)numpyr   &statsmodels.duration.hazard_regressionr   rO   rU    rP   rN   <module>rY      s_        8 8 8 8 8 8 !%o o o odW W W W WrP   