MCPcopy Create free account
hub / github.com/Open-Quant/openquant / mda_importance

Function mda_importance

python/openquant/feature_diagnostics.py:342–402  ·  view source on GitHub ↗
(
    X: Sequence[Sequence[float]],
    y: Sequence[float],
    feature_names: Sequence[str] | None = None,
    sample_weight: Sequence[float] | None = None,
    event_end_indices: Sequence[int] | None = None,
    n_splits: int = 5,
    pct_embargo: float = 0.01,
    scoring: str = "neg_log_loss",
)

Source from the content-addressed store, hash-verified

340
341
342def _recover_weighted_logistic(
343 design: list[list[float]],
344 y: Sequence[float],
345 weights: list[float],
346 ridge: float,
347 max_iter: int,
348 tol: float,
349) -> _LinearModel | None:
350 """Rare finite-input, binary-label recovery preserving the weighted objective and ridge.
351
352 Rational accumulation/elimination prevents overflow and keeps ridge terms on
353 rank-deficient designs. Exponential/logarithmic evaluations and returned
354 coefficients remain binary64; this is not a general arbitrary-precision fit.
355 The original iteration cap remains, so separable extreme-weight problems
356 need not reach their ridge optimum within that budget. Exponential tails
357 below the normal range use eight-way splitting only for |logit| <= 4096;
358 larger logits retain the zero-tail limit. A retry starts from zero with
359 the same iteration cap.
360 """
361 if not (
362 isfinite(ridge)
363 and ridge > 0
364 and all(isfinite(v) for row in design for v in row)
365 and all(v in (0.0, 1.0) for v in y)
366 and all(isfinite(w) and w >= 0 for w in weights)
367 and any(w > 0 for w in weights)
368 ):
369 return None
370 rows = [[Fraction(v) for v in row] for row in design]
371 labels = [Fraction(v) for v in y]
372 sw = [Fraction(w) for w in weights]
373 penalty = Fraction(ridge)
374 dim = len(rows[0])
375 beta = [0.0] * dim
376 objective = _fraction_logistic_loss(rows, labels, sw, [Fraction()] * dim, penalty)
377 for _ in range(max_iter):
378 bq = [Fraction(b) for b in beta]
379 gradient = [penalty * b for b in bq]
380 hessian = [[penalty if i == j else Fraction() for j in range(dim)] for i in range(dim)]
381 for row, yy, w in zip(rows, labels, sw):
382 if not w:
383 continue
384 z = sum((v * b for v, b in zip(row, bq)), Fraction())
385 prob, _ = _fraction_logistic(z)
386 resid = w * (prob - yy)
387 curv = w * prob * (1 - prob)
388 for i in range(dim):
389 gradient[i] += resid * row[i]
390 for j in range(i, dim):
391 hessian[i][j] += curv * row[i] * row[j]
392 for i in range(dim):
393 for j in range(i):
394 hessian[i][j] = hessian[j][i]
395 step = _solve_fraction_system(hessian, gradient)
396 scale = 1.0
397 while True:
398 candidate = [b - scale * s for b, s in zip(beta, step)]
399 cand_obj = _fraction_logistic_loss(

Callers 1

Calls 10

_as_matrixFunction · 0.85
_as_vectorFunction · 0.85
_feature_namesFunction · 0.85
_sample_weightFunction · 0.85
_build_intervalsFunction · 0.85
_purged_kfold_splitsFunction · 0.85
_score_with_perm_groupsFunction · 0.85
_importance_tableFunction · 0.85
_meanFunction · 0.85
appendMethod · 0.80

Tested by

no test coverage detected