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

Function _score

python/openquant/feature_diagnostics.py:199–231  ·  view source on GitHub ↗
(
    y_true: Sequence[float],
    prob: Sequence[float],
    scoring: str,
    sample_weight: Sequence[float] | None,
)

Source from the content-addressed store, hash-verified

197 for i in range(dim):
198 xtwy[i] += w * design[i] * yy
199 for j in range(dim):
200 xtwx[i][j] += w * design[i] * design[j]
201
202 for i in range(dim):
203 xtwx[i][i] += ridge
204
205 beta = _solve_linear_system(xtwx, xtwy)
206 return _LinearModel(coeffs=beta[1:], intercept=beta[0])
207
208
209def _predict_proba(model: _LinearModel, x: Sequence[Sequence[float]]) -> list[float]:
210 return [_sigmoid(model.intercept + _dot(row, model.coeffs)) for row in x]
211
212
213def _score(
214 y_true: Sequence[float],
215 prob: Sequence[float],
216 scoring: str,
217 sample_weight: Sequence[float] | None,
218) -> float:
219 weights = [1.0] * len(y_true) if sample_weight is None else [float(v) for v in sample_weight]
220 den = sum(weights)
221 if den <= 0:
222 return 0.0
223
224 if scoring == "neg_log_loss":
225 loss = 0.0
226 for y, p, w in zip(y_true, prob, weights):
227 p_clip = min(max(p, 1e-15), 1.0 - 1e-15)
228 loss += w * (-(y * log(p_clip) + (1.0 - y) * log(1.0 - p_clip)))
229 return -(loss / den)
230
231 pred = [1.0 if p >= 0.5 else 0.0 for p in prob]
232
233 if scoring == "accuracy":
234 correct = sum(w for y, p, w in zip(y_true, pred, weights) if abs(y - p) < 1e-12)

Callers 2

_score_with_perm_groupsFunction · 0.85
sfi_importanceFunction · 0.85

Calls 1

maxFunction · 0.85

Tested by

no test coverage detected