Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _log_1m_sf(self, params, times):
mu_, ln_sigma_, lambda_ = params
sigma_ = safe_exp(ln_sigma_)
Z = (log(times) - mu_) / sigma_
if lambda_ > 0:
v = gammaincln(1 / lambda_ ** 2, safe_exp(lambda_ * Z - 2 * np.log(lambda_)))
else:
v = gammainccln(1 / lambda_ ** 2, safe_exp(lambda_ * Z - 2 * np.log(-lambda_)))
return v
def _cumulative_hazard(self, params, T, Xs):
lambda_ = Xs["lambda_"] @ params["lambda_"]
sigma_ = safe_exp(Xs["sigma_"] @ params["sigma_"])
mu_ = Xs["mu_"] @ params["mu_"]
ilambda_2 = 1 / lambda_ ** 2
Z = (log(T) - mu_) / np.clip(sigma_, 1e-10, 1e20)
exp_term = np.clip(safe_exp(lambda_ * Z - 2 * log(np.abs(lambda_))), 1e-300, np.inf)
return -np.where(lambda_ > 0, gammainccln(ilambda_2, exp_term), gammaincln(ilambda_2, exp_term))
def _cumulative_hazard(self, params, times):
mu_, ln_sigma_, lambda_ = params
sigma_ = safe_exp(ln_sigma_)
Z = (log(times) - mu_) / sigma_
ilambda_2 = 1 / lambda_ ** 2
if lambda_ > 0:
v = -gammainccln(ilambda_2, safe_exp(lambda_ * Z - 2 * np.log(lambda_)))
else:
v = -gammaincln(ilambda_2, safe_exp(lambda_ * Z - 2 * np.log(-lambda_)))
return v
def _survival_function(self, params, times):
mu_, ln_sigma_, lambda_ = params
sigma_ = safe_exp(ln_sigma_)
Z = (log(times) - mu_) / sigma_
if lambda_ > 0:
return gammaincc(1 / lambda_ ** 2, safe_exp(lambda_ * Z - 2 * np.log(lambda_)))
else:
return gammainc(1 / lambda_ ** 2, safe_exp(lambda_ * Z - 2 * np.log(-lambda_)))
def _cumulative_hazard(self, params, T, Xs):
n = T.shape[0]
T = T.reshape((n, 1))
M = np.minimum(np.tile(self.breakpoints, (n, 1)), T)
M = np.hstack([M[:, tuple([0])], np.diff(M, axis=1)])
lambdas_ = np.array([safe_exp(-np.dot(Xs[param], params[param])) for param in self._fitted_parameter_names])
return (M * lambdas_.T).sum(1)
def _survival_function(self, params, T, Xs):
lambda_ = np.clip(Xs["lambda_"] @ params["lambda_"], 1e-25, 1e10)
sigma_ = safe_exp(Xs["sigma_"] @ params["sigma_"])
mu_ = Xs["mu_"] @ params["mu_"]
Z = (log(T) - mu_) / sigma_
ilambda_2 = 1 / lambda_ ** 2
exp_term = safe_exp(lambda_ * Z - 2 * log(np.abs(lambda_)))
return np.where(lambda_ > 0, gammaincc(ilambda_2, exp_term), gammainc(ilambda_2, exp_term))
def _log_hazard(self, params, T, Xs):
alpha_params = params["alpha_"]
log_alpha_ = np.dot(Xs["alpha_"], alpha_params)
alpha_ = safe_exp(log_alpha_)
beta_params = params["beta_"]
log_beta_ = np.dot(Xs["beta_"], beta_params)
beta_ = safe_exp(log_beta_)
return (
log_beta_
- log_alpha_
+ np.expm1(log_beta_) * (np.log(T) - log_alpha_)
- np.logaddexp(beta_ * (np.log(T) - np.log(alpha_)), 0)
)
def _log_hazard(self, params, T, Xs):
alpha_params = params["alpha_"]
log_alpha_ = np.dot(Xs["alpha_"], alpha_params)
alpha_ = safe_exp(log_alpha_)
beta_params = params["beta_"]
log_beta_ = np.dot(Xs["beta_"], beta_params)
beta_ = safe_exp(log_beta_)
return (
log_beta_
- log_alpha_
+ np.expm1(log_beta_) * (np.log(T) - log_alpha_)
- np.logaddexp(beta_ * (np.log(T) - np.log(alpha_)), 0)
)
def _log_hazard(self, params, T, Xs):
lambda_ = Xs["lambda_"] @ params["lambda_"]
ln_sigma_ = Xs["sigma_"] @ params["sigma_"]
mu_ = Xs["mu_"] @ params["mu_"]
ilambda_2 = 1 / lambda_ ** 2
Z = (log(T) - mu_) / np.clip(safe_exp(ln_sigma_), 1e-10, 1e20)
exp_term = np.clip(safe_exp(lambda_ * Z - 2 * log(np.abs(lambda_))), 1e-300, np.inf)
return (
log(np.abs(lambda_))
- log(T)
- ln_sigma_
- gammaln(ilambda_2)
+ (lambda_ * Z - 2 * log(np.abs(lambda_))) * ilambda_2
- exp_term
- np.where(lambda_ > 0, gammainccln(ilambda_2, exp_term), gammaincln(ilambda_2, exp_term))
)
def _log_1m_sf(self, params, T, Xs):
alpha_params = params["alpha_"]
log_alpha_ = np.dot(Xs["alpha_"], alpha_params)
alpha_ = safe_exp(log_alpha_)
beta_params = params["beta_"]
log_beta_ = np.dot(Xs["beta_"], beta_params)
beta_ = safe_exp(log_beta_)
return -np.logaddexp(-beta_ * (np.log(T) - np.log(alpha_)), 0)