Informar intervalos PIM no estimables
This commit is contained in:
+45
-4
@@ -149,9 +149,20 @@ class Fit:
|
||||
up[k] += step
|
||||
down[k] -= step
|
||||
gradient[k] = (self.risk(X, Z, years, up) - self.risk(X, Z, years, down)) / (2 * step)
|
||||
covariance = np.nan_to_num(self.covariance, nan=0.0)
|
||||
variance = np.einsum("kij,kl,lij->ij", gradient, covariance, gradient)
|
||||
return self.risk(X, Z, years), np.maximum(variance, 0.0)
|
||||
free = np.isfinite(np.diag(self.covariance))
|
||||
variance = np.full((len(X), len(years)), np.nan)
|
||||
block = self.covariance[np.ix_(free, free)]
|
||||
if block.size and np.isfinite(block).all() and np.allclose(block, block.T):
|
||||
try:
|
||||
positive = bool(np.all(np.linalg.eigvalsh(block) > 0))
|
||||
except np.linalg.LinAlgError:
|
||||
positive = False
|
||||
if positive:
|
||||
values = np.einsum("kij,kl,lij->ij", gradient[free], block, gradient[free])
|
||||
affected = np.any(np.abs(gradient[~free]) > 1e-10, axis=0)
|
||||
valid = ~affected & np.isfinite(values) & (values >= 0)
|
||||
variance[valid] = values[valid]
|
||||
return self.risk(X, Z, years), variance
|
||||
|
||||
|
||||
def fit(c, l, r, X, Z, baseline: ISpline, w) -> Fit:
|
||||
@@ -258,8 +269,38 @@ class Mixture(BaseEstimator):
|
||||
point, variance = self.fits_[key].risk_with_variance(
|
||||
unique[:, :n_x], unique[:, n_x:], self.horizons_years
|
||||
)
|
||||
relative = np.where(point > 0, np.sqrt(variance) / np.maximum(point, 1e-300), 0.0)
|
||||
relative = np.where(np.isfinite(variance),
|
||||
np.sqrt(variance) / np.maximum(point, 1e-300), np.nan)
|
||||
band = np.column_stack([point, np.clip(point * np.exp(-z * relative), 0, 1),
|
||||
np.clip(point * np.exp(z * relative), 0, 1)]) # fmt: skip
|
||||
out[index[placed]] = band[inverse.ravel()]
|
||||
return out
|
||||
|
||||
def variance_diagnostics(self, X: pd.DataFrame) -> dict:
|
||||
"""Per stratum covariance properties and input rows without a complete interval."""
|
||||
h = len(self.horizons_years)
|
||||
result = []
|
||||
for key, index in self._groups(X):
|
||||
f = self.fits_.get(key)
|
||||
predictions = self.predict(X.iloc[index])
|
||||
missing = np.isnan(predictions[:, h:]).any(axis=1)
|
||||
entry = {"stratum": [str(v) for v in key], "rows": len(index),
|
||||
"rows_without_interval": int(missing.sum())}
|
||||
if f is None:
|
||||
entry.update(status="unseen_stratum", frozen_parameters=[], negative_variances=[])
|
||||
else:
|
||||
diagonal = np.diag(f.covariance)
|
||||
frozen = np.flatnonzero(np.abs(f.params) >= FROZEN).tolist()
|
||||
negative = np.flatnonzero(np.isfinite(diagonal) & (diagonal < 0)).tolist()
|
||||
free = np.isfinite(diagonal)
|
||||
block = f.covariance[np.ix_(free, free)]
|
||||
try:
|
||||
positive = bool(block.size and np.isfinite(block).all()
|
||||
and np.allclose(block, block.T)
|
||||
and (np.linalg.eigvalsh(block) > 0).all())
|
||||
except np.linalg.LinAlgError:
|
||||
positive = False
|
||||
entry.update(status="positive_definite" if positive else "not_estimable",
|
||||
frozen_parameters=frozen, negative_variances=negative)
|
||||
result.append(entry)
|
||||
return {"strata": result, "horizons_years": list(self.horizons_years)}
|
||||
|
||||
Reference in New Issue
Block a user