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

Function sfi_importance

python/openquant/feature_diagnostics.py:405–454  ·  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

403 )
404 return {
405 "method": "mda",
406 "table": table,
407 "records": table.to_dicts(),
408 "viz_payload": payload,
409 "cv": {
410 "method": "purged_kfold" if event_end_indices is not None else "kfold_embargo_only",
411 "purged": event_end_indices is not None,
412 "n_splits": n_splits,
413 "pct_embargo": pct_embargo,
414 "fold_count": len(splits),
415 "scoring": scoring,
416 "mean_base_score": _mean(fold_scores),
417 },
418 }
419
420
421def sfi_importance(
422 X: Sequence[Sequence[float]],
423 y: Sequence[float],
424 feature_names: Sequence[str] | None = None,
425 sample_weight: Sequence[float] | None = None,
426 event_end_indices: Sequence[int] | None = None,
427 n_splits: int = 5,
428 pct_embargo: float = 0.01,
429 scoring: str = "neg_log_loss",
430 allow_unpurged: bool = False,
431) -> dict[str, object]:
432 x = _as_matrix(X)
433 yv = _as_vector(y, len(x))
434 names = _feature_names(len(x[0]), feature_names)
435 weights = _sample_weight(sample_weight, len(x))
436 intervals = _build_intervals(event_end_indices, len(x), allow_unpurged=allow_unpurged)
437 splits = _purged_kfold_splits(intervals, n_splits=n_splits, pct_embargo=pct_embargo)
438
439 per_feature: list[list[float]] = [[] for _ in names]
440
441 for j in range(len(names)):
442 xj = [[row[j]] for row in x]
443 for train_idx, test_idx in splits:
444 x_train = [xj[i] for i in train_idx]
445 y_train = [yv[i] for i in train_idx]
446 x_test = [xj[i] for i in test_idx]
447 y_test = [yv[i] for i in test_idx]
448 w_train = [weights[i] for i in train_idx] if weights is not None else None
449 w_test = [weights[i] for i in test_idx] if weights is not None else None
450
451 model = _fit_linear_probability_model(x_train, y_train, w_train)
452 score_val = _score(y_test, _predict_proba(model, x_test), scoring, w_test)
453 per_feature[j].append(score_val)
454
455 table = _importance_table(names, per_feature)
456 payload = viz.prepare_feature_importance_payload(
457 table["feature"].to_list(), table["mean"].to_list(), std=table["stderr"].to_list()

Callers

nothing calls this directly

Calls 11

_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
_scoreFunction · 0.85
_predict_probaFunction · 0.85
_importance_tableFunction · 0.85
appendMethod · 0.80

Tested by

no test coverage detected